-
Notifications
You must be signed in to change notification settings - Fork 1
/
field.go
92 lines (76 loc) · 2.04 KB
/
field.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package nero
import (
"reflect"
"github.com/jinzhu/inflection"
"github.com/stevenferrer/mira"
stringsx "github.com/stevenferrer/nero/x/strings"
)
// Field is a field
type Field struct {
// name is the field name
name string
// typeInfo is the field type info
typeInfo *mira.TypeInfo
// StructField overrides the struct field
structField string
// Auto is the auto-filled flag
auto,
// Optional is the optional flag
optional bool
}
// TypeInfo returns the type info
func (f *Field) TypeInfo() *mira.TypeInfo {
return f.typeInfo
}
// Name returns the field name
func (f *Field) Name() string {
return f.name
}
// StructField returns the struct field
func (f *Field) StructField() string {
structField := stringsx.ToCamel(f.name)
if len(f.structField) > 0 {
structField = f.structField
}
return structField
}
// Identifier returns the lower-camelized struct field
func (f *Field) Identifier() string {
return stringsx.ToLowerCamel(f.StructField())
}
// IdentifierPlural returns the plural form of identifier
func (f *Field) IdentifierPlural() string {
return inflection.Plural(f.Identifier())
}
// IsArray returns true if field is an array or a slice
func (f *Field) IsArray() bool {
kind := f.typeInfo.T().Kind()
return kind == reflect.Array ||
kind == reflect.Slice
}
// IsNillable returns true if the field is nillable
func (f *Field) IsNillable() bool {
return f.typeInfo.IsNillable()
}
// IsValueScanner returns true if field implements value scanner
func (f *Field) IsValueScanner() bool {
t := reflect.TypeOf(f.typeInfo.V())
if t.Kind() != reflect.Ptr {
t = reflect.New(t).Type()
}
return t.Implements(reflect.TypeOf(new(ValueScanner)).Elem())
}
// IsAuto returns the auto flag
func (f *Field) IsAuto() bool {
return f.auto
}
// IsOptional returns the optional flag
func (f *Field) IsOptional() bool {
return f.optional
}
// IsComparable returns true if field is comparable i.e. with comparisong operators
func (f *Field) IsComparable() bool {
kind := f.typeInfo.T().Kind()
return !(kind == reflect.Map ||
kind == reflect.Slice)
}