github.com/s7techlab/cckit@v0.10.5/state/mapping/state_keyref.go (about) 1 package mapping 2 3 import ( 4 "strings" 5 6 "github.com/s7techlab/cckit/state" 7 "github.com/s7techlab/cckit/state/schema" 8 ) 9 10 // KeyRefNamespace namespace for uniq indexes 11 const KeyRefNamespace = `_idx` 12 13 // KeyRefIDKeyer keyer for KeyRef entity 14 var KeyRefIDKeyer = attrsKeyer([]string{`Schema`, `Idx`, `RefKey`}) 15 16 var KeyRefMapper = &StateMapping{ 17 schema: &schema.KeyRef{}, 18 namespace: state.Key{KeyRefNamespace}, 19 primaryKeyer: KeyRefIDKeyer, 20 } 21 22 var KeyRefIDMapper = &StateMapping{ 23 schema: &schema.KeyRefId{}, 24 namespace: state.Key{KeyRefNamespace}, 25 primaryKeyer: KeyRefIDKeyer, 26 } 27 28 func NewKeyRef(target interface{}, idx string, refKey, pKey state.Key) *schema.KeyRef { 29 return &schema.KeyRef{ 30 Schema: strings.Join(SchemaNamespace(target), `-`), 31 Idx: idx, 32 RefKey: []string(refKey), 33 PKey: []string(pKey), 34 } 35 } 36 37 func NewKeyRefID(target interface{}, idx string, refKey state.Key) *schema.KeyRefId { 38 return &schema.KeyRefId{ 39 Schema: strings.Join(SchemaNamespace(target), `-`), 40 Idx: idx, 41 RefKey: []string(refKey), 42 } 43 } 44 45 func NewKeyRefInstance(target interface{}, idx string, refKey, pKey state.Key) *StateInstance { 46 return NewStateInstance(NewKeyRef(target, idx, refKey, pKey), KeyRefMapper, DefaultSerializer) 47 } 48 49 func NewKeyRefIDInstance(target interface{}, idx string, refKey state.Key) *StateInstance { 50 return NewStateInstance( 51 NewKeyRefID(target, idx, refKey), 52 KeyRefIDMapper, 53 DefaultSerializer, 54 ) 55 }