/
merge.go
309 lines (256 loc) · 9.2 KB
/
merge.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/*
* Copyright 2019 ObjectBox Ltd. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package generator
import (
"fmt"
"log"
"github.com/objectbox/objectbox-go/internal/generator/modelinfo"
)
func mergeBindingWithModelInfo(binding *Binding, modelInfo *modelinfo.ModelInfo) error {
// we need to first prepare all entities - otherwise relations wouldn't be able to find them in the model
var models = make([]*modelinfo.Entity, len(binding.Entities))
var err error
for k, bindingEntity := range binding.Entities {
models[k], err = getModelEntity(bindingEntity, modelInfo)
if err != nil {
return err
}
}
for k, bindingEntity := range binding.Entities {
if err := mergeModelEntity(bindingEntity, models[k], modelInfo); err != nil {
return err
}
}
// NOTE this is not ideal as there could be models across multiple packages
modelInfo.Package = binding.Package.Name()
return nil
}
func getModelEntity(bindingEntity *Entity, modelInfo *modelinfo.ModelInfo) (*modelinfo.Entity, error) {
if bindingEntity.Uid != 0 {
return modelInfo.FindEntityByUid(bindingEntity.Uid)
}
// we don't care about this error = either the entity is found or we create it
entity, _ := modelInfo.FindEntityByName(bindingEntity.Name)
// handle uid request
if bindingEntity.uidRequest {
var errInfo string
if entity != nil {
uid, err := entity.Id.GetUid()
if err != nil {
return nil, err
}
errInfo = fmt.Sprintf("model entity UID = %d", uid)
} else {
errInfo = "entity not found in the model"
}
return nil, fmt.Errorf("uid annotation value must not be empty (%s) on entity %s", errInfo, bindingEntity.Name)
}
if entity == nil {
return modelInfo.CreateEntity(bindingEntity.Name)
}
return entity, nil
}
func mergeModelEntity(bindingEntity *Entity, modelEntity *modelinfo.Entity, modelInfo *modelinfo.ModelInfo) (err error) {
modelEntity.Name = bindingEntity.Name
if bindingEntity.Id, bindingEntity.Uid, err = modelEntity.Id.Get(); err != nil {
return err
}
{ //region Properties
// add all properties from the bindings to the model and update/rename the changed ones
for _, bindingProperty := range bindingEntity.Properties {
if modelProperty, err := getModelProperty(bindingProperty, modelEntity, modelInfo); err != nil {
return err
} else if err := mergeModelProperty(bindingProperty, modelProperty); err != nil {
return err
}
}
// remove the missing (removed) properties
removedProperties := make([]*modelinfo.Property, 0)
for _, modelProperty := range modelEntity.Properties {
if !bindingPropertyExists(modelProperty, bindingEntity) {
removedProperties = append(removedProperties, modelProperty)
}
}
for _, property := range removedProperties {
if err := modelEntity.RemoveProperty(property); err != nil {
return err
}
}
bindingEntity.LastPropertyId = modelEntity.LastPropertyId
} //endregion
{ //region Relations
// add all standalone relations from the bindings to the model and update/rename the changed ones
for _, bindingRelation := range bindingEntity.Relations {
if modelRelation, err := getModelRelation(bindingRelation, modelEntity); err != nil {
return err
} else if err := mergeModelRelation(bindingRelation, modelRelation, modelInfo); err != nil {
return err
}
}
// remove the missing (removed) relations
removedRelations := make([]*modelinfo.StandaloneRelation, 0)
for _, modelRelation := range modelEntity.Relations {
if !bindingRelationExists(modelRelation, bindingEntity) {
removedRelations = append(removedRelations, modelRelation)
}
}
for _, relation := range removedRelations {
if err := modelEntity.RemoveRelation(relation); err != nil {
return err
}
}
} //endregion
return nil
}
func getModelProperty(bindingProperty *Property, modelEntity *modelinfo.Entity, modelInfo *modelinfo.ModelInfo) (*modelinfo.Property, error) {
if bindingProperty.Uid != 0 {
property, err := modelEntity.FindPropertyByUid(bindingProperty.Uid)
if err == nil {
return property, nil
}
// handle "reset property data" use-case - adding a new UID to an existing property
property, err2 := modelEntity.FindPropertyByName(bindingProperty.Name)
if err2 != nil {
return nil, fmt.Errorf("%v; %v", err, err2)
}
log.Printf("Notice - new UID was specified for the same property name '%s' - resetting value (recreating the property)", bindingProperty.Path())
return property, nil
}
// we don't care about this error, either the property is found or we create it
property, _ := modelEntity.FindPropertyByName(bindingProperty.Name)
// handle uid request
if bindingProperty.uidRequest {
if property != nil {
uid, err := property.Id.GetUid()
if err != nil {
return nil, err
}
newUid, err := modelInfo.GenerateUid()
if err != nil {
return nil, err
}
// handle "reset property data" use-case - adding a new UID to an existing property
return nil, fmt.Errorf(`uid annotation value must not be empty on property %s, entity %s:
[rename] apply the current UID %d
[change/reset] apply a new UID %d`,
bindingProperty.Name, bindingProperty.entity.Name, uid, newUid)
}
return nil, fmt.Errorf("uid annotation value must not be empty on an unknown property %s, entity %s",
bindingProperty.Name, bindingProperty.entity.Name)
}
if property == nil {
return modelEntity.CreateProperty()
}
return property, nil
}
func mergeModelProperty(bindingProperty *Property, modelProperty *modelinfo.Property) (err error) {
modelProperty.Name = bindingProperty.Name
// handle "reset property data" use-case - adding a new UID to an existing property
if bindingProperty.Uid != 0 {
id, _, err := modelProperty.Id.Get()
if err != nil {
return err
}
modelProperty.Id = modelinfo.CreateIdUid(id, bindingProperty.Uid)
}
if bindingProperty.Id, bindingProperty.Uid, err = modelProperty.Id.Get(); err != nil {
return err
}
if bindingProperty.Index == nil {
// if there shouldn't be an index
if modelProperty.IndexId != nil {
// if there originally was an index, remove it
if err = modelProperty.RemoveIndex(); err != nil {
return err
}
}
} else {
// if there should be an index, create it (or reuse an existing one)
if modelProperty.IndexId == nil {
if err = modelProperty.CreateIndex(); err != nil {
return err
}
}
if bindingProperty.Index.Id, bindingProperty.Index.Uid, err = modelProperty.IndexId.Get(); err != nil {
return err
}
}
if bindingProperty.Relation != nil {
modelProperty.RelationTarget = bindingProperty.Relation.Target.Name
} else {
modelProperty.RelationTarget = ""
}
modelProperty.Type = bindingProperty.ObType
modelProperty.Flags = bindingProperty.ObFlagsCombined()
return nil
}
func bindingPropertyExists(modelProperty *modelinfo.Property, bindingEntity *Entity) bool {
for _, bindingProperty := range bindingEntity.Properties {
if bindingProperty.Name == modelProperty.Name {
return true
}
}
return false
}
func getModelRelation(bindingRelation *StandaloneRelation, modelEntity *modelinfo.Entity) (*modelinfo.StandaloneRelation, error) {
if bindingRelation.Uid != 0 {
return modelEntity.FindRelationByUid(bindingRelation.Uid)
}
// we don't care about this error, either the relation is found or we create it
relation, _ := modelEntity.FindRelationByName(bindingRelation.Name)
// handle uid request
if bindingRelation.uidRequest {
var errInfo string
if relation != nil {
uid, err := relation.Id.GetUid()
if err != nil {
return nil, err
}
errInfo = fmt.Sprintf("model relation UID = %d", uid)
} else {
errInfo = "relation not found in the model"
}
return nil, fmt.Errorf("uid annotation value must not be empty (%s) on relation %s, entity %s",
errInfo, bindingRelation.Name, modelEntity.Name)
}
if relation == nil {
return modelEntity.CreateRelation()
}
return relation, nil
}
func mergeModelRelation(bindingRelation *StandaloneRelation, modelRelation *modelinfo.StandaloneRelation, modelInfo *modelinfo.ModelInfo) (err error) {
modelRelation.Name = bindingRelation.Name
if bindingRelation.Id, bindingRelation.Uid, err = modelRelation.Id.Get(); err != nil {
return err
}
// find the target entity & read it's ID/UID for the binding code
if targetEntity, err := modelInfo.FindEntityByName(bindingRelation.Target.Name); err != nil {
return err
} else if bindingRelation.Target.Id, bindingRelation.Target.Uid, err = targetEntity.Id.Get(); err != nil {
return err
} else {
modelRelation.SetTarget(targetEntity)
}
return nil
}
func bindingRelationExists(modelRelation *modelinfo.StandaloneRelation, bindingEntity *Entity) bool {
for _, bindingRelation := range bindingEntity.Relations {
if bindingRelation.Name == modelRelation.Name {
return true
}
}
return false
}