@@ -39,7 +39,59 @@ func Import(src any, opts ...Option) (*V, error) {
39
39
return res , nil
40
40
}
41
41
42
- // parserFunc 处理对应 reflect.Value 的函数
42
+ // ExtractAll firstly does Import, then checks all string-typed value. Once they
43
+ // could be successfully unmarshal again, they will be unmarshaled again until
44
+ // no sub-value could be unmarshaled any more.
45
+ //
46
+ // ExtractAll 首先执行 Import 操作, 然后再迭代检查所有 string 类型的值, 如果能够再成功执行
47
+ // unmarshal 操作, 则继续将这些值 unmarshal 掉。如此以往直至所有值均无法再反序列化。
48
+ func ExtractAll (src any , opts ... Option ) (* V , error ) {
49
+ v , err := Import (src , opts ... )
50
+ if err != nil {
51
+ return v , err
52
+ }
53
+ return extractValue (v ), nil
54
+ }
55
+
56
+ func extractValue (v * V ) * V {
57
+ switch v .valueType {
58
+ case String :
59
+ return extractStringValue (v )
60
+ case Object :
61
+ return extractObjectValue (v )
62
+ case Array :
63
+ return extractArrayValue (v )
64
+ default :
65
+ return v
66
+ }
67
+ }
68
+
69
+ func extractStringValue (v * V ) * V {
70
+ newV , err := UnmarshalString (v .valueStr )
71
+ if err != nil {
72
+ return v
73
+ }
74
+ return extractValue (newV )
75
+ }
76
+
77
+ func extractObjectValue (v * V ) * V {
78
+ for key , subV := range v .children .object {
79
+ v .children .object [key ] = childWithProperty {
80
+ id : subV .id ,
81
+ v : extractValue (subV .v ),
82
+ }
83
+ }
84
+ return v
85
+ }
86
+
87
+ func extractArrayValue (v * V ) * V {
88
+ for i , subV := range v .children .arr {
89
+ v .children .arr [i ] = extractValue (subV )
90
+ }
91
+ return v
92
+ }
93
+
94
+ // parserFunc handle functions operating various reflect.Value types
43
95
type parserFunc func (v reflect.Value , ex ext ) (* V , error )
44
96
45
97
type ext struct {
@@ -61,7 +113,7 @@ func (e ext) shouldOmitEmpty() bool {
61
113
return e .omitempty || e .private
62
114
}
63
115
64
- // validateValAndReturnParser 检查入参合法性并返回相应的处理函数
116
+ // validateValAndReturnParser checks incoming parameter and return corresponding handler
65
117
func validateValAndReturnParser (v reflect.Value , ex ext ) (out reflect.Value , fu parserFunc , err error ) {
66
118
out = v
67
119
@@ -138,7 +190,7 @@ func validateValAndReturnParser(v reflect.Value, ex ext) (out reflect.Value, fu
138
190
return
139
191
}
140
192
141
- // Hit marshaler if fu is not nil.
193
+ // Get marshaler if fu is not nil.
142
194
func checkAndParseMarshaler (v reflect.Value ) (out reflect.Value , fu parserFunc ) {
143
195
out = v
144
196
if ! v .IsValid () {
0 commit comments