Map receives a pointer to map or struct and maps it to columns and values.
(item interface{}, options *MapOptions)
| 236 | |
| 237 | // Map receives a pointer to map or struct and maps it to columns and values. |
| 238 | func Map(item interface{}, options *MapOptions) ([]string, []interface{}, error) { |
| 239 | var fv fieldValue |
| 240 | if options == nil { |
| 241 | options = &defaultMapOptions |
| 242 | } |
| 243 | |
| 244 | itemV := reflect.ValueOf(item) |
| 245 | if !itemV.IsValid() { |
| 246 | return nil, nil, nil |
| 247 | } |
| 248 | |
| 249 | itemT := itemV.Type() |
| 250 | |
| 251 | if itemT.Kind() == reflect.Ptr { |
| 252 | // Single dereference. Just in case the user passes a pointer to struct |
| 253 | // instead of a struct. |
| 254 | item = itemV.Elem().Interface() |
| 255 | itemV = reflect.ValueOf(item) |
| 256 | itemT = itemV.Type() |
| 257 | } |
| 258 | |
| 259 | switch itemT.Kind() { |
| 260 | case reflect.Struct: |
| 261 | fieldMap := Mapper.TypeMap(itemT).Names |
| 262 | nfields := len(fieldMap) |
| 263 | |
| 264 | fv.values = make([]interface{}, 0, nfields) |
| 265 | fv.fields = make([]string, 0, nfields) |
| 266 | |
| 267 | for _, fi := range fieldMap { |
| 268 | |
| 269 | // Check for deprecated JSONB tag |
| 270 | if _, hasJSONBTag := fi.Options["jsonb"]; hasJSONBTag { |
| 271 | return nil, nil, errDeprecatedJSONBTag |
| 272 | } |
| 273 | |
| 274 | // Field options |
| 275 | _, tagOmitEmpty := fi.Options["omitempty"] |
| 276 | |
| 277 | fld := reflectx.FieldByIndexesReadOnly(itemV, fi.Index) |
| 278 | if fld.Kind() == reflect.Ptr && fld.IsNil() { |
| 279 | if tagOmitEmpty && !options.IncludeNil { |
| 280 | continue |
| 281 | } |
| 282 | fv.fields = append(fv.fields, fi.Name) |
| 283 | if tagOmitEmpty { |
| 284 | fv.values = append(fv.values, sqlDefault) |
| 285 | } else { |
| 286 | fv.values = append(fv.values, nil) |
| 287 | } |
| 288 | continue |
| 289 | } |
| 290 | |
| 291 | value := fld.Interface() |
| 292 | |
| 293 | isZero := false |
| 294 | if t, ok := fld.Interface().(hasIsZero); ok { |
| 295 | if t.IsZero() { |
no test coverage detected