Skip to content

Commit

Permalink
Merge pull request #186 from wader/format-simplify-torepr
Browse files Browse the repository at this point in the history
format: Simplify torepr, no need for _f function
  • Loading branch information
wader authored Mar 8, 2022
2 parents aa250b4 + 27e7615 commit 0ed6b25
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 46 deletions.
16 changes: 7 additions & 9 deletions format/asn1/asn1_ber.jq
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
def _asn1_ber_torepr:
def _f:
if .class == "universal" then
if .tag | . == "sequence" or . == "set" then
.constructed | map(_f)
else .value | tovalue
end
else .constructed | map(_f)
end;
_f;
if .class == "universal" then
if .tag | . == "sequence" or . == "set" then
.constructed | map(_asn1_ber_torepr)
else .value | tovalue
end
else .constructed | map(_asn1_ber_torepr)
end;
17 changes: 16 additions & 1 deletion format/asn1/testdata/test.pem.fqtest
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$ fq -d raw 'frompem | asn1_ber | dv' test.pem
$ fq -d raw 'frompem | asn1_ber | dv, torepr' test.pem
|00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f|0123456789abcdef|.{}: (asn1_ber) 0x0-0xa1.7 (162)
0x00|30 |0 | class: "universal" (0) 0x0-0x0.1 (0.2)
0x00|30 |0 | form: "constructed" (1) 0x0.2-0x0.2 (0.1)
Expand Down Expand Up @@ -39,3 +39,18 @@ $ fq -d raw 'frompem | asn1_ber | dv' test.pem
0x10| 30 81 89 02 81 81 00 cc 61 f9| 0.......a.| value: raw bits 0x16-0xa1.7 (140)
0x20|ef 5a d0 bc 21 de 5b 3c a6 9e e7 25 d2 c5 04 ed|.Z..!.[<...%....|
* |until 0xa1.7 (end) (140) | |
[
[
[
1,
2,
840,
113549,
1,
1,
1
],
null
],
"<140>MIGJAoGBAMxh+e9a0Lwh3ls8pp7nJdLFBO35mm6XoCedlZ8jZO0hqu+McEVS1dGj3rLuGgvhVY48oYKxGowUOStt5SNGvHyIv3Xj+yufJ/qyH1zxmTTjEQ6krXKm8HP4qzi5k5u2OeeK1/Q0EZ2MwrG95Or5UTsFZQjJCO1DyZsM6yIsu+HvAgMBAAE="
]
22 changes: 10 additions & 12 deletions format/bencode/bencode.jq
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
def _bencode_torepr:
def _f:
if .type == "string" then .value | tovalue
elif .type == "integer" then .value | tovalue
elif .type == "list" then .values | map(_f)
elif .type == "dictionary" then
( .pairs
| map({key: (.key | _f), value: (.value | _f)})
| from_entries
)
else error("unknown type \(.type)")
end;
_f;
if .type == "string" then .value | tovalue
elif .type == "integer" then .value | tovalue
elif .type == "list" then .values | map(_bencode_torepr)
elif .type == "dictionary" then
( .pairs
| map({key: (.key | _bencode_torepr), value: (.value | _bencode_torepr)})
| from_entries
)
else error("unknown type \(.type)")
end;
21 changes: 9 additions & 12 deletions format/cbor/cbor.jq
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
def _cbor_torepr:
def _f:
( if .major_type == "map" then
( .pairs
| map({key: (.key | _f), value: (.value | _f)})
| from_entries
)
elif .major_type == "array" then .elements | map(_f)
elif .major_type == "bytes" then .value | tostring
else .value | tovalue
end
);
_f;
if .major_type == "map" then
( .pairs
| map({key: (.key | _cbor_torepr), value: (.value | _cbor_torepr)})
| from_entries
)
elif .major_type == "array" then .elements | map(_cbor_torepr)
elif .major_type == "bytes" then .value | tostring
else .value | tovalue
end;
21 changes: 9 additions & 12 deletions format/msgpack/msgpack.jq
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
def _msgpack_torepr:
def _f:
( if .type | . == "fixmap" or . == "map16" or . == "map32" then
( .pairs
| map({key: (.key | _f), value: (.value | _f)})
| from_entries
)
elif .type | . == "fixarray" or . == "array16" or . == "array32" then .elements | map(_f)
elif .type | . == "bin8" or . == "bin16" or . == "bin32" then .value | tostring
else .value | tovalue
end
);
_f;
if .type | . == "fixmap" or . == "map16" or . == "map32" then
( .pairs
| map({key: (.key | _msgpack_torepr), value: (.value | _msgpack_torepr)})
| from_entries
)
elif .type | . == "fixarray" or . == "array16" or . == "array32" then .elements | map(_msgpack_torepr)
elif .type | . == "bin8" or . == "bin16" or . == "bin32" then .value | tostring
else .value | tovalue
end;

0 comments on commit 0ed6b25

Please sign in to comment.