MCPcopy
hub / github.com/go-gorm/gorm / assignInterfacesToValue

Method assignInterfacesToValue

finisher_api.go:258–305  ·  view source on GitHub ↗
(values ...interface{})

Source from the content-addressed store, hash-verified

256}
257
258func (db *DB) assignInterfacesToValue(values ...interface{}) {
259 for _, value := range values {
260 switch v := value.(type) {
261 case []clause.Expression:
262 for _, expr := range v {
263 if eq, ok := expr.(clause.Eq); ok {
264 switch column := eq.Column.(type) {
265 case string:
266 if field := db.Statement.Schema.LookUpField(column); field != nil {
267 db.AddError(field.Set(db.Statement.Context, db.Statement.ReflectValue, eq.Value))
268 }
269 case clause.Column:
270 if field := db.Statement.Schema.LookUpField(column.Name); field != nil {
271 db.AddError(field.Set(db.Statement.Context, db.Statement.ReflectValue, eq.Value))
272 }
273 }
274 } else if andCond, ok := expr.(clause.AndConditions); ok {
275 db.assignInterfacesToValue(andCond.Exprs)
276 }
277 }
278 case clause.Expression, map[string]string, map[interface{}]interface{}, map[string]interface{}:
279 if exprs := db.Statement.BuildCondition(value); len(exprs) > 0 {
280 db.assignInterfacesToValue(exprs)
281 }
282 default:
283 if s, err := schema.Parse(value, db.cacheStore, db.NamingStrategy); err == nil {
284 reflectValue := reflect.Indirect(reflect.ValueOf(value))
285 switch reflectValue.Kind() {
286 case reflect.Struct:
287 for _, f := range s.Fields {
288 if f.Readable {
289 if v, isZero := f.ValueOf(db.Statement.Context, reflectValue); !isZero {
290 if field := db.Statement.Schema.LookUpField(f.Name); field != nil {
291 db.AddError(field.Set(db.Statement.Context, db.Statement.ReflectValue, v))
292 }
293 }
294 }
295 }
296 }
297 } else if len(values) > 0 {
298 if exprs := db.Statement.BuildCondition(values[0], values[1:]...); len(exprs) > 0 {
299 db.assignInterfacesToValue(exprs)
300 }
301 return
302 }
303 }
304 }
305}
306
307// FirstOrInit finds the first matching record, otherwise if not found initializes a new instance with given conds.
308// Each conds must be a struct or map.

Callers 2

FirstOrInitMethod · 0.80
FirstOrCreateMethod · 0.80

Calls 5

AddErrorMethod · 0.95
ParseFunction · 0.92
LookUpFieldMethod · 0.80
BuildConditionMethod · 0.80
SetMethod · 0.65

Tested by

no test coverage detected