1
1
mirror of https://github.com/wader/fq.git synced 2024-11-23 00:57:15 +03:00

interp: Add torepr/0 that converts decode value into what it reptresents

Ex: fq -d msgpack torepr file.msgpack
Willoutput the JSON representation of the msgpack

Make per format *_torepr functions internal
This commit is contained in:
Mattias Wadman 2022-01-10 18:25:36 +01:00
parent 3e2a233e50
commit 149cb3f45a
15 changed files with 56 additions and 28 deletions

View File

@ -281,6 +281,8 @@ unary uses input and if more than one argument all as arguments ignoring the inp
- `toactual/0` actual value (decoded etc) - `toactual/0` actual value (decoded etc)
- `tosym/0` symbolic value (mapped etc) - `tosym/0` symbolic value (mapped etc)
- `todescription/0` description of value - `todescription/0` description of value
- `torepr/0` convert decode value into what it reptresents. For example convert msgpack decode value
into a value representing its JSON representation.
- All regexp functions work with buffers as input and pattern argument with these differences - All regexp functions work with buffers as input and pattern argument with these differences
from the string versions: from the string versions:
- All offset and length will be in bytes. - All offset and length will be in bytes.

View File

@ -19,6 +19,7 @@ func init() {
Description: "BitTorrent bencoding", Description: "BitTorrent bencoding",
DecodeFn: decodeBencode, DecodeFn: decodeBencode,
Files: bencodeFS, Files: bencodeFS,
ToRepr: "_bencode_torepr",
}) })
} }

View File

@ -1,7 +1,7 @@
def bencode_torepr: def _bencode_torepr:
def _f: def _f:
if .type == "string" then .value if .type == "string" then .value | tovalue
elif .type == "integer" then .value elif .type == "integer" then .value | tovalue
elif .type == "list" then .values | map(_f) elif .type == "list" then .values | map(_f)
elif .type == "dictionary" then elif .type == "dictionary" then
( .pairs ( .pairs

View File

@ -275,7 +275,7 @@ $ fq -d bencode v bbb.torrent
0x3780|62 33 64 2e 72 65 6e 64 65 72 66 61 72 6d 69 6e|b3d.renderfarmin| 0x3780|62 33 64 2e 72 65 6e 64 65 72 66 61 72 6d 69 6e|b3d.renderfarmin|
0x3790|67 2e 6e 65 74 |g.net | 0x3790|67 2e 6e 65 74 |g.net |
0x3790| 65| | e| | end: "e" (valid) 0x3795-0x3795.7 (1) 0x3790| 65| | e| | end: "e" (valid) 0x3795-0x3795.7 (1)
$ fq -d bencode bencode_torepr bbb.torrent $ fq -d bencode torepr bbb.torrent
{ {
"announce": "udp://tracker.openbittorrent.com:80/announce", "announce": "udp://tracker.openbittorrent.com:80/announce",
"announce-list": [ "announce-list": [

View File

@ -21,6 +21,7 @@ func init() {
Description: "Binary JSON", Description: "Binary JSON",
DecodeFn: decodeBSON, DecodeFn: decodeBSON,
Files: bsonFS, Files: bsonFS,
ToRepr: "_bson_torepr",
}) })
} }

View File

@ -1,18 +1,18 @@
def bson_torepr: def _bson_torepr:
def _torepr: def _f:
( if .type == null or .type == "array" then ( if .type == null or .type == "array" then
( .value.elements ( .value.elements
| map(_torepr) | map(_f)
) )
elif .type == "document" then elif .type == "document" then
( .value.elements ( .value.elements
| map({key: .name, value: _torepr}) | map({key: .name, value: _f})
| from_entries | from_entries
) )
elif .type == "boolean" then .value != 0 elif .type == "boolean" then .value != 0
else .value else .value | tovalue
end end
); );
( {type: "document", value: .} ( {type: "document", value: .}
| _torepr | _f
); );

View File

@ -60,7 +60,7 @@ $ fq -d bson verbose /test.bson
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. |
0x70| 00| | .| | terminator: 0 (valid) 0x72-0x72.7 (1) 0x70| 00| | .| | terminator: 0 (valid) 0x72-0x72.7 (1)
$ fq -d bson bson_torepr /test.bson $ fq -d bson torepr /test.bson
{ {
"array": [ "array": [
1, 1,

View File

@ -22,6 +22,7 @@ func init() {
Description: "MessagePack", Description: "MessagePack",
DecodeFn: decodeMsgPack, DecodeFn: decodeMsgPack,
Files: msgPackFS, Files: msgPackFS,
ToRepr: "_msgpack_torepr",
}) })
} }

View File

@ -1,4 +1,4 @@
def msgpack_torepr: def _msgpack_torepr:
def _f: def _f:
( if .type | . == "fixmap" or . == "map16" or . == "map32" then ( if .type | . == "fixmap" or . == "map16" or . == "map32" then
( .pairs ( .pairs
@ -7,7 +7,7 @@ def msgpack_torepr:
) )
elif .type | . == "fixarray" or . == "array16" or . == "array32" then .elements | map(_f) elif .type | . == "fixarray" or . == "array16" or . == "array32" then .elements | map(_f)
elif .type | . == "bin8" or . == "bin16" or . == "bin32" then .value | tostring elif .type | . == "bin8" or . == "bin16" or . == "bin32" then .value | tostring
else .value else .value | tovalue
end end
); );
_f; _f;

View File

@ -83,7 +83,7 @@ $ fq -d msgpack v ints.msgpack
0x20| d2 | . | type: "int32" (0xd2) 0x2d-0x2d.7 (1) 0x20| d2 | . | type: "int32" (0xd2) 0x2d-0x2d.7 (1)
0x20| 80 00| ..| value: -2147483647 0x2e-0x31.7 (4) 0x20| 80 00| ..| value: -2147483647 0x2e-0x31.7 (4)
0x30|00 01| |..| | 0x30|00 01| |..| |
$ fq -d msgpack msgpack_torepr ints.msgpack $ fq -d msgpack torepr ints.msgpack
[ [
0, 0,
1, 1,

View File

@ -84,7 +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)
$ fq -d msgpack msgpack_torepr test.msgpack $ fq -d msgpack torepr test.msgpack
{ {
"array": [ "array": [
1, 1,

View File

@ -19,6 +19,7 @@ type Format struct {
RootName string RootName string
Dependencies []Dependency Dependencies []Dependency
Files fs.ReadDirFS Files fs.ReadDirFS
ToRepr string
} }
func FormatFn(d func(d *D, in interface{}) interface{}) Group { func FormatFn(d func(d *D, in interface{}) interface{}) Group {

View File

@ -75,6 +75,7 @@ func (i *Interp) _registry(c interface{}, a []interface{}) interface{} {
"probe_order": f.ProbeOrder, "probe_order": f.ProbeOrder,
"root_name": f.RootName, "root_name": f.RootName,
"root_array": f.RootArray, "root_array": f.RootArray,
"to_repr": f.ToRepr,
} }
var dependenciesVs []interface{} var dependenciesVs []interface{}

View File

@ -1,16 +1,31 @@
# note this is a "dynamic" include, outputted string will be used as source # note this is a "dynamic" include, outputted string will be used as source
( [ ( _registry.groups def _formats_source:
| to_entries[] ( [ ( _registry.groups
# TODO: nicer way to skip "all" which also would override builtin all/* | to_entries[]
| select(.key != "all") # TODO: nicer way to skip "all" which also would override builtin all/*
| "def \(.key)($opts): decode(\(.key | tojson); $opts);" | select(.key != "all")
, "def \(.key): decode(\(.key | tojson); {});" | "def \(.key)($opts): decode(\(.key | tojson); $opts);"
) , "def \(.key): decode(\(.key | tojson); {});"
, ( _registry.formats[] )
| select(.files) , ( _registry.formats[]
| .files[] | select(.files)
) | .files[]
] )
, ( "def torepr:"
, " ( format as $f"
, " | if $f == null then error(\"value is not a format root\") end"
, " | if false then error(\"unreachable\")"
, ( _registry.formats[]
| select(.to_repr != "")
| " elif $f == \(.name | tojson) then \(.to_repr)"
)
, " else error(\"format has no torepr\")"
, " end"
, " );"
)
]
| join("\n") | join("\n")
) );
_formats_source

6
pkg/interp/testdata/torepr.fqtest vendored Normal file
View File

@ -0,0 +1,6 @@
$ fq -i
null> torepr
error: value is not a format root
null> "{}" | json | torepr
error: format has no torepr
null> ^D