[#310] *: Implement string converters for enumerations

Implement `String` / `FromString` method pair in all levels of enum
definitions. From now `String()` returns canonical protojson-compatible
values.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-06-18 15:27:01 +03:00 committed by Alex Vanin
parent fdea892db7
commit 616b4b71a1
25 changed files with 1053 additions and 76 deletions

View file

@ -176,15 +176,30 @@ func (s NodeState) ToV2() netmap.NodeState {
}
}
// String returns string representation of NodeState.
//
// String mapping:
// * NodeStateOnline: ONLINE;
// * NodeStateOffline: OFFLINE;
// * default: UNSPECIFIED.
func (s NodeState) String() string {
switch s {
default:
return "STATE_UNSPECIFIED"
case NodeStateOffline:
return "OFFLINE"
case NodeStateOnline:
return "ONLINE"
return s.ToV2().String()
}
// FromString parses NodeState from a string representation.
// It is a reverse action to String().
//
// Returns true if s was parsed successfully.
func (s *NodeState) FromString(str string) bool {
var g netmap.NodeState
ok := g.FromString(str)
if ok {
*s = NodeStateFromV2(g)
}
return ok
}
// NewNodeAttribute creates and returns new NodeAttribute instance.