[#168] acl: Implement binary/JSON encoders/decoders on Record

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 14:48:19 +03:00 committed by Alex Vanin
parent 9ddc4c1f48
commit ae68790bbd
5 changed files with 106 additions and 5 deletions

View file

@ -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
}

View file

@ -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)
})
}

View file

@ -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
}

View file

@ -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) {

View file

@ -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)
})
}