Remove FromString method of enumerations (#393)

close #98
This commit is contained in:
Roman Khimov 2023-04-19 21:09:43 +03:00 committed by GitHub
commit 0bc98456e3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 117 additions and 54 deletions

View file

@ -127,21 +127,30 @@ func ActionFromV2(action v2acl.Action) (a Action) {
return a return a
} }
// String returns string representation of Action. // EncodeToString returns string representation of Action.
// //
// String mapping: // String mapping:
// - ActionAllow: ALLOW; // - ActionAllow: ALLOW;
// - ActionDeny: DENY; // - ActionDeny: DENY;
// - ActionUnknown, default: ACTION_UNSPECIFIED. // - ActionUnknown, default: ACTION_UNSPECIFIED.
func (a Action) String() string { func (a Action) EncodeToString() string {
return a.ToV2().String() return a.ToV2().String()
} }
// FromString parses Action from a string representation. // String implements fmt.Stringer.
// It is a reverse action to String(). //
// String is designed to be human-readable, and its format MAY differ between
// SDK versions. String MAY return same result as EncodeToString. String MUST NOT
// be used to encode ID into NeoFS protocol string.
func (a Action) String() string {
return a.EncodeToString()
}
// DecodeString parses Action from a string representation.
// It is a reverse action to EncodeToString().
// //
// Returns true if s was parsed successfully. // Returns true if s was parsed successfully.
func (a *Action) FromString(s string) bool { func (a *Action) DecodeString(s string) bool {
var g v2acl.Action var g v2acl.Action
ok := g.FromString(s) ok := g.FromString(s)
@ -199,7 +208,7 @@ func OperationFromV2(operation v2acl.Operation) (o Operation) {
return o return o
} }
// String returns string representation of Operation. // EncodeToString returns string representation of Operation.
// //
// String mapping: // String mapping:
// - OperationGet: GET; // - OperationGet: GET;
@ -210,15 +219,24 @@ func OperationFromV2(operation v2acl.Operation) (o Operation) {
// - OperationRange: GETRANGE; // - OperationRange: GETRANGE;
// - OperationRangeHash: GETRANGEHASH; // - OperationRangeHash: GETRANGEHASH;
// - OperationUnknown, default: OPERATION_UNSPECIFIED. // - OperationUnknown, default: OPERATION_UNSPECIFIED.
func (o Operation) String() string { func (o Operation) EncodeToString() string {
return o.ToV2().String() return o.ToV2().String()
} }
// FromString parses Operation from a string representation. // String implements fmt.Stringer.
// It is a reverse action to String(). //
// String is designed to be human-readable, and its format MAY differ between
// SDK versions. String MAY return same result as EncodeToString. String MUST NOT
// be used to encode ID into NeoFS protocol string.
func (o Operation) String() string {
return o.EncodeToString()
}
// DecodeString parses Operation from a string representation.
// It is a reverse action to EncodeToString().
// //
// Returns true if s was parsed successfully. // Returns true if s was parsed successfully.
func (o *Operation) FromString(s string) bool { func (o *Operation) DecodeString(s string) bool {
var g v2acl.Operation var g v2acl.Operation
ok := g.FromString(s) ok := g.FromString(s)
@ -260,22 +278,31 @@ func RoleFromV2(role v2acl.Role) (r Role) {
return r return r
} }
// String returns string representation of Role. // EncodeToString returns string representation of Role.
// //
// String mapping: // String mapping:
// - RoleUser: USER; // - RoleUser: USER;
// - RoleSystem: SYSTEM; // - RoleSystem: SYSTEM;
// - RoleOthers: OTHERS; // - RoleOthers: OTHERS;
// - RoleUnknown, default: ROLE_UNKNOWN. // - RoleUnknown, default: ROLE_UNKNOWN.
func (r Role) String() string { func (r Role) EncodeToString() string {
return r.ToV2().String() return r.ToV2().String()
} }
// FromString parses Role from a string representation. // String implements fmt.Stringer.
// It is a reverse action to String(). //
// String is designed to be human-readable, and its format MAY differ between
// SDK versions. String MAY return same result as EncodeToString. String MUST NOT
// be used to encode ID into NeoFS protocol string.
func (r Role) String() string {
return r.EncodeToString()
}
// DecodeString parses Role from a string representation.
// It is a reverse action to EncodeToString().
// //
// Returns true if s was parsed successfully. // Returns true if s was parsed successfully.
func (r *Role) FromString(s string) bool { func (r *Role) DecodeString(s string) bool {
var g v2acl.Role var g v2acl.Role
ok := g.FromString(s) ok := g.FromString(s)
@ -313,21 +340,30 @@ func MatchFromV2(match v2acl.MatchType) (m Match) {
return m return m
} }
// String returns string representation of Match. // EncodeToString returns string representation of Match.
// //
// String mapping: // String mapping:
// - MatchStringEqual: STRING_EQUAL; // - MatchStringEqual: STRING_EQUAL;
// - MatchStringNotEqual: STRING_NOT_EQUAL; // - MatchStringNotEqual: STRING_NOT_EQUAL;
// - MatchUnknown, default: MATCH_TYPE_UNSPECIFIED. // - MatchUnknown, default: MATCH_TYPE_UNSPECIFIED.
func (m Match) String() string { func (m Match) EncodeToString() string {
return m.ToV2().String() return m.ToV2().String()
} }
// FromString parses Match from a string representation. // String implements fmt.Stringer.
// It is a reverse action to String(). //
// String is designed to be human-readable, and its format MAY differ between
// SDK versions. String MAY return same result as EncodeToString. String MUST NOT
// be used to encode ID into NeoFS protocol string.
func (m Match) String() string {
return m.EncodeToString()
}
// DecodeString parses Match from a string representation.
// It is a reverse action to EncodeToString().
// //
// Returns true if s was parsed successfully. // Returns true if s was parsed successfully.
func (m *Match) FromString(s string) bool { func (m *Match) DecodeString(s string) bool {
var g v2acl.MatchType var g v2acl.MatchType
ok := g.FromString(s) ok := g.FromString(s)
@ -369,21 +405,30 @@ func FilterHeaderTypeFromV2(header v2acl.HeaderType) (h FilterHeaderType) {
return h return h
} }
// String returns string representation of FilterHeaderType. // EncodeToString returns string representation of FilterHeaderType.
// //
// String mapping: // String mapping:
// - HeaderFromRequest: REQUEST; // - HeaderFromRequest: REQUEST;
// - HeaderFromObject: OBJECT; // - HeaderFromObject: OBJECT;
// - HeaderTypeUnknown, default: HEADER_UNSPECIFIED. // - HeaderTypeUnknown, default: HEADER_UNSPECIFIED.
func (h FilterHeaderType) String() string { func (h FilterHeaderType) EncodeToString() string {
return h.ToV2().String() return h.ToV2().String()
} }
// FromString parses FilterHeaderType from a string representation. // String implements fmt.Stringer.
// It is a reverse action to String(). //
// String is designed to be human-readable, and its format MAY differ between
// SDK versions. String MAY return same result as EncodeToString. String MUST NOT
// be used to encode ID into NeoFS protocol string.
func (h FilterHeaderType) String() string {
return h.EncodeToString()
}
// DecodeString parses FilterHeaderType from a string representation.
// It is a reverse action to EncodeToString().
// //
// Returns true if s was parsed successfully. // Returns true if s was parsed successfully.
func (h *FilterHeaderType) FromString(s string) bool { func (h *FilterHeaderType) DecodeString(s string) bool {
var g v2acl.HeaderType var g v2acl.HeaderType
ok := g.FromString(s) ok := g.FromString(s)

View file

@ -118,8 +118,8 @@ func TestFilterHeaderType(t *testing.T) {
} }
type enumIface interface { type enumIface interface {
FromString(string) bool DecodeString(string) bool
String() string EncodeToString() string
} }
type enumStringItem struct { type enumStringItem struct {
@ -129,11 +129,11 @@ type enumStringItem struct {
func testEnumStrings(t *testing.T, e enumIface, items []enumStringItem) { func testEnumStrings(t *testing.T, e enumIface, items []enumStringItem) {
for _, item := range items { for _, item := range items {
require.Equal(t, item.str, item.val.String()) require.Equal(t, item.str, item.val.EncodeToString())
s := item.val.String() s := item.val.EncodeToString()
require.True(t, e.FromString(s), s) require.True(t, e.DecodeString(s), s)
require.EqualValues(t, item.val, e, item.val) require.EqualValues(t, item.val, e, item.val)
} }
@ -143,7 +143,7 @@ func testEnumStrings(t *testing.T, e enumIface, items []enumStringItem) {
"some string", "some string",
"UNSPECIFIED", "UNSPECIFIED",
} { } {
require.False(t, e.FromString(str)) require.False(t, e.DecodeString(str))
} }
} }

View file

@ -146,7 +146,7 @@ func (r *Record) AddObjectPayloadHashFilter(m Match, h checksum.Checksum) {
// AddObjectTypeFilter adds filter by object type. // AddObjectTypeFilter adds filter by object type.
func (r *Record) AddObjectTypeFilter(m Match, t object.Type) { func (r *Record) AddObjectTypeFilter(m Match, t object.Type) {
r.addObjectReservedFilter(m, fKeyObjType, staticStringer(t.String())) r.addObjectReservedFilter(m, fKeyObjType, staticStringer(t.EncodeToString()))
} }
// AddObjectHomomorphicHashFilter adds filter by object payload homomorphic hash value. // AddObjectHomomorphicHashFilter adds filter by object payload homomorphic hash value.

View file

@ -211,7 +211,7 @@ func TestReservedRecords(t *testing.T) {
}, },
{ {
f: func(r *Record) { f: func(r *Record) {
require.True(t, typ.FromString("REGULAR")) require.True(t, typ.DecodeString("REGULAR"))
r.AddObjectTypeFilter(MatchStringEqual, *typ) r.AddObjectTypeFilter(MatchStringEqual, *typ)
}, },
key: v2acl.FilterObjectType, key: v2acl.FilterObjectType,
@ -219,7 +219,7 @@ func TestReservedRecords(t *testing.T) {
}, },
{ {
f: func(r *Record) { f: func(r *Record) {
require.True(t, typ.FromString("TOMBSTONE")) require.True(t, typ.DecodeString("TOMBSTONE"))
r.AddObjectTypeFilter(MatchStringEqual, *typ) r.AddObjectTypeFilter(MatchStringEqual, *typ)
}, },
key: v2acl.FilterObjectType, key: v2acl.FilterObjectType,
@ -227,7 +227,7 @@ func TestReservedRecords(t *testing.T) {
}, },
{ {
f: func(r *Record) { f: func(r *Record) {
require.True(t, typ.FromString("STORAGE_GROUP")) require.True(t, typ.DecodeString("STORAGE_GROUP"))
r.AddObjectTypeFilter(MatchStringEqual, *typ) r.AddObjectTypeFilter(MatchStringEqual, *typ)
}, },
key: v2acl.FilterObjectType, key: v2acl.FilterObjectType,

View file

@ -57,7 +57,7 @@ func SearchMatchFromV2(t v2object.MatchType) (m SearchMatchType) {
return m return m
} }
// String returns string representation of SearchMatchType. // EncodeToString returns string representation of SearchMatchType.
// //
// String mapping: // String mapping:
// - MatchStringEqual: STRING_EQUAL; // - MatchStringEqual: STRING_EQUAL;
@ -65,15 +65,24 @@ func SearchMatchFromV2(t v2object.MatchType) (m SearchMatchType) {
// - MatchNotPresent: NOT_PRESENT; // - MatchNotPresent: NOT_PRESENT;
// - MatchCommonPrefix: COMMON_PREFIX; // - MatchCommonPrefix: COMMON_PREFIX;
// - MatchUnknown, default: MATCH_TYPE_UNSPECIFIED. // - MatchUnknown, default: MATCH_TYPE_UNSPECIFIED.
func (m SearchMatchType) String() string { func (m SearchMatchType) EncodeToString() string {
return m.ToV2().String() return m.ToV2().String()
} }
// FromString parses SearchMatchType from a string representation. // String implements fmt.Stringer.
// It is a reverse action to String(). //
// String is designed to be human-readable, and its format MAY differ between
// SDK versions. String MAY return same result as EncodeToString. String MUST NOT
// be used to encode ID into NeoFS protocol string.
func (m SearchMatchType) String() string {
return m.EncodeToString()
}
// DecodeString parses SearchMatchType from a string representation.
// It is a reverse action to EncodeToString().
// //
// Returns true if s was parsed successfully. // Returns true if s was parsed successfully.
func (m *SearchMatchType) FromString(s string) bool { func (m *SearchMatchType) DecodeString(s string) bool {
var g v2object.MatchType var g v2object.MatchType
ok := g.FromString(s) ok := g.FromString(s)
@ -282,7 +291,7 @@ func (f *SearchFilters) AddSplitIDFilter(m SearchMatchType, id *SplitID) {
// AddTypeFilter adds filter by object type. // AddTypeFilter adds filter by object type.
func (f *SearchFilters) AddTypeFilter(m SearchMatchType, typ Type) { func (f *SearchFilters) AddTypeFilter(m SearchMatchType, typ Type) {
f.addReservedFilter(m, fKeyType, staticStringer(typ.String())) f.addReservedFilter(m, fKeyType, staticStringer(typ.EncodeToString()))
} }
// MarshalJSON encodes SearchFilters to protobuf JSON format. // MarshalJSON encodes SearchFilters to protobuf JSON format.

View file

@ -176,7 +176,7 @@ func TestSearchFilters_AddTypeFilter(t *testing.T) {
require.Len(t, fsV2, 1) require.Len(t, fsV2, 1)
require.Equal(t, v2object.FilterHeaderObjectType, fsV2[0].GetKey()) require.Equal(t, v2object.FilterHeaderObjectType, fsV2[0].GetKey())
require.Equal(t, typ.String(), fsV2[0].GetValue()) require.Equal(t, typ.EncodeToString(), fsV2[0].GetValue())
require.Equal(t, v2object.MatchStringEqual, fsV2[0].GetMatchType()) require.Equal(t, v2object.MatchStringEqual, fsV2[0].GetMatchType())
}) })
} }

View file

@ -21,22 +21,31 @@ func TypeFromV2(t object.Type) Type {
return Type(t) return Type(t)
} }
// String returns string representation of Type. // EncodeToString returns string representation of Type.
// //
// String mapping: // String mapping:
// - TypeTombstone: TOMBSTONE; // - TypeTombstone: TOMBSTONE;
// - TypeStorageGroup: STORAGE_GROUP; // - TypeStorageGroup: STORAGE_GROUP;
// - TypeLock: LOCK; // - TypeLock: LOCK;
// - TypeRegular, default: REGULAR. // - TypeRegular, default: REGULAR.
func (t Type) String() string { func (t Type) EncodeToString() string {
return t.ToV2().String() return t.ToV2().String()
} }
// FromString parses Type from a string representation. // String implements fmt.Stringer.
// It is a reverse action to String(). //
// String is designed to be human-readable, and its format MAY differ between
// SDK versions. String MAY return same result as EncodeToString. String MUST NOT
// be used to encode ID into NeoFS protocol string.
func (t Type) String() string {
return t.EncodeToString()
}
// DecodeString parses Type from a string representation.
// It is a reverse action to EncodeToString().
// //
// Returns true if s was parsed successfully. // Returns true if s was parsed successfully.
func (t *Type) FromString(s string) bool { func (t *Type) DecodeString(s string) bool {
var g object.Type var g object.Type
ok := g.FromString(s) ok := g.FromString(s)

View file

@ -54,8 +54,8 @@ func TestType_String(t *testing.T) {
} }
type enumIface interface { type enumIface interface {
FromString(string) bool DecodeString(string) bool
String() string EncodeToString() string
} }
type enumStringItem struct { type enumStringItem struct {
@ -65,11 +65,11 @@ type enumStringItem struct {
func testEnumStrings(t *testing.T, e enumIface, items []enumStringItem) { func testEnumStrings(t *testing.T, e enumIface, items []enumStringItem) {
for _, item := range items { for _, item := range items {
require.Equal(t, item.str, item.val.String()) require.Equal(t, item.str, item.val.EncodeToString())
s := item.val.String() s := item.val.EncodeToString()
require.True(t, e.FromString(s), s) require.True(t, e.DecodeString(s), s)
require.EqualValues(t, item.val, e, item.val) require.EqualValues(t, item.val, e, item.val)
} }
@ -79,6 +79,6 @@ func testEnumStrings(t *testing.T, e enumIface, items []enumStringItem) {
"some string", "some string",
"undefined", "undefined",
} { } {
require.False(t, e.FromString(str)) require.False(t, e.DecodeString(str))
} }
} }

View file

@ -252,7 +252,7 @@ func (sg *StorageGroup) UnmarshalJSON(data []byte) error {
// error if object is not of TypeStorageGroup type. // error if object is not of TypeStorageGroup type.
func ReadFromObject(sg *StorageGroup, o objectSDK.Object) error { func ReadFromObject(sg *StorageGroup, o objectSDK.Object) error {
if typ := o.Type(); typ != objectSDK.TypeStorageGroup { if typ := o.Type(); typ != objectSDK.TypeStorageGroup {
return fmt.Errorf("object is not of StorageGroup type: %s", typ) return fmt.Errorf("object is not of StorageGroup type: %v", typ)
} }
err := sg.Unmarshal(o.Payload()) err := sg.Unmarshal(o.Payload())