diff --git a/pkg/acl/eacl/record.go b/pkg/acl/eacl/record.go index 126652e..db077aa 100644 --- a/pkg/acl/eacl/record.go +++ b/pkg/acl/eacl/record.go @@ -162,3 +162,47 @@ func NewRecordFromV2(record *v2acl.Record) *Record { return r } + +// Marshal marshals Record into a protobuf binary form. +// +// Buffer is allocated when the argument is empty. +// Otherwise, the first buffer is used. +func (r *Record) Marshal(b ...[]byte) ([]byte, error) { + var buf []byte + if len(b) > 0 { + buf = b[0] + } + + return r.ToV2(). + StableMarshal(buf) +} + +// Unmarshal unmarshals protobuf binary representation of Record. +func (r *Record) Unmarshal(data []byte) error { + fV2 := new(v2acl.Record) + if err := fV2.Unmarshal(data); err != nil { + return err + } + + *r = *NewRecordFromV2(fV2) + + return nil +} + +// MarshalJSON encodes Record to protobuf JSON format. +func (r *Record) MarshalJSON() ([]byte, error) { + return r.ToV2(). + MarshalJSON() +} + +// UnmarshalJSON decodes Record from protobuf JSON format. +func (r *Record) UnmarshalJSON(data []byte) error { + tV2 := new(v2acl.Record) + if err := tV2.UnmarshalJSON(data); err != nil { + return err + } + + *r = *NewRecordFromV2(tV2) + + return nil +} diff --git a/pkg/acl/eacl/record_test.go b/pkg/acl/eacl/record_test.go index 7cddd2e..fef691d 100644 --- a/pkg/acl/eacl/record_test.go +++ b/pkg/acl/eacl/record_test.go @@ -71,3 +71,31 @@ func TestRecord_AddFilter(t *testing.T) { require.Equal(t, filters, r.Filters()) } + +func TestRecordEncoding(t *testing.T) { + r := NewRecord() + r.SetOperation(OperationHead) + r.SetAction(ActionDeny) + r.AddObjectAttributeFilter(MatchStringEqual, "key", "value") + r.AddTarget(RoleSystem, test.DecodeKey(-1).PublicKey) + + t.Run("binary", func(t *testing.T) { + data, err := r.Marshal() + require.NoError(t, err) + + r2 := NewRecord() + require.NoError(t, r2.Unmarshal(data)) + + require.Equal(t, r, r2) + }) + + t.Run("json", func(t *testing.T) { + data, err := r.MarshalJSON() + require.NoError(t, err) + + r2 := NewRecord() + require.NoError(t, r2.UnmarshalJSON(data)) + + require.Equal(t, r, r2) + }) +} diff --git a/v2/acl/json.go b/v2/acl/json.go index 72cf022..a45f666 100644 --- a/v2/acl/json.go +++ b/v2/acl/json.go @@ -122,3 +122,23 @@ func (t *Target) UnmarshalJSON(data []byte) error { return nil } + +func (r *Record) MarshalJSON() ([]byte, error) { + return protojson.MarshalOptions{ + EmitUnpopulated: true, + }.Marshal( + RecordToGRPCMessage(r), + ) +} + +func (r *Record) UnmarshalJSON(data []byte) error { + msg := new(acl.EACLRecord) + + if err := protojson.Unmarshal(data, msg); err != nil { + return err + } + + *r = *RecordFromGRPCMessage(msg) + + return nil +} diff --git a/v2/acl/marshal.go b/v2/acl/marshal.go index 8b61472..fcf70b7 100644 --- a/v2/acl/marshal.go +++ b/v2/acl/marshal.go @@ -165,6 +165,17 @@ func (r *Record) StableSize() (size int) { return size } +func (r *Record) Unmarshal(data []byte) error { + m := new(acl.EACLRecord) + if err := proto.Unmarshal(data, m); err != nil { + return err + } + + *r = *RecordFromGRPCMessage(m) + + return nil +} + // StableMarshal marshals unified header filter structure in a protobuf // compatible way without field order shuffle. func (f *HeaderFilter) StableMarshal(buf []byte) ([]byte, error) { diff --git a/v2/acl/marshal_test.go b/v2/acl/marshal_test.go index f2df14a..8d79bc1 100644 --- a/v2/acl/marshal_test.go +++ b/v2/acl/marshal_test.go @@ -156,17 +156,15 @@ func TestTargetInfo_StableMarshal(t *testing.T) { func TestRecord_StableMarshal(t *testing.T) { recordFrom := generateRecord(false) - transport := new(grpc.EACLRecord) t.Run("non empty", func(t *testing.T) { wire, err := recordFrom.StableMarshal(nil) require.NoError(t, err) - err = goproto.Unmarshal(wire, transport) - require.NoError(t, err) + to := new(acl.Record) + require.NoError(t, to.Unmarshal(wire)) - recordTo := acl.RecordFromGRPCMessage(transport) - require.Equal(t, recordFrom, recordTo) + require.Equal(t, recordFrom, to) }) }