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

Commit

Permalink
Merge 06b0f93 into 6282b30
Browse files Browse the repository at this point in the history
  • Loading branch information
vitiko committed Feb 16, 2021
2 parents 6282b30 + 06b0f93 commit 6afdb87
Show file tree
Hide file tree
Showing 13 changed files with 1,224 additions and 574 deletions.
3 changes: 2 additions & 1 deletion router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package router

import (
"fmt"
"go.uber.org/zap"
"os"

"github.com/hyperledger/fabric-chaincode-go/shim"
"github.com/hyperledger/fabric-protos-go/peer"
"github.com/s7techlab/cckit/response"
"go.uber.org/zap"
)

type (
Expand Down Expand Up @@ -252,6 +252,7 @@ func NewLogger(name string) *zap.Logger {
conf.Level = zap.NewAtomicLevel()
}

conf.DisableStacktrace = true
logger, err := conf.Build()
if err != nil {
panic(err)
Expand Down
32 changes: 28 additions & 4 deletions state/mapping/mapping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,22 @@ var _ = Describe(`Mapping`, func() {
create3 := testdata.CreateEntityWithCompositeId[2]

It("Allow to get mapping data by namespace", func() {
mapping, err := testdata.EntityWithCompositeIdStateMapping.GetByNamespace(state.Key{`EntityWithCompositeId`})
// key base on entity type
baseKey := state.Key{`EntityWithCompositeId`}

mapping, err := testdata.EntityWithCompositeIdStateMapping.GetByNamespace(baseKey)
Expect(err).NotTo(HaveOccurred())
Expect(mapping.Schema()).To(BeEquivalentTo(&schema.EntityWithCompositeId{}))

key, err := mapping.PrimaryKey(&schema.EntityWithCompositeId{
IdFirstPart: create1.IdFirstPart,
IdSecondPart: create1.IdSecondPart,
IdThirdPart: create1.IdThirdPart,
})

Expect(err).NotTo(HaveOccurred())
Expect(key).To(Equal(
baseKey.Append(state.Key{create1.IdFirstPart, create1.IdSecondPart, testdata.Dates[0]})))
})

It("Allow to add data to chaincode state", func(done Done) {
Expand Down Expand Up @@ -89,14 +102,18 @@ var _ = Describe(`Mapping`, func() {
dataFromCC := compositeIDCC.Query(`get`,
&schema.EntityCompositeId{
IdFirstPart: create1.IdFirstPart,
IdSecondPart: create1.IdSecondPart},
IdSecondPart: create1.IdSecondPart,
IdThirdPart: create1.IdThirdPart,
},
).Payload

e := &schema.EntityWithCompositeId{
IdFirstPart: create1.IdFirstPart,
IdSecondPart: create1.IdSecondPart,
Name: create1.Name,
Value: create1.Value,
IdThirdPart: create1.IdThirdPart,

Name: create1.Name,
Value: create1.Value,
}
Expect(dataFromCC).To(Equal(testcc.MustProtoMarshal(e)))
})
Expand All @@ -105,6 +122,7 @@ var _ = Describe(`Mapping`, func() {
expectcc.ResponseOk(compositeIDCC.Invoke(`update`, &schema.UpdateEntityWithCompositeId{
IdFirstPart: create1.IdFirstPart,
IdSecondPart: create1.IdSecondPart,
IdThirdPart: create1.IdThirdPart,
Name: `New name`,
Value: 1000,
}))
Expand All @@ -113,6 +131,7 @@ var _ = Describe(`Mapping`, func() {
compositeIDCC.Query(`get`, &schema.EntityCompositeId{
IdFirstPart: create1.IdFirstPart,
IdSecondPart: create1.IdSecondPart,
IdThirdPart: create1.IdThirdPart,
}),
&schema.EntityWithCompositeId{}).(*schema.EntityWithCompositeId)

Expand All @@ -125,6 +144,7 @@ var _ = Describe(`Mapping`, func() {
toDelete := &schema.EntityCompositeId{
IdFirstPart: create1.IdFirstPart,
IdSecondPart: create1.IdSecondPart,
IdThirdPart: create1.IdThirdPart,
}

expectcc.ResponseOk(compositeIDCC.Invoke(`delete`, toDelete))
Expand All @@ -140,6 +160,10 @@ var _ = Describe(`Mapping`, func() {
expectcc.ResponseOk(compositeIDCC.Invoke(`create`, create1))
})

It("Check entry keying", func() {

})

})

Describe(`Entity with complex id`, func() {
Expand Down
1 change: 1 addition & 0 deletions state/mapping/state_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type (
Schema() interface{}
List() interface{}
Namespace() state.Key
// PrimaryKey returns primary key for entry
PrimaryKey(instance interface{}) (key state.Key, err error)
Keys(instance interface{}) (key []state.KeyValue, err error)
KeyerFor() interface{}
Expand Down
45 changes: 34 additions & 11 deletions state/mapping/state_mapping_opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@ import (
"strings"

"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/golang/protobuf/ptypes/timestamp"
"github.com/s7techlab/cckit/state"
)

const (
TimestampKeyLayout = `2006-01-02`
)

// StateNamespace sets namespace for mapping
func StateNamespace(namespace state.Key) StateMappingOpt {
return func(sm *StateMapping, smm StateMappings) {
Expand Down Expand Up @@ -132,6 +138,7 @@ func skipField(name string, field reflect.Value) bool {
return false
}

// attrFrom extracts list of field names from struct
func attrsFrom(schema interface{}) (attrs []string) {
// fields from schema
s := reflect.ValueOf(schema).Elem()
Expand Down Expand Up @@ -212,23 +219,39 @@ func keysFromValue(v reflect.Value) ([]state.Key, error) {
return keys, nil
}

// keyFromValue creates string representation of value for state key
func keyFromValue(v reflect.Value) (state.Key, error) {
var key state.Key

if v.Kind() == reflect.Ptr {
s := reflect.ValueOf(v.Interface()).Elem()
fs := s.Type()
// get all field values from struct
for i := 0; i < s.NumField(); i++ {
field := s.Field(i)
if skipField(fs.Field(i).Name, field) {
continue
} else {
key = append(key, reflect.Indirect(v).Field(i).String())

// todo: extract key producer and add custom serializers
switch val := v.Interface().(type) {

case *timestamp.Timestamp:
t, err := ptypes.Timestamp(val)
if err != nil {
return nil, fmt.Errorf(`timestamp key to time: %w`, err)
}
return state.Key{t.Format(TimestampKeyLayout)}, nil

default:

s := reflect.ValueOf(v.Interface()).Elem()
fs := s.Type()
// get all field values from struct
for i := 0; i < s.NumField(); i++ {
field := s.Field(i)
if skipField(fs.Field(i).Name, field) {
continue
} else {
key = append(key, reflect.Indirect(v).Field(i).String())
}
}
}

return key, nil
return key, nil

}
}

switch v.Type().String() {
Expand Down
7 changes: 6 additions & 1 deletion state/mapping/testdata/cc_composite_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func invokeCreateComposite(c router.Context) (interface{}, error) {
entity := &schema.EntityWithCompositeId{
IdFirstPart: create.IdFirstPart,
IdSecondPart: create.IdSecondPart,
IdThirdPart: create.IdThirdPart,
Name: create.Name,
Value: create.Value,
}
Expand All @@ -65,7 +66,11 @@ func invokeCreateComposite(c router.Context) (interface{}, error) {
func invokeUpdateComposite(c router.Context) (interface{}, error) {
update := c.Param().(*schema.UpdateEntityWithCompositeId)
entity, _ := c.State().Get(
&schema.EntityCompositeId{IdFirstPart: update.IdFirstPart, IdSecondPart: update.IdSecondPart},
&schema.EntityCompositeId{
IdFirstPart: update.IdFirstPart,
IdSecondPart: update.IdSecondPart,
IdThirdPart: update.IdThirdPart,
},
&schema.EntityWithCompositeId{})

e := entity.(*schema.EntityWithCompositeId)
Expand Down

0 comments on commit 6afdb87

Please sign in to comment.