Transform converts the source into the target struct with compose types transformer and the specified transformers if any.
(source any, target any, additionalTransformers ...Transformer)
| 295 | // Transform converts the source into the target struct with compose types transformer |
| 296 | // and the specified transformers if any. |
| 297 | func Transform(source any, target any, additionalTransformers ...Transformer) error { |
| 298 | data := mapstructure.Metadata{} |
| 299 | config := &mapstructure.DecoderConfig{ |
| 300 | DecodeHook: mapstructure.ComposeDecodeHookFunc( |
| 301 | createTransformHook(additionalTransformers...), |
| 302 | mapstructure.StringToTimeDurationHookFunc()), |
| 303 | Result: target, |
| 304 | Metadata: &data, |
| 305 | } |
| 306 | decoder, err := mapstructure.NewDecoder(config) |
| 307 | if err != nil { |
| 308 | return err |
| 309 | } |
| 310 | return decoder.Decode(source) |
| 311 | } |
| 312 | |
| 313 | // TransformerFunc defines a function to perform the actual transformation |
| 314 | type TransformerFunc func(any) (any, error) |
searching dependent graphs…