2014-07-31 19:02:49 +00:00
|
|
|
/*
|
|
|
|
Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package conversion
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
)
|
|
|
|
|
|
|
|
type typePair struct {
|
|
|
|
source reflect.Type
|
|
|
|
dest reflect.Type
|
|
|
|
}
|
|
|
|
|
2014-11-22 06:49:50 +00:00
|
|
|
type typeNamePair struct {
|
|
|
|
fieldType reflect.Type
|
|
|
|
fieldName string
|
|
|
|
}
|
|
|
|
|
2014-07-31 19:02:49 +00:00
|
|
|
// DebugLogger allows you to get debugging messages if necessary.
|
|
|
|
type DebugLogger interface {
|
|
|
|
Logf(format string, args ...interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converter knows how to convert one type to another.
|
|
|
|
type Converter struct {
|
|
|
|
// Map from the conversion pair to a function which can
|
|
|
|
// do the conversion.
|
|
|
|
funcs map[typePair]reflect.Value
|
|
|
|
|
2014-11-22 06:49:50 +00:00
|
|
|
// This is a map from a source field type and name, to a list of destination
|
|
|
|
// field type and name.
|
|
|
|
structFieldDests map[typeNamePair][]typeNamePair
|
|
|
|
|
|
|
|
// Allows for the opposite lookup of structFieldDests. So that SourceFromDest
|
|
|
|
// copy flag also works. So this is a map of destination field name, to potential
|
|
|
|
// source field name and type to look for.
|
|
|
|
structFieldSources map[typeNamePair][]typeNamePair
|
|
|
|
|
2014-09-05 23:02:39 +00:00
|
|
|
// If non-nil, will be called to print helpful debugging info. Quite verbose.
|
2014-07-31 19:02:49 +00:00
|
|
|
Debug DebugLogger
|
2014-09-10 05:51:33 +00:00
|
|
|
|
2014-09-11 19:31:46 +00:00
|
|
|
// NameFunc is called to retrieve the name of a type; this name is used for the
|
2014-09-10 05:51:33 +00:00
|
|
|
// purpose of deciding whether two types match or not (i.e., will we attempt to
|
|
|
|
// do a conversion). The default returns the go type name.
|
2014-09-11 19:31:46 +00:00
|
|
|
NameFunc func(t reflect.Type) string
|
2014-07-31 19:02:49 +00:00
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// NewConverter creates a new Converter object.
|
2014-07-31 19:02:49 +00:00
|
|
|
func NewConverter() *Converter {
|
|
|
|
return &Converter{
|
2014-11-22 06:49:50 +00:00
|
|
|
funcs: map[typePair]reflect.Value{},
|
|
|
|
NameFunc: func(t reflect.Type) string { return t.Name() },
|
|
|
|
structFieldDests: map[typeNamePair][]typeNamePair{},
|
|
|
|
structFieldSources: map[typeNamePair][]typeNamePair{},
|
2014-07-31 19:02:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 23:02:39 +00:00
|
|
|
// Scope is passed to conversion funcs to allow them to continue an ongoing conversion.
|
|
|
|
// If multiple converters exist in the system, Scope will allow you to use the correct one
|
|
|
|
// from a conversion function--that is, the one your conversion function was called by.
|
|
|
|
type Scope interface {
|
|
|
|
// Call Convert to convert sub-objects. Note that if you call it with your own exact
|
|
|
|
// parameters, you'll run out of stack space before anything useful happens.
|
|
|
|
Convert(src, dest interface{}, flags FieldMatchingFlags) error
|
2014-09-10 05:51:33 +00:00
|
|
|
|
|
|
|
// SrcTags and DestTags contain the struct tags that src and dest had, respectively.
|
|
|
|
// If the enclosing object was not a struct, then these will contain no tags, of course.
|
|
|
|
SrcTag() reflect.StructTag
|
|
|
|
DestTag() reflect.StructTag
|
|
|
|
|
|
|
|
// Flags returns the flags with which the conversion was started.
|
|
|
|
Flags() FieldMatchingFlags
|
|
|
|
|
|
|
|
// Meta returns any information originally passed to Convert.
|
2014-09-11 19:31:46 +00:00
|
|
|
Meta() *Meta
|
|
|
|
}
|
|
|
|
|
|
|
|
// Meta is supplied by Scheme, when it calls Convert.
|
|
|
|
type Meta struct {
|
|
|
|
SrcVersion string
|
|
|
|
DestVersion string
|
|
|
|
|
|
|
|
// TODO: If needed, add a user data field here.
|
2014-09-10 05:51:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// scope contains information about an ongoing conversion.
|
|
|
|
type scope struct {
|
|
|
|
converter *Converter
|
2014-09-11 19:31:46 +00:00
|
|
|
meta *Meta
|
2014-09-10 05:51:33 +00:00
|
|
|
flags FieldMatchingFlags
|
|
|
|
srcTagStack []reflect.StructTag
|
|
|
|
destTagStack []reflect.StructTag
|
|
|
|
}
|
|
|
|
|
|
|
|
// push adds a level to the src/dest tag stacks.
|
2014-09-11 19:31:46 +00:00
|
|
|
func (s *scope) push() {
|
|
|
|
s.srcTagStack = append(s.srcTagStack, "")
|
|
|
|
s.destTagStack = append(s.destTagStack, "")
|
2014-09-10 05:51:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// pop removes a level to the src/dest tag stacks.
|
2014-09-11 19:31:46 +00:00
|
|
|
func (s *scope) pop() {
|
|
|
|
n := len(s.srcTagStack)
|
|
|
|
s.srcTagStack = s.srcTagStack[:n-1]
|
|
|
|
s.destTagStack = s.destTagStack[:n-1]
|
2014-09-10 05:51:33 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 19:31:46 +00:00
|
|
|
func (s *scope) setSrcTag(tag reflect.StructTag) {
|
|
|
|
s.srcTagStack[len(s.srcTagStack)-1] = tag
|
2014-09-10 05:51:33 +00:00
|
|
|
}
|
|
|
|
|
2014-09-11 19:31:46 +00:00
|
|
|
func (s *scope) setDestTag(tag reflect.StructTag) {
|
|
|
|
s.destTagStack[len(s.destTagStack)-1] = tag
|
2014-09-10 05:51:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert continues a conversion.
|
2014-09-11 19:31:46 +00:00
|
|
|
func (s *scope) Convert(src, dest interface{}, flags FieldMatchingFlags) error {
|
|
|
|
return s.converter.Convert(src, dest, flags, s.meta)
|
2014-09-10 05:51:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SrcTag returns the tag of the struct containing the current source item, if any.
|
2014-09-11 19:31:46 +00:00
|
|
|
func (s *scope) SrcTag() reflect.StructTag {
|
|
|
|
return s.srcTagStack[len(s.srcTagStack)-1]
|
2014-09-10 05:51:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DestTag returns the tag of the struct containing the current dest item, if any.
|
2014-09-11 19:31:46 +00:00
|
|
|
func (s *scope) DestTag() reflect.StructTag {
|
|
|
|
return s.destTagStack[len(s.destTagStack)-1]
|
2014-09-10 05:51:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Flags returns the flags with which the current conversion was started.
|
2014-09-11 19:31:46 +00:00
|
|
|
func (s *scope) Flags() FieldMatchingFlags {
|
|
|
|
return s.flags
|
2014-09-10 05:51:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Meta returns the meta object that was originally passed to Convert.
|
2014-09-11 19:31:46 +00:00
|
|
|
func (s *scope) Meta() *Meta {
|
|
|
|
return s.meta
|
2014-09-05 23:02:39 +00:00
|
|
|
}
|
|
|
|
|
2014-07-31 19:02:49 +00:00
|
|
|
// Register registers a conversion func with the Converter. conversionFunc must take
|
2014-09-05 23:02:39 +00:00
|
|
|
// three parameters: a pointer to the input type, a pointer to the output type, and
|
|
|
|
// a conversion.Scope (which should be used if recursive conversion calls are desired).
|
|
|
|
// It must return an error.
|
2014-07-31 19:02:49 +00:00
|
|
|
//
|
|
|
|
// Example:
|
2014-09-05 23:02:39 +00:00
|
|
|
// c.Register(func(in *Pod, out *v1beta1.Pod, s Scope) error { ... return nil })
|
2014-07-31 19:02:49 +00:00
|
|
|
func (c *Converter) Register(conversionFunc interface{}) error {
|
|
|
|
fv := reflect.ValueOf(conversionFunc)
|
|
|
|
ft := fv.Type()
|
|
|
|
if ft.Kind() != reflect.Func {
|
|
|
|
return fmt.Errorf("expected func, got: %v", ft)
|
|
|
|
}
|
2014-09-05 23:02:39 +00:00
|
|
|
if ft.NumIn() != 3 {
|
|
|
|
return fmt.Errorf("expected three 'in' params, got: %v", ft)
|
2014-07-31 19:02:49 +00:00
|
|
|
}
|
|
|
|
if ft.NumOut() != 1 {
|
2014-09-05 23:02:39 +00:00
|
|
|
return fmt.Errorf("expected one 'out' param, got: %v", ft)
|
2014-07-31 19:02:49 +00:00
|
|
|
}
|
|
|
|
if ft.In(0).Kind() != reflect.Ptr {
|
2014-09-05 23:02:39 +00:00
|
|
|
return fmt.Errorf("expected pointer arg for 'in' param 0, got: %v", ft)
|
2014-07-31 19:02:49 +00:00
|
|
|
}
|
|
|
|
if ft.In(1).Kind() != reflect.Ptr {
|
2014-09-05 23:02:39 +00:00
|
|
|
return fmt.Errorf("expected pointer arg for 'in' param 1, got: %v", ft)
|
|
|
|
}
|
2014-09-10 05:51:33 +00:00
|
|
|
scopeType := Scope(nil)
|
2014-09-05 23:02:39 +00:00
|
|
|
if e, a := reflect.TypeOf(&scopeType).Elem(), ft.In(2); e != a {
|
|
|
|
return fmt.Errorf("expected '%v' arg for 'in' param 2, got '%v' (%v)", e, a, ft)
|
2014-07-31 19:02:49 +00:00
|
|
|
}
|
|
|
|
var forErrorType error
|
|
|
|
// This convolution is necessary, otherwise TypeOf picks up on the fact
|
|
|
|
// that forErrorType is nil.
|
|
|
|
errorType := reflect.TypeOf(&forErrorType).Elem()
|
|
|
|
if ft.Out(0) != errorType {
|
|
|
|
return fmt.Errorf("expected error return, got: %v", ft)
|
|
|
|
}
|
|
|
|
c.funcs[typePair{ft.In(0).Elem(), ft.In(1).Elem()}] = fv
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-22 06:49:50 +00:00
|
|
|
// SetStructFieldCopy registers a correspondence. Whenever a struct field is encountered
|
|
|
|
// which has a type and name matching srcFieldType and srcFieldName, it wil be copied
|
|
|
|
// into the field in the destination struct matching destFieldType & Name, if such a
|
|
|
|
// field exists.
|
|
|
|
// May be called multiple times, even for the same source field & type--all applicable
|
|
|
|
// copies will be performed.
|
|
|
|
func (c *Converter) SetStructFieldCopy(srcFieldType interface{}, srcFieldName string, destFieldType interface{}, destFieldName string) error {
|
|
|
|
st := reflect.TypeOf(srcFieldType)
|
|
|
|
dt := reflect.TypeOf(destFieldType)
|
|
|
|
srcKey := typeNamePair{st, srcFieldName}
|
|
|
|
destKey := typeNamePair{dt, destFieldName}
|
|
|
|
c.structFieldDests[srcKey] = append(c.structFieldDests[srcKey], destKey)
|
|
|
|
c.structFieldSources[destKey] = append(c.structFieldSources[destKey], srcKey)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// FieldMatchingFlags contains a list of ways in which struct fields could be
|
2014-07-31 19:02:49 +00:00
|
|
|
// copied. These constants may be | combined.
|
|
|
|
type FieldMatchingFlags int
|
|
|
|
|
|
|
|
const (
|
2014-08-12 19:14:00 +00:00
|
|
|
// Loop through destination fields, search for matching source
|
2014-07-31 19:02:49 +00:00
|
|
|
// field to copy it from. Source fields with no corresponding
|
|
|
|
// destination field will be ignored. If SourceToDest is
|
|
|
|
// specified, this flag is ignored. If niether is specified,
|
2014-08-01 21:24:12 +00:00
|
|
|
// or no flags are passed, this flag is the default.
|
|
|
|
DestFromSource FieldMatchingFlags = 0
|
|
|
|
// Loop through source fields, search for matching dest field
|
|
|
|
// to copy it into. Destination fields with no corresponding
|
|
|
|
// source field will be ignored.
|
|
|
|
SourceToDest FieldMatchingFlags = 1 << iota
|
2014-07-31 19:02:49 +00:00
|
|
|
// Don't treat it as an error if the corresponding source or
|
|
|
|
// dest field can't be found.
|
|
|
|
IgnoreMissingFields
|
|
|
|
// Don't require type names to match.
|
2014-08-01 21:24:12 +00:00
|
|
|
AllowDifferentFieldTypeNames
|
2014-07-31 19:02:49 +00:00
|
|
|
)
|
|
|
|
|
2014-09-02 10:00:28 +00:00
|
|
|
// IsSet returns true if the given flag or combination of flags is set.
|
2014-07-31 19:02:49 +00:00
|
|
|
func (f FieldMatchingFlags) IsSet(flag FieldMatchingFlags) bool {
|
2014-11-22 06:49:50 +00:00
|
|
|
if flag == DestFromSource {
|
|
|
|
// The bit logic doesn't work on the default value.
|
|
|
|
return f&SourceToDest != SourceToDest
|
|
|
|
}
|
2014-07-31 19:02:49 +00:00
|
|
|
return f&flag == flag
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert will translate src to dest if it knows how. Both must be pointers.
|
|
|
|
// If no conversion func is registered and the default copying mechanism
|
|
|
|
// doesn't work on this type pair, an error will be returned.
|
2014-09-10 05:51:33 +00:00
|
|
|
// Read the comments on the various FieldMatchingFlags constants to understand
|
|
|
|
// what the 'flags' parameter does.
|
|
|
|
// 'meta' is given to allow you to pass information to conversion functions,
|
2014-09-11 19:31:46 +00:00
|
|
|
// it is not used by Convert() other than storing it in the scope.
|
2014-07-31 19:02:49 +00:00
|
|
|
// Not safe for objects with cyclic references!
|
2014-09-11 19:31:46 +00:00
|
|
|
func (c *Converter) Convert(src, dest interface{}, flags FieldMatchingFlags, meta *Meta) error {
|
2014-10-28 08:02:29 +00:00
|
|
|
dv, err := EnforcePtr(dest)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-07-31 19:02:49 +00:00
|
|
|
}
|
|
|
|
if !dv.CanAddr() {
|
2014-11-20 10:00:36 +00:00
|
|
|
return fmt.Errorf("can't write to dest")
|
2014-07-31 19:02:49 +00:00
|
|
|
}
|
2014-10-28 08:02:29 +00:00
|
|
|
sv, err := EnforcePtr(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-09-10 05:51:33 +00:00
|
|
|
s := &scope{
|
|
|
|
converter: c,
|
|
|
|
flags: flags,
|
|
|
|
meta: meta,
|
|
|
|
}
|
|
|
|
s.push() // Easy way to make SrcTag and DestTag never fail
|
|
|
|
return c.convert(sv, dv, s)
|
2014-07-31 19:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// convert recursively copies sv into dv, calling an appropriate conversion function if
|
|
|
|
// one is registered.
|
2014-09-10 05:51:33 +00:00
|
|
|
func (c *Converter) convert(sv, dv reflect.Value, scope *scope) error {
|
2014-07-31 19:02:49 +00:00
|
|
|
dt, st := dv.Type(), sv.Type()
|
|
|
|
if fv, ok := c.funcs[typePair{st, dt}]; ok {
|
|
|
|
if c.Debug != nil {
|
|
|
|
c.Debug.Logf("Calling custom conversion of '%v' to '%v'", st, dt)
|
|
|
|
}
|
2014-09-10 05:51:33 +00:00
|
|
|
args := []reflect.Value{sv.Addr(), dv.Addr(), reflect.ValueOf(scope)}
|
2014-09-05 23:02:39 +00:00
|
|
|
ret := fv.Call(args)[0].Interface()
|
2014-10-21 16:57:20 +00:00
|
|
|
// This convolution is necessary because nil interfaces won't convert
|
2014-07-31 19:02:49 +00:00
|
|
|
// to errors.
|
|
|
|
if ret == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ret.(error)
|
|
|
|
}
|
|
|
|
|
2014-09-11 19:31:46 +00:00
|
|
|
if !scope.flags.IsSet(AllowDifferentFieldTypeNames) && c.NameFunc(dt) != c.NameFunc(st) {
|
2014-11-20 10:00:36 +00:00
|
|
|
return fmt.Errorf("can't convert %v to %v because type names don't match (%v, %v).", st, dt, c.NameFunc(st), c.NameFunc(dt))
|
2014-07-31 19:02:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This should handle all simple types.
|
|
|
|
if st.AssignableTo(dt) {
|
|
|
|
dv.Set(sv)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if st.ConvertibleTo(dt) {
|
|
|
|
dv.Set(sv.Convert(dt))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Debug != nil {
|
|
|
|
c.Debug.Logf("Trying to convert '%v' to '%v'", st, dt)
|
|
|
|
}
|
|
|
|
|
2014-09-10 05:51:33 +00:00
|
|
|
scope.push()
|
|
|
|
defer scope.pop()
|
|
|
|
|
2014-07-31 19:02:49 +00:00
|
|
|
switch dv.Kind() {
|
|
|
|
case reflect.Struct:
|
2014-11-22 06:49:50 +00:00
|
|
|
return c.convertStruct(sv, dv, scope)
|
2014-07-31 19:02:49 +00:00
|
|
|
case reflect.Slice:
|
|
|
|
if sv.IsNil() {
|
|
|
|
// Don't make a zero-length slice.
|
|
|
|
dv.Set(reflect.Zero(dt))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dv.Set(reflect.MakeSlice(dt, sv.Len(), sv.Cap()))
|
|
|
|
for i := 0; i < sv.Len(); i++ {
|
2014-09-10 05:51:33 +00:00
|
|
|
if err := c.convert(sv.Index(i), dv.Index(i), scope); err != nil {
|
2014-07-31 19:02:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case reflect.Ptr:
|
|
|
|
if sv.IsNil() {
|
|
|
|
// Don't copy a nil ptr!
|
|
|
|
dv.Set(reflect.Zero(dt))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dv.Set(reflect.New(dt.Elem()))
|
2014-09-10 05:51:33 +00:00
|
|
|
return c.convert(sv.Elem(), dv.Elem(), scope)
|
2014-07-31 19:02:49 +00:00
|
|
|
case reflect.Map:
|
|
|
|
if sv.IsNil() {
|
|
|
|
// Don't copy a nil ptr!
|
|
|
|
dv.Set(reflect.Zero(dt))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
dv.Set(reflect.MakeMap(dt))
|
|
|
|
for _, sk := range sv.MapKeys() {
|
|
|
|
dk := reflect.New(dt.Key()).Elem()
|
2014-09-10 05:51:33 +00:00
|
|
|
if err := c.convert(sk, dk, scope); err != nil {
|
2014-07-31 19:02:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
dkv := reflect.New(dt.Elem()).Elem()
|
2014-09-10 05:51:33 +00:00
|
|
|
if err := c.convert(sv.MapIndex(sk), dkv, scope); err != nil {
|
2014-07-31 19:02:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
dv.SetMapIndex(dk, dkv)
|
|
|
|
}
|
|
|
|
default:
|
2014-11-20 10:00:36 +00:00
|
|
|
return fmt.Errorf("couldn't copy '%v' into '%v'", st, dt)
|
2014-07-31 19:02:49 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2014-11-22 06:49:50 +00:00
|
|
|
|
|
|
|
func (c *Converter) convertStruct(sv, dv reflect.Value, scope *scope) error {
|
|
|
|
dt, st := dv.Type(), sv.Type()
|
|
|
|
|
|
|
|
listType := dt
|
|
|
|
if scope.flags.IsSet(SourceToDest) {
|
|
|
|
listType = st
|
|
|
|
}
|
|
|
|
for i := 0; i < listType.NumField(); i++ {
|
|
|
|
f := listType.Field(i)
|
|
|
|
if found, err := c.checkStructField(f.Name, sv, dv, scope); found {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
df := dv.FieldByName(f.Name)
|
|
|
|
sf := sv.FieldByName(f.Name)
|
|
|
|
if sf.IsValid() {
|
|
|
|
// No need to check error, since we know it's valid.
|
|
|
|
field, _ := st.FieldByName(f.Name)
|
|
|
|
scope.setSrcTag(field.Tag)
|
|
|
|
}
|
|
|
|
if df.IsValid() {
|
|
|
|
field, _ := dt.FieldByName(f.Name)
|
|
|
|
scope.setDestTag(field.Tag)
|
|
|
|
}
|
|
|
|
// TODO: set top level of scope.src/destTagStack with these field tags here.
|
|
|
|
if !df.IsValid() || !sf.IsValid() {
|
|
|
|
switch {
|
|
|
|
case scope.flags.IsSet(IgnoreMissingFields):
|
|
|
|
// No error.
|
|
|
|
case scope.flags.IsSet(SourceToDest):
|
|
|
|
return fmt.Errorf("%v not present in dest (%v to %v)", f.Name, st, dt)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("%v not present in src (%v to %v)", f.Name, st, dt)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err := c.convert(sf, df, scope); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkStructField returns true if the field name matches any of the struct
|
|
|
|
// field copying rules. The error should be ignored if it returns false.
|
|
|
|
func (c *Converter) checkStructField(fieldName string, sv, dv reflect.Value, scope *scope) (bool, error) {
|
|
|
|
replacementMade := false
|
|
|
|
if scope.flags.IsSet(DestFromSource) {
|
|
|
|
df := dv.FieldByName(fieldName)
|
|
|
|
if !df.IsValid() {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
destKey := typeNamePair{df.Type(), fieldName}
|
|
|
|
// Check each of the potential source (type, name) pairs to see if they're
|
|
|
|
// present in sv.
|
|
|
|
for _, potentialSourceKey := range c.structFieldSources[destKey] {
|
|
|
|
sf := sv.FieldByName(potentialSourceKey.fieldName)
|
|
|
|
if !sf.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if sf.Type() == potentialSourceKey.fieldType {
|
|
|
|
// Both the source's name and type matched, so copy.
|
|
|
|
if err := c.convert(sf, df, scope); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
replacementMade = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return replacementMade, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sf := sv.FieldByName(fieldName)
|
|
|
|
if !sf.IsValid() {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
srcKey := typeNamePair{sf.Type(), fieldName}
|
|
|
|
// Check each of the potential dest (type, name) pairs to see if they're
|
|
|
|
// present in dv.
|
|
|
|
for _, potentialDestKey := range c.structFieldDests[srcKey] {
|
|
|
|
df := dv.FieldByName(potentialDestKey.fieldName)
|
|
|
|
if !df.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if df.Type() == potentialDestKey.fieldType {
|
|
|
|
// Both the dest's name and type matched, so copy.
|
|
|
|
if err := c.convert(sf, df, scope); err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
replacementMade = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return replacementMade, nil
|
|
|
|
}
|