From cb188e63b767322d8fcdbf0210f13dda160bb7c9 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Mon, 19 Oct 2020 16:25:42 +0300 Subject: [PATCH] [#172] v2/acl: Add JSON converters for EACL and bearer token Signed-off-by: Alex Vanin --- go.mod | 1 + go.sum | 1 + v2/acl/json.go | 96 +++++++++++++++++++++++++++++++++++++++++++++ v2/acl/json_test.go | 50 +++++++++++++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 v2/acl/json.go create mode 100644 v2/acl/json_test.go diff --git a/go.mod b/go.mod index d9b42dd7..b0907642 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/nspcc-dev/neofs-api-go go 1.14 require ( + github.com/gogo/protobuf v1.1.1 github.com/golang/protobuf v1.4.2 github.com/google/uuid v1.1.1 github.com/mr-tron/base58 v1.1.2 diff --git a/go.sum b/go.sum index 565fd48c..38c59006 100644 --- a/go.sum +++ b/go.sum @@ -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-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/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/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= diff --git a/v2/acl/json.go b/v2/acl/json.go new file mode 100644 index 00000000..27077936 --- /dev/null +++ b/v2/acl/json.go @@ -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) +} diff --git a/v2/acl/json_test.go b/v2/acl/json_test.go new file mode 100644 index 00000000..046141c4 --- /dev/null +++ b/v2/acl/json_test.go @@ -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) + }) +}