forked from TrueCloudLab/frostfs-api-go
[#168] acl: Implement binary/JSON encoders/decoders on BearerToken
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
d8fa8df442
commit
ec957be60c
6 changed files with 108 additions and 44 deletions
|
@ -1,40 +1,10 @@
|
|||
package acl
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
acl "github.com/nspcc-dev/neofs-api-go/v2/acl/grpc"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
)
|
||||
|
||||
var (
|
||||
errEmptyInput = errors.New("empty input")
|
||||
)
|
||||
|
||||
func BearerTokenToJSON(t *BearerToken) ([]byte, error) {
|
||||
if t == nil {
|
||||
return nil, errEmptyInput
|
||||
}
|
||||
|
||||
msg := BearerTokenToGRPCMessage(t)
|
||||
|
||||
return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg)
|
||||
}
|
||||
|
||||
func BearerTokenFromJSON(data []byte) (*BearerToken, error) {
|
||||
if len(data) == 0 {
|
||||
return nil, errEmptyInput
|
||||
}
|
||||
|
||||
msg := new(acl.BearerToken)
|
||||
|
||||
if err := protojson.Unmarshal(data, msg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return BearerTokenFromGRPCMessage(msg), nil
|
||||
}
|
||||
|
||||
func (f *HeaderFilter) MarshalJSON() ([]byte, error) {
|
||||
return protojson.MarshalOptions{
|
||||
EmitUnpopulated: true,
|
||||
|
@ -154,3 +124,23 @@ func (bt *BearerTokenBody) UnmarshalJSON(data []byte) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (bt *BearerToken) MarshalJSON() ([]byte, error) {
|
||||
return protojson.MarshalOptions{
|
||||
EmitUnpopulated: true,
|
||||
}.Marshal(
|
||||
BearerTokenToGRPCMessage(bt),
|
||||
)
|
||||
}
|
||||
|
||||
func (bt *BearerToken) UnmarshalJSON(data []byte) error {
|
||||
msg := new(acl.BearerToken)
|
||||
|
||||
if err := protojson.Unmarshal(data, msg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*bt = *BearerTokenFromGRPCMessage(msg)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue