44
55package protobuf
66
7- import "google.golang.org/protobuf/proto"
7+ import (
8+ "encoding/json"
9+
10+ "google.golang.org/protobuf/proto"
11+ )
812
913// Spec should be proto.Message and pointer.
1014type Spec [T any ] interface {
@@ -28,11 +32,23 @@ func (spec ResourceSpec[T, S]) DeepCopy() ResourceSpec[T, S] {
2832 }
2933}
3034
35+ // MarshalJSON implements json.Marshaler.
36+ func (spec * ResourceSpec [T , S ]) MarshalJSON () ([]byte , error ) {
37+ return json .Marshal (spec .Value )
38+ }
39+
3140// MarshalProto implements ProtoMarshaler.
32- func (spec ResourceSpec [T , S ]) MarshalProto () ([]byte , error ) {
41+ func (spec * ResourceSpec [T , S ]) MarshalProto () ([]byte , error ) {
3342 return ProtoMarshal (spec .Value )
3443}
3544
45+ // UnmarshalJSON implements json.Unmarshaler.
46+ func (spec * ResourceSpec [T , S ]) UnmarshalJSON (bytes []byte ) error {
47+ spec .Value = new (T )
48+
49+ return json .Unmarshal (bytes , & spec .Value )
50+ }
51+
3652// UnmarshalProto implements protobuf.ResourceUnmarshaler.
3753func (spec * ResourceSpec [T , S ]) UnmarshalProto (protoBytes []byte ) error {
3854 spec .Value = new (T )
@@ -41,13 +57,13 @@ func (spec *ResourceSpec[T, S]) UnmarshalProto(protoBytes []byte) error {
4157}
4258
4359// GetValue returns wrapped protobuf object.
44- func (spec ResourceSpec [T , S ]) GetValue () proto.Message { //nolint:ireturn
60+ func (spec * ResourceSpec [T , S ]) GetValue () proto.Message { //nolint:ireturn
4561 return spec .Value
4662}
4763
4864// Equal implements spec equality check.
49- func (spec ResourceSpec [T , S ]) Equal (other interface {}) bool {
50- otherSpec , ok := other .(ResourceSpec [T , S ])
65+ func (spec * ResourceSpec [T , S ]) Equal (other interface {}) bool {
66+ otherSpec , ok := other .(* ResourceSpec [T , S ])
5167 if ! ok {
5268 return false
5369 }
0 commit comments