elodina / go-avro

Apache Avro for Golang
http://elodina.github.io/go-avro/
Apache License 2.0
128 stars 55 forks source link

Can the code gen handle maps (et al)? #51

Closed mrwilby closed 9 years ago

mrwilby commented 9 years ago

I tried this, which is based on an example I groked on Pentaho's website:

...

schemas = []string {
                    `{
    "type": "record",
    "namespace": "com.philips.lighting.dna.ingestion",
    "name": "LongList",
    "fields": [
        {
            "type": "map",
            "name": "inner_name",
            "values": {
                "type": "record",
                "name": "ATM",
                "fields": [
                    {
                        "name": "serial_no",
                        "type": "string"
                    },
                    {
                        "name": "location",
                        "type": "string"
                    }
                ]
            }
        }
    ]
}`
}
            gen := avro.NewCodeGenerator(schemas)
            code, err := gen.Generate()

And the following error is generated:

2015/09/21 11:58:05 Unknown type name: map make: *\ [codegen] Error 1

It looks like the code that parses the "type" sees a string value associated with the "type: map" line, and then only can interpret basic types, excluding maps, arrays, enums etc.

Perhaps I misunderstood or then this example avro schema is invalid? (source for the schema: http://wiki.pentaho.com/display/EAI/Avro+Input)

serejja commented 9 years ago

Given schema is invalid. This one parses well:

{
    "type": "record",
    "namespace": "com.philips.lighting.dna.ingestion",
    "name": "LongList",
    "fields": [
        {
            "name": "inner_name",
            "type": {
                "type": "map",
                "values": {
                    "type": "record",
                    "name": "ATM",
                    "fields": [
                        {
                            "name": "serial_no",
                            "type": "string"
                        },
                        {
                            "name": "location",
                            "type": "string"
                        }
                    ]
                }
            }
        }
    ]
}

Please notice that the proper map definition would not be "type": "map" but "type": {"type": "map", "values": "whatever"}

mrwilby commented 9 years ago

Thank you. Finding a decent example of map schemas is actually quite difficult. Almost all seem broken!

Appreciate the quick feedback