66 "sort"
77 "strconv"
88 "strings"
9+ "time"
910)
1011
1112const (
@@ -39,15 +40,7 @@ func (w *walker) walk(modified, current reflect.Value, pointer JSONPointer) erro
3940 case reflect .Interface :
4041 return w .processInterface (modified , current , pointer )
4142 case reflect .String :
42- if modified .String () != current .String () {
43- if modified .String () == "" {
44- w .remove (pointer , current .String ())
45- } else if current .String () == "" {
46- w .add (pointer , modified .String ())
47- } else {
48- w .replace (pointer , modified .String (), current .String ())
49- }
50- }
43+ return w .processString (modified , current , pointer )
5144 case reflect .Int , reflect .Int8 , reflect .Int16 , reflect .Int32 , reflect .Int64 :
5245 if modified .Int () != current .Int () {
5346 w .replace (pointer , modified .Int (), current .Int ())
@@ -80,6 +73,21 @@ func (w *walker) processInterface(modified reflect.Value, current reflect.Value,
8073 return nil
8174}
8275
76+ // processString processes reflect.String values
77+ func (w * walker ) processString (modified reflect.Value , current reflect.Value , pointer JSONPointer ) error {
78+ if modified .String () != current .String () {
79+ if modified .String () == "" {
80+ w .remove (pointer , current .String ())
81+ } else if current .String () == "" {
82+ w .add (pointer , modified .String ())
83+ } else {
84+ w .replace (pointer , modified .String (), current .String ())
85+ }
86+ }
87+
88+ return nil
89+ }
90+
8391// processMap processes reflect.Map values
8492func (w * walker ) processMap (modified reflect.Value , current reflect.Value , pointer JSONPointer ) error {
8593 // NOTE: currently only map[string]interface{} are supported
@@ -248,6 +256,19 @@ func (w *walker) processStruct(modified, current reflect.Value, pointer JSONPoin
248256 return nil
249257 }
250258
259+ if modified .Type ().PkgPath () == "time" && modified .Type ().Name () == "Time" {
260+ m , err := toTimeStrValue (modified )
261+ if err != nil {
262+ return err
263+ }
264+
265+ c , err := toTimeStrValue (current )
266+ if err != nil {
267+ return err
268+ }
269+ return w .processString (m , c , pointer )
270+ }
271+
251272 // process all struct fields, the order of the fields of the modified and current JSON object is identical because their types match
252273 for j := 0 ; j < modified .NumField (); j ++ {
253274 tag := strings .Split (modified .Type ().Field (j ).Tag .Get (jsonTag ), "," )[0 ]
@@ -264,6 +285,14 @@ func (w *walker) processStruct(modified, current reflect.Value, pointer JSONPoin
264285 return nil
265286}
266287
288+ func toTimeStrValue (v reflect.Value ) (reflect.Value , error ) {
289+ t , err := v .Interface ().(time.Time ).MarshalText ()
290+ if err != nil {
291+ return reflect.Value {}, err
292+ }
293+ return reflect .ValueOf (string (t )), nil
294+ }
295+
267296// extractIgnoreSliceOrderMatchValue extracts the value which is used to match the modified and current values to ignore the slice order
268297func extractIgnoreSliceOrderMatchValue (value reflect.Value , fieldName string ) string {
269298 switch value .Kind () {
0 commit comments