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

Commit a1cf54a

Browse files
authored
static key for mapping (#69)
1 parent 792dcfa commit a1cf54a

6 files changed

Lines changed: 238 additions & 6 deletions

File tree

state/mapping/mapping_test.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestState(t *testing.T) {
2828
}
2929

3030
var (
31-
compositeIDCC, complexIDCC, sliceIDCC, indexesCC *testcc.MockStub
31+
compositeIDCC, complexIDCC, sliceIDCC, indexesCC, configCC *testcc.MockStub
3232

3333
Owner = identitytestdata.Certificates[0].MustIdentity(`SOME_MSP`)
3434
)
@@ -47,6 +47,9 @@ var _ = Describe(`Mapping`, func() {
4747

4848
indexesCC = testcc.NewMockStub(`indexes`, testdata.NewIndexesCC())
4949
indexesCC.From(Owner).Init()
50+
51+
configCC = testcc.NewMockStub(`config`, testdata.NewCCWithConfig())
52+
configCC.From(Owner).Init()
5053
})
5154

5255
Describe(`Entity with composite id`, func() {
@@ -369,4 +372,26 @@ var _ = Describe(`Mapping`, func() {
369372
})
370373

371374
})
375+
376+
Describe(`Entity with static key`, func() {
377+
configSample := &schema.Config{
378+
Field1: `aaa`,
379+
Field2: `bbb`,
380+
}
381+
382+
It("Disallow to get config before set", func() {
383+
expectcc.ResponseError(configCC.Invoke(`configGet`), `state entry not found: Config | config`)
384+
})
385+
386+
It("Allow to set config", func() {
387+
expectcc.ResponseOk(configCC.Invoke(`configSet`, configSample))
388+
})
389+
390+
It("Allow to get config", func() {
391+
confFromCC := expectcc.PayloadIs(configCC.Invoke(`configGet`), &schema.Config{}).(*schema.Config)
392+
Expect(confFromCC.Field1).To(Equal(configSample.Field1))
393+
Expect(confFromCC.Field2).To(Equal(configSample.Field2))
394+
})
395+
396+
})
372397
})

state/mapping/state_mapping_opt.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,22 @@ const (
1616
TimestampKeyLayout = `2006-01-02`
1717
)
1818

19-
// StateNamespace sets namespace for mapping
20-
func StateNamespace(namespace state.Key) StateMappingOpt {
19+
// WithStateNamespace sets namespace for mapping
20+
func WithStateNamespace(namespace state.Key) StateMappingOpt {
2121
return func(sm *StateMapping, smm StateMappings) {
2222
sm.namespace = namespace
2323
}
2424
}
2525

26+
// WithStaticPKey set static key for all instances of mapped entry
27+
func WithStaticPKey(key state.Key) StateMappingOpt {
28+
return func(sm *StateMapping, smm StateMappings) {
29+
sm.primaryKeyer = func(_ interface{}) (state.Key, error) {
30+
return key, nil
31+
}
32+
}
33+
}
34+
2635
func KeyerFor(schema interface{}) StateMappingOpt {
2736
return func(sm *StateMapping, smm StateMappings) {
2837
sm.keyerForSchema = schema
@@ -99,7 +108,7 @@ func PKeySchema(pkeySchema interface{}) StateMappingOpt {
99108
//add mapping for schema identifier
100109
smm.Add(
101110
pkeySchema,
102-
StateNamespace(namespace),
111+
WithStateNamespace(namespace),
103112
PKeyAttr(attrs...),
104113
KeyerFor(sm.schema))
105114
}
@@ -131,7 +140,7 @@ func PKeyComplexId(pkeySchema interface{}) StateMappingOpt {
131140
return func(sm *StateMapping, smm StateMappings) {
132141
sm.primaryKeyer = attrsKeyer([]string{`Id`})
133142
smm.Add(pkeySchema,
134-
StateNamespace(SchemaNamespace(sm.schema)),
143+
WithStateNamespace(SchemaNamespace(sm.schema)),
135144
PKeyAttr(attrsFrom(pkeySchema)...),
136145
KeyerFor(sm.schema))
137146
}

state/mapping/testdata/cc_composite_id.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ var (
1414
EntityCompositeIdNamespace = state.Key{`entity-composite-id`}
1515
EntityWithCompositeIdStateMapping = mapping.StateMappings{}.Add(&schema.EntityWithCompositeId{},
1616
// explicit set namespace for primary key, otherwise namespace will be based of schema type string representation
17-
mapping.StateNamespace(EntityCompositeIdNamespace),
17+
mapping.WithStateNamespace(EntityCompositeIdNamespace),
1818
// schema for Primary Key
1919
mapping.PKeySchema(&schema.EntityCompositeId{}),
2020
mapping.List(&schema.EntityWithCompositeIdList{}))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package testdata
2+
3+
import (
4+
"github.com/s7techlab/cckit/extensions/owner"
5+
"github.com/s7techlab/cckit/router"
6+
"github.com/s7techlab/cckit/router/param/defparam"
7+
"github.com/s7techlab/cckit/state"
8+
m "github.com/s7techlab/cckit/state/mapping"
9+
"github.com/s7techlab/cckit/state/mapping/testdata/schema"
10+
)
11+
12+
func NewCCWithConfig() *router.Chaincode {
13+
r := router.New(`withConfig`)
14+
15+
// Mappings for chaincode state
16+
r.Use(m.MapStates(m.StateMappings{}.
17+
//key will be <`config`>
18+
Add(&schema.Config{}, m.WithStaticPKey(state.Key{`config`}))))
19+
20+
r.Init(owner.InvokeSetFromCreator)
21+
22+
r.Group(`config`).
23+
Invoke(`Set`, configSet, defparam.Proto(&schema.Config{})).
24+
Invoke(`Get`, configGet)
25+
26+
return router.NewChaincode(r)
27+
}
28+
29+
func configGet(c router.Context) (interface{}, error) {
30+
return c.State().Get(&schema.Config{}, &schema.Config{})
31+
}
32+
33+
func configSet(c router.Context) (interface{}, error) {
34+
conf := c.Param()
35+
return conf, c.State().Put(conf)
36+
}

state/mapping/testdata/schema/some_config.pb.go

Lines changed: 155 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
package schema;
3+
4+
message Config {
5+
string field1 = 1;
6+
string field2 = 2;
7+
}

0 commit comments

Comments
 (0)