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

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-11-13 14:55:45 +03:00 committed by Alex Vanin
parent ae68790bbd
commit 233756ca8f
6 changed files with 117 additions and 96 deletions

View file

@ -11,54 +11,6 @@ var (
errEmptyInput = errors.New("empty input")
)
func RecordToJSON(r *Record) ([]byte, error) {
if r == nil {
return nil, errEmptyInput
}
msg := RecordToGRPCMessage(r)
return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg)
}
func RecordFromJSON(data []byte) (*Record, error) {
if len(data) == 0 {
return nil, errEmptyInput
}
msg := new(acl.EACLRecord)
if err := protojson.Unmarshal(data, msg); err != nil {
return nil, err
}
return RecordFromGRPCMessage(msg), nil
}
func TableToJSON(t *Table) ([]byte, error) {
if t == nil {
return nil, errEmptyInput
}
msg := TableToGRPCMessage(t)
return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg)
}
func TableFromJSON(data []byte) (*Table, error) {
if len(data) == 0 {
return nil, errEmptyInput
}
msg := new(acl.EACLTable)
if err := protojson.Unmarshal(data, msg); err != nil {
return nil, err
}
return TableFromGRPCMessage(msg), nil
}
func BearerTokenToJSON(t *BearerToken) ([]byte, error) {
if t == nil {
return nil, errEmptyInput
@ -142,3 +94,23 @@ func (r *Record) UnmarshalJSON(data []byte) error {
return nil
}
func (t *Table) MarshalJSON() ([]byte, error) {
return protojson.MarshalOptions{
EmitUnpopulated: true,
}.Marshal(
TableToGRPCMessage(t),
)
}
func (t *Table) UnmarshalJSON(data []byte) error {
msg := new(acl.EACLTable)
if err := protojson.Unmarshal(data, msg); err != nil {
return err
}
*t = *TableFromGRPCMessage(msg)
return nil
}