-
Notifications
You must be signed in to change notification settings - Fork 5
/
types.go
126 lines (102 loc) · 2.28 KB
/
types.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package dal
import (
"fmt"
"strings"
"github.com/ghetzel/go-stockutil/log"
"github.com/ghetzel/go-stockutil/maputil"
)
type Type string
const (
StringType Type = `str`
AutoType = `auto`
BooleanType = `bool`
IntType = `int`
FloatType = `float`
TimeType = `time`
ObjectType = `object`
RawType = `raw`
ArrayType = `array`
)
func (self Type) String() string {
return string(self)
}
func ParseFieldType(in string) Type {
switch in {
case `str`:
return StringType
case `bool`:
return BooleanType
case `int`:
return IntType
case `float`:
return FloatType
case `time`:
return TimeType
case `object`:
return ObjectType
case `raw`:
return RawType
case `array`:
return ArrayType
default:
return ``
}
}
type FieldOperation int
const (
PersistOperation FieldOperation = iota
RetrieveOperation
)
type FieldValidatorFunc func(interface{}) error
type FieldFormatterFunc func(interface{}, FieldOperation) (interface{}, error)
type CollectionValidatorFunc func(*Record) error
type DeltaType string
const (
CollectionDelta DeltaType = `collection`
FieldDelta = `field`
)
type DeltaIssue int
const (
UnknownIssue DeltaIssue = iota
CollectionNameIssue
CollectionKeyNameIssue
CollectionKeyTypeIssue
FieldMissingIssue
FieldNameIssue
FieldLengthIssue
FieldTypeIssue
FieldPropertyIssue
)
type SchemaDelta struct {
Type DeltaType
Issue DeltaIssue
Message string
Collection string
Name string
Parameter string
Desired interface{}
Actual interface{}
ReferenceField *Field
}
func (self SchemaDelta) DesiredField(from Field) *Field {
field := &from
maputil.M(field).Set(self.Parameter, self.Desired)
log.Noticef("DESIRED: %+v", self.Actual)
return field
}
func (self SchemaDelta) String() string {
msg := fmt.Sprintf("%s '%s'", strings.Title(string(self.Type)), self.Name)
if self.Parameter != `` {
msg += fmt.Sprintf(", parameter '%s'", self.Parameter)
}
msg += fmt.Sprintf(": %s", self.Message)
dV := fmt.Sprintf("%v", self.Desired)
aV := fmt.Sprintf("%v", self.Actual)
if len(dV) <= 12 && len(aV) <= 12 {
msg += fmt.Sprintf(" (desired: %v, actual: %v)", dV, aV)
}
return msg
}
type Migratable interface {
Migrate() error
}