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

Support convenience decimal type syntax in IDL files #300

Open
crypticmind opened this issue May 5, 2020 · 3 comments
Open

Support convenience decimal type syntax in IDL files #300

crypticmind opened this issue May 5, 2020 · 3 comments

Comments

@crypticmind
Copy link

I have a schema like this:

protocol P {
  record R {
    decimal(10, 2) dec;
  }
}

However, protocol parsing chokes on the opening parenthesis with:

Error: invalid token {"pos":xx,"id":"operator","val":"("}: expected ID name

As I see it, avsc support logical types from JSON schema, but not from IDL. Is there any way to enable this, or does the IDL parser require updating?

@mtth
Copy link
Owner

mtth commented May 6, 2020

Hi @crypticmind, avsc doesn't support decimal logical type sugar in IDL files yet. In the meantime, you can use annotations. It would look something like:

protocol P {
  record R {
    @logicalType("decimal") @precision(10) @scale(2) bytes dec;
  }
}

If you use the type in many places, you can also add it via typeRefs:

const opts = {
  typeRefs: {my_decimal: {type: 'bytes', logicalType: 'decimal', precision: 10, scale: 2}}
};
avro.assembleProtocol(str, opts, (err, schema) => { /* ... */ });

Simplifying your protocol to:

protocol P {
  record R {
    my_decimal dec;
  }
}

@mtth mtth changed the title How to parse a decimal type in IDL [Question] Support convenience decimal type syntax in IDL files May 6, 2020
@crypticmind
Copy link
Author

@mtth thank you for the quick response.

@JohannesKlauss
Copy link

@mtth I have the same issue, but I am only reading the schema, I can't change it.
The result is that I get a decoded object looking like this:

price: Price {
    net: <Buffer 01 48 20>,
    gross: <Buffer 01 86 a0>,
    currency: 'EUR'
}

The schema looks like this:

record Price {
    decimal(10,4) net;
    decimal(10,4) gross;
    string currency;
}

Can I somehow tell the resolver how to decode those values?

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

No branches or pull requests

3 participants