Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't create a proper typescript classes out of the following schema #171

Open
manuelarte opened this issue Mar 14, 2025 · 0 comments
Open

Comments

@manuelarte
Copy link

So I have the following schema (I don't have control over it, so I can't change it):

{
  "type": "record",
  "name": "mySchema",
  "namespace": "com.manuelarte",
  "doc": "Create, update or delete a VM resource",
  "fields": [
    {
      "name": "message",
      "type": [
        "null",
        {
          "type": "record",
          "name": "Message",
          "fields": [
            {
              "name": "messageId",
              "type": { "type": "string", "logicalType": "uuid" },
              "doc": "Business key to be used to uniquely identify the message"
            }
          ]
        }
      ],
      "doc": "Event specific information",
      "default": null
    },
    {
      "name": "operation",
      "type": [
        "null",
        {
          "type": "enum",
          "name": "Operation",
          "symbols": ["Created", "Updated", "Deleted"]
        }
      ],
      "doc": "The operation performed on the resource: Created, Deleted, Updated",
      "default": null
    },
    {
      "name": "deployment",
      "type": [
        "null",
        {
          "type": "record",
          "name": "Deployment",
          "fields": [
            {
              "name": "ciName",
              "type": ["null", "string"],
              "doc": "CiName",
              "default": null
            },
            {
              "name": "deployment",
              "type": ["null", "string"],
              "doc": "Deployment Id, e.g. srv320023761, s21041123dev, PDBID (DBaaS)",
              "default": null
            },
            {
              "name": "description",
              "type": ["null", "string"],
              "doc": "Description",
              "default": null
            },
            {
              "name": "environment",
              "type": [
                "null",
                {
                  "type": "enum",
                  "name": "DTAP",
                  "symbols": ["Development", "Test", "Acceptance", "Production"]
                }
              ],
              "doc": "The environment (or business use), used as additional information on the Kaching report",
              "default": null
            },
            {
              "name": "timestamp",
              "type": { "type": "long", "logicalType": "timestamp-millis" },
              "doc": "Timestamp of deployment, in milliseconds since epoch."
            }
          ]
        }
      ],
      "doc": "Deployment specific information",
      "default": null
    },
    {
      "name": "resource",
      "type": [
        "null",
        {
          "type": "record",
          "name": "Resource",
          "fields": [
            {
              "name": "os",
              "type": ["null", "string"],
              "doc": "Linux or Windows",
              "default": null
            },
            {
              "name": "osVersion",
              "type": ["null", "string"],
              "doc": "rhel8, rhel9",
              "default": null
            }
          ]
        }
      ],
      "doc": "Resource specific information and configuration",
      "default": null
    },
    {
      "name": "storage",
      "type": [
        "null",
        {
          "type": "record",
          "name": "Storage",
          "fields": [
            {
              "name": "disks",
              "type": [
                "null",
                {
                  "type": "array",
                  "items": {
                    "type": "record",
                    "name": "Disk",
                    "fields": [
                      {
                        "name": "label",
                        "type": ["null", "string"],
                        "doc": "Hard disk 0",
                        "default": null
                      },
                      {
                        "name": "diskName",
                        "type": ["null", "string"],
                        "doc": "disk0",
                        "default": null
                      },
                      {
                        "name": "diskSize",
                        "type": ["null", "int"],
                        "doc": "40",
                        "default": null
                      },
                      {
                        "name": "diskSizeUnit",
                        "type": [
                          "null",
                          {
                            "type": "enum",
                            "name": "SizeUnit",
                            "symbols": [
                              "PB",
                              "TB",
                              "GB",
                              "MB",
                              "PiB",
                              "TiB",
                              "GiB",
                              "MiB"
                            ]
                          }
                        ],
                        "doc": "Size unit diskSize, e.g. Giga Bytes (GB, base10, 10^9); Gibi Bytes (GiB, base2, 2^30)",
                        "default": null
                      },
                      {
                        "name": "diskType",
                        "type": [
                          "null",
                          {
                            "type": "enum",
                            "name": "DiskType",
                            "symbols": ["vDisk", "pDisk"]
                          }
                        ],
                        "doc": "virtual or physical disk",
                        "default": null
                      },
                      {
                        "name": "storageCluster",
                        "type": ["null", "string"],
                        "doc": "cluster or .... e.g DC-WPR-R-SHR-32-002",
                        "default": null
                      }
                    ]
                  }
                }
              ],
              "default": null
            }
          ]
        }
      ],
      "doc": "Storage configuration",
      "default": null
    },
    {
      "name": "system",
      "type": [
        "null",
        {
          "type": "record",
          "name": "System",
          "fields": [
            {
              "name": "cpu",
              "type": [
                "null",
                {
                  "type": "record",
                  "name": "Cpu",
                  "fields": [
                    {
                      "name": "count",
                      "type": ["null", "int"],
                      "doc": "Number of (m)cores",
                      "default": null
                    },
                    {
                      "name": "type",
                      "type": [
                        "null",
                        {
                          "type": "enum",
                          "name": "CpuType",
                          "symbols": ["vCPU", "mCore", "GPU"]
                        }
                      ],
                      "doc": "Type of processor: vCPU, mCore, GPU",
                      "default": null
                    }
                  ]
                }
              ],
              "doc": "CPU information, including count and type",
              "default": null
            },
            {
              "name": "memory",
              "type": [
                "null",
                {
                  "type": "record",
                  "name": "Memory",
                  "fields": [
                    {
                      "name": "amount",
                      "type": ["null", "int"],
                      "doc": "Amount of memory",
                      "default": null
                    },
                    {
                      "name": "type",
                      "type": [
                        "null",
                        {
                          "type": "enum",
                          "name": "MemoryType",
                          "symbols": ["vRAM", "pRAM"]
                        }
                      ],
                      "doc": "Type of memory: virtual or physical",
                      "default": null
                    },
                    {
                      "name": "sizeUnit",
                      "type": [
                        "null",
                        {
                          "type": "enum",
                          "name": "MemorySizeUnit",
                          "symbols": ["TB", "GB", "MB"]
                        }
                      ],
                      "doc": "Size unit for memory, e.g., GB or MB",
                      "default": null
                    }
                  ]
                }
              ],
              "doc": "Memory information, including amount, type, and size unit",
              "default": null
            }
          ]
        }
      ],
      "doc": "System configuration: cpu and memory",
      "default": null
    }
  ]
}

But when I try to generate the typescript classes with:

import { toTypeScript } from '@ovotech/avro-ts';
import * as fs from "node:fs";

const avro = fs.readFileSync("./resources/schema/myschema.avsc", { encoding: 'utf8', flag: 'r' });
export const ts = toTypeScript(avro, { withTypescriptEnums: true });
console.log(ts)

I get, an output that it's not correct, starting with:

export type , GB or MB",
                      "default": null
                    }
                  ]
                }
              ],
              "doc": "Memory information, including amount, type, and size unit",
              "default": null
            }
          ]
        }
      ],
      "doc": "System configuration: cpu and memory",
      "default": null
    }
...

am I doing something wrong?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant