2
0
Fork 0
mirror of https://github.com/ii64/sonic.git synced 2026-06-21 00:46:43 +08:00

feat:(ast) support cast null to empty value (#278)

This commit is contained in:
Yi Duan 2022-08-22 16:11:26 +08:00 committed by GitHub
parent 94f95f0479
commit cf08d54edd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 7 deletions

View file

@ -177,6 +177,7 @@ func (self *Node) Bool() (bool, error) {
switch self.t { switch self.t {
case types.V_TRUE : return true , nil case types.V_TRUE : return true , nil
case types.V_FALSE : return false, nil case types.V_FALSE : return false, nil
case types.V_NULL : return false, nil
case _V_ANY : case _V_ANY :
if v, ok := self.packAny().(bool); ok { if v, ok := self.packAny().(bool); ok {
return v, nil return v, nil
@ -197,6 +198,7 @@ func (self *Node) Int64() (int64, error) {
case _V_NUMBER, types.V_STRING : return numberToInt64(self) case _V_NUMBER, types.V_STRING : return numberToInt64(self)
case types.V_TRUE : return 1, nil case types.V_TRUE : return 1, nil
case types.V_FALSE : return 0, nil case types.V_FALSE : return 0, nil
case types.V_NULL : return 0, nil
case _V_ANY : case _V_ANY :
any := self.packAny() any := self.packAny()
switch v := any.(type) { switch v := any.(type) {
@ -260,7 +262,8 @@ func (self *Node) Number() (json.Number, error) {
} }
case types.V_TRUE : return json.Number("1"), nil case types.V_TRUE : return json.Number("1"), nil
case types.V_FALSE : return json.Number("0"), 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 { if v, ok := self.packAny().(json.Number); ok {
return v, nil return v, nil
} else { } else {
@ -289,7 +292,7 @@ func (self *Node) StrictNumber() (json.Number, error) {
// String returns raw string value if node type is V_STRING. // String returns raw string value if node type is V_STRING.
// Or return the string representation of other types: // Or return the string representation of other types:
// V_NULL => "null", // V_NULL => "",
// V_TRUE => "true", // V_TRUE => "true",
// V_FALSE => "false", // V_FALSE => "false",
// V_NUMBER => "[0-9\.]*" // V_NUMBER => "[0-9\.]*"
@ -300,7 +303,7 @@ func (self *Node) String() (string, error) {
} }
switch self.t { switch self.t {
case _V_NUMBER : return toNumber(self).String(), nil 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_TRUE : return "true" , nil
case types.V_FALSE : return "false", nil case types.V_FALSE : return "false", nil
case types.V_STRING : return addr2str(self.p, self.v), nil case types.V_STRING : return addr2str(self.p, self.v), nil
@ -321,7 +324,6 @@ func (self *Node) StrictString() (string, error) {
return "", err return "", err
} }
switch self.t { switch self.t {
case types.V_NULL : return "", nil
case types.V_STRING : return addr2str(self.p, self.v), nil case types.V_STRING : return addr2str(self.p, self.v), nil
case _V_ANY : case _V_ANY :
if v, ok := self.packAny().(string); ok { if v, ok := self.packAny().(string); ok {
@ -342,7 +344,8 @@ func (self *Node) Float64() (float64, error) {
case _V_NUMBER, types.V_STRING : return numberToFloat64(self) case _V_NUMBER, types.V_STRING : return numberToFloat64(self)
case types.V_TRUE : return 1.0, nil case types.V_TRUE : return 1.0, nil
case types.V_FALSE : return 0.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() any := self.packAny()
switch v := any.(type) { switch v := any.(type) {
case float32 : return float64(v), nil case float32 : return float64(v), nil

View file

@ -282,6 +282,7 @@ func TestTypeCast(t *testing.T) {
{"Bool", NewAny(false), false, nil}, {"Bool", NewAny(false), false, nil},
{"Bool", NewRaw("true"), true, nil}, {"Bool", NewRaw("true"), true, nil},
{"Bool", NewRaw("false"), false, nil}, {"Bool", NewRaw("false"), false, nil},
{"Bool", NewRaw("null"), false, nil},
{"Int64", NewRaw("true"), int64(1), nil}, {"Int64", NewRaw("true"), int64(1), nil},
{"Int64", NewRaw("false"), int64(0), nil}, {"Int64", NewRaw("false"), int64(0), nil},
{"Int64", NewRaw("\"1\""), int64(1), nil}, {"Int64", NewRaw("\"1\""), int64(1), nil},
@ -297,6 +298,7 @@ func TestTypeCast(t *testing.T) {
{"Int64", NewAny(uint64(0)), int64(0), nil}, {"Int64", NewAny(uint64(0)), int64(0), nil},
{"Int64", Node{}, int64(0), ErrUnsupportType}, {"Int64", Node{}, int64(0), ErrUnsupportType},
{"Int64", NewRaw("0"), int64(0), nil}, {"Int64", NewRaw("0"), int64(0), nil},
{"Int64", NewRaw("null"), int64(0), nil},
{"StrictInt64", NewRaw("true"), int64(0), ErrUnsupportType}, {"StrictInt64", NewRaw("true"), int64(0), ErrUnsupportType},
{"StrictInt64", NewRaw("false"), int64(0), ErrUnsupportType}, {"StrictInt64", NewRaw("false"), int64(0), ErrUnsupportType},
{"StrictInt64", NewAny(int(0)), int64(0), nil}, {"StrictInt64", NewAny(int(0)), int64(0), nil},
@ -310,6 +312,7 @@ func TestTypeCast(t *testing.T) {
{"StrictInt64", NewAny(uint64(0)), int64(0), nil}, {"StrictInt64", NewAny(uint64(0)), int64(0), nil},
{"StrictInt64", Node{}, int64(0), ErrUnsupportType}, {"StrictInt64", Node{}, int64(0), ErrUnsupportType},
{"StrictInt64", NewRaw("0"), int64(0), nil}, {"StrictInt64", NewRaw("0"), int64(0), nil},
{"StrictInt64", NewRaw("null"), int64(0), ErrUnsupportType},
{"Float64", NewRaw("true"), float64(1), nil}, {"Float64", NewRaw("true"), float64(1), nil},
{"Float64", NewRaw("false"), float64(0), nil}, {"Float64", NewRaw("false"), float64(0), nil},
{"Float64", NewRaw("\"1.0\""), float64(1.0), nil}, {"Float64", NewRaw("\"1.0\""), float64(1.0), nil},
@ -318,12 +321,14 @@ func TestTypeCast(t *testing.T) {
{"Float64", NewAny(float32(0)), float64(0), nil}, {"Float64", NewAny(float32(0)), float64(0), nil},
{"Float64", NewAny(float64(0)), float64(0), nil}, {"Float64", NewAny(float64(0)), float64(0), nil},
{"Float64", NewRaw("0.0"), float64(0.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("true"), float64(0), ErrUnsupportType},
{"StrictFloat64", NewRaw("false"), float64(0), ErrUnsupportType}, {"StrictFloat64", NewRaw("false"), float64(0), ErrUnsupportType},
{"StrictFloat64", Node{}, float64(0), ErrUnsupportType}, {"StrictFloat64", Node{}, float64(0), ErrUnsupportType},
{"StrictFloat64", NewAny(float32(0)), float64(0), nil}, {"StrictFloat64", NewAny(float32(0)), float64(0), nil},
{"StrictFloat64", NewAny(float64(0)), float64(0), nil}, {"StrictFloat64", NewAny(float64(0)), float64(0), nil},
{"StrictFloat64", NewRaw("0.0"), float64(0.0), nil}, {"StrictFloat64", NewRaw("0.0"), float64(0.0), nil},
{"StrictFloat64", NewRaw("null"), float64(0.0), ErrUnsupportType},
{"Number", Node{}, json.Number(""), ErrUnsupportType}, {"Number", Node{}, json.Number(""), ErrUnsupportType},
{"Number", NewAny(json.Number("0")), json.Number("0"), nil}, {"Number", NewAny(json.Number("0")), json.Number("0"), nil},
{"Number", NewRaw("0.0"), json.Number("0.0"), nil}, {"Number", NewRaw("0.0"), json.Number("0.0"), nil},
@ -332,27 +337,29 @@ func TestTypeCast(t *testing.T) {
{"Number", NewRaw("\"0.x0\""), json.Number(""), nonEmptyErr}, {"Number", NewRaw("\"0.x0\""), json.Number(""), nonEmptyErr},
{"Number", NewRaw("true"), json.Number("1"), nil}, {"Number", NewRaw("true"), json.Number("1"), nil},
{"Number", NewRaw("false"), json.Number("0"), nil}, {"Number", NewRaw("false"), json.Number("0"), nil},
{"Number", NewRaw("null"), json.Number("0"), nil},
{"StrictNumber", NewRaw("true"), json.Number(""), ErrUnsupportType}, {"StrictNumber", NewRaw("true"), json.Number(""), ErrUnsupportType},
{"StrictNumber", NewRaw("false"), json.Number(""), ErrUnsupportType}, {"StrictNumber", NewRaw("false"), json.Number(""), ErrUnsupportType},
{"StrictNumber", Node{}, json.Number(""), ErrUnsupportType}, {"StrictNumber", Node{}, json.Number(""), ErrUnsupportType},
{"StrictNumber", NewAny(json.Number("0")), json.Number("0"), nil}, {"StrictNumber", NewAny(json.Number("0")), json.Number("0"), nil},
{"StrictNumber", NewRaw("0.0"), json.Number("0.0"), nil}, {"StrictNumber", NewRaw("0.0"), json.Number("0.0"), nil},
{"StrictNumber", NewRaw("null"), json.Number(""), ErrUnsupportType},
{"String", Node{}, "", ErrUnsupportType}, {"String", Node{}, "", ErrUnsupportType},
{"String", NewAny(`\u263a`), `\u263a`, nil}, {"String", NewAny(`\u263a`), `\u263a`, nil},
{"String", NewRaw(`"\u263a"`), ``, nil}, {"String", NewRaw(`"\u263a"`), ``, nil},
{"String", NewString(`\u263a`), `\u263a`, nil}, {"String", NewString(`\u263a`), `\u263a`, nil},
{"String", NewRaw(`0.0`), "0.0", nil}, {"String", NewRaw(`0.0`), "0.0", nil},
{"String", NewRaw(`null`), "null", nil},
{"String", NewRaw(`true`), "true", nil}, {"String", NewRaw(`true`), "true", nil},
{"String", NewRaw(`false`), "false", nil}, {"String", NewRaw(`false`), "false", nil},
{"String", NewRaw(`null`), "", nil},
{"StrictString", Node{}, "", ErrUnsupportType}, {"StrictString", Node{}, "", ErrUnsupportType},
{"StrictString", NewAny(`\u263a`), `\u263a`, nil}, {"StrictString", NewAny(`\u263a`), `\u263a`, nil},
{"StrictString", NewRaw(`"\u263a"`), ``, nil}, {"StrictString", NewRaw(`"\u263a"`), ``, nil},
{"StrictString", NewString(`\u263a`), `\u263a`, nil}, {"StrictString", NewString(`\u263a`), `\u263a`, nil},
{"StrictString", NewRaw(`0.0`), "", ErrUnsupportType}, {"StrictString", NewRaw(`0.0`), "", ErrUnsupportType},
{"StrictString", NewRaw(`null`), "", nil},
{"StrictString", NewRaw(`true`), "", ErrUnsupportType}, {"StrictString", NewRaw(`true`), "", ErrUnsupportType},
{"StrictString", NewRaw(`false`), "", ErrUnsupportType}, {"StrictString", NewRaw(`false`), "", ErrUnsupportType},
{"StrictString", NewRaw(`null`), "", ErrUnsupportType},
{"Len", Node{}, 0, nil}, {"Len", Node{}, 0, nil},
{"Len", NewAny(0), 0, ErrUnsupportType}, {"Len", NewAny(0), 0, ErrUnsupportType},
{"Len", NewNull(), 0, nil}, {"Len", NewNull(), 0, nil},