Skip to content

Commit 4465100

Browse files
author
v_llxjliu
committed
fix: 适配 gf框架 gtime
1 parent f9810b5 commit 4465100

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

filter/interface.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package filter
2+
3+
import "github.com/gogf/gf/v2/os/gtime"
4+
5+
type GTime interface {
6+
Clone() *gtime.Time
7+
}

filter/parser.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package filter
22

33
import (
44
"encoding"
5+
"encoding/json"
56
"fmt"
67
"reflect"
8+
"strings"
79
)
810

911
type tagInfo struct {
@@ -244,7 +246,7 @@ func parserStruct(typeOf reflect.Type, valueOf reflect.Value, t *fieldNodeTree,
244246
IsAnonymous: isAnonymous,
245247
}
246248
value := valueOf.Field(i)
247-
if tag.Function != "" {
249+
if tag.Function != "" { //解析并调用func选择器
248250
function := valueOf.MethodByName(tag.Function)
249251
if !function.IsValid() {
250252
if valueOf.CanAddr() {
@@ -281,6 +283,19 @@ func parserStruct(typeOf reflect.Type, valueOf reflect.Value, t *fieldNodeTree,
281283
}
282284
}
283285

286+
valueInterface := value.Interface()
287+
if v, ok := valueInterface.(json.Marshaler); ok {
288+
if _, ok1 := value.Addr().Interface().(GTime); ok1 {
289+
marshalJSON, err := v.MarshalJSON()
290+
if err != nil {
291+
fmt.Println("json marshal error:", err)
292+
} else {
293+
str := string(marshalJSON)
294+
value = reflect.ValueOf(strings.Trim(str, `"`))
295+
}
296+
}
297+
298+
}
284299
tree.parseAny(tag.UseFieldName, scene, value, isSelect)
285300

286301
if t.IsAnonymous {

go.mod

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
module github.com/liu-cn/json-filter
22

3-
go 1.17
3+
go 1.17
4+
5+
require github.com/gogf/gf/v2 v2.7.1
6+
7+
require (
8+
go.opentelemetry.io/otel v1.14.0 // indirect
9+
go.opentelemetry.io/otel/trace v1.14.0 // indirect
10+
)

test/test_time_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/gogf/gf/v2/os/gtime"
6+
"github.com/liu-cn/json-filter/filter"
7+
"testing"
8+
"time"
9+
)
10+
11+
type GTime struct {
12+
Create *gtime.Time `json:"create,select(timeTest)"`
13+
Test string `json:"test,select(timeTest)"`
14+
}
15+
16+
func TestGTime(t *testing.T) {
17+
18+
gt := GTime{
19+
Create: gtime.New(time.Now()),
20+
Test: "test",
21+
}
22+
fmt.Println(filter.Select("timeTest", &gt))
23+
}

0 commit comments

Comments
 (0)