func isLocalizable(scope *gorm.Scope) (isLocalizable bool) { if scope.GetModelStruct().ModelType == nil { return false } _, isLocalizable = reflect.New(scope.GetModelStruct().ModelType).Interface().(l10nInterface) return }
func syncColumns(scope *gorm.Scope) (columns []string) { for _, field := range scope.GetModelStruct().StructFields { if isSyncField(field) { columns = append(columns, field.DBName) } } return }
func beforeCreate(scope *gorm.Scope) { if isLocalizable(scope) { if locale, ok := getLocale(scope); ok { // is locale if isLocaleCreateable(scope) || !scope.PrimaryKeyZero() { setLocale(scope, locale) } else { err := fmt.Errorf("the resource %v cannot be created in %v", scope.GetModelStruct().ModelType.Name(), locale) scope.Err(err) } } else { setLocale(scope, Global) } } }
func getFuncMap(scope *gorm.Scope, field *gorm.Field, filename string) template.FuncMap { hash := func() string { return strings.Replace(time.Now().Format("20060102150506.000000000"), ".", "", -1) } return template.FuncMap{ "class": func() string { return strings.ToLower(inflection.Plural(scope.GetModelStruct().ModelType.Name())) }, "primary_key": func() string { return fmt.Sprintf("%v", scope.PrimaryKeyValue()) }, "column": func() string { return field.Name }, "filename": func() string { return filename }, "basename": func() string { return strings.TrimSuffix(path.Base(filename), path.Ext(filename)) }, "hash": hash, "filename_with_hash": func() string { return fmt.Sprintf("%v.%v%v", strings.TrimSuffix(filename, path.Ext(filename)), hash(), path.Ext(filename)) }, "extension": func() string { return strings.TrimPrefix(path.Ext(filename), ".") }, } }
func Stringify(object interface{}) string { if obj, ok := object.(interface { Stringify() string }); ok { return obj.Stringify() } scope := gorm.Scope{Value: object} for _, column := range []string{"Name", "Title"} { if field, ok := scope.FieldByName(column); ok { return fmt.Sprintf("%v", field.Field.Interface()) } } if scope.PrimaryKeyZero() { return "" } else { return fmt.Sprintf("%v#%v", scope.GetModelStruct().ModelType.Name(), scope.PrimaryKeyValue()) } }
func stringify(object interface{}) string { if obj, ok := object.(interface { Stringify() string }); ok { return obj.Stringify() } scope := gorm.Scope{Value: object} for _, column := range []string{"Description", "Name", "Title", "Code"} { if field, ok := scope.FieldByName(column); ok { return fmt.Sprintf("%v", field.Field.Interface()) } } if scope.PrimaryField() != nil { if scope.PrimaryKeyZero() { return "" } return fmt.Sprintf("%v#%v", scope.GetModelStruct().ModelType.Name(), scope.PrimaryKeyValue()) } return fmt.Sprint(reflect.Indirect(reflect.ValueOf(object)).Interface()) }
func isLocaleCreateable(scope *gorm.Scope) (ok bool) { _, ok = reflect.New(scope.GetModelStruct().ModelType).Interface().(LocaleCreateableInterface) return }
// Label is a label including model type, primary key and column name func (err Error) Label() string { scope := gorm.Scope{Value: err.Resource} return fmt.Sprintf("%v_%v_%v", scope.GetModelStruct().ModelType.Name(), scope.PrimaryKeyValue(), err.Column) }