rbystrit / avro_gen

Avro record class and reader generator
Apache License 2.0
20 stars 18 forks source link

How to generate module from the multiple schemas have dependency between #4

Closed wsapiens closed 5 years ago

wsapiens commented 5 years ago

Hi, I want to pass multiple schema files have dependencies each other to generate modules. I have the schema which has many custom types defined in other schema files. write_schema_files looks accept one schema file at a time. Is there a way to pass multiple? I generated the component module first, but not sure how to pass the component information to the method while generating a contract module. So, I got below error message when I try to generate a contract module.

I don't want to recompose all those schema files to one since there are too many schema files involved.

avro.schema.SchemaParseException: Unknown named schema 'inbound.contract.SubContractComponent', known names: ['inbound.contract.SubContract'].

Below are two snipped schemas related to the above error, but I have over 50 schema files to build complete JSON payload.

[ contract.avsc ]

{
    "namespace": "inbound.contract",
    "name": "SubContract",
    "type": "record",
    "fields": [
        {
            "name": "MainJobNumber",
            "type": "string"
        },
        {
            "name": "SubJobNumber",
            "type": "string"
        },
               :
        {
            "name": "Components",
            "type": {
                "type": "array",
                "items": "inbound.contract.SubContractComponent"
            }
        }
    ]
}

[ contract_component.avsc ]

{
    "namespace": "inbound.contract",
    "name": "SubContractComponent",
    "type": "record",
    "fields": [
        {
            "name": "MainJobNumber",
            "type": "string"
        },
        {
            "name": "SubcontractItemNumber",
            "type": [
                "null",
                "string"
            ],
            "default": null
        },
        {
            "name": "ComponentDescription",
            "type": [
                "null",
                "string"
            ],
            "default": null
        }
    ]
}
wsapiens commented 5 years ago

I found that I can pass multiple schemas as a list to the write_schema_files method and check all the modules are generated

[
    {
        "namespace": "inbound.contract",
        "name": "SubContractComponent",
        "type": "record",
        "fields": [
            {
                "name": "MainJobNumber",
                "type": "string"
            },
            {
                "name": "SubcontractItemNumber",
                "type": [
                    "null",
                    "string"
                ],
                "default": null
            },
            {
                "name": "ComponentDescription",
                "type": [
                    "null",
                    "string"
                ],
                "default": null
            }
        ]
    },
    {
        "namespace": "inbound.contract",
        "name": "SubContract",
        "type": "record",
        "fields": [
            {
                "name": "MainJobNumber",
                "type": "string"
            },
            {
                "name": "SubJobNumber",
                "type": "string"
            },
                   :
            {
                "name": "Components",
                "type": {
                    "type": "array",
                    "items": "inbound.contract.SubContractComponent"
                }
            }
        ]
    }
]