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

avro-decimal does not work with latest version of decimal.js #158

Open
TimShilov opened this issue Apr 8, 2024 · 0 comments · May be fixed by #159
Open

avro-decimal does not work with latest version of decimal.js #158

TimShilov opened this issue Apr 8, 2024 · 0 comments · May be fixed by #159

Comments

@TimShilov
Copy link

I've been trying to make example code (https://github.com/ovotech/castle/blob/main/packages/avro-decimal/examples/simple.ts) working and it was throwing an error.

After looking into it it seem that avro-decimal does not work with versions of decimal.js bigger than 10.2.1. Seems like they've changed something in exports and the value no longer recognized as instanceof Decimal.
Downgrading it fixes the issue.

Examples:

import avro from "avsc";
import { AvroDecimal } from "@ovotech/avro-decimal";
import decimal from "decimal.js";

const { Decimal } = decimal;

const decimalSchema = {
    type: "bytes",
    logicalType: "decimal",
    precision: 16,
    scale: 8,
};

export const DecimalType = avro.Type.forSchema(decimalSchema, {
    logicalTypes: { decimal: AvroDecimal },
});

const encoded = DecimalType.toBuffer(new Decimal("100.01"));
const decoded = DecimalType.fromBuffer(encoded);

console.log("decoded", decoded); // output: decoded 100.01
  • Install [email protected]
  • Run following code (the only difference is import of decimal.js):
import avro from "avsc";
import { AvroDecimal } from "@ovotech/avro-decimal";
import { Decimal } from "decimal.js";

const decimalSchema = {
    type: "bytes",
    logicalType: "decimal",
    precision: 16,
    scale: 8,
};

export const DecimalType = avro.Type.forSchema(decimalSchema, {
    logicalTypes: { decimal: AvroDecimal },
});

const encoded = DecimalType.toBuffer(new Decimal("100.01"));
const decoded = DecimalType.fromBuffer(encoded);

console.log("decoded", decoded);

That throws this error:

/Users/tim.shilov/IdeaProjects/affiliate-etl/node_modules/.pnpm/@[email protected][email protected]/node_modules/@ovotech/avro-decimal/dist/index.js:63
             throw new Error('expecting Decimal type');
                   ^

Error: expecting Decimal type
    at AvroDecimal._toValue (/Users/tim.shilov/IdeaProjects/affiliate-etl/node_modules/.pnpm/@[email protected][email protected]/node_modules/@ovotech/avro-decimal/dist/index.js:63:20)
    at LogicalType._write (/Users/tim.shilov/IdeaProjects/affiliate-etl/node_modules/.pnpm/[email protected]/node_modules/avsc/lib/types.js:2627:40)
    at Type.toBuffer (/Users/tim.shilov/IdeaProjects/affiliate-etl/node_modules/.pnpm/[email protected]/node_modules/avsc/lib/types.js:658:8)
    at file:///Users/tim.shilov/IdeaProjects/affiliate-etl/apps/etl-engine/src/decimal-test.js:16:29
    at ModuleJob.run (node:internal/modules/esm/module_job:222:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

Node.js v20.12.1
@TimShilov TimShilov linked a pull request Apr 11, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant