From e4d94bbe0306bdebdc1f8e315a2c6a915809ce8b Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 29 Oct 2020 20:13:07 +0300 Subject: [PATCH] [#186] v2/netmap: Add JSON converter for placement policy Signed-off-by: Alex Vanin --- go.mod | 1 + go.sum | 3 +++ v2/netmap/json.go | 24 ++++++++++++++++++++++++ v2/netmap/json_test.go | 22 ++++++++++++++++++++++ 4 files changed, 50 insertions(+) diff --git a/go.mod b/go.mod index 6ea4d696..49151e15 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/alecthomas/participle v0.6.0 github.com/golang/protobuf v1.4.3 github.com/google/uuid v1.1.1 github.com/mr-tron/base58 v1.1.2 diff --git a/go.sum b/go.sum index 163f26cc..612803aa 100644 --- a/go.sum +++ b/go.sum @@ -11,6 +11,9 @@ github.com/Workiva/go-datastructures v1.0.50/go.mod h1:Z+F2Rca0qCsVYDS8z7bAGm8f3 github.com/abiosoft/ishell v2.0.0+incompatible/go.mod h1:HQR9AqF2R3P4XXpMpI0NAzgHf/aS6+zVXRj14cVk9qg= github.com/abiosoft/readline v0.0.0-20180607040430-155bce2042db/go.mod h1:rB3B4rKii8V21ydCbIzH5hZiCQE7f5E9SzUb/ZZx530= github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= +github.com/alecthomas/participle v0.6.0 h1:Pvo8XUCQKgIywVjz/+Ci3IsjGg+g/TdKkMcfgghKCEw= +github.com/alecthomas/participle v0.6.0/go.mod h1:HfdmEuwvr12HXQN44HPWXR0lHmVolVYe4dyL6lQ3duY= +github.com/alecthomas/repr v0.0.0-20181024024818-d37bc2a10ba1/go.mod h1:xTS7Pm1pD1mvyM075QCDSRqH6qRLXylzS24ZTpRiSzQ= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= diff --git a/v2/netmap/json.go b/v2/netmap/json.go index cfe4dbf8..03c79f79 100644 --- a/v2/netmap/json.go +++ b/v2/netmap/json.go @@ -34,3 +34,27 @@ func NodeInfoFromJSON(data []byte) (*NodeInfo, error) { return NodeInfoFromGRPCMessage(msg), nil } + +func PlacementPolicyToJSON(n *PlacementPolicy) ([]byte, error) { + if n == nil { + return nil, errEmptyInput + } + + msg := PlacementPolicyToGRPCMessage(n) + + return protojson.MarshalOptions{EmitUnpopulated: true}.Marshal(msg) +} + +func PlacementPolicyFromJSON(data []byte) (*PlacementPolicy, error) { + if len(data) == 0 { + return nil, errEmptyInput + } + + msg := new(netmap.PlacementPolicy) + + if err := protojson.Unmarshal(data, msg); err != nil { + return nil, err + } + + return PlacementPolicyFromGRPCMessage(msg), nil +} diff --git a/v2/netmap/json_test.go b/v2/netmap/json_test.go index 528fb3ee..34abad15 100644 --- a/v2/netmap/json_test.go +++ b/v2/netmap/json_test.go @@ -28,3 +28,25 @@ func TestNodeInfoJSON(t *testing.T) { require.Error(t, err) }) } + +func TestPlacementPolicyJSON(t *testing.T) { + exp := generatePolicy(3) + + t.Run("non empty", func(t *testing.T) { + data, err := netmap.PlacementPolicyToJSON(exp) + require.NoError(t, err) + + got, err := netmap.PlacementPolicyFromJSON(data) + require.NoError(t, err) + + require.Equal(t, exp, got) + }) + + t.Run("empty", func(t *testing.T) { + _, err := netmap.PlacementPolicyToJSON(nil) + require.Error(t, err) + + _, err = netmap.PlacementPolicyFromJSON(nil) + require.Error(t, err) + }) +}