func (d *appData) setUserType(user interface{}) { var typ reflect.Type if tt, ok := user.(reflect.Type); ok { typ = tt } else { typ = reflect.TypeOf(user) } if typ != nil { for typ.Kind() == reflect.Ptr { typ = typ.Elem() } } if typ == nil { panic(fmt.Errorf("User type is not set - configure it with users.SetType(&MyUserType{})")) } s, err := structs.New(typ, nil, nil) if err != nil { panic(err) } if !s.Embeds(innerType) { panic(fmt.Errorf("invalid User type %s: must embed %s e.g type %s struct {\t\t%s\n\t...\n}", typ, innerType, typ.Name(), innerType)) } for _, v := range d.enabledSocialAccountTypes() { if !s.Has(v.Name.String(), v.Type) { panic(&missingFieldError{typ, v.Name.String(), v.Type}) } } d.userType = typ }
func (f *Form) appendVal(val interface{}) error { v, err := types.SettableValue(val) if err != nil { return err } s, err := structs.New(val, formTags, formStructConfigurator{}) if err != nil { return err } f.values = append(f.values, v) f.structs = append(f.structs, s) return nil }
func (o *Orm) registerLocked(t interface{}, opts *Options) (*Table, error) { s, err := structs.New(t, o.dtags(), ormStructConfigurator{}) if err != nil { switch err { case structs.ErrNoStruct: return nil, fmt.Errorf("only structs can be registered as models (tried to register %T)", t) case structs.ErrNoFields: return nil, fmt.Errorf("type %T has no fields", t) } return nil, err } var table string if opts != nil { table = opts.Table } if table == "" { table = defaultTableName(s.Type) } if globalRegistry.names[o.tags] == nil { globalRegistry.names[o.tags] = nameRegistry{} globalRegistry.types[o.tags] = typeRegistry{} } names := globalRegistry.names[o.tags] types := globalRegistry.types[o.tags] if _, ok := names[table]; ok { return nil, fmt.Errorf("duplicate ORM table name %q", table) } if _, ok := types[s.Type]; ok { return nil, fmt.Errorf("duplicate ORM type %s", s.Type) } fields, references, err := o.fields(table, s) if err != nil { return nil, err } var name string if opts != nil && opts.Name != "" { name = opts.Name } else { name = typeName(s.Type) } if opts != nil { if len(opts.PrimaryKey) > 0 { if fields.PrimaryKey >= 0 { return nil, fmt.Errorf("duplicate primary key in model %q. tags define %q as PK, Options define %v", name, fields.QNames[fields.PrimaryKey], opts.PrimaryKey) } fields.CompositePrimaryKey = make([]int, len(opts.PrimaryKey)) for ii, v := range opts.PrimaryKey { pos, ok := fields.QNameMap[v] if !ok { return nil, fmt.Errorf("can't map qualified name %q on model %q when creating composite key", v, name) } fields.CompositePrimaryKey[ii] = pos } } } model := &model{ fields: fields, name: name, shortName: s.Type.Name(), references: references, options: opts, table: table, tags: o.tags, } names[table] = model types[s.Type] = model log.Debugf("Registered model %v (%q) with tags %q", s.Type, name, o.tags) o.typeRegistry = types.clone() return tableWithModel(model), nil }