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

Commit ce2a4c0

Browse files
authored
fix uint mapping in entity key (#78)
1 parent d43b186 commit ce2a4c0

5 files changed

Lines changed: 36 additions & 29 deletions

File tree

state/mapping/mapping_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package mapping_test
22

33
import (
4+
"strconv"
45
"strings"
56
"testing"
67
"time"
@@ -71,7 +72,7 @@ var _ = Describe(`State mapping in chaincode`, func() {
7172
Expect(err).NotTo(HaveOccurred())
7273
Expect(key).To(Equal(
7374
testdata.EntityCompositeIdNamespace.Append(
74-
state.Key{create1.IdFirstPart, create1.IdSecondPart, testdata.Dates[0]})))
75+
state.Key{create1.IdFirstPart, strconv.Itoa(int(create1.IdSecondPart)), testdata.Dates[0]})))
7576
})
7677

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

state/mapping/state_mapping_opt.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mapping
33
import (
44
"fmt"
55
"reflect"
6+
"strconv"
67
"strings"
78

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

252-
// enum in protobuf
253-
case reflect.Int32:
253+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
254+
return state.Key{strconv.Itoa(int(v.Uint()))}, nil
255+
256+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
257+
// if it is enum in protobuf
254258
if stringer, ok := v.Interface().(fmt.Stringer); ok {
255259
return state.Key{stringer.String()}, nil
256260
}
257261

262+
return state.Key{strconv.Itoa(int(v.Int()))}, nil
263+
258264
case reflect.Ptr:
259265
// todo: extract key producer and add custom serializers
260266
switch val := v.Interface().(type) {
@@ -290,7 +296,7 @@ func keyFromValue(v reflect.Value) (state.Key, error) {
290296

291297
switch v.Type().String() {
292298

293-
case `string`, `int32`, `bool`:
299+
case `string`, `int32`, `uint32`, `bool`:
294300
// multi key possible
295301
return state.Key{v.String()}, nil
296302

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

Lines changed: 18 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

state/mapping/testdata/schema/with_composite_id.proto

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import "google/protobuf/timestamp.proto";
66
// EntityWithCompositeId
77
message EntityWithCompositeId {
88
string id_first_part = 1; // part of composite primary key
9-
string id_second_part = 2; // part of composite primary key
9+
uint32 id_second_part = 2; // part of composite primary key
1010
google.protobuf.Timestamp id_third_part = 3; // part of composite primary key
1111

1212
string name = 4;
@@ -16,7 +16,7 @@ message EntityWithCompositeId {
1616
// EntityCompositeId - container for composite primary key
1717
message EntityCompositeId {
1818
string id_first_part = 1;
19-
string id_second_part = 2;
19+
uint32 id_second_part = 2;
2020
google.protobuf.Timestamp id_third_part = 3;
2121
}
2222

@@ -28,7 +28,7 @@ message EntityWithCompositeIdList {
2828
// CreateEntityWithCompositeId
2929
message CreateEntityWithCompositeId {
3030
string id_first_part = 1;
31-
string id_second_part = 2;
31+
uint32 id_second_part = 2;
3232
google.protobuf.Timestamp id_third_part = 3;
3333
string name = 4;
3434
int32 value = 5;
@@ -37,7 +37,7 @@ message CreateEntityWithCompositeId {
3737
// UpdateEntityWithCompositeId
3838
message UpdateEntityWithCompositeId {
3939
string id_first_part = 1;
40-
string id_second_part = 2;
40+
uint32 id_second_part = 2;
4141
google.protobuf.Timestamp id_third_part = 3;
4242
string name = 4;
4343
int32 value = 5;

state/mapping/testdata/testdata.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,19 @@ var (
1818

1919
CreateEntityWithCompositeId = []*schema.CreateEntityWithCompositeId{{
2020
IdFirstPart: "A",
21-
IdSecondPart: "1",
21+
IdSecondPart: 1,
2222
IdThirdPart: testing.MustTime(Dates[0] + `T00:00:00Z`),
2323
Name: "Lorem",
2424
Value: 1,
2525
}, {
2626
IdFirstPart: "B",
27-
IdSecondPart: "1",
27+
IdSecondPart: 1,
2828
IdThirdPart: testing.MustTime(Dates[1] + `T00:00:00Z`),
2929
Name: "Ipsum",
3030
Value: 2,
3131
}, {
3232
IdFirstPart: "B",
33-
IdSecondPart: "2",
33+
IdSecondPart: 2,
3434
IdThirdPart: testing.MustTime(Dates[2] + `T00:00:00Z`),
3535
Name: "Dolor",
3636
Value: 3,

0 commit comments

Comments
 (0)