2020-10-19 13:25:42 +00:00
|
|
|
package acl
|
|
|
|
|
|
|
|
import (
|
2020-10-28 12:10:54 +00:00
|
|
|
"errors"
|
|
|
|
|
2020-10-19 13:25:42 +00:00
|
|
|
acl "github.com/nspcc-dev/neofs-api-go/v2/acl/grpc"
|
2020-10-19 19:09:13 +00:00
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
2020-10-19 13:25:42 +00:00
|
|
|
)
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
var (
|
|
|
|
errEmptyInput = errors.New("empty input")
|
|
|
|
)
|
|
|
|
|
|
|
|
func RecordToJSON(r *Record) ([]byte, error) {
|
2020-10-19 13:25:42 +00:00
|
|
|
if r == nil {
|
2020-10-28 12:10:54 +00:00
|
|
|
return nil, errEmptyInput
|
2020-10-19 13:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := RecordToGRPCMessage(r)
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg)
|
2020-10-19 13:25:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
func RecordFromJSON(data []byte) (*Record, error) {
|
2020-10-19 13:25:42 +00:00
|
|
|
if len(data) == 0 {
|
2020-10-28 12:10:54 +00:00
|
|
|
return nil, errEmptyInput
|
2020-10-19 13:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := new(acl.EACLRecord)
|
|
|
|
|
2020-10-19 19:09:13 +00:00
|
|
|
if err := protojson.Unmarshal(data, msg); err != nil {
|
2020-10-28 12:10:54 +00:00
|
|
|
return nil, err
|
2020-10-19 13:25:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
return RecordFromGRPCMessage(msg), nil
|
2020-10-19 13:25:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
func TableToJSON(t *Table) ([]byte, error) {
|
2020-10-19 13:25:42 +00:00
|
|
|
if t == nil {
|
2020-10-28 12:10:54 +00:00
|
|
|
return nil, errEmptyInput
|
2020-10-19 13:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := TableToGRPCMessage(t)
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg)
|
2020-10-19 13:25:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
func TableFromJSON(data []byte) (*Table, error) {
|
2020-10-19 13:25:42 +00:00
|
|
|
if len(data) == 0 {
|
2020-10-28 12:10:54 +00:00
|
|
|
return nil, errEmptyInput
|
2020-10-19 13:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := new(acl.EACLTable)
|
|
|
|
|
2020-10-19 19:09:13 +00:00
|
|
|
if err := protojson.Unmarshal(data, msg); err != nil {
|
2020-10-28 12:10:54 +00:00
|
|
|
return nil, err
|
2020-10-19 13:25:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
return TableFromGRPCMessage(msg), nil
|
2020-10-19 13:25:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
func BearerTokenToJSON(t *BearerToken) ([]byte, error) {
|
2020-10-19 13:25:42 +00:00
|
|
|
if t == nil {
|
2020-10-28 12:10:54 +00:00
|
|
|
return nil, errEmptyInput
|
2020-10-19 13:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := BearerTokenToGRPCMessage(t)
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg)
|
2020-10-19 13:25:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
func BearerTokenFromJSON(data []byte) (*BearerToken, error) {
|
2020-10-19 13:25:42 +00:00
|
|
|
if len(data) == 0 {
|
2020-10-28 12:10:54 +00:00
|
|
|
return nil, errEmptyInput
|
2020-10-19 13:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := new(acl.BearerToken)
|
|
|
|
|
2020-10-19 19:09:13 +00:00
|
|
|
if err := protojson.Unmarshal(data, msg); err != nil {
|
2020-10-28 12:10:54 +00:00
|
|
|
return nil, err
|
2020-10-19 13:25:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-28 12:10:54 +00:00
|
|
|
return BearerTokenFromGRPCMessage(msg), nil
|
2020-10-19 13:25:42 +00:00
|
|
|
}
|
2020-11-13 11:31:53 +00:00
|
|
|
|
|
|
|
func (f *HeaderFilter) MarshalJSON() ([]byte, error) {
|
|
|
|
return protojson.MarshalOptions{
|
|
|
|
EmitUnpopulated: true,
|
|
|
|
}.Marshal(
|
|
|
|
HeaderFilterToGRPCMessage(f),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *HeaderFilter) UnmarshalJSON(data []byte) error {
|
|
|
|
msg := new(acl.EACLRecord_Filter)
|
|
|
|
|
|
|
|
if err := protojson.Unmarshal(data, msg); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
*f = *HeaderFilterFromGRPCMessage(msg)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|