From 53e275676275a717404893a1525907ba8f54e698 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 17 Dec 2020 18:46:19 +0300 Subject: [PATCH] [#231] object: Implement string encode/decode methods on Type Signed-off-by: Leonard Lyubich --- pkg/object/type.go | 11 +++++++++++ pkg/object/type_test.go | 10 ++++++++++ v2/object/string.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) create mode 100644 v2/object/string.go diff --git a/pkg/object/type.go b/pkg/object/type.go index afcbdf28..fde1aaa0 100644 --- a/pkg/object/type.go +++ b/pkg/object/type.go @@ -33,3 +33,14 @@ func TypeFromV2(t object.Type) Type { return TypeRegular } } + +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), + ) +} diff --git a/pkg/object/type_test.go b/pkg/object/type_test.go index 82f8bf22..7ade2ef4 100644 --- a/pkg/object/type_test.go +++ b/pkg/object/type_test.go @@ -34,3 +34,13 @@ func TestType_ToV2(t *testing.T) { require.Equal(t, item.t, TypeFromV2(item.t2)) } } + +func TestType_String(t *testing.T) { + for _, typ := range []Type{ + TypeRegular, + TypeTombstone, + TypeStorageGroup, + } { + require.Equal(t, typ, TypeFromString(typ.String())) + } +} diff --git a/v2/object/string.go b/v2/object/string.go new file mode 100644 index 00000000..ad82a124 --- /dev/null +++ b/v2/object/string.go @@ -0,0 +1,29 @@ +package object + +const ( + typeRegularString = "Regular" + typeTombstoneString = "Tombstone" + typeStorageGroupString = "StorageGroup" +) + +func (t Type) String() string { + switch t { + default: + return typeRegularString + case TypeTombstone: + return typeTombstoneString + case TypeStorageGroup: + return typeStorageGroupString + } +} + +func TypeFromString(s string) Type { + switch s { + default: + return TypeRegular + case typeTombstoneString: + return TypeTombstone + case typeStorageGroupString: + return TypeStorageGroup + } +}