From fb1d6bb34f8b97c71b4b53370154c6fed8cb650d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 20 Apr 2025 17:57:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=9B=B8=E5=85=B3BUG=20+=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95,=20?= =?UTF-8?q?=E8=A6=86=E7=9B=96=E7=8E=87100%?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- data_type.go | 50 +++++------ data_type_test.go | 188 +++++++++++++++++++++++++++++++++++++++++ enums/data_location.go | 8 -- enums/data_type.go | 8 -- file_type.go | 61 +++++++++---- file_type_test.go | 41 +++++++++ header.go | 12 +-- header_test.go | 32 +++++++ http_code_test.go | 27 ++++++ logger.go | 52 ++++++++---- logger_test.go | 55 +++++++++++- redis.go | 10 +-- redis_test.go | 6 +- request.go | 56 ++++++------ request_test.go | 68 +++++++++++++++ scheme.go | 8 +- scheme_test.go | 32 +++++++ validator.go | 25 +++--- validator_test.go | 64 ++++++++++++++ 19 files changed, 669 insertions(+), 134 deletions(-) create mode 100644 data_type_test.go delete mode 100644 enums/data_location.go delete mode 100644 enums/data_type.go create mode 100644 file_type_test.go create mode 100644 header_test.go create mode 100644 http_code_test.go create mode 100644 request_test.go create mode 100644 scheme_test.go create mode 100644 validator_test.go diff --git a/data_type.go b/data_type.go index aec1c48..e2de9b9 100644 --- a/data_type.go +++ b/data_type.go @@ -14,12 +14,12 @@ package consts // Date : 14:10 2024/11/25 type DataType string -func (df *DataType) String() string { - return string(*df) +func (df DataType) String() string { + return string(df) } -func (df *DataType) MarshalJSON() ([]byte, error) { - return []byte(df.String()), nil +func (df DataType) MarshalJSON() ([]byte, error) { + return []byte(`"` + df.String() + `"`), nil } // IsValid 判断枚举值是否有效 @@ -27,9 +27,9 @@ func (df *DataType) MarshalJSON() ([]byte, error) { // Author : go_developer@163.com<白茶清欢> // // Date : 14:45 2024/11/25 -func (df *DataType) IsValid() bool { +func (df DataType) IsValid() bool { for _, item := range DataTypeList { - if item.Value == *df { + if item.Value == df || item.Value.String() == df.String() { return true } } @@ -369,49 +369,49 @@ func GetDataTypeDefaultValue(dataType DataType) any { case DataTypeBoolPtr: return new(bool) case DataTypeMapStrAny: - return new(map[string]any) + return map[string]any{} case DataTypeMapAnyAny: - return new(map[any]any) + return map[any]any{} case DataTypeMapStrBool: - return new(map[string]bool) + return map[string]bool{} case DataTypeMapStrInt: - return new(map[string]int) + return map[string]int{} case DataTypeMapStrFloat: - return new(map[string]float64) + return map[string]float64{} case DataTypeMapStrUint: - return new(map[string]uint) + return map[string]uint{} case DataTypeMapStrSlice: - return new(map[string][]any) + return map[string][]any{} case DataTypeMapStrStr: - return new(map[string]string) + return map[string]string{} case DataTypeSliceAny: - return new([]any) + return []any{} case DataTypeSliceString: - return new([]string) + return []string{} case DataTypeSliceBool: - return new([]bool) + return []bool{} case DataTypeSliceInt: - return new([]int) + return []int{} case DataTypeSliceUint: - return new([]uint) + return []uint{} case DataTypeSliceFloat: - return new([]float64) + return []float64{} case DataTypeSliceMapAnyAny: // map[any]any 序列化会有问题,当做 map[string]any 处理 - return new([]map[string]any) + return []map[string]any{} case DataTypeSliceMapStringAny: - return new([]map[string]any) + return []map[string]any{} default: // 序列化之后的map for _, dataTypeItem := range DataTypeMapMarshal { if dataTypeItem == dataType { - return "" + return "{}" } } // 序列化之后的slice for _, dataTypeItem := range DataTypeSliceMarshal { if dataTypeItem == dataType { - return "" + return "[]" } } // 指定分隔符分割的slice @@ -423,7 +423,7 @@ func GetDataTypeDefaultValue(dataType DataType) any { // 未枚举的slice类型用any接收 for _, dataTypeItem := range DataTypeSlice { if dataTypeItem == dataType { - return new([]any) + return []any{} } } } diff --git a/data_type_test.go b/data_type_test.go new file mode 100644 index 0000000..24eeaeb --- /dev/null +++ b/data_type_test.go @@ -0,0 +1,188 @@ +// Package consts ... +// +// Description : consts ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-04-20 15:06 +package consts + +import ( + "encoding/json" + . "github.com/smartystreets/goconvey/convey" + "testing" +) + +func TestDataType_String(t *testing.T) { + Convey("data type 字符串值", t, func() { + byteData, err := json.Marshal(DataTypeString) + So(err, ShouldBeNil) + So(DataTypeString.String(), ShouldEqual, "string") + So(string(byteData), ShouldEqual, `"string"`) + }) + Convey("data type MarshalJSON", t, func() { + str, err := DataTypeString.MarshalJSON() + So(err, ShouldBeNil) + So(string(str), ShouldEqual, `"string"`) + dataList := []DataType{DataTypeString} + jsonData, err := json.Marshal(dataList) + So(err, ShouldBeNil) + So(string(jsonData), ShouldEqual, `["string"]`) + }) +} + +func TestDataType_IsValid(t *testing.T) { + Convey("data type 非法验证", t, func() { + So(DataType("Invalid").IsValid(), ShouldBeFalse) + }) + Convey("data type 合法验证", t, func() { + So(DataType("string").IsValid(), ShouldBeTrue) + }) +} + +func TestGetDataTypeDefaultValue(t *testing.T) { + Convey("int 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeInt), ShouldEqual, 0) + }) + Convey("int8 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeInt8), ShouldEqual, int8(0)) + }) + Convey("int16 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeInt16), ShouldEqual, int16(0)) + }) + Convey("int32 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeInt32), ShouldEqual, int32(0)) + }) + Convey("int64 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeInt64), ShouldEqual, int64(0)) + }) + Convey("uint 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeUint), ShouldEqual, uint(0)) + }) + Convey("uint8 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeUint8), ShouldEqual, uint8(0)) + }) + Convey("uint16 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeUint16), ShouldEqual, uint16(0)) + }) + Convey("uint32 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeUint32), ShouldEqual, uint32(0)) + }) + Convey("uint64 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeUint64), ShouldEqual, uint64(0)) + }) + Convey("float32 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeFloat32), ShouldEqual, float32(0)) + }) + Convey("uint64 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeFloat64), ShouldEqual, float64(0)) + }) + Convey("bool 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeBool), ShouldBeFalse) + }) + Convey("string 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeString), ShouldBeEmpty) + }) + Convey("IntPtr 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeIntPtr), ShouldEqual, new(int)) + }) + Convey("Int8Ptr 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeInt8Ptr), ShouldEqual, new(int8)) + }) + Convey("Int16Ptr 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeInt16Ptr), ShouldEqual, new(int16)) + }) + Convey("Int32Ptr 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeInt32Ptr), ShouldEqual, new(int32)) + }) + Convey("Int64Ptr 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeInt64Ptr), ShouldEqual, new(int64)) + }) + Convey("UintPtr 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeUintPtr), ShouldEqual, new(uint)) + }) + Convey("Uint8Ptr 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeUint8Ptr), ShouldEqual, new(uint8)) + }) + Convey("Uint16Ptr 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeUint16Ptr), ShouldEqual, new(uint16)) + }) + Convey("Uint32Ptr 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeUint32Ptr), ShouldEqual, new(uint32)) + }) + Convey("Uint64Ptr 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeUint64Ptr), ShouldEqual, new(uint64)) + }) + Convey("Float32Ptr 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeFloat32Ptr), ShouldEqual, new(float32)) + }) + Convey("Float64Ptr 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeFloat64Ptr), ShouldEqual, new(float64)) + }) + Convey("BoolPtr 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeBoolPtr), ShouldEqual, new(bool)) + }) + Convey("map[string]any 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeMapStrAny), ShouldEqual, map[string]any{}) + }) + Convey("map[any]any 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeMapAnyAny), ShouldEqual, map[any]any{}) + }) + Convey("map[string]bool 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeMapStrBool), ShouldEqual, map[string]bool{}) + }) + Convey("map[string]int 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeMapStrInt), ShouldEqual, map[string]int{}) + }) + Convey("map[string]float 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeMapStrFloat), ShouldEqual, map[string]float64{}) + }) + Convey("map[string]uint 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeMapStrUint), ShouldEqual, map[string]uint{}) + }) + Convey("map[string]slice 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeMapStrSlice), ShouldEqual, map[string][]any{}) + }) + Convey("map[string]string 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeMapStrStr), ShouldEqual, map[string]string{}) + }) + Convey("[]any 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeSliceAny), ShouldEqual, []any{}) + }) + Convey("[]string 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeSliceString), ShouldEqual, []string{}) + }) + Convey("[]bool 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeSliceBool), ShouldEqual, []bool{}) + }) + Convey("[]int 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeSliceInt), ShouldEqual, []int{}) + }) + Convey("[]uint 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeSliceUint), ShouldEqual, []uint{}) + }) + Convey("[]float 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeSliceFloat), ShouldEqual, []float64{}) + }) + Convey("[]map[string]any 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeSliceMapStringAny), ShouldEqual, []map[string]any{}) + }) + Convey("[]map[any]any 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeSliceMapAnyAny), ShouldEqual, []map[string]any{}) + }) + Convey("map marshal 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeMapStrAnyWithMarshal), ShouldEqual, "{}") + }) + Convey("slice marshal 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeSliceBoolWithMarshal), ShouldEqual, "[]") + }) + Convey("slice split 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeSliceIntWithChar), ShouldEqual, "") + }) + Convey("未枚举的alice 默认值", t, func() { + So(GetDataTypeDefaultValue(DataTypeSliceSlice), ShouldEqual, []any{}) + }) + Convey("unknown 默认值", t, func() { + So(GetDataTypeDefaultValue(DataType("unknown")), ShouldEqual, nil) + }) +} diff --git a/enums/data_location.go b/enums/data_location.go deleted file mode 100644 index 68ff7de..0000000 --- a/enums/data_location.go +++ /dev/null @@ -1,8 +0,0 @@ -// Package enums ... -// -// Description : enums ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 2024-11-25 14:39 -package enums diff --git a/enums/data_type.go b/enums/data_type.go deleted file mode 100644 index da55a81..0000000 --- a/enums/data_type.go +++ /dev/null @@ -1,8 +0,0 @@ -// Package enums ... -// -// Description : enums ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 2024-11-25 14:08 -package enums diff --git a/file_type.go b/file_type.go index 3db8248..58720bd 100644 --- a/file_type.go +++ b/file_type.go @@ -9,26 +9,17 @@ package consts type FileType string -func (ft *FileType) String() string { - return string(*ft) +func (ft FileType) String() string { + return string(ft) } -func (ft *FileType) MarshalJSON() ([]byte, error) { - return []byte(ft.String()), nil +func (ft FileType) MarshalJSON() ([]byte, error) { + return []byte(`"` + ft.String() + `"`), nil } -func (ft *FileType) IsValid() bool { - supportFileTypeList := []FileType{ - FileTypeJson, - FileTypeIni, - FileTypeYml, - FileTypeYaml, - FileTypeToml, - FileTypeXml, - FileTypeTxt, - } - for _, fileType := range supportFileTypeList { - if fileType == *ft { +func (ft FileType) IsValid() bool { + for _, fileType := range SupportFileTypeList { + if fileType.Value == ft || fileType.Value.String() == ft.String() { return true } } @@ -45,3 +36,41 @@ const ( FileTypeTxt FileType = "txt" // txt格式文件 FileTypeUnknown FileType = "unknown" // 未知格式 ) + +type FileTypeDesc struct { + Value FileType + Desc string +} + +var ( + SupportFileTypeList = []FileTypeDesc{ + { + Value: FileTypeJson, + Desc: "json格式文件", + }, + { + Value: FileTypeIni, + Desc: "init格式文件", + }, + { + Value: FileTypeYml, + Desc: "yml格式文件", + }, + { + Value: FileTypeYaml, + Desc: "yaml格式文件", + }, + { + Value: FileTypeToml, + Desc: "toml格式文件", + }, + { + Value: FileTypeTxt, + Desc: "txt格式文件", + }, + { + Value: FileTypeXml, + Desc: "xml格式文件", + }, + } +) diff --git a/file_type_test.go b/file_type_test.go new file mode 100644 index 0000000..040f9e9 --- /dev/null +++ b/file_type_test.go @@ -0,0 +1,41 @@ +// Package consts ... +// +// Description : consts ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-04-20 15:17 +package consts + +import ( + "encoding/json" + . "github.com/smartystreets/goconvey/convey" + "testing" +) + +func TestFileType_String(t *testing.T) { + Convey("file type 字符串值", t, func() { + byteData, err := json.Marshal(FileTypeJson) + So(err, ShouldBeNil) + So(FileTypeJson.String(), ShouldEqual, "json") + So(string(byteData), ShouldEqual, `"json"`) + }) + Convey("file type MarshalJSON", t, func() { + str, err := FileTypeJson.MarshalJSON() + So(err, ShouldBeNil) + So(string(str), ShouldEqual, `"json"`) + dataList := []FileType{FileTypeJson} + jsonData, err := json.Marshal(dataList) + So(err, ShouldBeNil) + So(string(jsonData), ShouldEqual, `["json"]`) + }) +} + +func TestFileType_IsValid(t *testing.T) { + Convey("file type 非法验证", t, func() { + So(FileType("Invalid").IsValid(), ShouldBeFalse) + }) + Convey("file type 合法验证", t, func() { + So(FileType("json").IsValid(), ShouldBeTrue) + }) +} diff --git a/header.go b/header.go index 55d772a..1339234 100644 --- a/header.go +++ b/header.go @@ -9,15 +9,15 @@ package consts type HttpHeader string -func (hh *HttpHeader) String() string { - return string(*hh) +func (hh HttpHeader) String() string { + return string(hh) } -func (hh *HttpHeader) MarshalJSON() ([]byte, error) { - return []byte(hh.String()), nil +func (hh HttpHeader) MarshalJSON() ([]byte, error) { + return []byte(`"` + hh.String() + `"`), nil } -var ( +const ( HeaderKeyAccessControlAllowOrigin HttpHeader = "Access-Control-Allow-Origin" // 标识允许哪个域的请求 HeaderKeyAccessControlAllowHeaders HttpHeader = "Access-Control-Allow-Headers" // 标识允许的请求header HeaderKeyAccessControlAllowMethods HttpHeader = "Access-Control-Allow-Methods" // 标识允许的请求方法 @@ -25,7 +25,7 @@ var ( HeaderKeyAccessControlMaxAge HttpHeader = "Access-Control-Max-Age" // 控制发送预检请求options的频率,单位 : 秒 ) -var ( +const ( HeaderKeyContentType HttpHeader = "Content-Type" // 请求头中Content-Type的key HeaderKeyReferer HttpHeader = "Referer" // 请求头中Referer的key HeaderKeyUserAgent HttpHeader = "User-Agent" // 请求头中User-Agent的key diff --git a/header_test.go b/header_test.go new file mode 100644 index 0000000..92b7093 --- /dev/null +++ b/header_test.go @@ -0,0 +1,32 @@ +// Package consts ... +// +// Description : consts ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-04-20 14:22 +package consts + +import ( + "encoding/json" + . "github.com/smartystreets/goconvey/convey" + "testing" +) + +func TestHttpHeader_String(t *testing.T) { + Convey("http header字符串值", t, func() { + byteData, err := json.Marshal(HeaderKeyContentType) + So(err, ShouldBeNil) + So(HeaderKeyContentType.String(), ShouldEqual, "Content-Type") + So(string(byteData), ShouldEqual, `"Content-Type"`) + }) + Convey("http header MarshalJSON", t, func() { + str, err := HeaderKeyContentType.MarshalJSON() + So(err, ShouldBeNil) + So(string(str), ShouldEqual, `"Content-Type"`) + dataList := []HttpHeader{HeaderKeyContentType} + jsonData, err := json.Marshal(dataList) + So(err, ShouldBeNil) + So(string(jsonData), ShouldEqual, `["Content-Type"]`) + }) +} diff --git a/http_code_test.go b/http_code_test.go new file mode 100644 index 0000000..6931ca5 --- /dev/null +++ b/http_code_test.go @@ -0,0 +1,27 @@ +// Package consts ... +// +// Description : consts ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-04-20 15:29 +package consts + +import ( + . "github.com/smartystreets/goconvey/convey" + "strings" + "testing" +) + +func Test_newHttpCodeData(t *testing.T) { + Convey("newHttpCode 验证", t, func() { + hCode := newHttpCodeData(499) + So(hCode.Code, ShouldEqual, 499) + So(hCode.Desc, ShouldEqual, "client request timeout") + So(hCode.Value, ShouldEqual, 499) + hCode = newHttpCodeData(4990) + So(hCode.Code, ShouldEqual, 4990) + So(strings.Contains(hCode.Desc, "unknown error"), ShouldBeTrue) + So(hCode.Value, ShouldEqual, 4990) + }) +} diff --git a/logger.go b/logger.go index bfe26e0..c6108ce 100644 --- a/logger.go +++ b/logger.go @@ -9,15 +9,15 @@ package consts type LogLevel string -func (ll *LogLevel) String() string { - return string(*ll) +func (ll LogLevel) String() string { + return string(ll) } -func (ll *LogLevel) MarshalJSON() ([]byte, error) { - return []byte(ll.String()), nil +func (ll LogLevel) MarshalJSON() ([]byte, error) { + return []byte(`"` + ll.String() + `"`), nil } -func (ll *LogLevel) IsValid() bool { +func (ll LogLevel) IsValid() bool { levelList := []LogLevel{ LogLevelDebug, LogLevelInfo, @@ -26,14 +26,14 @@ func (ll *LogLevel) IsValid() bool { LogLevelPanic, } for _, level := range levelList { - if level == *ll { + if level == ll { return true } } return false } -var ( +const ( LogLevelDebug LogLevel = "DEBUG" LogLevelInfo LogLevel = "INFO" LogLevelWarn LogLevel = "WARN" @@ -47,18 +47,12 @@ func (ls LogSplit) String() string { return string(ls) } func (ls LogSplit) MarshalJSON() ([]byte, error) { - return []byte(ls.String()), nil + return []byte(`"` + ls.String() + `"`), nil } func (ls LogSplit) IsValid() bool { - supportSplitList := []LogSplit{ - LogSplitHour, - LogSplitDay, - LogSplitMonth, - LogSplitYear, - } - for _, supportSplit := range supportSplitList { - if supportSplit == ls { + for _, supportSplit := range SupportLogSplitList { + if supportSplit.Value == ls || supportSplit.Value.String() == ls.String() { return true } } @@ -72,6 +66,32 @@ const ( LogSplitYear LogSplit = "YEAR" ) +type LogSplitDesc struct { + Value LogSplit `json:"value"` + Desc string `json:"desc"` +} + +var ( + SupportLogSplitList = []LogSplitDesc{ + { + Value: LogSplitHour, + Desc: "按小时切割", + }, + { + Value: LogSplitDay, + Desc: "按天切割", + }, + { + Value: LogSplitMonth, + Desc: "按月切割", + }, + { + Value: LogSplitYear, + Desc: "按年切割", + }, + } +) + const ( LogPathDefault = "logs" ) diff --git a/logger_test.go b/logger_test.go index baafa04..b7ee529 100644 --- a/logger_test.go +++ b/logger_test.go @@ -9,11 +9,60 @@ package consts import ( "encoding/json" - "fmt" + . "github.com/smartystreets/goconvey/convey" "testing" ) func TestLogLevel_String(t *testing.T) { - byteData, err := json.Marshal(LogLevelDebug) - fmt.Println(string(byteData), err) + Convey("logger type字符串值", t, func() { + byteData, err := json.Marshal(LogLevelDebug) + So(err, ShouldBeNil) + So(LogLevelDebug.String(), ShouldEqual, "DEBUG") + So(string(byteData), ShouldEqual, `"DEBUG"`) + }) + Convey("logger type MarshalJSON", t, func() { + str, err := LogLevelDebug.MarshalJSON() + So(err, ShouldBeNil) + So(string(str), ShouldEqual, `"DEBUG"`) + dataList := []LogLevel{LogLevelDebug} + jsonData, err := json.Marshal(dataList) + So(err, ShouldBeNil) + So(string(jsonData), ShouldEqual, `["DEBUG"]`) + }) +} + +func TestLogLevel_IsValid(t *testing.T) { + Convey("logger type 非法验证", t, func() { + So(LogLevel("Invalid").IsValid(), ShouldBeFalse) + }) + Convey("logger type 合法验证", t, func() { + So(LogLevelDebug.IsValid(), ShouldBeTrue) + }) +} + +func TestLogSplit_String(t *testing.T) { + Convey("logger split 字符串值", t, func() { + byteData, err := json.Marshal(LogSplitHour) + So(err, ShouldBeNil) + So(LogSplitHour.String(), ShouldEqual, "HOUR") + So(string(byteData), ShouldEqual, `"HOUR"`) + }) + Convey("logger split MarshalJSON", t, func() { + str, err := LogSplitHour.MarshalJSON() + So(err, ShouldBeNil) + So(string(str), ShouldEqual, `"HOUR"`) + dataList := []LogSplit{LogSplitHour} + jsonData, err := json.Marshal(dataList) + So(err, ShouldBeNil) + So(string(jsonData), ShouldEqual, `["HOUR"]`) + }) +} + +func TestLogSplit_IsValid(t *testing.T) { + Convey("logger split 非法验证", t, func() { + So(LogSplit("Invalid").IsValid(), ShouldBeFalse) + }) + Convey("logger split 合法验证", t, func() { + So(LogSplit("HOUR").IsValid(), ShouldBeTrue) + }) } diff --git a/redis.go b/redis.go index 0bd8a46..11ef9ce 100644 --- a/redis.go +++ b/redis.go @@ -9,15 +9,15 @@ package consts type RedisCmd string -func (rc *RedisCmd) String() string { - return string(*rc) +func (rc RedisCmd) String() string { + return string(rc) } -func (rc *RedisCmd) MarshalJSON() ([]byte, error) { - return []byte(rc.String()), nil +func (rc RedisCmd) MarshalJSON() ([]byte, error) { + return []byte(`"` + rc.String() + `"`), nil } -var ( +const ( RedisCommandExists RedisCmd = "EXISTS" RedisCommandTTL RedisCmd = "TTL" RedisCommandSet RedisCmd = "SET" diff --git a/redis_test.go b/redis_test.go index f9900b8..1b91860 100644 --- a/redis_test.go +++ b/redis_test.go @@ -24,6 +24,10 @@ func TestRedisCmd_String(t *testing.T) { Convey("redis cmd MarshalJSON", t, func() { str, err := RedisCommandSet.MarshalJSON() So(err, ShouldBeNil) - So(string(str), ShouldEqual, `SET`) + So(string(str), ShouldEqual, `"SET"`) + dataList := []RedisCmd{RedisCommandSet} + jsonData, err := json.Marshal(dataList) + So(err, ShouldBeNil) + So(string(jsonData), ShouldEqual, `["SET"]`) }) } diff --git a/request.go b/request.go index ab55710..c631a04 100644 --- a/request.go +++ b/request.go @@ -19,43 +19,19 @@ func (rdl RequestDataLocation) String() string { } func (rdl RequestDataLocation) MarshalJSON() ([]byte, error) { - return []byte(rdl.String()), nil + return []byte(`"` + rdl.String() + `"`), nil } func (rdl RequestDataLocation) IsValid() bool { for _, item := range RequestDataLocationList { - if item.Value == rdl { + if item.Value == rdl || item.Value.String() == rdl.String() { return true } } return false } -// ResponseDataLocation 响应数据所在位置 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 14:41 2024/11/25 -type ResponseDataLocation string - -func (rdl *ResponseDataLocation) String() string { - return string(*rdl) -} - -func (rdl *ResponseDataLocation) MarshalJSON() ([]byte, error) { - return []byte(rdl.String()), nil -} - -func (rdl *ResponseDataLocation) IsValid() bool { - for _, item := range ResponseDataLocationList { - if item.Value == *rdl { - return true - } - } - return false -} - -var ( +const ( RequestDataLocationHeader RequestDataLocation = "HEADER" // header RequestDataLocationCookie RequestDataLocation = "COOKIE" // cookie RequestDataLocationBody RequestDataLocation = "BODY" // body @@ -66,7 +42,31 @@ var ( RequestDataLocationCustomConfig RequestDataLocation = "CUSTOM_CONFIG" // 针对接口的一些自定义配置规则 ) -var ( +// ResponseDataLocation 响应数据所在位置 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 14:41 2024/11/25 +type ResponseDataLocation string + +func (rdl ResponseDataLocation) String() string { + return string(rdl) +} + +func (rdl ResponseDataLocation) MarshalJSON() ([]byte, error) { + return []byte(`"` + rdl.String() + `"`), nil +} + +func (rdl ResponseDataLocation) IsValid() bool { + for _, item := range ResponseDataLocationList { + if item.Value == rdl || item.Value.String() == rdl.String() { + return true + } + } + return false +} + +const ( ResponseDataLocationHeader ResponseDataLocation = "HEADER" // header ResponseDataLocationCookie ResponseDataLocation = "COOKIE" // cookie ResponseDataLocationBody ResponseDataLocation = "BODY" // body diff --git a/request_test.go b/request_test.go new file mode 100644 index 0000000..4715193 --- /dev/null +++ b/request_test.go @@ -0,0 +1,68 @@ +// Package consts ... +// +// Description : consts ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-04-20 14:37 +package consts + +import ( + "encoding/json" + . "github.com/smartystreets/goconvey/convey" + "testing" +) + +func TestRequestDataLocation_String(t *testing.T) { + Convey("request data location字符串值", t, func() { + byteData, err := json.Marshal(RequestDataLocationBody) + So(err, ShouldBeNil) + So(RequestDataLocationBody.String(), ShouldEqual, "BODY") + So(string(byteData), ShouldEqual, `"BODY"`) + }) + Convey("request data location MarshalJSON", t, func() { + str, err := RequestDataLocationBody.MarshalJSON() + So(err, ShouldBeNil) + So(string(str), ShouldEqual, `"BODY"`) + dataList := []RequestDataLocation{RequestDataLocationBody} + jsonData, err := json.Marshal(dataList) + So(err, ShouldBeNil) + So(string(jsonData), ShouldEqual, `["BODY"]`) + }) +} + +func TestRequestDataLocation_IsValid(t *testing.T) { + Convey("request data location 非法验证", t, func() { + So(RequestDataLocation("Invalid").IsValid(), ShouldBeFalse) + }) + Convey("request data location 合法验证", t, func() { + So(RequestDataLocation("BODY").IsValid(), ShouldBeTrue) + }) +} + +func TestResponseDataLocation_String(t *testing.T) { + Convey("response data location字符串值", t, func() { + byteData, err := json.Marshal(ResponseDataLocationBody) + So(err, ShouldBeNil) + So(RequestDataLocationBody.String(), ShouldEqual, "BODY") + So(string(byteData), ShouldEqual, `"BODY"`) + }) + Convey("response data location MarshalJSON", t, func() { + str, err := ResponseDataLocationBody.MarshalJSON() + So(err, ShouldBeNil) + So(string(str), ShouldEqual, `"BODY"`) + dataList := []ResponseDataLocation{ResponseDataLocationBody} + jsonData, err := json.Marshal(dataList) + So(err, ShouldBeNil) + So(string(jsonData), ShouldEqual, `["BODY"]`) + }) +} + +func TestResponseDataLocation_IsValid(t *testing.T) { + Convey("response data location 非法验证", t, func() { + So(ResponseDataLocation("Invalid").IsValid(), ShouldBeFalse) + }) + Convey("response data location 合法验证", t, func() { + So(ResponseDataLocation("BODY").IsValid(), ShouldBeTrue) + }) +} diff --git a/scheme.go b/scheme.go index 18abbfe..c7371cb 100644 --- a/scheme.go +++ b/scheme.go @@ -9,12 +9,12 @@ package consts type HttpScheme string -func (hs *HttpScheme) String() string { - return string(*hs) +func (hs HttpScheme) String() string { + return string(hs) } -func (hs *HttpScheme) MarshalJSON() ([]byte, error) { - return []byte(hs.String()), nil +func (hs HttpScheme) MarshalJSON() ([]byte, error) { + return []byte(`"` + hs.String() + `"`), nil } var ( diff --git a/scheme_test.go b/scheme_test.go new file mode 100644 index 0000000..c430d00 --- /dev/null +++ b/scheme_test.go @@ -0,0 +1,32 @@ +// Package consts ... +// +// Description : consts ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-04-20 15:47 +package consts + +import ( + "encoding/json" + . "github.com/smartystreets/goconvey/convey" + "testing" +) + +func TestHttpScheme_String(t *testing.T) { + Convey("scheme 字符串值", t, func() { + byteData, err := json.Marshal(SchemeHTTPS) + So(err, ShouldBeNil) + So(SchemeHTTPS.String(), ShouldEqual, "https") + So(string(byteData), ShouldEqual, `"https"`) + }) + Convey("scheme MarshalJSON", t, func() { + str, err := SchemeHTTPS.MarshalJSON() + So(err, ShouldBeNil) + So(string(str), ShouldEqual, `"https"`) + dataList := []HttpScheme{SchemeHTTPS} + jsonData, err := json.Marshal(dataList) + So(err, ShouldBeNil) + So(string(jsonData), ShouldEqual, `["https"]`) + }) +} diff --git a/validator.go b/validator.go index 91c203c..3f9abe6 100644 --- a/validator.go +++ b/validator.go @@ -11,25 +11,22 @@ package consts type ValidatorRule string // 验证规则 -func (vr *ValidatorRule) String() string { - if nil == vr { - return "" - } - return string(*vr) +func (vr ValidatorRule) String() string { + return string(vr) } -func (vr *ValidatorRule) MarshalJSON() ([]byte, error) { - return []byte(vr.String()), nil +func (vr ValidatorRule) MarshalJSON() ([]byte, error) { + return []byte(`"` + vr.String() + `"`), nil } // IsValid 验证规则是否有效 -func (vr *ValidatorRule) IsValid() bool { - _, exist := ValidatorRuleSupportDataTypeTable[*vr] +func (vr ValidatorRule) IsValid() bool { + _, exist := ValidatorRuleSupportDataTypeTable[vr] if !exist { return false } - rule := ValidatorRuleSupportDataTypeTable[*vr].ValidatorRule - if vr.String() != (&rule).String() { + rule := ValidatorRuleSupportDataTypeTable[vr].ValidatorRule + if vr.String() != rule.String() { return false } return exist @@ -40,8 +37,8 @@ func (vr *ValidatorRule) IsValid() bool { // Author : go_developer@163.com<白茶清欢> // // Date : 16:10 2025/1/22 -func (vr *ValidatorRule) Config() ValidatorRuleConfig { - return ValidatorRuleSupportDataTypeTable[*vr] +func (vr ValidatorRule) Config() ValidatorRuleConfig { + return ValidatorRuleSupportDataTypeTable[vr] } // IsSupportDataType 是否支持指定的数据类型 @@ -49,7 +46,7 @@ func (vr *ValidatorRule) Config() ValidatorRuleConfig { // Author : go_developer@163.com<白茶清欢> // // Date : 16:11 2025/1/22 -func (vr *ValidatorRule) IsSupportDataType(dataType DataType) bool { +func (vr ValidatorRule) IsSupportDataType(dataType DataType) bool { dataTypeList := vr.Config().SupportDataTypeList if len(dataTypeList) == 0 { // 未配置则认为支持 diff --git a/validator_test.go b/validator_test.go new file mode 100644 index 0000000..2a1f39a --- /dev/null +++ b/validator_test.go @@ -0,0 +1,64 @@ +// Package consts ... +// +// Description : consts ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-04-20 17:37 +package consts + +import ( + "encoding/json" + . "github.com/smartystreets/goconvey/convey" + "testing" +) + +func TestValidatorRule_String(t *testing.T) { + Convey("validate rule 字符串值", t, func() { + byteData, err := json.Marshal(ValidateRuleGt) + So(err, ShouldBeNil) + So(ValidateRuleGt.String(), ShouldEqual, "gt") + So(string(byteData), ShouldEqual, `"gt"`) + }) + Convey("validate rule MarshalJSON", t, func() { + str, err := ValidateRuleGt.MarshalJSON() + So(err, ShouldBeNil) + So(string(str), ShouldEqual, `"gt"`) + dataList := []ValidatorRule{ValidateRuleGt} + jsonData, err := json.Marshal(dataList) + So(err, ShouldBeNil) + So(string(jsonData), ShouldEqual, `["gt"]`) + }) +} + +func TestValidatorRule_IsValid(t *testing.T) { + Convey("validator rule 不存在", t, func() { + So(ValidatorRule("Invalid").IsValid(), ShouldBeFalse) + }) + Convey("validator rule 配置值错误", t, func() { + ValidatorRuleSupportDataTypeTable[ValidatorRule("Invalid")] = ValidatorRuleConfig{} + So(ValidatorRule("Invalid").IsValid(), ShouldBeFalse) + So(ValidatorRule("gt").IsValid(), ShouldBeTrue) + }) + Convey("validator rule 配置值", t, func() { + So(ValidatorRuleGtcsfield.Config().MinParamCnt, ShouldEqual, 1) + }) +} + +func TestValidatorRule_IsSupportDataType(t *testing.T) { + Convey("validator rule 是否支持指定数据类型", t, func() { + So(ValidatorRuleGtcsfield.IsSupportDataType(DataTypeUint), ShouldBeTrue) + So(ValidatorRuleGtcsfield.IsSupportDataType(DataTypeString), ShouldBeFalse) + }) + Convey("validator rule 未指定类型默认支持", t, func() { + So(ValidatorRuleCommonOmitempty.IsSupportDataType(DataTypeString), ShouldBeTrue) + }) +} + +func TestRegisterCustomValidatorRule(t *testing.T) { + Convey("注册自定义验证规则", t, func() { + RegisterCustomValidatorRule(ValidatorRule("Invalid"), ValidatorRuleConfig{}) + _, exist := ValidatorRuleSupportDataTypeTable[ValidatorRule("Invalid")] + So(exist, ShouldBeTrue) + }) +}