LCVcode / jockey

MIT License
2 stars 3 forks source link

Strong typing using Juju's command schema #38

Closed johnlettman closed 1 week ago

johnlettman commented 3 weeks ago

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:

[
  {
    "Name": "Action",
    "Version": 7,
    "Schema": {
      "type": "object",
      "properties": {
        "Actions": {
          "type": "object",
          "properties": {
            "Params": {
              "$ref": "#/definitions/Entities"
            },
            "Result": {
              "$ref": "#/definitions/ActionResults"
            }
[...]

Adding strong typing could improve the development experience in the project while including additional linting guarantees.

johnlettman commented 3 weeks 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
      )
    }
johnlettman commented 3 weeks ago

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.

image

johnlettman commented 3 weeks ago

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.