Closed johnlettman closed 1 week ago
I'm not sure, but I think I achieved using every feature in JQ at once.
# Stage 1: merge the definitions
reduce .[] as $item ({}; . + $item.Schema.definitions) | . as $defs
# Stage 2: refactor definitions for schema with $refs
| to_entries[]
| {
"$schema": "https://json-schema.org/draft/2020-12/schema",
title: .key
} + .value
| . + {
definitions: (
$defs | to_entries | map({
(.key): {
"type": "object",
"$ref": (
"file:" + (
.key
| gsub("(?<a>[a-z0-9])(?<b>[A-Z])"; "\(.a)_\(.b)")
| gsub("(?<a>[A-Z]+)(?<b>[A-Z][a-z])"; "\(.a)_\(.b)")
| ascii_downcase
) + ".json"
)
}
}) | add
)
}
Overkill? Maybe. I'm not sure how terrible it would be to check in, but the compromise here is having a whitelist that somehow traverses what's needed for each of the things we want, like model, controller, application, etc.
I suppose it's scuffed to keep attempting this in bash. I'll switch to Python because implementing the allow-list functionality to narrow this all down will get ridiculous. As an added benefit, I can probably do this crazy filtering in a more maintainable and readable way for everyone else to work on.
The Juju project contains an interesting repository called
schemagen
. It may be possible to generate Python types from the JSON schema output of this project:Adding strong typing could improve the development experience in the project while including additional linting guarantees.