mirror of
https://github.com/wader/fq.git
synced 2024-12-23 13:22:58 +03:00
Merge pull request #74 from wader/torepr
interp: Add torepr/0 that converts decode value into what it reptresents
This commit is contained in:
commit
b66729449e
@ -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)
|
||||
- `tosym/0` symbolic value (mapped etc)
|
||||
- `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
|
||||
from the string versions:
|
||||
- All offset and length will be in bytes.
|
||||
|
@ -19,6 +19,7 @@ func init() {
|
||||
Description: "BitTorrent bencoding",
|
||||
DecodeFn: decodeBencode,
|
||||
Files: bencodeFS,
|
||||
ToRepr: "_bencode_torepr",
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
def bencode_torepr:
|
||||
def _bencode_torepr:
|
||||
def _f:
|
||||
if .type == "string" then .value
|
||||
elif .type == "integer" then .value
|
||||
if .type == "string" then .value | tovalue
|
||||
elif .type == "integer" then .value | tovalue
|
||||
elif .type == "list" then .values | map(_f)
|
||||
elif .type == "dictionary" then
|
||||
( .pairs
|
||||
|
2
format/bencode/testdata/bbb.fqtest
vendored
2
format/bencode/testdata/bbb.fqtest
vendored
@ -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|
|
||||
0x3790|67 2e 6e 65 74 |g.net |
|
||||
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-list": [
|
||||
|
@ -21,6 +21,7 @@ func init() {
|
||||
Description: "Binary JSON",
|
||||
DecodeFn: decodeBSON,
|
||||
Files: bsonFS,
|
||||
ToRepr: "_bson_torepr",
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -1,18 +1,18 @@
|
||||
def bson_torepr:
|
||||
def _torepr:
|
||||
def _bson_torepr:
|
||||
def _f:
|
||||
( if .type == null or .type == "array" then
|
||||
( .value.elements
|
||||
| map(_torepr)
|
||||
| map(_f)
|
||||
)
|
||||
elif .type == "document" then
|
||||
( .value.elements
|
||||
| map({key: .name, value: _torepr})
|
||||
| map({key: .name, value: _f})
|
||||
| from_entries
|
||||
)
|
||||
elif .type == "boolean" then .value != 0
|
||||
else .value
|
||||
else .value | tovalue
|
||||
end
|
||||
);
|
||||
( {type: "document", value: .}
|
||||
| _torepr
|
||||
| _f
|
||||
);
|
||||
|
2
format/bson/testdata/test.fqtest
vendored
2
format/bson/testdata/test.fqtest
vendored
@ -60,7 +60,7 @@ $ fq -d bson verbose /test.bson
|
||||
0x60| 6e 75 6c| nul| name: "null" 0x6d-0x71.7 (5)
|
||||
0x70|6c 00 |l. |
|
||||
0x70| 00| | .| | terminator: 0 (valid) 0x72-0x72.7 (1)
|
||||
$ fq -d bson bson_torepr /test.bson
|
||||
$ fq -d bson torepr /test.bson
|
||||
{
|
||||
"array": [
|
||||
1,
|
||||
|
@ -22,6 +22,7 @@ func init() {
|
||||
Description: "MessagePack",
|
||||
DecodeFn: decodeMsgPack,
|
||||
Files: msgPackFS,
|
||||
ToRepr: "_msgpack_torepr",
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
def msgpack_torepr:
|
||||
def _msgpack_torepr:
|
||||
def _f:
|
||||
( if .type | . == "fixmap" or . == "map16" or . == "map32" then
|
||||
( .pairs
|
||||
@ -7,7 +7,7 @@ def msgpack_torepr:
|
||||
)
|
||||
elif .type | . == "fixarray" or . == "array16" or . == "array32" then .elements | map(_f)
|
||||
elif .type | . == "bin8" or . == "bin16" or . == "bin32" then .value | tostring
|
||||
else .value
|
||||
else .value | tovalue
|
||||
end
|
||||
);
|
||||
_f;
|
||||
|
2
format/msgpack/testdata/ints.fqtest
vendored
2
format/msgpack/testdata/ints.fqtest
vendored
@ -83,7 +83,7 @@ $ fq -d msgpack v ints.msgpack
|
||||
0x20| d2 | . | type: "int32" (0xd2) 0x2d-0x2d.7 (1)
|
||||
0x20| 80 00| ..| value: -2147483647 0x2e-0x31.7 (4)
|
||||
0x30|00 01| |..| |
|
||||
$ fq -d msgpack msgpack_torepr ints.msgpack
|
||||
$ fq -d msgpack torepr ints.msgpack
|
||||
[
|
||||
0,
|
||||
1,
|
||||
|
2
format/msgpack/testdata/test.fqtest
vendored
2
format/msgpack/testdata/test.fqtest
vendored
@ -84,7 +84,7 @@ $ fq -d msgpack v test.msgpack
|
||||
0x40|6c 6c |ll |
|
||||
| | | value{}: 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": [
|
||||
1,
|
||||
|
@ -19,6 +19,7 @@ type Format struct {
|
||||
RootName string
|
||||
Dependencies []Dependency
|
||||
Files fs.ReadDirFS
|
||||
ToRepr string
|
||||
}
|
||||
|
||||
func FormatFn(d func(d *D, in interface{}) interface{}) Group {
|
||||
|
@ -75,6 +75,7 @@ func (i *Interp) _registry(c interface{}, a []interface{}) interface{} {
|
||||
"probe_order": f.ProbeOrder,
|
||||
"root_name": f.RootName,
|
||||
"root_array": f.RootArray,
|
||||
"to_repr": f.ToRepr,
|
||||
}
|
||||
|
||||
var dependenciesVs []interface{}
|
||||
|
@ -1,16 +1,31 @@
|
||||
# note this is a "dynamic" include, outputted string will be used as source
|
||||
|
||||
( [ ( _registry.groups
|
||||
| to_entries[]
|
||||
# TODO: nicer way to skip "all" which also would override builtin all/*
|
||||
| select(.key != "all")
|
||||
| "def \(.key)($opts): decode(\(.key | tojson); $opts);"
|
||||
, "def \(.key): decode(\(.key | tojson); {});"
|
||||
)
|
||||
, ( _registry.formats[]
|
||||
| select(.files)
|
||||
| .files[]
|
||||
)
|
||||
]
|
||||
def _formats_source:
|
||||
( [ ( _registry.groups
|
||||
| to_entries[]
|
||||
# TODO: nicer way to skip "all" which also would override builtin all/*
|
||||
| select(.key != "all")
|
||||
| "def \(.key)($opts): decode(\(.key | tojson); $opts);"
|
||||
, "def \(.key): decode(\(.key | tojson); {});"
|
||||
)
|
||||
, ( _registry.formats[]
|
||||
| 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")
|
||||
)
|
||||
);
|
||||
|
||||
_formats_source
|
||||
|
6
pkg/interp/testdata/torepr.fqtest
vendored
Normal file
6
pkg/interp/testdata/torepr.fqtest
vendored
Normal 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
|
Loading…
Reference in New Issue
Block a user