增加指针 nil 验证

This commit is contained in:
白茶清欢 2024-09-05 17:49:50 +08:00
parent a6583fe84d
commit 98d9eec6bb
2 changed files with 26 additions and 0 deletions

5
run.go
View File

@ -153,6 +153,11 @@ func getDataStatus(val gjson.Result, dataType string) string {
if len(val.Map()) == 0 {
return consts.DataStatusIsEmpty
}
} else if strings.HasPrefix(dataType, "*") {
// 指针类型
if nil == val.Value() {
return consts.DataStatusIsNil
}
}
}
return ""

View File

@ -10,6 +10,8 @@ package validator
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/tidwall/gjson"
"testing"
)
@ -21,3 +23,22 @@ func TestRun(t *testing.T) {
byteData, _ := json.Marshal(sourceData)
fmt.Println(string(byteData))
}
func Test_getDataStatus(t *testing.T) {
type args struct {
val gjson.Result
dataType string
}
tests := []struct {
name string
args args
want string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equalf(t, tt.want, getDataStatus(tt.args.val, tt.args.dataType), "getDataStatus(%v, %v)", tt.args.val, tt.args.dataType)
})
}
}