castle icon indicating copy to clipboard operation
castle copied to clipboard

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

Open manuelarte opened this issue 11 months ago • 0 comments

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?

manuelarte avatar Mar 14 '25 11:03 manuelarte