/
primary_keys_load.go
65 lines (52 loc) · 1.96 KB
/
primary_keys_load.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
package record
import (
"github.com/kyaxcorp/go-core/core/clients/db/helper"
"github.com/kyaxcorp/go-core/core/helpers/_struct"
)
func (r *Record) reloadPrimaryKeys() {
//=============== PRIMARY KEYS =================\\
// Get the model primary keys
primaryKeys := helper.GetModelPrimaryKeys(r.modelStruct)
if len(primaryKeys) == 0 {
panic("record model doesn't have any primary keys")
}
// create the vars
mHelper := _struct.New(r.modelStruct)
//mDataHelper := _struct.New(r.NonPtrObj)
pKeys := make(map[string]primaryKey)
var pKeysOrdered []primaryKey
for _, pkey := range primaryKeys {
pk := primaryKey{
fieldName: pkey,
}
// set the initial field val by taking the value from the r.NonPtrObj
//fieldValue := mDataHelper.GetFieldValue(pkey)
fieldValue, _err := r.GetInputFieldValue(pkey)
if _err != nil {
// TODO: should we do anything if the field doesn't exist from the input!?
// should we simply skip this field?! continue?
// well, if any of the primary keys is missing it doesn't mean anything... because in this case
// the user wants to create a record... and not all of the keys were supplied...
// so, we receive an error, and in this case we should take the value from the ModelStruct, why?
// because it contains the default value/state of that field!, and after that we need to use it
// in the find query...
// let's get the default value
fieldValue = _struct.GetFieldValue(r.modelStruct, pkey)
}
pk.initialFieldValue = fieldValue
dbDefaultFieldVal := mHelper.GetFieldTagKeyValue(pkey, "gorm", "default")
if dbDefaultFieldVal != "" {
pk.dbDefaultValue = dbDefaultFieldVal
pk.hasDBDefaultValue = true
}
fieldType := mHelper.GetFieldTypeName(pkey)
pk.fieldType = fieldType
// add to our maps...
pKeys[pkey] = pk
pKeysOrdered = append(pKeysOrdered, pk)
}
// Set to record
r.primaryKeys = pKeys
r.primaryKeysOrdered = pKeysOrdered
//=============== PRIMARY KEYS =================\\
}