From 98d9eec6bbc3a520f38c31217b0861c7823eac28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 5 Sep 2024 17:49:50 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8C=87=E9=92=88=20nil=20?= =?UTF-8?q?=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- run.go | 5 +++++ run_test.go | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/run.go b/run.go index b060c28..1e8f0c5 100644 --- a/run.go +++ b/run.go @@ -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 "" diff --git a/run_test.go b/run_test.go index b3581e0..989110b 100644 --- a/run_test.go +++ b/run_test.go @@ -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) + }) + } +}