MCPcopy Create free account
hub / github.com/devfeel/mapper / convertstructfieldInternal

Method convertstructfieldInternal

mapper_object_internal.go:129–173  ·  view source on GitHub ↗

convertstructfieldInternal to convert the fields of different structs for assignment.

(fieldName string, fromFieldInfo, toElem reflect.Value)

Source from the content-addressed store, hash-verified

127
128// convertstructfieldInternal to convert the fields of different structs for assignment.
129func (dm *mapperObject) convertstructfieldInternal(fieldName string, fromFieldInfo, toElem reflect.Value) error {
130 // check field is exists
131 realFieldName, exists := dm.CheckExistsField(toElem, fieldName)
132 if !exists {
133 return nil
134 }
135
136 toFieldInfo := toElem.FieldByName(realFieldName)
137 // check field is same type
138 if dm.setting.EnabledTypeChecking {
139 if fromFieldInfo.Kind() != toFieldInfo.Kind() {
140 return nil
141 }
142 }
143
144 if dm.setting.EnabledMapperStructField &&
145 toFieldInfo.Kind() == reflect.Struct && fromFieldInfo.Kind() == reflect.Struct &&
146 toFieldInfo.Type() != fromFieldInfo.Type() &&
147 !dm.checkIsTypeWrapper(toFieldInfo) && !dm.checkIsTypeWrapper(fromFieldInfo) {
148 x := reflect.New(toFieldInfo.Type()).Elem()
149 err := dm.elemMapper(fromFieldInfo, x)
150 if err != nil {
151 return err
152 } else {
153 toFieldInfo.Set(x)
154 }
155 } else {
156 isSet := false
157 if dm.setting.EnabledAutoTypeConvert {
158 if dm.DefaultTimeWrapper.IsType(fromFieldInfo) && toFieldInfo.Kind() == reflect.Int64 {
159 fromTime := fromFieldInfo.Interface().(time.Time)
160 toFieldInfo.Set(reflect.ValueOf(TimeToUnix(fromTime)))
161 isSet = true
162 } else if dm.DefaultTimeWrapper.IsType(toFieldInfo) && fromFieldInfo.Kind() == reflect.Int64 {
163 fromTime := fromFieldInfo.Interface().(int64)
164 toFieldInfo.Set(reflect.ValueOf(UnixToTime(fromTime)))
165 isSet = true
166 }
167 }
168 if !isSet {
169 toFieldInfo.Set(fromFieldInfo)
170 }
171 }
172 return nil
173}
174
175func (dm *mapperObject) elemToMap(fromElem, toElem reflect.Value) {
176 for i := 0; i < fromElem.NumField(); i++ {

Callers 1

elemToStructMethod · 0.95

Calls 7

CheckExistsFieldMethod · 0.95
checkIsTypeWrapperMethod · 0.95
elemMapperMethod · 0.95
TimeToUnixFunction · 0.85
UnixToTimeFunction · 0.85
SetMethod · 0.80
IsTypeMethod · 0.65

Tested by

no test coverage detected