修复相关BUG + 增加单元测试, 覆盖率100%

This commit is contained in:
白茶清欢 2025-04-20 17:57:01 +08:00
parent 299edfcc9a
commit fb1d6bb34f
19 changed files with 669 additions and 134 deletions

View File

@ -14,12 +14,12 @@ package consts
// Date : 14:10 2024/11/25 // Date : 14:10 2024/11/25
type DataType string type DataType string
func (df *DataType) String() string { func (df DataType) String() string {
return string(*df) return string(df)
} }
func (df *DataType) MarshalJSON() ([]byte, error) { func (df DataType) MarshalJSON() ([]byte, error) {
return []byte(df.String()), nil return []byte(`"` + df.String() + `"`), nil
} }
// IsValid 判断枚举值是否有效 // IsValid 判断枚举值是否有效
@ -27,9 +27,9 @@ func (df *DataType) MarshalJSON() ([]byte, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 14:45 2024/11/25 // Date : 14:45 2024/11/25
func (df *DataType) IsValid() bool { func (df DataType) IsValid() bool {
for _, item := range DataTypeList { for _, item := range DataTypeList {
if item.Value == *df { if item.Value == df || item.Value.String() == df.String() {
return true return true
} }
} }
@ -369,49 +369,49 @@ func GetDataTypeDefaultValue(dataType DataType) any {
case DataTypeBoolPtr: case DataTypeBoolPtr:
return new(bool) return new(bool)
case DataTypeMapStrAny: case DataTypeMapStrAny:
return new(map[string]any) return map[string]any{}
case DataTypeMapAnyAny: case DataTypeMapAnyAny:
return new(map[any]any) return map[any]any{}
case DataTypeMapStrBool: case DataTypeMapStrBool:
return new(map[string]bool) return map[string]bool{}
case DataTypeMapStrInt: case DataTypeMapStrInt:
return new(map[string]int) return map[string]int{}
case DataTypeMapStrFloat: case DataTypeMapStrFloat:
return new(map[string]float64) return map[string]float64{}
case DataTypeMapStrUint: case DataTypeMapStrUint:
return new(map[string]uint) return map[string]uint{}
case DataTypeMapStrSlice: case DataTypeMapStrSlice:
return new(map[string][]any) return map[string][]any{}
case DataTypeMapStrStr: case DataTypeMapStrStr:
return new(map[string]string) return map[string]string{}
case DataTypeSliceAny: case DataTypeSliceAny:
return new([]any) return []any{}
case DataTypeSliceString: case DataTypeSliceString:
return new([]string) return []string{}
case DataTypeSliceBool: case DataTypeSliceBool:
return new([]bool) return []bool{}
case DataTypeSliceInt: case DataTypeSliceInt:
return new([]int) return []int{}
case DataTypeSliceUint: case DataTypeSliceUint:
return new([]uint) return []uint{}
case DataTypeSliceFloat: case DataTypeSliceFloat:
return new([]float64) return []float64{}
case DataTypeSliceMapAnyAny: case DataTypeSliceMapAnyAny:
// map[any]any 序列化会有问题,当做 map[string]any 处理 // map[any]any 序列化会有问题,当做 map[string]any 处理
return new([]map[string]any) return []map[string]any{}
case DataTypeSliceMapStringAny: case DataTypeSliceMapStringAny:
return new([]map[string]any) return []map[string]any{}
default: default:
// 序列化之后的map // 序列化之后的map
for _, dataTypeItem := range DataTypeMapMarshal { for _, dataTypeItem := range DataTypeMapMarshal {
if dataTypeItem == dataType { if dataTypeItem == dataType {
return "" return "{}"
} }
} }
// 序列化之后的slice // 序列化之后的slice
for _, dataTypeItem := range DataTypeSliceMarshal { for _, dataTypeItem := range DataTypeSliceMarshal {
if dataTypeItem == dataType { if dataTypeItem == dataType {
return "" return "[]"
} }
} }
// 指定分隔符分割的slice // 指定分隔符分割的slice
@ -423,7 +423,7 @@ func GetDataTypeDefaultValue(dataType DataType) any {
// 未枚举的slice类型用any接收 // 未枚举的slice类型用any接收
for _, dataTypeItem := range DataTypeSlice { for _, dataTypeItem := range DataTypeSlice {
if dataTypeItem == dataType { if dataTypeItem == dataType {
return new([]any) return []any{}
} }
} }
} }

188
data_type_test.go Normal file
View File

@ -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)
})
}

View File

@ -1,8 +0,0 @@
// Package enums ...
//
// Description : enums ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-11-25 14:39
package enums

View File

@ -1,8 +0,0 @@
// Package enums ...
//
// Description : enums ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-11-25 14:08
package enums

View File

@ -9,26 +9,17 @@ package consts
type FileType string type FileType string
func (ft *FileType) String() string { func (ft FileType) String() string {
return string(*ft) return string(ft)
} }
func (ft *FileType) MarshalJSON() ([]byte, error) { func (ft FileType) MarshalJSON() ([]byte, error) {
return []byte(ft.String()), nil return []byte(`"` + ft.String() + `"`), nil
} }
func (ft *FileType) IsValid() bool { func (ft FileType) IsValid() bool {
supportFileTypeList := []FileType{ for _, fileType := range SupportFileTypeList {
FileTypeJson, if fileType.Value == ft || fileType.Value.String() == ft.String() {
FileTypeIni,
FileTypeYml,
FileTypeYaml,
FileTypeToml,
FileTypeXml,
FileTypeTxt,
}
for _, fileType := range supportFileTypeList {
if fileType == *ft {
return true return true
} }
} }
@ -45,3 +36,41 @@ const (
FileTypeTxt FileType = "txt" // txt格式文件 FileTypeTxt FileType = "txt" // txt格式文件
FileTypeUnknown FileType = "unknown" // 未知格式 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格式文件",
},
}
)

41
file_type_test.go Normal file
View File

@ -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)
})
}

View File

@ -9,15 +9,15 @@ package consts
type HttpHeader string type HttpHeader string
func (hh *HttpHeader) String() string { func (hh HttpHeader) String() string {
return string(*hh) return string(hh)
} }
func (hh *HttpHeader) MarshalJSON() ([]byte, error) { func (hh HttpHeader) MarshalJSON() ([]byte, error) {
return []byte(hh.String()), nil return []byte(`"` + hh.String() + `"`), nil
} }
var ( const (
HeaderKeyAccessControlAllowOrigin HttpHeader = "Access-Control-Allow-Origin" // 标识允许哪个域的请求 HeaderKeyAccessControlAllowOrigin HttpHeader = "Access-Control-Allow-Origin" // 标识允许哪个域的请求
HeaderKeyAccessControlAllowHeaders HttpHeader = "Access-Control-Allow-Headers" // 标识允许的请求header HeaderKeyAccessControlAllowHeaders HttpHeader = "Access-Control-Allow-Headers" // 标识允许的请求header
HeaderKeyAccessControlAllowMethods HttpHeader = "Access-Control-Allow-Methods" // 标识允许的请求方法 HeaderKeyAccessControlAllowMethods HttpHeader = "Access-Control-Allow-Methods" // 标识允许的请求方法
@ -25,7 +25,7 @@ var (
HeaderKeyAccessControlMaxAge HttpHeader = "Access-Control-Max-Age" // 控制发送预检请求options的频率单位 : 秒 HeaderKeyAccessControlMaxAge HttpHeader = "Access-Control-Max-Age" // 控制发送预检请求options的频率单位 : 秒
) )
var ( const (
HeaderKeyContentType HttpHeader = "Content-Type" // 请求头中Content-Type的key HeaderKeyContentType HttpHeader = "Content-Type" // 请求头中Content-Type的key
HeaderKeyReferer HttpHeader = "Referer" // 请求头中Referer的key HeaderKeyReferer HttpHeader = "Referer" // 请求头中Referer的key
HeaderKeyUserAgent HttpHeader = "User-Agent" // 请求头中User-Agent的key HeaderKeyUserAgent HttpHeader = "User-Agent" // 请求头中User-Agent的key

32
header_test.go Normal file
View File

@ -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"]`)
})
}

27
http_code_test.go Normal file
View File

@ -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)
})
}

View File

@ -9,15 +9,15 @@ package consts
type LogLevel string type LogLevel string
func (ll *LogLevel) String() string { func (ll LogLevel) String() string {
return string(*ll) return string(ll)
} }
func (ll *LogLevel) MarshalJSON() ([]byte, error) { func (ll LogLevel) MarshalJSON() ([]byte, error) {
return []byte(ll.String()), nil return []byte(`"` + ll.String() + `"`), nil
} }
func (ll *LogLevel) IsValid() bool { func (ll LogLevel) IsValid() bool {
levelList := []LogLevel{ levelList := []LogLevel{
LogLevelDebug, LogLevelDebug,
LogLevelInfo, LogLevelInfo,
@ -26,14 +26,14 @@ func (ll *LogLevel) IsValid() bool {
LogLevelPanic, LogLevelPanic,
} }
for _, level := range levelList { for _, level := range levelList {
if level == *ll { if level == ll {
return true return true
} }
} }
return false return false
} }
var ( const (
LogLevelDebug LogLevel = "DEBUG" LogLevelDebug LogLevel = "DEBUG"
LogLevelInfo LogLevel = "INFO" LogLevelInfo LogLevel = "INFO"
LogLevelWarn LogLevel = "WARN" LogLevelWarn LogLevel = "WARN"
@ -47,18 +47,12 @@ func (ls LogSplit) String() string {
return string(ls) return string(ls)
} }
func (ls LogSplit) MarshalJSON() ([]byte, error) { func (ls LogSplit) MarshalJSON() ([]byte, error) {
return []byte(ls.String()), nil return []byte(`"` + ls.String() + `"`), nil
} }
func (ls LogSplit) IsValid() bool { func (ls LogSplit) IsValid() bool {
supportSplitList := []LogSplit{ for _, supportSplit := range SupportLogSplitList {
LogSplitHour, if supportSplit.Value == ls || supportSplit.Value.String() == ls.String() {
LogSplitDay,
LogSplitMonth,
LogSplitYear,
}
for _, supportSplit := range supportSplitList {
if supportSplit == ls {
return true return true
} }
} }
@ -72,6 +66,32 @@ const (
LogSplitYear LogSplit = "YEAR" 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 ( const (
LogPathDefault = "logs" LogPathDefault = "logs"
) )

View File

@ -9,11 +9,60 @@ package consts
import ( import (
"encoding/json" "encoding/json"
"fmt" . "github.com/smartystreets/goconvey/convey"
"testing" "testing"
) )
func TestLogLevel_String(t *testing.T) { func TestLogLevel_String(t *testing.T) {
byteData, err := json.Marshal(LogLevelDebug) Convey("logger type字符串值", t, func() {
fmt.Println(string(byteData), err) 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)
})
} }

View File

@ -9,15 +9,15 @@ package consts
type RedisCmd string type RedisCmd string
func (rc *RedisCmd) String() string { func (rc RedisCmd) String() string {
return string(*rc) return string(rc)
} }
func (rc *RedisCmd) MarshalJSON() ([]byte, error) { func (rc RedisCmd) MarshalJSON() ([]byte, error) {
return []byte(rc.String()), nil return []byte(`"` + rc.String() + `"`), nil
} }
var ( const (
RedisCommandExists RedisCmd = "EXISTS" RedisCommandExists RedisCmd = "EXISTS"
RedisCommandTTL RedisCmd = "TTL" RedisCommandTTL RedisCmd = "TTL"
RedisCommandSet RedisCmd = "SET" RedisCommandSet RedisCmd = "SET"

View File

@ -24,6 +24,10 @@ func TestRedisCmd_String(t *testing.T) {
Convey("redis cmd MarshalJSON", t, func() { Convey("redis cmd MarshalJSON", t, func() {
str, err := RedisCommandSet.MarshalJSON() str, err := RedisCommandSet.MarshalJSON()
So(err, ShouldBeNil) 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"]`)
}) })
} }

View File

@ -19,43 +19,19 @@ func (rdl RequestDataLocation) String() string {
} }
func (rdl RequestDataLocation) MarshalJSON() ([]byte, error) { func (rdl RequestDataLocation) MarshalJSON() ([]byte, error) {
return []byte(rdl.String()), nil return []byte(`"` + rdl.String() + `"`), nil
} }
func (rdl RequestDataLocation) IsValid() bool { func (rdl RequestDataLocation) IsValid() bool {
for _, item := range RequestDataLocationList { for _, item := range RequestDataLocationList {
if item.Value == rdl { if item.Value == rdl || item.Value.String() == rdl.String() {
return true return true
} }
} }
return false return false
} }
// ResponseDataLocation 响应数据所在位置 const (
//
// 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 (
RequestDataLocationHeader RequestDataLocation = "HEADER" // header RequestDataLocationHeader RequestDataLocation = "HEADER" // header
RequestDataLocationCookie RequestDataLocation = "COOKIE" // cookie RequestDataLocationCookie RequestDataLocation = "COOKIE" // cookie
RequestDataLocationBody RequestDataLocation = "BODY" // body RequestDataLocationBody RequestDataLocation = "BODY" // body
@ -66,7 +42,31 @@ var (
RequestDataLocationCustomConfig RequestDataLocation = "CUSTOM_CONFIG" // 针对接口的一些自定义配置规则 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 ResponseDataLocationHeader ResponseDataLocation = "HEADER" // header
ResponseDataLocationCookie ResponseDataLocation = "COOKIE" // cookie ResponseDataLocationCookie ResponseDataLocation = "COOKIE" // cookie
ResponseDataLocationBody ResponseDataLocation = "BODY" // body ResponseDataLocationBody ResponseDataLocation = "BODY" // body

68
request_test.go Normal file
View File

@ -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)
})
}

View File

@ -9,12 +9,12 @@ package consts
type HttpScheme string type HttpScheme string
func (hs *HttpScheme) String() string { func (hs HttpScheme) String() string {
return string(*hs) return string(hs)
} }
func (hs *HttpScheme) MarshalJSON() ([]byte, error) { func (hs HttpScheme) MarshalJSON() ([]byte, error) {
return []byte(hs.String()), nil return []byte(`"` + hs.String() + `"`), nil
} }
var ( var (

32
scheme_test.go Normal file
View File

@ -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"]`)
})
}

View File

@ -11,25 +11,22 @@ package consts
type ValidatorRule string // 验证规则 type ValidatorRule string // 验证规则
func (vr *ValidatorRule) String() string { func (vr ValidatorRule) String() string {
if nil == vr { return string(vr)
return ""
}
return string(*vr)
} }
func (vr *ValidatorRule) MarshalJSON() ([]byte, error) { func (vr ValidatorRule) MarshalJSON() ([]byte, error) {
return []byte(vr.String()), nil return []byte(`"` + vr.String() + `"`), nil
} }
// IsValid 验证规则是否有效 // IsValid 验证规则是否有效
func (vr *ValidatorRule) IsValid() bool { func (vr ValidatorRule) IsValid() bool {
_, exist := ValidatorRuleSupportDataTypeTable[*vr] _, exist := ValidatorRuleSupportDataTypeTable[vr]
if !exist { if !exist {
return false return false
} }
rule := ValidatorRuleSupportDataTypeTable[*vr].ValidatorRule rule := ValidatorRuleSupportDataTypeTable[vr].ValidatorRule
if vr.String() != (&rule).String() { if vr.String() != rule.String() {
return false return false
} }
return exist return exist
@ -40,8 +37,8 @@ func (vr *ValidatorRule) IsValid() bool {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 16:10 2025/1/22 // Date : 16:10 2025/1/22
func (vr *ValidatorRule) Config() ValidatorRuleConfig { func (vr ValidatorRule) Config() ValidatorRuleConfig {
return ValidatorRuleSupportDataTypeTable[*vr] return ValidatorRuleSupportDataTypeTable[vr]
} }
// IsSupportDataType 是否支持指定的数据类型 // IsSupportDataType 是否支持指定的数据类型
@ -49,7 +46,7 @@ func (vr *ValidatorRule) Config() ValidatorRuleConfig {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 16:11 2025/1/22 // Date : 16:11 2025/1/22
func (vr *ValidatorRule) IsSupportDataType(dataType DataType) bool { func (vr ValidatorRule) IsSupportDataType(dataType DataType) bool {
dataTypeList := vr.Config().SupportDataTypeList dataTypeList := vr.Config().SupportDataTypeList
if len(dataTypeList) == 0 { if len(dataTypeList) == 0 {
// 未配置则认为支持 // 未配置则认为支持

64
validator_test.go Normal file
View File

@ -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)
})
}