[#185] v2: Return errors in JSON converters

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-10-28 15:10:54 +03:00 committed by Alex Vanin
parent ac38d13f04
commit 352e99d9b9
6 changed files with 105 additions and 80 deletions

View file

@ -1,93 +1,84 @@
package acl package acl
import ( import (
"errors"
acl "github.com/nspcc-dev/neofs-api-go/v2/acl/grpc" acl "github.com/nspcc-dev/neofs-api-go/v2/acl/grpc"
"google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/encoding/protojson"
) )
func RecordToJSON(r *Record) (data []byte) { var (
errEmptyInput = errors.New("empty input")
)
func RecordToJSON(r *Record) ([]byte, error) {
if r == nil { if r == nil {
return nil return nil, errEmptyInput
} }
msg := RecordToGRPCMessage(r) msg := RecordToGRPCMessage(r)
data, err := protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg) return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg)
if err != nil {
return nil
} }
return func RecordFromJSON(data []byte) (*Record, error) {
}
func RecordFromJSON(data []byte) *Record {
if len(data) == 0 { if len(data) == 0 {
return nil return nil, errEmptyInput
} }
msg := new(acl.EACLRecord) msg := new(acl.EACLRecord)
if err := protojson.Unmarshal(data, msg); err != nil { if err := protojson.Unmarshal(data, msg); err != nil {
return nil return nil, err
} }
return RecordFromGRPCMessage(msg) return RecordFromGRPCMessage(msg), nil
} }
func TableToJSON(t *Table) (data []byte) { func TableToJSON(t *Table) ([]byte, error) {
if t == nil { if t == nil {
return nil return nil, errEmptyInput
} }
msg := TableToGRPCMessage(t) msg := TableToGRPCMessage(t)
data, err := protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg) return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg)
if err != nil {
return nil
} }
return func TableFromJSON(data []byte) (*Table, error) {
}
func TableFromJSON(data []byte) *Table {
if len(data) == 0 { if len(data) == 0 {
return nil return nil, errEmptyInput
} }
msg := new(acl.EACLTable) msg := new(acl.EACLTable)
if err := protojson.Unmarshal(data, msg); err != nil { if err := protojson.Unmarshal(data, msg); err != nil {
return nil return nil, err
} }
return TableFromGRPCMessage(msg) return TableFromGRPCMessage(msg), nil
} }
func BearerTokenToJSON(t *BearerToken) (data []byte) { func BearerTokenToJSON(t *BearerToken) ([]byte, error) {
if t == nil { if t == nil {
return nil return nil, errEmptyInput
} }
msg := BearerTokenToGRPCMessage(t) msg := BearerTokenToGRPCMessage(t)
data, err := protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg) return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg)
if err != nil {
return nil
} }
return func BearerTokenFromJSON(data []byte) (*BearerToken, error) {
}
func BearerTokenFromJSON(data []byte) *BearerToken {
if len(data) == 0 { if len(data) == 0 {
return nil return nil, errEmptyInput
} }
msg := new(acl.BearerToken) msg := new(acl.BearerToken)
if err := protojson.Unmarshal(data, msg); err != nil { if err := protojson.Unmarshal(data, msg); err != nil {
return nil return nil, err
} }
return BearerTokenFromGRPCMessage(msg) return BearerTokenFromGRPCMessage(msg), nil
} }

View file

@ -11,39 +11,55 @@ func TestRecordJSON(t *testing.T) {
exp := generateRecord(false) exp := generateRecord(false)
t.Run("non empty", func(t *testing.T) { t.Run("non empty", func(t *testing.T) {
data := acl.RecordToJSON(exp) data, err := acl.RecordToJSON(exp)
require.NotNil(t, data) require.NoError(t, err)
got := acl.RecordFromJSON(data) got, err := acl.RecordFromJSON(data)
require.NotNil(t, got) require.NoError(t, err)
require.Equal(t, exp, got) require.Equal(t, exp, got)
}) })
t.Run("empty", func(t *testing.T) {
_, err := acl.RecordToJSON(nil)
require.Error(t, err)
_, err = acl.RecordFromJSON(nil)
require.Error(t, err)
})
} }
func TestEACLTableJSON(t *testing.T) { func TestEACLTableJSON(t *testing.T) {
exp := generateEACL() exp := generateEACL()
t.Run("non empty", func(t *testing.T) { t.Run("non empty", func(t *testing.T) {
data := acl.TableToJSON(exp) data, err := acl.TableToJSON(exp)
require.NotNil(t, data) require.NoError(t, err)
got := acl.TableFromJSON(data) got, err := acl.TableFromJSON(data)
require.NotNil(t, got) require.NoError(t, err)
require.Equal(t, exp, got) require.Equal(t, exp, got)
}) })
t.Run("empty", func(t *testing.T) {
_, err := acl.TableToJSON(nil)
require.Error(t, err)
_, err = acl.TableFromJSON(nil)
require.Error(t, err)
})
} }
func TestBearerTokenJSON(t *testing.T) { func TestBearerTokenJSON(t *testing.T) {
exp := generateBearerToken("token") exp := generateBearerToken("token")
t.Run("non empty", func(t *testing.T) { t.Run("non empty", func(t *testing.T) {
data := acl.BearerTokenToJSON(exp) data, err := acl.BearerTokenToJSON(exp)
require.NotNil(t, data) require.NoError(t, err)
got := acl.BearerTokenFromJSON(data) got, err := acl.BearerTokenFromJSON(data)
require.NotNil(t, got) require.NoError(t, err)
require.Equal(t, exp, got) require.Equal(t, exp, got)
}) })

View file

@ -1,35 +1,36 @@
package container package container
import ( import (
"errors"
container "github.com/nspcc-dev/neofs-api-go/v2/container/grpc" container "github.com/nspcc-dev/neofs-api-go/v2/container/grpc"
"google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/encoding/protojson"
) )
func ContainerToJSON(c *Container) (data []byte) { var (
errEmptyInput = errors.New("empty input")
)
func ContainerToJSON(c *Container) ([]byte, error) {
if c == nil { if c == nil {
return nil return nil, errEmptyInput
} }
msg := ContainerToGRPCMessage(c) msg := ContainerToGRPCMessage(c)
data, err := protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg) return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg)
if err != nil {
return nil
} }
return func ContainerFromJSON(data []byte) (*Container, error) {
}
func ContainerFromJSON(data []byte) *Container {
if len(data) == 0 { if len(data) == 0 {
return nil return nil, errEmptyInput
} }
msg := new(container.Container) msg := new(container.Container)
if err := protojson.Unmarshal(data, msg); err != nil { if err := protojson.Unmarshal(data, msg); err != nil {
return nil return nil, err
} }
return ContainerFromGRPCMessage(msg) return ContainerFromGRPCMessage(msg), nil
} }

View file

@ -11,12 +11,20 @@ func TestContainerJSON(t *testing.T) {
exp := generateContainer("container") exp := generateContainer("container")
t.Run("non empty", func(t *testing.T) { t.Run("non empty", func(t *testing.T) {
data := container.ContainerToJSON(exp) data, err := container.ContainerToJSON(exp)
require.NotNil(t, data) require.NoError(t, err)
got := container.ContainerFromJSON(data) got, err := container.ContainerFromJSON(data)
require.NotNil(t, got) require.NoError(t, err)
require.Equal(t, exp, got) require.Equal(t, exp, got)
}) })
t.Run("empty", func(t *testing.T) {
_, err := container.ContainerToJSON(nil)
require.Error(t, err)
_, err = container.ContainerFromJSON(nil)
require.Error(t, err)
})
} }

View file

@ -1,35 +1,36 @@
package netmap package netmap
import ( import (
"errors"
netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc" netmap "github.com/nspcc-dev/neofs-api-go/v2/netmap/grpc"
"google.golang.org/protobuf/encoding/protojson" "google.golang.org/protobuf/encoding/protojson"
) )
func NodeInfoToJSON(n *NodeInfo) (data []byte) { var (
errEmptyInput = errors.New("empty input")
)
func NodeInfoToJSON(n *NodeInfo) ([]byte, error) {
if n == nil { if n == nil {
return nil return nil, errEmptyInput
} }
msg := NodeInfoToGRPCMessage(n) msg := NodeInfoToGRPCMessage(n)
data, err := protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg) return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg)
if err != nil {
return nil
} }
return func NodeInfoFromJSON(data []byte) (*NodeInfo, error) {
}
func NodeInfoFromJSON(data []byte) *NodeInfo {
if len(data) == 0 { if len(data) == 0 {
return nil return nil, errEmptyInput
} }
msg := new(netmap.NodeInfo) msg := new(netmap.NodeInfo)
if err := protojson.Unmarshal(data, msg); err != nil { if err := protojson.Unmarshal(data, msg); err != nil {
return nil return nil, err
} }
return NodeInfoFromGRPCMessage(msg) return NodeInfoFromGRPCMessage(msg), nil
} }

View file

@ -11,12 +11,20 @@ func TestNodeInfoJSON(t *testing.T) {
exp := generateNodeInfo("public key", "/multi/addr", 2) exp := generateNodeInfo("public key", "/multi/addr", 2)
t.Run("non empty", func(t *testing.T) { t.Run("non empty", func(t *testing.T) {
data := netmap.NodeInfoToJSON(exp) data, err := netmap.NodeInfoToJSON(exp)
require.NotNil(t, data) require.NoError(t, err)
got := netmap.NodeInfoFromJSON(data) got, err := netmap.NodeInfoFromJSON(data)
require.NotNil(t, got) require.NoError(t, err)
require.Equal(t, exp, got) require.Equal(t, exp, got)
}) })
t.Run("empty", func(t *testing.T) {
_, err := netmap.NodeInfoToJSON(nil)
require.Error(t, err)
_, err = netmap.NodeInfoFromJSON(nil)
require.Error(t, err)
})
} }