1
1
mirror of https://github.com/wader/fq.git synced 2024-12-01 02:30:32 +03:00
fq/pkg/interp/formats.jq
Mattias Wadman 149cb3f45a 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
2022-01-12 17:33:58 +01:00

32 lines
897 B
Plaintext

# note this is a "dynamic" include, outputted string will be used as source
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