@@ -113,27 +113,6 @@ func (u *Unmarshaler) unmarshalValuer(m Valuer, v any, fullName string) error {
113
113
return u .unmarshalWithFullName (simpleValuer {current : m }, v , fullName )
114
114
}
115
115
116
- func (u * Unmarshaler ) fillJsonUnmarshalerStruct (fieldType reflect.Type ,
117
- value reflect.Value , targetValue string ) error {
118
- if ! value .CanSet () {
119
- return errValueNotSettable
120
- }
121
-
122
- baseType := Deref (fieldType )
123
- target := reflect .New (baseType )
124
- unmarshaler , ok := target .Interface ().(json.Unmarshaler )
125
- if ! ok {
126
- return errUnsupportedType
127
- }
128
-
129
- if err := unmarshaler .UnmarshalJSON ([]byte (targetValue )); err != nil {
130
- return err
131
- }
132
-
133
- value .Set (target )
134
- return nil
135
- }
136
-
137
116
func (u * Unmarshaler ) fillMap (fieldType reflect.Type , value reflect.Value ,
138
117
mapValue any , fullName string ) error {
139
118
if ! value .CanSet () {
@@ -330,6 +309,32 @@ func (u *Unmarshaler) fillSliceWithDefault(derefedType reflect.Type, value refle
330
309
return u .fillSlice (derefedType , value , slice , fullName )
331
310
}
332
311
312
+ func (u * Unmarshaler ) fillUnmarshalerStruct (fieldType reflect.Type ,
313
+ value reflect.Value , targetValue string ) error {
314
+ if ! value .CanSet () {
315
+ return errValueNotSettable
316
+ }
317
+
318
+ baseType := Deref (fieldType )
319
+ target := reflect .New (baseType )
320
+ switch u .key {
321
+ case jsonTagKey :
322
+ unmarshaler , ok := target .Interface ().(json.Unmarshaler )
323
+ if ! ok {
324
+ return errUnsupportedType
325
+ }
326
+
327
+ if err := unmarshaler .UnmarshalJSON ([]byte (targetValue )); err != nil {
328
+ return err
329
+ }
330
+ default :
331
+ return errUnsupportedType
332
+ }
333
+
334
+ value .Set (target )
335
+ return nil
336
+ }
337
+
333
338
func (u * Unmarshaler ) generateMap (keyType , elemType reflect.Type , mapValue any ,
334
339
fullName string ) (reflect.Value , error ) {
335
340
mapType := reflect .MapOf (keyType , elemType )
@@ -423,6 +428,15 @@ func (u *Unmarshaler) generateMap(keyType, elemType reflect.Type, mapValue any,
423
428
return targetValue , nil
424
429
}
425
430
431
+ func (u * Unmarshaler ) implementsUnmarshaler (t reflect.Type ) bool {
432
+ switch u .key {
433
+ case jsonTagKey :
434
+ return t .Implements (reflect .TypeOf ((* json .Unmarshaler )(nil )).Elem ())
435
+ default :
436
+ return false
437
+ }
438
+ }
439
+
426
440
func (u * Unmarshaler ) parseOptionsWithContext (field reflect.StructField , m Valuer , fullName string ) (
427
441
string , * fieldOptionsWithContext , error ) {
428
442
key , options , err := parseKeyAndOptions (u .key , field )
@@ -600,8 +614,8 @@ func (u *Unmarshaler) processFieldNotFromString(fieldType reflect.Type, value re
600
614
return u .fillSliceFromString (fieldType , value , mapValue , fullName )
601
615
case valueKind == reflect .String && derefedFieldType == durationType :
602
616
return fillDurationValue (fieldType , value , mapValue .(string ))
603
- case valueKind == reflect .String && typeKind == reflect .Struct && implementsJsonUnmarshaler (fieldType ):
604
- return u .fillJsonUnmarshalerStruct (fieldType , value , mapValue .(string ))
617
+ case valueKind == reflect .String && typeKind == reflect .Struct && u . implementsUnmarshaler (fieldType ):
618
+ return u .fillUnmarshalerStruct (fieldType , value , mapValue .(string ))
605
619
default :
606
620
return u .processFieldPrimitive (fieldType , value , mapValue , opts , fullName )
607
621
}
@@ -1087,10 +1101,6 @@ func getValueWithChainedKeys(m valuerWithParent, keys []string) (any, bool) {
1087
1101
}
1088
1102
}
1089
1103
1090
- func implementsJsonUnmarshaler (t reflect.Type ) bool {
1091
- return t .Implements (reflect .TypeOf ((* json .Unmarshaler )(nil )).Elem ())
1092
- }
1093
-
1094
1104
func join (elem ... string ) string {
1095
1105
var builder strings.Builder
1096
1106
0 commit comments