This repository has been archived by the owner on Aug 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
field.go
222 lines (189 loc) · 4.27 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package property
import (
"context"
"errors"
"github.com/reearth/reearth-backend/pkg/dataset"
)
var (
ErrInvalidPropertyValue = errors.New("invalid property value")
ErrCannotLinkDataset = errors.New("cannot link dataset")
ErrInvalidPropertyType = errors.New("invalid property type")
ErrInvalidPropertyField = errors.New("invalid property field")
)
type Field struct {
field FieldID
links *Links
v *OptionalValue
}
func (p *Field) Clone() *Field {
if p == nil {
return nil
}
return &Field{
field: p.field,
links: p.links.Clone(),
v: p.v.Clone(),
}
}
func (p *Field) Field() FieldID {
return p.field
}
func (p *Field) FieldRef() *FieldID {
if p == nil {
return nil
}
return p.field.Ref()
}
func (p *Field) Links() *Links {
if p == nil {
return nil
}
return p.links
}
func (p *Field) Type() ValueType {
if p == nil {
return ValueTypeUnknown
}
return p.v.Type()
}
func (p *Field) Value() *Value {
if p == nil {
return nil
}
return p.v.Value()
}
func (p *Field) TypeAndValue() *OptionalValue {
if p == nil {
return nil
}
return p.v
}
func (p *Field) ActualValue(ds *dataset.Dataset) *ValueAndDatasetValue {
if p == nil {
return nil
}
var dv *dataset.Value
if p.links != nil {
if l := p.links.Last(); l != nil {
d := l.Dataset()
if d != nil && ds.ID() == *d && l.DatasetSchemaField() != nil {
dv = ds.Field(*l.DatasetSchemaField()).Value()
} else {
return nil
}
} else {
return nil
}
}
return NewValueAndDatasetValue(p.Type(), dv, p.Value())
}
func (p *Field) Datasets() []DatasetID {
if p == nil {
return nil
}
res := []DatasetID{}
if p.Links().IsLinkedFully() {
dsid := p.Links().Last().Dataset()
if dsid != nil {
res = append(res, *dsid)
}
}
return res
}
func (p *Field) IsDatasetLinked(s DatasetSchemaID, i DatasetID) bool {
return p.Links().HasDatasetSchemaAndDataset(s, i)
}
func (p *Field) Update(value *Value, field *SchemaField) error {
if p == nil {
return nil
}
if field == nil || p.field != field.ID() || !field.Validate(p.v) {
return ErrInvalidPropertyValue
}
p.v.SetValue(value)
return nil
}
func (p *Field) UpdateUnsafe(value *Value) {
if p == nil {
return
}
p.v.SetValue(value)
}
func (p *Field) Cast(t ValueType) bool {
if p == nil || t == ValueTypeUnknown || p.Type() == ValueTypeUnknown || p.Type() == t {
return false
}
p.v = p.v.Cast(t)
p.Unlink()
return true
}
func (p *Field) Link(links *Links) {
if p == nil {
return
}
p.links = links.Clone()
}
func (p *Field) Unlink() {
p.Link(nil)
}
func (p *Field) UpdateField(field FieldID) {
if p == nil {
return
}
p.field = field
}
func (p *Field) IsEmpty() bool {
return p == nil || p.Value().IsEmpty() && p.Links().IsEmpty()
}
func (p *Field) MigrateSchema(ctx context.Context, newSchema *Schema, dl dataset.Loader) bool {
if p == nil || dl == nil || newSchema == nil {
return false
}
fid := p.Field()
schemaField := newSchema.Groups().Field(fid)
// If field is not found in new schema, this field should be removed
invalid := schemaField == nil
// if value is not compatible for type, value will be cleared
if !schemaField.Validate(p.v) {
p.UpdateUnsafe(nil)
}
// If linked dataset is not compatible for type, it will be unlinked
l := p.Links()
if dl != nil && l.IsLinkedFully() {
if dsid, dsfid := l.Last().Dataset(), l.Last().DatasetSchemaField(); dsid != nil && dsfid != nil {
dss, _ := dl(ctx, *dsid)
if dsf := dss[0].Field(*dsfid); dsf != nil {
if schemaField.Type() != ValueType(dsf.Type()) {
p.Unlink()
}
}
}
}
return !invalid
}
func (p *Field) MigrateDataset(q DatasetMigrationParam) {
if p == nil {
return
}
link := p.Links()
link.Replace(q.OldDatasetSchemaMap, q.OldDatasetMap, q.DatasetFieldIDMap)
if !link.Validate(q.NewDatasetSchemaMap, q.NewDatasetMap) {
p.Unlink()
}
}
func (f *Field) GuessSchema() *SchemaField {
if f == nil {
return nil
}
if f, err := NewSchemaField().ID(f.Field()).Type(f.Type()).Build(); err == nil {
return f
}
return nil
}
type DatasetMigrationParam struct {
OldDatasetSchemaMap map[DatasetSchemaID]DatasetSchemaID
OldDatasetMap map[DatasetID]DatasetID
DatasetFieldIDMap map[DatasetFieldID]DatasetFieldID
NewDatasetSchemaMap map[DatasetSchemaID]*dataset.Schema
NewDatasetMap map[DatasetID]*dataset.Dataset
}