From 5d3fcd6f55341b35f8f73c9274eb3bcd1a5fac18 Mon Sep 17 00:00:00 2001 From: Evgenii Baidakov Date: Wed, 19 Apr 2023 14:33:35 +0400 Subject: [PATCH 1/2] enums: Remove FromString method of enumerations Signed-off-by: Evgenii Baidakov --- eacl/enums.go | 95 +++++++++++++++++++++++++++++++------------ eacl/enums_test.go | 12 +++--- eacl/record.go | 2 +- eacl/record_test.go | 6 +-- object/search.go | 21 +++++++--- object/search_test.go | 2 +- object/type.go | 19 ++++++--- object/type_test.go | 12 +++--- 8 files changed, 116 insertions(+), 53 deletions(-) diff --git a/eacl/enums.go b/eacl/enums.go index 17c9623..3465c2f 100644 --- a/eacl/enums.go +++ b/eacl/enums.go @@ -127,21 +127,30 @@ func ActionFromV2(action v2acl.Action) (a Action) { return a } -// String returns string representation of Action. +// EncodeToString returns string representation of Action. // // String mapping: // - ActionAllow: ALLOW; // - ActionDeny: DENY; // - ActionUnknown, default: ACTION_UNSPECIFIED. -func (a Action) String() string { +func (a Action) EncodeToString() string { return a.ToV2().String() } -// FromString parses Action from a string representation. -// It is a reverse action to String(). +// String implements fmt.Stringer. +// +// 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. -func (a *Action) FromString(s string) bool { +func (a *Action) DecodeString(s string) bool { var g v2acl.Action ok := g.FromString(s) @@ -199,7 +208,7 @@ func OperationFromV2(operation v2acl.Operation) (o Operation) { return o } -// String returns string representation of Operation. +// EncodeToString returns string representation of Operation. // // String mapping: // - OperationGet: GET; @@ -210,15 +219,24 @@ func OperationFromV2(operation v2acl.Operation) (o Operation) { // - OperationRange: GETRANGE; // - OperationRangeHash: GETRANGEHASH; // - OperationUnknown, default: OPERATION_UNSPECIFIED. -func (o Operation) String() string { +func (o Operation) EncodeToString() string { return o.ToV2().String() } -// FromString parses Operation from a string representation. -// It is a reverse action to String(). +// String implements fmt.Stringer. +// +// 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. -func (o *Operation) FromString(s string) bool { +func (o *Operation) DecodeString(s string) bool { var g v2acl.Operation ok := g.FromString(s) @@ -260,22 +278,31 @@ func RoleFromV2(role v2acl.Role) (r Role) { return r } -// String returns string representation of Role. +// EncodeToString returns string representation of Role. // // String mapping: // - RoleUser: USER; // - RoleSystem: SYSTEM; // - RoleOthers: OTHERS; // - RoleUnknown, default: ROLE_UNKNOWN. -func (r Role) String() string { +func (r Role) EncodeToString() string { return r.ToV2().String() } -// FromString parses Role from a string representation. -// It is a reverse action to String(). +// String implements fmt.Stringer. +// +// 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. -func (r *Role) FromString(s string) bool { +func (r *Role) DecodeString(s string) bool { var g v2acl.Role ok := g.FromString(s) @@ -313,21 +340,30 @@ func MatchFromV2(match v2acl.MatchType) (m Match) { return m } -// String returns string representation of Match. +// EncodeToString returns string representation of Match. // // String mapping: // - MatchStringEqual: STRING_EQUAL; // - MatchStringNotEqual: STRING_NOT_EQUAL; // - MatchUnknown, default: MATCH_TYPE_UNSPECIFIED. -func (m Match) String() string { +func (m Match) EncodeToString() string { return m.ToV2().String() } -// FromString parses Match from a string representation. -// It is a reverse action to String(). +// String implements fmt.Stringer. +// +// 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. -func (m *Match) FromString(s string) bool { +func (m *Match) DecodeString(s string) bool { var g v2acl.MatchType ok := g.FromString(s) @@ -369,21 +405,30 @@ func FilterHeaderTypeFromV2(header v2acl.HeaderType) (h FilterHeaderType) { return h } -// String returns string representation of FilterHeaderType. +// EncodeToString returns string representation of FilterHeaderType. // // String mapping: // - HeaderFromRequest: REQUEST; // - HeaderFromObject: OBJECT; // - HeaderTypeUnknown, default: HEADER_UNSPECIFIED. -func (h FilterHeaderType) String() string { +func (h FilterHeaderType) EncodeToString() string { return h.ToV2().String() } -// FromString parses FilterHeaderType from a string representation. -// It is a reverse action to String(). +// String implements fmt.Stringer. +// +// 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. -func (h *FilterHeaderType) FromString(s string) bool { +func (h *FilterHeaderType) DecodeString(s string) bool { var g v2acl.HeaderType ok := g.FromString(s) diff --git a/eacl/enums_test.go b/eacl/enums_test.go index 44e6488..23ea5ea 100644 --- a/eacl/enums_test.go +++ b/eacl/enums_test.go @@ -118,8 +118,8 @@ func TestFilterHeaderType(t *testing.T) { } type enumIface interface { - FromString(string) bool - String() string + DecodeString(string) bool + EncodeToString() string } type enumStringItem struct { @@ -129,11 +129,11 @@ type enumStringItem struct { func testEnumStrings(t *testing.T, e enumIface, items []enumStringItem) { 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) } @@ -143,7 +143,7 @@ func testEnumStrings(t *testing.T, e enumIface, items []enumStringItem) { "some string", "UNSPECIFIED", } { - require.False(t, e.FromString(str)) + require.False(t, e.DecodeString(str)) } } diff --git a/eacl/record.go b/eacl/record.go index 806c79d..3030dc1 100644 --- a/eacl/record.go +++ b/eacl/record.go @@ -146,7 +146,7 @@ func (r *Record) AddObjectPayloadHashFilter(m Match, h checksum.Checksum) { // AddObjectTypeFilter adds filter by 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. diff --git a/eacl/record_test.go b/eacl/record_test.go index 5b42b91..c6d38d1 100644 --- a/eacl/record_test.go +++ b/eacl/record_test.go @@ -211,7 +211,7 @@ func TestReservedRecords(t *testing.T) { }, { f: func(r *Record) { - require.True(t, typ.FromString("REGULAR")) + require.True(t, typ.DecodeString("REGULAR")) r.AddObjectTypeFilter(MatchStringEqual, *typ) }, key: v2acl.FilterObjectType, @@ -219,7 +219,7 @@ func TestReservedRecords(t *testing.T) { }, { f: func(r *Record) { - require.True(t, typ.FromString("TOMBSTONE")) + require.True(t, typ.DecodeString("TOMBSTONE")) r.AddObjectTypeFilter(MatchStringEqual, *typ) }, key: v2acl.FilterObjectType, @@ -227,7 +227,7 @@ func TestReservedRecords(t *testing.T) { }, { f: func(r *Record) { - require.True(t, typ.FromString("STORAGE_GROUP")) + require.True(t, typ.DecodeString("STORAGE_GROUP")) r.AddObjectTypeFilter(MatchStringEqual, *typ) }, key: v2acl.FilterObjectType, diff --git a/object/search.go b/object/search.go index b78e5e0..9d91714 100644 --- a/object/search.go +++ b/object/search.go @@ -57,7 +57,7 @@ func SearchMatchFromV2(t v2object.MatchType) (m SearchMatchType) { return m } -// String returns string representation of SearchMatchType. +// EncodeToString returns string representation of SearchMatchType. // // String mapping: // - MatchStringEqual: STRING_EQUAL; @@ -65,15 +65,24 @@ func SearchMatchFromV2(t v2object.MatchType) (m SearchMatchType) { // - MatchNotPresent: NOT_PRESENT; // - MatchCommonPrefix: COMMON_PREFIX; // - MatchUnknown, default: MATCH_TYPE_UNSPECIFIED. -func (m SearchMatchType) String() string { +func (m SearchMatchType) EncodeToString() string { return m.ToV2().String() } -// FromString parses SearchMatchType from a string representation. -// It is a reverse action to String(). +// String implements fmt.Stringer. +// +// 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. -func (m *SearchMatchType) FromString(s string) bool { +func (m *SearchMatchType) DecodeString(s string) bool { var g v2object.MatchType ok := g.FromString(s) @@ -282,7 +291,7 @@ func (f *SearchFilters) AddSplitIDFilter(m SearchMatchType, id *SplitID) { // AddTypeFilter adds filter by object 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. diff --git a/object/search_test.go b/object/search_test.go index 04fb5ac..b3d4fa4 100644 --- a/object/search_test.go +++ b/object/search_test.go @@ -176,7 +176,7 @@ func TestSearchFilters_AddTypeFilter(t *testing.T) { require.Len(t, fsV2, 1) 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()) }) } diff --git a/object/type.go b/object/type.go index f87d108..52b80c1 100644 --- a/object/type.go +++ b/object/type.go @@ -21,22 +21,31 @@ func TypeFromV2(t object.Type) Type { return Type(t) } -// String returns string representation of Type. +// EncodeToString returns string representation of Type. // // String mapping: // - TypeTombstone: TOMBSTONE; // - TypeStorageGroup: STORAGE_GROUP; // - TypeLock: LOCK; // - TypeRegular, default: REGULAR. -func (t Type) String() string { +func (t Type) EncodeToString() string { return t.ToV2().String() } -// FromString parses Type from a string representation. -// It is a reverse action to String(). +// String implements fmt.Stringer. +// +// 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. -func (t *Type) FromString(s string) bool { +func (t *Type) DecodeString(s string) bool { var g object.Type ok := g.FromString(s) diff --git a/object/type_test.go b/object/type_test.go index d95a41e..e83cef7 100644 --- a/object/type_test.go +++ b/object/type_test.go @@ -54,8 +54,8 @@ func TestType_String(t *testing.T) { } type enumIface interface { - FromString(string) bool - String() string + DecodeString(string) bool + EncodeToString() string } type enumStringItem struct { @@ -65,11 +65,11 @@ type enumStringItem struct { func testEnumStrings(t *testing.T, e enumIface, items []enumStringItem) { 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) } @@ -79,6 +79,6 @@ func testEnumStrings(t *testing.T, e enumIface, items []enumStringItem) { "some string", "undefined", } { - require.False(t, e.FromString(str)) + require.False(t, e.DecodeString(str)) } } From 6757c0a7065654dcbcabb2ab8cf41328fc9e6fc5 Mon Sep 17 00:00:00 2001 From: Evgenii Baidakov Date: Wed, 19 Apr 2023 14:33:52 +0400 Subject: [PATCH 2/2] linter: Fix fmt.Errorf format for type variable Signed-off-by: Evgenii Baidakov --- storagegroup/storagegroup.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/storagegroup/storagegroup.go b/storagegroup/storagegroup.go index 94bc2f7..aa36cb6 100644 --- a/storagegroup/storagegroup.go +++ b/storagegroup/storagegroup.go @@ -252,7 +252,7 @@ func (sg *StorageGroup) UnmarshalJSON(data []byte) error { // error if object is not of TypeStorageGroup type. func ReadFromObject(sg *StorageGroup, o objectSDK.Object) error { 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())