Skip to content
This repository was archived by the owner on Nov 8, 2023. It is now read-only.

Commit 792dcfa

Browse files
authored
fix key namespace, must be same as in parent state mapper (#68)
1 parent 9e00af0 commit 792dcfa

4 files changed

Lines changed: 43 additions & 20 deletions

File tree

state/mapping/mapping_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,7 @@ var _ = Describe(`Mapping`, func() {
5555
create3 := testdata.CreateEntityWithCompositeId[2]
5656

5757
It("Allow to get mapping data by namespace", func() {
58-
// key base on entity type
59-
baseKey := state.Key{`EntityWithCompositeId`}
60-
61-
mapping, err := testdata.EntityWithCompositeIdStateMapping.GetByNamespace(baseKey)
58+
mapping, err := testdata.EntityWithCompositeIdStateMapping.GetByNamespace(testdata.EntityCompositeIdNamespace)
6259
Expect(err).NotTo(HaveOccurred())
6360
Expect(mapping.Schema()).To(BeEquivalentTo(&schema.EntityWithCompositeId{}))
6461

@@ -70,7 +67,8 @@ var _ = Describe(`Mapping`, func() {
7067

7168
Expect(err).NotTo(HaveOccurred())
7269
Expect(key).To(Equal(
73-
baseKey.Append(state.Key{create1.IdFirstPart, create1.IdSecondPart, testdata.Dates[0]})))
70+
testdata.EntityCompositeIdNamespace.Append(
71+
state.Key{create1.IdFirstPart, create1.IdSecondPart, testdata.Dates[0]})))
7472
})
7573

7674
It("Allow to add data to chaincode state", func(done Done) {

state/mapping/state_mapping.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ type (
7575
StateMappingOpt func(*StateMapping, StateMappings)
7676
)
7777

78-
func mapKey(entry interface{}) string {
79-
return reflect.TypeOf(entry).String()
78+
// mapKey schema string representation
79+
func mapKey(schema interface{}) string {
80+
return reflect.TypeOf(schema).String()
8081
}
8182

8283
func (smm StateMappings) Add(schema interface{}, opts ...StateMappingOpt) StateMappings {
@@ -96,10 +97,11 @@ func (smm StateMappings) Add(schema interface{}, opts ...StateMappingOpt) StateM
9697
func applyStateMappingDefaults(sm *StateMapping) {
9798
// default namespace based on type name
9899
if len(sm.namespace) == 0 {
99-
sm.namespace = SchemaNamespace(sm.schema)
100+
sm.namespace = sm.DefaultNamespace()
100101
}
101102
}
102103

104+
// SchemaNamespace produces string representation of Schema type
103105
func SchemaNamespace(schema interface{}) state.Key {
104106
t := reflect.TypeOf(schema).String()
105107
return state.Key{t[strings.Index(t, `.`)+1:]}
@@ -108,18 +110,15 @@ func SchemaNamespace(schema interface{}) state.Key {
108110
// Get mapper for mapped entry
109111
func (smm StateMappings) Get(entry interface{}) (StateMapper, error) {
110112
switch id := entry.(type) {
113+
// entry is Key, namespace is first element of key
111114
case []string:
112115
return smm.GetByNamespace(id[0:1])
113116
default:
114-
m, ok := smm[mapKey(entry)]
115-
if !ok {
116-
return nil, fmt.Errorf(`%s: %s`, ErrStateMappingNotFound, mapKey(entry))
117-
}
118-
return m, nil
117+
return smm.GetBySchema(entry)
119118
}
120119
}
121120

122-
// Get mapper by string namespace. It can be used in block explorer: we know state key, but don't know
121+
// GetByNamespace returns mapper by string namespace. It can be used in block explorer: we know state key, but don't know
123122
// type actually mapped to state
124123
func (smm StateMappings) GetByNamespace(namespace state.Key) (StateMapper, error) {
125124
for _, m := range smm {
@@ -130,6 +129,14 @@ func (smm StateMappings) GetByNamespace(namespace state.Key) (StateMapper, error
130129
return nil, fmt.Errorf(`%s: %s`, ErrStateMappingNotFound, namespace)
131130
}
132131

132+
func (smm StateMappings) GetBySchema(schema interface{}) (StateMapper, error) {
133+
m, ok := smm[mapKey(schema)]
134+
if !ok {
135+
return nil, fmt.Errorf(`%s: %s`, ErrStateMappingNotFound, mapKey(schema))
136+
}
137+
return m, nil
138+
}
139+
133140
func (smm StateMappings) Exists(entry interface{}) bool {
134141
_, err := smm.Get(entry)
135142
return err == nil
@@ -167,6 +174,10 @@ func (sm *StateMapping) Namespace() state.Key {
167174
return sm.namespace
168175
}
169176

177+
func (sm *StateMapping) DefaultNamespace() state.Key {
178+
return SchemaNamespace(sm.schema)
179+
}
180+
170181
func (sm *StateMapping) Indexes() []*StateIndex {
171182
return sm.indexes
172183
}

state/mapping/state_mapping_opt.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,18 @@ func PKeySchema(pkeySchema interface{}) StateMappingOpt {
9090
return func(sm *StateMapping, smm StateMappings) {
9191
sm.primaryKeyer = attrsKeyer(attrs)
9292

93-
//add mapping namespace for id schema same as schema
94-
smm.Add(pkeySchema, StateNamespace(SchemaNamespace(sm.schema)), PKeyAttr(attrs...), KeyerFor(sm.schema))
93+
// inherit namespace from "parent" mapping
94+
namespace := sm.namespace
95+
if len(namespace) == 0 {
96+
namespace = sm.DefaultNamespace()
97+
}
98+
99+
//add mapping for schema identifier
100+
smm.Add(
101+
pkeySchema,
102+
StateNamespace(namespace),
103+
PKeyAttr(attrs...),
104+
KeyerFor(sm.schema))
95105
}
96106
}
97107

state/mapping/testdata/cc_composite_id.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,19 @@ import (
55
"github.com/s7techlab/cckit/extensions/owner"
66
"github.com/s7techlab/cckit/router"
77
"github.com/s7techlab/cckit/router/param/defparam"
8+
"github.com/s7techlab/cckit/state"
89
"github.com/s7techlab/cckit/state/mapping"
910
"github.com/s7techlab/cckit/state/mapping/testdata/schema"
1011
)
1112

1213
var (
13-
EntityWithCompositeIdStateMapping = mapping.StateMappings{}.
14-
Add(&schema.EntityWithCompositeId{},
15-
mapping.PKeySchema(&schema.EntityCompositeId{}),
16-
mapping.List(&schema.EntityWithCompositeIdList{}))
14+
EntityCompositeIdNamespace = state.Key{`entity-composite-id`}
15+
EntityWithCompositeIdStateMapping = mapping.StateMappings{}.Add(&schema.EntityWithCompositeId{},
16+
// explicit set namespace for primary key, otherwise namespace will be based of schema type string representation
17+
mapping.StateNamespace(EntityCompositeIdNamespace),
18+
// schema for Primary Key
19+
mapping.PKeySchema(&schema.EntityCompositeId{}),
20+
mapping.List(&schema.EntityWithCompositeIdList{}))
1721
)
1822

1923
func NewCompositeIdCC() *router.Chaincode {

0 commit comments

Comments
 (0)