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

@ -48,6 +48,33 @@ func SearchMatchFromV2(t v2object.MatchType) (m SearchMatchType) {
return m
}
// String returns string representation of SearchMatchType.
//
// String mapping:
// * MatchStringEqual: STRING_EQUAL;
// * MatchStringNotEqual: STRING_NOT_EQUAL;
// * MatchNotPresent: NOT_PRESENT;
// * MatchUnknown, default: MATCH_TYPE_UNSPECIFIED.
func (m SearchMatchType) String() string {
return m.ToV2().String()
}
// FromString parses SearchMatchType from a string representation.
// It is a reverse action to String().
//
// Returns true if s was parsed successfully.
func (m *SearchMatchType) FromString(s string) bool {
var g v2object.MatchType
ok := g.FromString(s)
if ok {
*m = SearchMatchFromV2(g)
}
return ok
}
type SearchFilter struct {
header filterKey
value fmt.Stringer

View file

@ -192,3 +192,16 @@ func TestSearchFiltersEncoding(t *testing.T) {
require.Equal(t, fs, fs2)
})
}
func TestSearchMatchType_String(t *testing.T) {
toPtr := func(v object.SearchMatchType) *object.SearchMatchType {
return &v
}
testEnumStrings(t, new(object.SearchMatchType), []enumStringItem{
{val: toPtr(object.MatchStringEqual), str: "STRING_EQUAL"},
{val: toPtr(object.MatchStringNotEqual), str: "STRING_NOT_EQUAL"},
{val: toPtr(object.MatchNotPresent), str: "NOT_PRESENT"},
{val: toPtr(object.MatchUnknown), str: "MATCH_TYPE_UNSPECIFIED"},
})
}

View file

@ -34,13 +34,36 @@ func TypeFromV2(t object.Type) Type {
}
}
// String returns string representation of Type.
//
// String mapping:
// * TypeTombstone: TOMBSTONE;
// * TypeStorageGroup: STORAGE_GROUP;
// * TypeRegular, default: REGULAR.
func (t Type) String() string {
return t.ToV2().String()
}
// TypeFromString parses Type from its string representation.
func TypeFromString(s string) Type {
return TypeFromV2(
object.TypeFromString(s),
)
// 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.Type
ok := g.FromString(s)
if ok {
*t = TypeFromV2(g)
}
return ok
}
// TypeFromString parses Type from its string representation.
//
// Deprecated: use FromString method.
func TypeFromString(s string) (t Type) {
t.FromString(s)
return
}

View file

@ -1,28 +1,29 @@
package object
package object_test
import (
"testing"
"github.com/nspcc-dev/neofs-api-go/v2/object"
"github.com/nspcc-dev/neofs-api-go/pkg/object"
v2object "github.com/nspcc-dev/neofs-api-go/v2/object"
"github.com/stretchr/testify/require"
)
func TestType_ToV2(t *testing.T) {
typs := []struct {
t Type
t2 object.Type
t object.Type
t2 v2object.Type
}{
{
t: TypeRegular,
t2: object.TypeRegular,
t: object.TypeRegular,
t2: v2object.TypeRegular,
},
{
t: TypeTombstone,
t2: object.TypeTombstone,
t: object.TypeTombstone,
t2: v2object.TypeTombstone,
},
{
t: TypeStorageGroup,
t2: object.TypeStorageGroup,
t: object.TypeStorageGroup,
t2: v2object.TypeStorageGroup,
},
}
@ -31,16 +32,48 @@ func TestType_ToV2(t *testing.T) {
require.Equal(t, item.t2, t2)
require.Equal(t, item.t, TypeFromV2(item.t2))
require.Equal(t, item.t, object.TypeFromV2(item.t2))
}
}
func TestType_String(t *testing.T) {
for _, typ := range []Type{
TypeRegular,
TypeTombstone,
TypeStorageGroup,
toPtr := func(v object.Type) *object.Type {
return &v
}
testEnumStrings(t, new(object.Type), []enumStringItem{
{val: toPtr(object.TypeTombstone), str: "TOMBSTONE"},
{val: toPtr(object.TypeStorageGroup), str: "STORAGE_GROUP"},
{val: toPtr(object.TypeRegular), str: "REGULAR"},
})
}
type enumIface interface {
FromString(string) bool
String() string
}
type enumStringItem struct {
val enumIface
str string
}
func testEnumStrings(t *testing.T, e enumIface, items []enumStringItem) {
for _, item := range items {
require.Equal(t, item.str, item.val.String())
s := item.val.String()
require.True(t, e.FromString(s), s)
require.EqualValues(t, item.val, e, item.val)
}
// incorrect strings
for _, str := range []string{
"some string",
"undefined",
} {
require.Equal(t, typ, TypeFromString(typ.String()))
require.False(t, e.FromString(str))
}
}