-
Notifications
You must be signed in to change notification settings - Fork 14
/
scope.go
76 lines (64 loc) · 1.75 KB
/
scope.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package l10n
import (
"reflect"
"github.com/jinzhu/gorm"
"github.com/qor/qor/utils"
)
// IsLocalizable return model is localizable or not
func IsLocalizable(scope *gorm.Scope) (IsLocalizable bool) {
if scope.GetModelStruct().ModelType == nil {
return false
}
_, IsLocalizable = reflect.New(scope.GetModelStruct().ModelType).Interface().(l10nInterface)
return
}
type localeCreatableInterface interface {
CreatableFromLocale()
}
type localeCreatableInterface2 interface {
LocaleCreatable()
}
func isLocaleCreatable(scope *gorm.Scope) (ok bool) {
if _, ok = reflect.New(scope.GetModelStruct().ModelType).Interface().(localeCreatableInterface); ok {
return
}
_, ok = reflect.New(scope.GetModelStruct().ModelType).Interface().(localeCreatableInterface2)
return
}
func setLocale(scope *gorm.Scope, locale string) {
for _, field := range scope.Fields() {
if field.Name == "LanguageCode" {
field.Set(locale)
}
}
}
func getQueryLocale(scope *gorm.Scope) (locale string, isLocale bool) {
if str, ok := scope.DB().Get("l10n:locale"); ok {
if locale, ok := str.(string); ok && locale != "" {
return locale, locale != Global
}
}
return Global, false
}
func getLocale(scope *gorm.Scope) (locale string, isLocale bool) {
if str, ok := scope.DB().Get("l10n:localize_to"); ok {
if locale, ok := str.(string); ok && locale != "" {
return locale, locale != Global
}
}
return getQueryLocale(scope)
}
func isSyncField(field *gorm.StructField) bool {
if _, ok := utils.ParseTagOption(field.Tag.Get("l10n"))["SYNC"]; ok {
return true
}
return false
}
func syncColumns(scope *gorm.Scope) (columns []string) {
for _, field := range scope.GetModelStruct().StructFields {
if isSyncField(field) {
columns = append(columns, field.DBName)
}
}
return
}