diff options
author | Ting-Wei Lan <tingwei.lan@cobinhood.com> | 2019-03-27 11:01:46 +0800 |
---|---|---|
committer | Jhih-Ming Huang <jm.huang@cobinhood.com> | 2019-05-06 10:44:04 +0800 |
commit | 997eb4d86724d7bc6c4b2784e6da54801d65cd7f (patch) | |
tree | 6452d6914d633a0376459667e24cf582b34d32dc | |
parent | e3902490aaeafce0a128d4106e1bda317f4c0988 (diff) | |
download | dexon-997eb4d86724d7bc6c4b2784e6da54801d65cd7f.tar.gz dexon-997eb4d86724d7bc6c4b2784e6da54801d65cd7f.tar.zst dexon-997eb4d86724d7bc6c4b2784e6da54801d65cd7f.zip |
core: vm: sqlvm: ast: make DataTypeDecode a method of DataType
Since we have dropped DataTypeEncode in favor of GetType method of
TypeNode, rename DataTypeDecode to GetNode and make it a method.
Drop unnecessary zero checks because we don't care minor bytes when
there is only one type belong to a category.
-rw-r--r-- | core/vm/sqlvm/ast/types.go | 37 | ||||
-rw-r--r-- | core/vm/sqlvm/ast/types_test.go | 10 | ||||
-rw-r--r-- | core/vm/sqlvm/errors/errors.go | 2 |
3 files changed, 19 insertions, 30 deletions
diff --git a/core/vm/sqlvm/ast/types.go b/core/vm/sqlvm/ast/types.go index 5151334a9..d75d29294 100644 --- a/core/vm/sqlvm/ast/types.go +++ b/core/vm/sqlvm/ast/types.go @@ -101,60 +101,55 @@ func (dt DataType) Size() uint8 { } } -// DataTypeDecode decodes DataType into data type node. -func DataTypeDecode(t DataType) (TypeNode, error) { - major, minor := DecomposeDataType(t) +// GetNode constructs an AST node from a data type. +func (dt DataType) GetNode() TypeNode { + major, minor := DecomposeDataType(dt) switch major { - // TODO(wmin0): define unsupported error for special type. case DataTypeMajorBool: - if minor == 0 { - return &BoolTypeNode{}, nil - } + return &BoolTypeNode{} case DataTypeMajorAddress: - if minor == 0 { - return &AddressTypeNode{}, nil - } + return &AddressTypeNode{} case DataTypeMajorInt: if minor <= 0x1f { size := (uint32(minor) + 1) * 8 - return &IntTypeNode{Unsigned: false, Size: size}, nil + return &IntTypeNode{Unsigned: false, Size: size} } case DataTypeMajorUint: if minor <= 0x1f { size := (uint32(minor) + 1) * 8 - return &IntTypeNode{Unsigned: true, Size: size}, nil + return &IntTypeNode{Unsigned: true, Size: size} } case DataTypeMajorFixedBytes: if minor <= 0x1f { size := uint32(minor) + 1 - return &FixedBytesTypeNode{Size: size}, nil + return &FixedBytesTypeNode{Size: size} } case DataTypeMajorDynamicBytes: - if minor == 0 { - return &DynamicBytesTypeNode{}, nil - } + return &DynamicBytesTypeNode{} } switch { case major.IsFixedRange(): if minor <= 80 { size := (uint32(major-DataTypeMajorFixed) + 1) * 8 + fractionalDigits := uint32(minor) return &FixedTypeNode{ Unsigned: false, Size: size, - FractionalDigits: uint32(minor), - }, nil + FractionalDigits: fractionalDigits, + } } case major.IsUfixedRange(): if minor <= 80 { size := (uint32(major-DataTypeMajorUfixed) + 1) * 8 + fractionalDigits := uint32(minor) return &FixedTypeNode{ Unsigned: true, Size: size, - FractionalDigits: uint32(minor), - }, nil + FractionalDigits: fractionalDigits, + } } } - return nil, se.ErrorCodeDataTypeDecode + return nil } func decimalToBig(d decimal.Decimal) (b *big.Int) { diff --git a/core/vm/sqlvm/ast/types_test.go b/core/vm/sqlvm/ast/types_test.go index 2107cc206..3d0ecb395 100644 --- a/core/vm/sqlvm/ast/types_test.go +++ b/core/vm/sqlvm/ast/types_test.go @@ -29,8 +29,7 @@ func (s *TypesTestSuite) requireEncodeAndDecodeDataTypeNoError( s.Require().Zero(code) s.Require().Empty(message) s.Require().Equal(d, encode) - decode, err := DataTypeDecode(d) - s.Require().NoError(err) + decode := d.GetNode() s.Require().Equal(t, decode) } @@ -41,8 +40,8 @@ func (s *TypesTestSuite) requireEncodeDataTypeError(input TypeNode) { } func (s *TypesTestSuite) requireDecodeDataTypeError(input DataType) { - _, err := DataTypeDecode(input) - s.Require().Error(err) + decode := input.GetNode() + s.Require().Nil(decode) } func (s *TypesTestSuite) TestEncodeAndDecodeDataType() { @@ -84,12 +83,9 @@ func (s *TypesTestSuite) TestEncodeDataTypeError() { func (s *TypesTestSuite) TestDecodeDataTypeError() { s.requireDecodeDataTypeError(DataTypeUnknown) - s.requireDecodeDataTypeError(ComposeDataType(DataTypeMajorBool, 1)) - s.requireDecodeDataTypeError(ComposeDataType(DataTypeMajorAddress, 1)) s.requireDecodeDataTypeError(ComposeDataType(DataTypeMajorInt, 0x20)) s.requireDecodeDataTypeError(ComposeDataType(DataTypeMajorUint, 0x20)) s.requireDecodeDataTypeError(ComposeDataType(DataTypeMajorFixedBytes, 0x20)) - s.requireDecodeDataTypeError(ComposeDataType(DataTypeMajorDynamicBytes, 1)) s.requireDecodeDataTypeError(ComposeDataType(DataTypeMajorFixed, 81)) s.requireDecodeDataTypeError(ComposeDataType(DataTypeMajorUfixed, 81)) s.requireDecodeDataTypeError(ComposeDataType(DataTypeMajorUfixed+0x20, 80)) diff --git a/core/vm/sqlvm/errors/errors.go b/core/vm/sqlvm/errors/errors.go index 41faae93e..40d4e0a8b 100644 --- a/core/vm/sqlvm/errors/errors.go +++ b/core/vm/sqlvm/errors/errors.go @@ -108,7 +108,6 @@ const ( ErrorCodeInvalidUfixedSize ErrorCodeInvalidFixedFractionalDigits ErrorCodeInvalidUfixedFractionalDigits - ErrorCodeDataTypeDecode ErrorCodeDecimalEncode ErrorCodeDecimalDecode ErrorCodeGetMinMax @@ -141,7 +140,6 @@ var errorCodeMap = [...]string{ ErrorCodeInvalidUfixedSize: "invalid ufixed size", ErrorCodeInvalidFixedFractionalDigits: "invalid fixed fractional digits", ErrorCodeInvalidUfixedFractionalDigits: "invalid ufixed fractional digits", - ErrorCodeDataTypeDecode: "data type decode failed", ErrorCodeDecimalEncode: "decimal encode failed", ErrorCodeDecimalDecode: "decimal decode failed", ErrorCodeGetMinMax: "get (min, max) failed", |