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

Not being able to use records that use the "bytes" type field #460

Open
dbrito opened this issue May 3, 2024 · 2 comments
Open

Not being able to use records that use the "bytes" type field #460

dbrito opened this issue May 3, 2024 · 2 comments

Comments

@dbrito
Copy link

dbrito commented May 3, 2024

Hello

Has anyone here suffered from the problem of not being able to use records that use the "bytes" type field? I've already tried passing the information in byte format to the registry in many different ways (Uint8Array, Buffer, TextEncoder) but all without success

Code example in CodePen

Below is the test code

const avro = require('avsc');

const type = avro.Type.forSchema({
    type: 'record',
    name: 'Pet',
    fields: [
        { name: 'name', type: 'string' },
        { name: 'payload', type: 'bytes' },
    ]
});

const AVRO_RECORD = {
    name: 'Douglas',
    payload: new TextEncoder().encode('This is a string')
}

try {
    const buf = type.toBuffer(AVRO_RECORD); // Encoded buffer.
    const val = type.fromBuffer(buf); // AVRO_RECORD { name: 'Douglas', payload: .....}
    console.log('VALUE', val)
} catch (e) {
    console.error(e)
}
@mtth
Copy link
Owner

mtth commented May 4, 2024

Hi @dbrito. bytes are mapped to Buffer instances in avsc's current release. Wrapping the text encoder's output in the CodePen works:

import avro from "https://esm.sh/avsc";
import buffer from "https://esm.sh/buffer";

const type = avro.Type.forSchema({
  type: 'record',
  name: 'Pet',
  fields: [         
    {name: 'name', type: 'string'},
    {name: 'payload', type: 'bytes'},
  ]
}); 


const AVRO_RECORD = {
  name: 'Douglas',
  payload: buffer.Buffer.from(new TextEncoder().encode('This is a string'))
}

@dbrito
Copy link
Author

dbrito commented May 6, 2024

Hello @mtth

Thank you very much! This will help immensely in the rest of the project here, I'm implementing privacy-sandbox's attribution-reporting and I was stuck at this stage of generating the avro files for the reports

One more thank you very much

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

No branches or pull requests

2 participants