forked from TrueCloudLab/frostfs-api-go
[#168] container: Implement binary/JSON encoders/decoders on Container
Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
parent
bc5d9dcce5
commit
a684da6118
6 changed files with 97 additions and 50 deletions
|
@ -1,40 +1,10 @@
|
|||
package container
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
container "github.com/nspcc-dev/neofs-api-go/v2/container/grpc"
|
||||
"google.golang.org/protobuf/encoding/protojson"
|
||||
)
|
||||
|
||||
var (
|
||||
errEmptyInput = errors.New("empty input")
|
||||
)
|
||||
|
||||
func ContainerToJSON(c *Container) ([]byte, error) {
|
||||
if c == nil {
|
||||
return nil, errEmptyInput
|
||||
}
|
||||
|
||||
msg := ContainerToGRPCMessage(c)
|
||||
|
||||
return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg)
|
||||
}
|
||||
|
||||
func ContainerFromJSON(data []byte) (*Container, error) {
|
||||
if len(data) == 0 {
|
||||
return nil, errEmptyInput
|
||||
}
|
||||
|
||||
msg := new(container.Container)
|
||||
|
||||
if err := protojson.Unmarshal(data, msg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return ContainerFromGRPCMessage(msg), nil
|
||||
}
|
||||
|
||||
func (a *Attribute) MarshalJSON() ([]byte, error) {
|
||||
return protojson.MarshalOptions{
|
||||
EmitUnpopulated: true,
|
||||
|
@ -54,3 +24,23 @@ func (a *Attribute) UnmarshalJSON(data []byte) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Container) MarshalJSON() ([]byte, error) {
|
||||
return protojson.MarshalOptions{
|
||||
EmitUnpopulated: true,
|
||||
}.Marshal(
|
||||
ContainerToGRPCMessage(c),
|
||||
)
|
||||
}
|
||||
|
||||
func (c *Container) UnmarshalJSON(data []byte) error {
|
||||
msg := new(container.Container)
|
||||
|
||||
if err := protojson.Unmarshal(data, msg); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*c = *ContainerFromGRPCMessage(msg)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue