From c3b6405f0fe84e4ee60bfcb47e1184a8d2e75d05 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 12 Feb 2021 23:02:51 +0300 Subject: [PATCH] transaction: fix asset type JSONization It's stringy in C#. --- pkg/core/transaction/asset_type.go | 70 ++++++++++++++++++++++++++++++ pkg/rpc/client/rpc_test.go | 2 +- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/pkg/core/transaction/asset_type.go b/pkg/core/transaction/asset_type.go index 9d54eb28f..2a3caba05 100644 --- a/pkg/core/transaction/asset_type.go +++ b/pkg/core/transaction/asset_type.go @@ -1,5 +1,10 @@ package transaction +import ( + "errors" + "fmt" +) + // AssetType represents a NEO asset type. type AssetType uint8 @@ -14,3 +19,68 @@ const ( Invoice AssetType = DutyFlag | 0x18 Token AssetType = CreditFlag | 0x20 ) + +// String implements Stringer interface. +func (a AssetType) String() string { + switch a { + case CreditFlag: + return "CreditFlag" + case DutyFlag: + return "DutyFlag" + case GoverningToken: + return "GoverningToken" + case UtilityToken: + return "UtilityToken" + case Currency: + return "Currency" + case Share: + return "Share" + case Invoice: + return "Invoice" + case Token: + return "Token" + default: + return fmt.Sprintf("Unknonwn (%d)", a) + + } +} + +// MarshalJSON implements the json marshaller interface. +func (a AssetType) MarshalJSON() ([]byte, error) { + return []byte(`"` + a.String() + `"`), nil +} + +// UnmarshalJSON implements the json.Unmarshaler interface. +func (a *AssetType) UnmarshalJSON(data []byte) error { + l := len(data) + if l < 2 || data[0] != '"' || data[l-1] != '"' { + return errors.New("wrong format") + } + var err error + *a, err = AssetTypeFromString(string(data[1 : l-1])) + return err +} + +// AssetTypeFromString converts string into AssetType. +func AssetTypeFromString(s string) (AssetType, error) { + switch s { + case "CreditFlag": + return CreditFlag, nil + case "DutyFlag": + return DutyFlag, nil + case "GoverningToken": + return GoverningToken, nil + case "UtilityToken": + return UtilityToken, nil + case "Currency": + return Currency, nil + case "Share": + return Share, nil + case "Invoice": + return Invoice, nil + case "Token": + return Token, nil + default: + return 0, errors.New("bad asset type") + } +} diff --git a/pkg/rpc/client/rpc_test.go b/pkg/rpc/client/rpc_test.go index a0aba662e..f87e57296 100644 --- a/pkg/rpc/client/rpc_test.go +++ b/pkg/rpc/client/rpc_test.go @@ -178,7 +178,7 @@ var rpcClientTestCases = map[string][]rpcClientTestCase{ invoke: func(c *Client) (interface{}, error) { return c.GetAssetState(util.Uint256{}) }, - serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"id":"0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b","type":0,"name":"NEO","amount":"100000000","available":"100000000","precision":0,"owner":"00","admin":"Abf2qMs1pzQb8kYk9RuxtUb9jtRKJVuBJt","issuer":"AFmseVrdL9f9oyCzZefL9tG6UbvhPbdYzM","expiration":4000000,"frozen":false}}`, + serverResponse: `{"id":1,"jsonrpc":"2.0","result":{"id":"0xc56f33fc6ecfcd0c225c4ab356fee59390af8560be0e930faebe74a6daff7c9b","type":"GoverningToken","name":"NEO","amount":"100000000","available":"100000000","precision":0,"owner":"00","admin":"Abf2qMs1pzQb8kYk9RuxtUb9jtRKJVuBJt","issuer":"AFmseVrdL9f9oyCzZefL9tG6UbvhPbdYzM","expiration":4000000,"frozen":false}}`, result: func(c *Client) interface{} { return &result.AssetState{ ID: core.GoverningTokenID(),