-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_entity_field.go
101 lines (96 loc) · 2.98 KB
/
edit_entity_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
package orm
import (
"hash/maphash"
"reflect"
"github.com/puzpuzpuz/xsync/v2"
)
func EditEntityField(ctx Context, entity any, field string, value any) error {
return editEntityField(ctx, entity, field, value)
}
func editEntityField(ctx Context, entity any, field string, value any) error {
schema := getEntitySchemaFromSource(ctx, entity)
setter, has := schema.fieldBindSetters[field]
if !has {
return &BindError{field, "unknown field"}
}
newValue, err := setter(value)
if err != nil {
return err
}
reflectValue := reflect.ValueOf(entity)
elem := reflectValue.Elem()
getter := schema.fieldGetters[field]
v := getter(elem)
oldValue, err := setter(v)
if err == nil && oldValue == newValue {
return nil
}
id := elem.Field(0).Uint()
cImplementation := ctx.(*ormImplementation)
var asyncError error
func() {
cImplementation.mutexFlush.Lock()
defer cImplementation.mutexFlush.Unlock()
if cImplementation.trackedEntities == nil {
cImplementation.trackedEntities = xsync.NewTypedMapOf[uint64, *xsync.MapOf[uint64, EntityFlush]](func(seed maphash.Seed, u uint64) uint64 {
return u
})
}
entities, _ := cImplementation.trackedEntities.LoadOrCompute(schema.index, func() *xsync.MapOf[uint64, EntityFlush] {
return xsync.NewTypedMapOf[uint64, EntityFlush](func(seed maphash.Seed, u uint64) uint64 {
return u
})
})
actual, loaded := entities.LoadOrCompute(id, func() EntityFlush {
editable := &editableFields{}
editable.ctx = ctx
editable.schema = schema
editable.id = id
editable.value = reflectValue
editable.newBind = Bind{field: newValue}
editable.oldBind = Bind{field: oldValue}
addUniqueIndexFieldsToBind(schema, field, editable.oldBind, editable.newBind, elem)
return editable
})
if loaded {
editable, is := actual.(*editableFields)
if is {
editable.newBind[field] = newValue
editable.oldBind[field] = oldValue
addUniqueIndexFieldsToBind(schema, field, editable.oldBind, editable.newBind, elem)
return
}
fSetter := schema.fieldSetters[field]
editableE, is := actual.(*editableEntity)
if is {
fSetter(newValue, editableE.value.Elem())
return
}
insertableE, is := actual.(*insertableEntity)
if is {
fSetter(newValue, insertableE.value.Elem())
return
}
asyncError = &BindError{Field: field, Message: "setting field in entity marked to delete not allowed"}
}
}()
return asyncError
}
func addUniqueIndexFieldsToBind(schema *entitySchema, field string, oldBind, newBind Bind, elem reflect.Value) {
uniqueIndexes := schema.GetUniqueIndexes()
for _, indexColumns := range uniqueIndexes {
for _, indexColumn := range indexColumns {
if indexColumn == field {
for _, indexColumnToAdd := range indexColumns {
if oldBind[indexColumnToAdd] == nil {
setter, _ := schema.fieldBindSetters[indexColumnToAdd]
val, _ := setter(elem.FieldByName(indexColumnToAdd).Interface())
newBind[indexColumnToAdd] = val
oldBind[indexColumnToAdd] = val
}
}
break
}
}
}
}