File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -145,6 +145,31 @@ func convertJSONToType[T any](value any) (T, error) {
145145 return * new (T ), fmt .Errorf ("marshaling for type conversion: %w" , err )
146146 }
147147
148+ fmt .Println ("convertJSONToType:" , string (jsonBytes ))
149+
150+ // Check if T is an interface type
151+ var zero T
152+ typeOfT := reflect .TypeOf (& zero ).Elem ()
153+
154+ if typeOfT .Kind () == reflect .Interface {
155+ // T is interface - need to get concrete type from value
156+ concreteType := reflect .TypeOf (value )
157+ if concreteType .Kind () == reflect .Pointer {
158+ concreteType = concreteType .Elem ()
159+ }
160+
161+ // Create new instance of concrete type
162+ newInstance := reflect .New (concreteType )
163+
164+ // Unmarshal into the concrete type
165+ if err := json .Unmarshal (jsonBytes , newInstance .Interface ()); err != nil {
166+ return * new (T ), fmt .Errorf ("unmarshaling for type conversion: %w" , err )
167+ }
168+
169+ // Convert to interface type T
170+ return newInstance .Elem ().Interface ().(T ), nil
171+ }
172+
148173 var typedResult T
149174 if err := json .Unmarshal (jsonBytes , & typedResult ); err != nil {
150175 return * new (T ), fmt .Errorf ("unmarshaling for type conversion: %w" , err )
You can’t perform that action at this time.
0 commit comments