[#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

@ -1,29 +1,55 @@
package object
const (
typeRegularString = "Regular"
typeTombstoneString = "Tombstone"
typeStorageGroupString = "StorageGroup"
import (
object "github.com/nspcc-dev/neofs-api-go/v2/object/grpc"
)
// String returns string representation of Type.
func (t Type) String() string {
switch t {
default:
return typeRegularString
case TypeTombstone:
return typeTombstoneString
case TypeStorageGroup:
return typeStorageGroupString
}
return TypeToGRPCField(t).String()
}
func TypeFromString(s string) Type {
switch s {
default:
return TypeRegular
case typeTombstoneString:
return TypeTombstone
case typeStorageGroupString:
return TypeStorageGroup
// FromString parses Type from a string representation.
// It is a reverse action to String().
//
// Returns true if s was parsed successfully.
func (t *Type) FromString(s string) bool {
var g object.ObjectType
ok := g.FromString(s)
if ok {
*t = TypeFromGRPCField(g)
}
return ok
}
// TypeFromString converts string to Type.
//
// Deprecated: use FromString method.
func TypeFromString(s string) (t Type) {
t.FromString(s)
return
}
// String returns string representation of MatchType.
func (t MatchType) String() string {
return MatchTypeToGRPCField(t).String()
}
// FromString parses MatchType from a string representation.
// It is a reverse action to String().
//
// Returns true if s was parsed successfully.
func (t *MatchType) FromString(s string) bool {
var g object.MatchType
ok := g.FromString(s)
if ok {
*t = MatchTypeFromGRPCField(g)
}
return ok
}