aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTing-Wei Lan <tingwei.lan@cobinhood.com>2019-03-27 11:01:46 +0800
committerlantw44 <lantw44@gmail.com>2019-03-29 18:40:42 +0800
commit55f8e6a9c909256d1e363d787e2df96abc7ffa44 (patch)
treeb1f80b86f03414a3bb9ff447449bbbd6713f95f6
parentb7896e6fca1dac7c4d206abeb229049b3403aaba (diff)
downloaddexon-55f8e6a9c909256d1e363d787e2df96abc7ffa44.tar.gz
dexon-55f8e6a9c909256d1e363d787e2df96abc7ffa44.tar.zst
dexon-55f8e6a9c909256d1e363d787e2df96abc7ffa44.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.go37
-rw-r--r--core/vm/sqlvm/ast/types_test.go10
-rw-r--r--core/vm/sqlvm/errors/errors.go2
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",