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

Commit

Permalink
fix uint mapping in entity key (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitiko authored Nov 15, 2021
1 parent d43b186 commit ce2a4c0
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 29 deletions.
3 changes: 2 additions & 1 deletion state/mapping/mapping_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mapping_test

import (
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -71,7 +72,7 @@ var _ = Describe(`State mapping in chaincode`, func() {
Expect(err).NotTo(HaveOccurred())
Expect(key).To(Equal(
testdata.EntityCompositeIdNamespace.Append(
state.Key{create1.IdFirstPart, create1.IdSecondPart, testdata.Dates[0]})))
state.Key{create1.IdFirstPart, strconv.Itoa(int(create1.IdSecondPart)), testdata.Dates[0]})))
})

It("Allow to add data to chaincode state", func(done Done) {
Expand Down
12 changes: 9 additions & 3 deletions state/mapping/state_mapping_opt.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mapping
import (
"fmt"
"reflect"
"strconv"
"strings"

"github.com/golang/protobuf/proto"
Expand Down Expand Up @@ -249,12 +250,17 @@ func keysFromValue(v reflect.Value) ([]state.Key, error) {
func keyFromValue(v reflect.Value) (state.Key, error) {
switch v.Kind() {

// enum in protobuf
case reflect.Int32:
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return state.Key{strconv.Itoa(int(v.Uint()))}, nil

case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
// if it is enum in protobuf
if stringer, ok := v.Interface().(fmt.Stringer); ok {
return state.Key{stringer.String()}, nil
}

return state.Key{strconv.Itoa(int(v.Int()))}, nil

case reflect.Ptr:
// todo: extract key producer and add custom serializers
switch val := v.Interface().(type) {
Expand Down Expand Up @@ -290,7 +296,7 @@ func keyFromValue(v reflect.Value) (state.Key, error) {

switch v.Type().String() {

case `string`, `int32`, `bool`:
case `string`, `int32`, `uint32`, `bool`:
// multi key possible
return state.Key{v.String()}, nil

Expand Down
36 changes: 18 additions & 18 deletions state/mapping/testdata/schema/with_composite_id.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions state/mapping/testdata/schema/with_composite_id.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "google/protobuf/timestamp.proto";
// EntityWithCompositeId
message EntityWithCompositeId {
string id_first_part = 1; // part of composite primary key
string id_second_part = 2; // part of composite primary key
uint32 id_second_part = 2; // part of composite primary key
google.protobuf.Timestamp id_third_part = 3; // part of composite primary key

string name = 4;
Expand All @@ -16,7 +16,7 @@ message EntityWithCompositeId {
// EntityCompositeId - container for composite primary key
message EntityCompositeId {
string id_first_part = 1;
string id_second_part = 2;
uint32 id_second_part = 2;
google.protobuf.Timestamp id_third_part = 3;
}

Expand All @@ -28,7 +28,7 @@ message EntityWithCompositeIdList {
// CreateEntityWithCompositeId
message CreateEntityWithCompositeId {
string id_first_part = 1;
string id_second_part = 2;
uint32 id_second_part = 2;
google.protobuf.Timestamp id_third_part = 3;
string name = 4;
int32 value = 5;
Expand All @@ -37,7 +37,7 @@ message CreateEntityWithCompositeId {
// UpdateEntityWithCompositeId
message UpdateEntityWithCompositeId {
string id_first_part = 1;
string id_second_part = 2;
uint32 id_second_part = 2;
google.protobuf.Timestamp id_third_part = 3;
string name = 4;
int32 value = 5;
Expand Down
6 changes: 3 additions & 3 deletions state/mapping/testdata/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ var (

CreateEntityWithCompositeId = []*schema.CreateEntityWithCompositeId{{
IdFirstPart: "A",
IdSecondPart: "1",
IdSecondPart: 1,
IdThirdPart: testing.MustTime(Dates[0] + `T00:00:00Z`),
Name: "Lorem",
Value: 1,
}, {
IdFirstPart: "B",
IdSecondPart: "1",
IdSecondPart: 1,
IdThirdPart: testing.MustTime(Dates[1] + `T00:00:00Z`),
Name: "Ipsum",
Value: 2,
}, {
IdFirstPart: "B",
IdSecondPart: "2",
IdSecondPart: 2,
IdThirdPart: testing.MustTime(Dates[2] + `T00:00:00Z`),
Name: "Dolor",
Value: 3,
Expand Down

0 comments on commit ce2a4c0

Please sign in to comment.