mtth / avsc

Avro for JavaScript :zap:
MIT License
1.27k stars 147 forks source link

Support ?-syntax for optional fields in avdl #442

Closed adamlindqvist closed 7 months ago

adamlindqvist commented 11 months ago

I'm trying to parse a avdl file with an optional field but it seems that avsc does not support using the ?-syntax to describe a optional/nullable field.

Example

.avdl file

protocol Protocol {
    record Record {
        string? text;
    }
}

Code:

const path = path.resolve('./path-to-avdl-file.avdl');
const schema = fs.readFileSync(path, { encoding: 'utf8' });
const protocol = avsc.readProtocol(schema);

Resulting in error:

Error: invalid token {"pos":47,"id":"operator","val":"?"}: expected ID name

Is this a known issue or is it possible to work around this?

Context: https://avro.apache.org/docs/1.11.1/idl-language/#unions

mtth commented 11 months ago

Hi @adamlindqvist. Thanks for reporting - the optional shorthand isn't currently supported (PR very welcome!).

You'll need to stick to the long form until it is:

protocol Protocol {
    record Record {
        union { null, string } text;
    }
}
yehonatanz commented 5 months ago

@mtth Since the corresponding PR is now merged, any chance to release a new version to NPM to make this feature accessible?