Load reads the values of the fields of the target struct from the accessor.
(target interface{}, accessor Accessor, options LoadOptions)
| 10 | |
| 11 | // Load reads the values of the fields of the target struct from the accessor. |
| 12 | func Load(target interface{}, accessor Accessor, options LoadOptions) error { |
| 13 | return forEachField(target, func(tag *fieldTag, field reflect.Value) error { |
| 14 | if !tag.matchesLoad(options) { |
| 15 | return nil |
| 16 | } |
| 17 | |
| 18 | accessorValue, err := accessor.Get(FieldGet{ |
| 19 | Path: tag.path, Type: tag.fType, |
| 20 | }) |
| 21 | if err != nil { |
| 22 | return err |
| 23 | } |
| 24 | |
| 25 | if accessorValue == nil { |
| 26 | field.Set(reflect.Zero(field.Type())) |
| 27 | return nil |
| 28 | } |
| 29 | |
| 30 | if tag.isPtr { |
| 31 | setValue := ptypes.NewPtr(accessorValue) |
| 32 | if setValue.IsValid() { |
| 33 | field.Set(setValue) |
| 34 | } |
| 35 | } else { |
| 36 | field.Set(reflect.ValueOf(accessorValue)) |
| 37 | } |
| 38 | |
| 39 | return nil |
| 40 | }, |
| 41 | ) |
| 42 | } |
| 43 | |
| 44 | // Store writes the values of the fields of the target struct to the accessor. |
| 45 | // Nil slice and map fields are skipped and do not overwrite existing accessor values. |
no test coverage detected