stackitem: allow to convert type from string
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
parent
c16bb466a0
commit
ee5ff40b17
2 changed files with 46 additions and 0 deletions
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
|
16
pkg/vm/stackitem/type_test.go
Normal file
16
pkg/vm/stackitem/type_test.go
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue