[#172] v2/acl: Add JSON converters for EACL and bearer token

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-10-19 16:25:42 +03:00 committed by Alex Vanin
parent 5f6720f456
commit cb188e63b7
4 changed files with 148 additions and 0 deletions

1
go.mod
View file

@ -3,6 +3,7 @@ module github.com/nspcc-dev/neofs-api-go
go 1.14 go 1.14
require ( require (
github.com/gogo/protobuf v1.1.1
github.com/golang/protobuf v1.4.2 github.com/golang/protobuf v1.4.2
github.com/google/uuid v1.1.1 github.com/google/uuid v1.1.1
github.com/mr-tron/base58 v1.1.2 github.com/mr-tron/base58 v1.1.2

1
go.sum
View file

@ -67,6 +67,7 @@ github.com/go-redis/redis v6.10.2+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8w
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o= github.com/go-yaml/yaml v2.1.0+incompatible h1:RYi2hDdss1u4YE7GwixGzWwVo47T8UQwnTLB6vQiq+o=
github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0= github.com/go-yaml/yaml v2.1.0+incompatible/go.mod h1:w2MrLa16VYP0jy6N7M5kHaCkaLENm+P+Tv+MfurjSw0=
github.com/gogo/protobuf v1.1.1 h1:72R+M5VuhED/KujmZVcIquuo8mBgX4oVda//DQb3PXo=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=

96
v2/acl/json.go Normal file
View file

@ -0,0 +1,96 @@
package acl
import (
"github.com/golang/protobuf/jsonpb"
acl "github.com/nspcc-dev/neofs-api-go/v2/acl/grpc"
)
func RecordToJSON(r *Record) []byte {
if r == nil {
return nil
}
msg := RecordToGRPCMessage(r)
m := jsonpb.Marshaler{}
s, err := m.MarshalToString(msg)
if err != nil {
return nil
}
return []byte(s)
}
func RecordFromJSON(data []byte) *Record {
if len(data) == 0 {
return nil
}
msg := new(acl.EACLRecord)
if err := jsonpb.UnmarshalString(string(data), msg); err != nil {
return nil
}
return RecordFromGRPCMessage(msg)
}
func TableToJSON(t *Table) (data []byte) {
if t == nil {
return nil
}
msg := TableToGRPCMessage(t)
m := jsonpb.Marshaler{}
s, err := m.MarshalToString(msg)
if err != nil {
return nil
}
return []byte(s)
}
func TableFromJSON(data []byte) *Table {
if len(data) == 0 {
return nil
}
msg := new(acl.EACLTable)
if jsonpb.UnmarshalString(string(data), msg) != nil {
return nil
}
return TableFromGRPCMessage(msg)
}
func BearerTokenToJSON(t *BearerToken) (data []byte) {
if t == nil {
return nil
}
msg := BearerTokenToGRPCMessage(t)
m := jsonpb.Marshaler{}
s, err := m.MarshalToString(msg)
if err != nil {
return nil
}
return []byte(s)
}
func BearerTokenFromJSON(data []byte) *BearerToken {
if len(data) == 0 {
return nil
}
msg := new(acl.BearerToken)
if jsonpb.UnmarshalString(string(data), msg) != nil {
return nil
}
return BearerTokenFromGRPCMessage(msg)
}

50
v2/acl/json_test.go Normal file
View file

@ -0,0 +1,50 @@
package acl_test
import (
"testing"
"github.com/nspcc-dev/neofs-api-go/v2/acl"
"github.com/stretchr/testify/require"
)
func TestRecordJSON(t *testing.T) {
exp := generateRecord(false)
t.Run("non empty", func(t *testing.T) {
data := acl.RecordToJSON(exp)
require.NotNil(t, data)
got := acl.RecordFromJSON(data)
require.NotNil(t, got)
require.Equal(t, exp, got)
})
}
func TestEACLTableJSON(t *testing.T) {
exp := generateEACL()
t.Run("non empty", func(t *testing.T) {
data := acl.TableToJSON(exp)
require.NotNil(t, data)
got := acl.TableFromJSON(data)
require.NotNil(t, got)
require.Equal(t, exp, got)
})
}
func TestBearerTokenJSON(t *testing.T) {
exp := generateBearerToken("token")
t.Run("non empty", func(t *testing.T) {
data := acl.BearerTokenToJSON(exp)
require.NotNil(t, data)
got := acl.BearerTokenFromJSON(data)
require.NotNil(t, got)
require.Equal(t, exp, got)
})
}