Skip to content

Commit 39a4aa6

Browse files
committed
add MarshalStructpb method for non-native scalar types
1 parent e9846d8 commit 39a4aa6

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ require (
77
github.com/ethereum/go-ethereum v1.12.2
88
github.com/graph-gophers/graphql-go v1.5.0
99
github.com/stretchr/testify v1.9.0
10+
google.golang.org/protobuf v1.33.0
1011
gopkg.in/yaml.v3 v3.0.1
1112
)
1213

go.sum

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ github.com/ethereum/go-ethereum v1.12.2/go.mod h1:1cRAEV+rp/xX0zraSCBnu9Py3HQ+ge
88
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
99
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
1010
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
11+
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
1112
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
1213
github.com/graph-gophers/graphql-go v1.5.0 h1:fDqblo50TEpD0LY7RXk/LFVYEVqo3+tXMNMPSVXA1yc=
1314
github.com/graph-gophers/graphql-go v1.5.0/go.mod h1:YtmJZDLbF1YYNrlNAuiO5zAStUWc3XZT07iGsVqe1Os=
@@ -26,6 +27,10 @@ golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq
2627
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
2728
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
2829
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
30+
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df h1:5Pf6pFKu98ODmgnpvkJ3kFUOQGGLIzLIkbzUHp47618=
31+
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df/go.mod h1:K8+ghG5WaK9qNqU5K3HdILfMLy1f3aNYFI/wnl100a8=
32+
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
33+
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
2934
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
3035
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3136
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

tools/config.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package: types
33
import:
44
- github.com/ethereum/go-ethereum/common
55
- github.com/ethereum/go-ethereum/common/hexutil
6+
- google.golang.org/protobuf/types/known/structpb
67
- time
78

89
json-package: encoding/json

tools/gen.go

+6
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,12 @@ func (s *#{name}) String() string {
171171
`, "name", scalarType.TypeName())
172172
default:
173173
g.w.Out("type %s struct { %s }\n", scalarType.TypeName(), scalarGoType)
174+
g.w.Outf(`
175+
func (s *#{name}) MarshalStructpb() *structpb.Value {
176+
return structpb.NewStringValue(s.String())
177+
}
178+
179+
`, "name", scalarType.TypeName())
174180
}
175181
}
176182
g.w.Out(`

types/types.go

+50
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"github.com/ethereum/go-ethereum/common"
99
"github.com/ethereum/go-ethereum/common/hexutil"
10+
"google.golang.org/protobuf/types/known/structpb"
1011
"reflect"
1112
"strconv"
1213
"time"
@@ -17,16 +18,41 @@ import (
1718
// --------------------
1819

1920
type Address struct{ common.Hash }
21+
22+
func (s *Address) MarshalStructpb() *structpb.Value {
23+
return structpb.NewStringValue(s.String())
24+
}
25+
2026
type AssetId struct{ common.Hash }
27+
28+
func (s *AssetId) MarshalStructpb() *structpb.Value {
29+
return structpb.NewStringValue(s.String())
30+
}
31+
2132
type BlockId struct{ common.Hash }
33+
34+
func (s *BlockId) MarshalStructpb() *structpb.Value {
35+
return structpb.NewStringValue(s.String())
36+
}
37+
2238
type Boolean bool
2339

2440
func (s *Boolean) String() string {
2541
return strconv.FormatBool(bool(*s))
2642
}
2743

2844
type Bytes32 struct{ common.Hash }
45+
46+
func (s *Bytes32) MarshalStructpb() *structpb.Value {
47+
return structpb.NewStringValue(s.String())
48+
}
49+
2950
type ContractId struct{ common.Hash }
51+
52+
func (s *ContractId) MarshalStructpb() *structpb.Value {
53+
return structpb.NewStringValue(s.String())
54+
}
55+
3056
type Float float64
3157

3258
func (s *Float) UnmarshalJSON(raw []byte) error {
@@ -47,6 +73,11 @@ func (s *Float) String() string {
4773
}
4874

4975
type HexString struct{ hexutil.Bytes }
76+
77+
func (s *HexString) MarshalStructpb() *structpb.Value {
78+
return structpb.NewStringValue(s.String())
79+
}
80+
5081
type ID string
5182
type Int int32
5283

@@ -70,9 +101,24 @@ func (s *Int) String() string {
70101
type Nonce string
71102
type Salt string
72103
type Signature struct{ hexutil.Bytes }
104+
105+
func (s *Signature) MarshalStructpb() *structpb.Value {
106+
return structpb.NewStringValue(s.String())
107+
}
108+
73109
type String string
74110
type Tai64Timestamp struct{ time.Time }
111+
112+
func (s *Tai64Timestamp) MarshalStructpb() *structpb.Value {
113+
return structpb.NewStringValue(s.String())
114+
}
115+
75116
type TransactionId struct{ common.Hash }
117+
118+
func (s *TransactionId) MarshalStructpb() *structpb.Value {
119+
return structpb.NewStringValue(s.String())
120+
}
121+
76122
type TxPointer string
77123
type U32 uint32
78124

@@ -133,6 +179,10 @@ func (s *U8) String() string {
133179

134180
type UtxoId struct{ hexutil.Bytes }
135181

182+
func (s *UtxoId) MarshalStructpb() *structpb.Value {
183+
return structpb.NewStringValue(s.String())
184+
}
185+
136186
func UnmarshalJSONUInt(raw []byte) (uint64, error) {
137187
if len(raw) >= 2 && raw[0] == '"' && raw[len(raw)-1] == '"' {
138188
raw = raw[1 : len(raw)-1]

0 commit comments

Comments
 (0)