Skip to content

Commit 796c406

Browse files
committed
support proto serialization
1 parent ac5d12c commit 796c406

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

operations/validation.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff 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.
1417
func 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+
2836
func 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()

operations/validation_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff 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

1921
func 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 {

0 commit comments

Comments
 (0)