Skip to content

Commit

Permalink
feat:(ast) support cast null to empty value (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
AsterDY authored and liuq19 committed Aug 29, 2022
1 parent 9a3d373 commit b6220e9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
13 changes: 8 additions & 5 deletions ast/node.go
Expand Up @@ -177,6 +177,7 @@ func (self *Node) Bool() (bool, error) {
switch self.t {
case types.V_TRUE : return true , nil
case types.V_FALSE : return false, nil
case types.V_NULL : return false, nil
case _V_ANY :
if v, ok := self.packAny().(bool); ok {
return v, nil
Expand All @@ -197,6 +198,7 @@ func (self *Node) Int64() (int64, error) {
case _V_NUMBER, types.V_STRING : return numberToInt64(self)
case types.V_TRUE : return 1, nil
case types.V_FALSE : return 0, nil
case types.V_NULL : return 0, nil
case _V_ANY :
any := self.packAny()
switch v := any.(type) {
Expand Down Expand Up @@ -260,7 +262,8 @@ func (self *Node) Number() (json.Number, error) {
}
case types.V_TRUE : return json.Number("1"), nil
case types.V_FALSE : return json.Number("0"), nil
case _V_ANY :
case types.V_NULL : return json.Number("0"), nil
case _V_ANY :
if v, ok := self.packAny().(json.Number); ok {
return v, nil
} else {
Expand Down Expand Up @@ -289,7 +292,7 @@ func (self *Node) StrictNumber() (json.Number, error) {

// String returns raw string value if node type is V_STRING.
// Or return the string representation of other types:
// V_NULL => "null",
// V_NULL => "",
// V_TRUE => "true",
// V_FALSE => "false",
// V_NUMBER => "[0-9\.]*"
Expand All @@ -300,7 +303,7 @@ func (self *Node) String() (string, error) {
}
switch self.t {
case _V_NUMBER : return toNumber(self).String(), nil
case types.V_NULL : return "null" , nil
case types.V_NULL : return "" , nil
case types.V_TRUE : return "true" , nil
case types.V_FALSE : return "false", nil
case types.V_STRING : return addr2str(self.p, self.v), nil
Expand All @@ -321,7 +324,6 @@ func (self *Node) StrictString() (string, error) {
return "", err
}
switch self.t {
case types.V_NULL : return "", nil
case types.V_STRING : return addr2str(self.p, self.v), nil
case _V_ANY :
if v, ok := self.packAny().(string); ok {
Expand All @@ -342,7 +344,8 @@ func (self *Node) Float64() (float64, error) {
case _V_NUMBER, types.V_STRING : return numberToFloat64(self)
case types.V_TRUE : return 1.0, nil
case types.V_FALSE : return 0.0, nil
case _V_ANY :
case types.V_NULL : return 0.0, nil
case _V_ANY :
any := self.packAny()
switch v := any.(type) {
case float32 : return float64(v), nil
Expand Down
11 changes: 9 additions & 2 deletions ast/node_test.go
Expand Up @@ -282,6 +282,7 @@ func TestTypeCast(t *testing.T) {
{"Bool", NewAny(false), false, nil},
{"Bool", NewRaw("true"), true, nil},
{"Bool", NewRaw("false"), false, nil},
{"Bool", NewRaw("null"), false, nil},
{"Int64", NewRaw("true"), int64(1), nil},
{"Int64", NewRaw("false"), int64(0), nil},
{"Int64", NewRaw("\"1\""), int64(1), nil},
Expand All @@ -297,6 +298,7 @@ func TestTypeCast(t *testing.T) {
{"Int64", NewAny(uint64(0)), int64(0), nil},
{"Int64", Node{}, int64(0), ErrUnsupportType},
{"Int64", NewRaw("0"), int64(0), nil},
{"Int64", NewRaw("null"), int64(0), nil},
{"StrictInt64", NewRaw("true"), int64(0), ErrUnsupportType},
{"StrictInt64", NewRaw("false"), int64(0), ErrUnsupportType},
{"StrictInt64", NewAny(int(0)), int64(0), nil},
Expand All @@ -310,6 +312,7 @@ func TestTypeCast(t *testing.T) {
{"StrictInt64", NewAny(uint64(0)), int64(0), nil},
{"StrictInt64", Node{}, int64(0), ErrUnsupportType},
{"StrictInt64", NewRaw("0"), int64(0), nil},
{"StrictInt64", NewRaw("null"), int64(0), ErrUnsupportType},
{"Float64", NewRaw("true"), float64(1), nil},
{"Float64", NewRaw("false"), float64(0), nil},
{"Float64", NewRaw("\"1.0\""), float64(1.0), nil},
Expand All @@ -318,12 +321,14 @@ func TestTypeCast(t *testing.T) {
{"Float64", NewAny(float32(0)), float64(0), nil},
{"Float64", NewAny(float64(0)), float64(0), nil},
{"Float64", NewRaw("0.0"), float64(0.0), nil},
{"Float64", NewRaw("null"), float64(0.0), nil},
{"StrictFloat64", NewRaw("true"), float64(0), ErrUnsupportType},
{"StrictFloat64", NewRaw("false"), float64(0), ErrUnsupportType},
{"StrictFloat64", Node{}, float64(0), ErrUnsupportType},
{"StrictFloat64", NewAny(float32(0)), float64(0), nil},
{"StrictFloat64", NewAny(float64(0)), float64(0), nil},
{"StrictFloat64", NewRaw("0.0"), float64(0.0), nil},
{"StrictFloat64", NewRaw("null"), float64(0.0), ErrUnsupportType},
{"Number", Node{}, json.Number(""), ErrUnsupportType},
{"Number", NewAny(json.Number("0")), json.Number("0"), nil},
{"Number", NewRaw("0.0"), json.Number("0.0"), nil},
Expand All @@ -332,27 +337,29 @@ func TestTypeCast(t *testing.T) {
{"Number", NewRaw("\"0.x0\""), json.Number(""), nonEmptyErr},
{"Number", NewRaw("true"), json.Number("1"), nil},
{"Number", NewRaw("false"), json.Number("0"), nil},
{"Number", NewRaw("null"), json.Number("0"), nil},
{"StrictNumber", NewRaw("true"), json.Number(""), ErrUnsupportType},
{"StrictNumber", NewRaw("false"), json.Number(""), ErrUnsupportType},
{"StrictNumber", Node{}, json.Number(""), ErrUnsupportType},
{"StrictNumber", NewAny(json.Number("0")), json.Number("0"), nil},
{"StrictNumber", NewRaw("0.0"), json.Number("0.0"), nil},
{"StrictNumber", NewRaw("null"), json.Number(""), ErrUnsupportType},
{"String", Node{}, "", ErrUnsupportType},
{"String", NewAny(`\u263a`), `\u263a`, nil},
{"String", NewRaw(`"\u263a"`), `☺`, nil},
{"String", NewString(`\u263a`), `\u263a`, nil},
{"String", NewRaw(`0.0`), "0.0", nil},
{"String", NewRaw(`null`), "null", nil},
{"String", NewRaw(`true`), "true", nil},
{"String", NewRaw(`false`), "false", nil},
{"String", NewRaw(`null`), "", nil},
{"StrictString", Node{}, "", ErrUnsupportType},
{"StrictString", NewAny(`\u263a`), `\u263a`, nil},
{"StrictString", NewRaw(`"\u263a"`), `☺`, nil},
{"StrictString", NewString(`\u263a`), `\u263a`, nil},
{"StrictString", NewRaw(`0.0`), "", ErrUnsupportType},
{"StrictString", NewRaw(`null`), "", nil},
{"StrictString", NewRaw(`true`), "", ErrUnsupportType},
{"StrictString", NewRaw(`false`), "", ErrUnsupportType},
{"StrictString", NewRaw(`null`), "", ErrUnsupportType},
{"Len", Node{}, 0, nil},
{"Len", NewAny(0), 0, ErrUnsupportType},
{"Len", NewNull(), 0, nil},
Expand Down

0 comments on commit b6220e9

Please sign in to comment.