1
1
mirror of https://github.com/wader/fq.git synced 2024-12-23 13:22:58 +03:00

decode,interp: Finish up nil value support

Update msgpack, bson and cbor to use it.
This commit is contained in:
Mattias Wadman 2022-01-17 10:49:43 +01:00
parent cc79735817
commit 548a065388
8 changed files with 14 additions and 2 deletions

View File

@ -82,6 +82,7 @@ func decodeBSONDocument(d *decode.D) {
d.FieldU8("subtype") d.FieldU8("subtype")
d.FieldRawLen("value", int64(length)*8) d.FieldRawLen("value", int64(length)*8)
case elementTypeUndefined: case elementTypeUndefined:
//deprecated
case elementTypeObjectID: case elementTypeObjectID:
d.FieldRawLen("value", 12*8) d.FieldRawLen("value", 12*8)
case elementTypeBoolean: case elementTypeBoolean:
@ -89,6 +90,7 @@ func decodeBSONDocument(d *decode.D) {
case elementTypeDatatime: case elementTypeDatatime:
d.FieldS32("value") d.FieldS32("value")
case elementTypeNull: case elementTypeNull:
d.FieldValueNil("value")
case elementTypeRegexp: case elementTypeRegexp:
d.FieldUTF8Null("value") d.FieldUTF8Null("value")
d.FieldUTF8Null("options") d.FieldUTF8Null("options")

View File

@ -59,6 +59,7 @@ $ fq -d bson verbose /test.bson
0x60| 0a | . | type: "null" (10) (Null value) 0x6c-0x6c.7 (1) 0x60| 0a | . | type: "null" (10) (Null value) 0x6c-0x6c.7 (1)
0x60| 6e 75 6c| nul| name: "null" 0x6d-0x71.7 (5) 0x60| 6e 75 6c| nul| name: "null" 0x6d-0x71.7 (5)
0x70|6c 00 |l. | 0x70|6c 00 |l. |
| | | value: null 0x72-NA (0)
0x70| 00| | .| | terminator: 0 (valid) 0x72-0x72.7 (1) 0x70| 00| | .| | terminator: 0 (valid) 0x72-0x72.7 (1)
$ fq -d bson torepr /test.bson $ fq -d bson torepr /test.bson
{ {

View File

@ -218,7 +218,7 @@ func decodeCBORValue(d *decode.D) interface{} {
case shortCountSpecialTrue: case shortCountSpecialTrue:
d.FieldValueBool("value", true) d.FieldValueBool("value", true)
case shortCountSpecialNull: case shortCountSpecialNull:
// TODO: null d.FieldValueNil("value")
case shortCountSpecialUndefined: case shortCountSpecialUndefined:
// TODO: undefined // TODO: undefined
case 24: case 24:

View File

@ -100,7 +100,7 @@ func decodeMsgPackValue(d *decode.D) {
d.FieldUTF8("value", int(length)) d.FieldUTF8("value", int(length))
}}, }},
{r: [2]byte{0xc0, 0xc0}, s: scalar.S{Sym: "nil"}, d: func(d *decode.D) { {r: [2]byte{0xc0, 0xc0}, s: scalar.S{Sym: "nil"}, d: func(d *decode.D) {
// TODO: fq has no good null type atm d.FieldValueNil("value")
}}, }},
{r: [2]byte{0xc1, 0xc1}, s: scalar.S{Sym: "never_used"}, d: func(d *decode.D) { {r: [2]byte{0xc1, 0xc1}, s: scalar.S{Sym: "never_used"}, d: func(d *decode.D) {
d.Fatalf("0xc1 never used") d.Fatalf("0xc1 never used")

View File

@ -84,6 +84,7 @@ $ fq -d msgpack v test.msgpack
0x40|6c 6c |ll | 0x40|6c 6c |ll |
| | | value{}: 0x42-0x42.7 (1) | | | value{}: 0x42-0x42.7 (1)
0x40| c0| | .| | type: "nil" (0xc0) 0x42-0x42.7 (1) 0x40| c0| | .| | type: "nil" (0xc0) 0x42-0x42.7 (1)
| | | value: null 0x43-NA (0)
$ fq -d msgpack torepr test.msgpack $ fq -d msgpack torepr test.msgpack
{ {
"array": [ "array": [

View File

@ -87,6 +87,7 @@ func ToInt(x interface{}) (int, bool) {
case gojq.JQValue: case gojq.JQValue:
return ToInt(x.JQValueToGoJQ()) return ToInt(x.JQValueToGoJQ())
default: default:
// nil and other should fail, "null | tonumber" in jq is an error
return 0, false return 0, false
} }
} }
@ -112,6 +113,7 @@ func ToFloat(x interface{}) (float64, bool) {
case gojq.JQValue: case gojq.JQValue:
return ToFloat(x.JQValueToGoJQ()) return ToFloat(x.JQValueToGoJQ())
default: default:
// nil and other should fail, "null | tonumber" in jq is an error
return 0.0, false return 0.0, false
} }
} }

View File

@ -653,6 +653,10 @@ func (d *D) FieldValueStr(name string, a string, sms ...scalar.Mapper) {
d.FieldScalarFn(name, func(_ scalar.S) (scalar.S, error) { return scalar.S{Actual: a}, nil }, sms...) d.FieldScalarFn(name, func(_ scalar.S) (scalar.S, error) { return scalar.S{Actual: a}, nil }, sms...)
} }
func (d *D) FieldValueNil(name string, sms ...scalar.Mapper) {
d.FieldScalarFn(name, func(_ scalar.S) (scalar.S, error) { return scalar.S{Actual: nil}, nil }, sms...)
}
func (d *D) FieldValueRaw(name string, a []byte, sms ...scalar.Mapper) { func (d *D) FieldValueRaw(name string, a []byte, sms ...scalar.Mapper) {
d.FieldScalarFn(name, func(_ scalar.S) (scalar.S, error) { d.FieldScalarFn(name, func(_ scalar.S) (scalar.S, error) {
return scalar.S{Actual: bitio.NewBufferFromBytes(a, -1)}, nil return scalar.S{Actual: bitio.NewBufferFromBytes(a, -1)}, nil

View File

@ -33,6 +33,8 @@ func previewValue(v interface{}, df scalar.DisplayFormat) string {
return fmt.Sprintf("%q", vv[0:50]) + "..." return fmt.Sprintf("%q", vv[0:50]) + "..."
} }
return fmt.Sprintf("%q", vv) return fmt.Sprintf("%q", vv)
case nil:
return "null"
case *bitio.Buffer: case *bitio.Buffer:
return "raw bits" return "raw bits"
case *big.Int: case *big.Int: