From ee5ff40b1731e03d82bfd53937bb2706e290dbbf Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 30 Jul 2020 13:20:36 +0300 Subject: [PATCH] stackitem: allow to convert type from string Signed-off-by: Evgenii Stratonikov --- pkg/vm/stackitem/type.go | 30 ++++++++++++++++++++++++++++++ pkg/vm/stackitem/type_test.go | 16 ++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 pkg/vm/stackitem/type_test.go diff --git a/pkg/vm/stackitem/type.go b/pkg/vm/stackitem/type.go index 1c12ec35f..ea69fc48b 100644 --- a/pkg/vm/stackitem/type.go +++ b/pkg/vm/stackitem/type.go @@ -1,5 +1,7 @@ package stackitem +import "errors" + // Type represents type of the stack item. type Type byte @@ -54,3 +56,31 @@ func (t Type) IsValid() bool { return false } } + +// FromString returns stackitem type from string. +func FromString(s string) (Type, error) { + switch s { + case "Any": + return AnyT, nil + case "Pointer": + return PointerT, nil + case "Boolean": + return BooleanT, nil + case "Integer": + return IntegerT, nil + case "ByteString": + return ByteArrayT, nil + case "Buffer": + return BufferT, nil + case "Array": + return ArrayT, nil + case "Struct": + return StructT, nil + case "Map": + return MapT, nil + case "Interop": + return InteropT, nil + default: + return 0xFF, errors.New("invalid type") + } +} diff --git a/pkg/vm/stackitem/type_test.go b/pkg/vm/stackitem/type_test.go new file mode 100644 index 000000000..dd550fc7d --- /dev/null +++ b/pkg/vm/stackitem/type_test.go @@ -0,0 +1,16 @@ +package stackitem + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestFromString(t *testing.T) { + typs := []Type{AnyT, PointerT, BooleanT, IntegerT, ByteArrayT, BufferT, ArrayT, StructT, MapT, InteropT} + for _, typ := range typs { + actual, err := FromString(typ.String()) + require.NoError(t, err) + require.Equal(t, typ, actual) + } +}