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

[BUG-95]: Fix parseAvroSchema for record type fields #98

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions src/schemas/parseAvroSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,30 @@ export async function parseAvroSchema(schemaFile: any) {
}


async function convertAvroSchemaToJson(schema: any): Promise<any> {
let jsonSchema = [];
function convertAvroSchemaToJson(schema: any, nonRoot: boolean = false): any {
let jsonSchema = [];
schema.forEach(table => {
let schema = {
_meta: {
topic: table.name
}
};
let schema = {};
if(!nonRoot) {
schema = {
_meta: {
topic: table.name
}
};
}
table.fields.forEach(column => {
if (column.type === 'record') {
schema[column.name] = convertAvroSchemaToJson(column.type);
} else {

if ((column.type === 'record')) {

schema[column.name] = convertAvroSchemaToJson(column.type, true)[0];

} else if(typeof column.type === "object" && !Array.isArray(column.type) && column.type.type === 'record') {

schema[column.name] = convertAvroSchemaToJson([column.type], true)[0];

}

else {
if (Array.isArray(column.type)) {
if (column.type.length === 2 && column.type[0] === 'null') {
return schema[column.name] = avroTypesToFakerJs(column.type[1]);
Expand All @@ -50,7 +62,6 @@ async function convertAvroSchemaToJson(schema: any): Promise<any> {
});
jsonSchema.push(schema);
});

return jsonSchema;
}

Expand Down Expand Up @@ -87,4 +98,4 @@ function avroTypesToFakerJs(avroType: any) {
default:
return 'faker.datatype.string()';
}
}
}
70 changes: 70 additions & 0 deletions tests/complex-schema.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
"type": "record",
"name": "products",
"namespace": "exp.products.v1",
"fields": [
{
"name": "id",
"type": "string"
},
{
"name": "productId",
"type": [
"null",
"string"
]
},
{
"name": "title",
"type": "string"
},
{
"name": "price",
"type": "int"
},
{
"name": "isLimited",
"type": "boolean"
},
{
"name": "sizes",
"type": [
"null",
"string"
],
"default": null
},
{
"name": "ownerIds",
"type": {
"type": "array",
"items": "string"
}
},
{
"name": "data",
"type": {
"type": "record",
"name": "Data",
"fields": [
{
"name": "ID",
"type": [
"null",
"string"
],
"default": null
},
{
"name": "STREET",
"type": [
"null",
"string"
],
"default": null
}
]
}
}
]
}
8 changes: 8 additions & 0 deletions tests/datagen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ describe('Schema Parsing Tests', () => {
expect(output).toContain('Dry run: Skipping record production...');
expect(output).toContain('Stopping the data generator');
});
it('should parse complex avro schema', () => {
const schema = './tests/complex-schema.avsc';
const output = datagen(`-s ${schema} -n 2`);
expect(output).toContain('Parsing Avro schema...');
expect(output).toContain('Dry run: Skipping record production...');
expect(output).toContain('STREET');
expect(output).toContain('Stopping the data generator');
});
test('should parse sql schema', () => {
const schema = './tests/products.sql';
const output = datagen(`-s ${schema} -n 2`);
Expand Down