forked from TrueCloudLab/frostfs-api-go
[#186] v2/netmap: Add JSON converter for placement policy
Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
parent
2b171e1617
commit
e4d94bbe03
4 changed files with 50 additions and 0 deletions
1
go.mod
1
go.mod
|
@ -3,6 +3,7 @@ module github.com/nspcc-dev/neofs-api-go
|
||||||
go 1.14
|
go 1.14
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/alecthomas/participle v0.6.0
|
||||||
github.com/golang/protobuf v1.4.3
|
github.com/golang/protobuf v1.4.3
|
||||||
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
|
||||||
|
|
3
go.sum
3
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/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/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/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-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
|
||||||
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/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=
|
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
|
||||||
|
|
|
@ -34,3 +34,27 @@ func NodeInfoFromJSON(data []byte) (*NodeInfo, error) {
|
||||||
|
|
||||||
return NodeInfoFromGRPCMessage(msg), nil
|
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
|
||||||
|
}
|
||||||
|
|
|
@ -28,3 +28,25 @@ func TestNodeInfoJSON(t *testing.T) {
|
||||||
require.Error(t, err)
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue