File tree Expand file tree Collapse file tree 2 files changed +24
-1
lines changed
Expand file tree Collapse file tree 2 files changed +24
-1
lines changed Original file line number Diff line number Diff line change @@ -4,13 +4,16 @@ import (
44 "encoding/json"
55 "reflect"
66
7+ "github.com/gogo/protobuf/proto"
8+
79 "github.com/smartcontractkit/chainlink-common/pkg/logger"
810)
911
1012// IsSerializable returns true if the value can be marshaled and unmarshaled without losing information, false otherwise.
1113// For idempotency and reporting purposes, we need to ensure that the value can be marshaled and unmarshaled
1214// without losing information.
1315// If the value implements json.Marshaler and json.Unmarshaler, it is assumed to be serializable.
16+ // If the value is a protobuf message, it is also considered serializable.
1417func IsSerializable (lggr logger.Logger , v any ) bool {
1518 if ! isValueSerializable (lggr , reflect .ValueOf (v )) {
1619 return false
@@ -25,11 +28,19 @@ func IsSerializable(lggr logger.Logger, v any) bool {
2528 return true
2629}
2730
31+ func isProtoMessage (v reflect.Value ) bool {
32+ protoMsgType := reflect .TypeOf ((* proto .Message )(nil )).Elem ()
33+ return v .Type ().Implements (protoMsgType )
34+ }
35+
2836func isValueSerializable (lggr logger.Logger , v reflect.Value ) bool {
2937 // Handle nil values
3038 if ! v .IsValid () {
3139 return true
3240 }
41+ if isProtoMessage (v ) {
42+ return true
43+ }
3344
3445 // Check if type implements json.Marshaler and json.Unmarshaler
3546 fieldTypeRef := v .Type ()
Original file line number Diff line number Diff line change @@ -8,12 +8,14 @@ import (
88
99 "github.com/Masterminds/semver/v3"
1010 chainsel "github.com/smartcontractkit/chain-selectors"
11- "github.com/smartcontractkit/chainlink-common/pkg/logger"
1211 mcmslib "github.com/smartcontractkit/mcms"
1312 "github.com/smartcontractkit/mcms/types"
1413 "github.com/stretchr/testify/require"
1514
15+ "github.com/smartcontractkit/chainlink-common/pkg/logger"
16+
1617 "github.com/smartcontractkit/chainlink-deployments-framework/datastore"
18+ pb "github.com/smartcontractkit/chainlink-protos/op-catalog/v1/datastore"
1719)
1820
1921func Test_IsSerializable (t * testing.T ) {
@@ -151,6 +153,16 @@ func Test_IsSerializable(t *testing.T) {
151153 v : datastore.AddressRef {},
152154 want : true ,
153155 },
156+ {
157+ name : "should serialize proto message" ,
158+ // this is arbitrary proto message just to test the code path
159+ v : & pb.AddressReferenceEditResponse {
160+ Record : & pb.AddressReference {
161+ Domain : "anything" ,
162+ },
163+ },
164+ want : true ,
165+ },
154166 }
155167
156168 for _ , tt := range tests {
You can’t perform that action at this time.
0 commit comments