Skip to content

Commit

Permalink
Allow top level enum types
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Wood authored and Joe Wood committed Sep 12, 2020
1 parent e7093c8 commit 40230c1
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 14 deletions.
9 changes: 9 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp

// List of extensions which should be recommended for users of this workspace.
"recommendations": ["streetsidesoftware.avro"],
// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
"unwantedRecommendations": []
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "avro-typescript",
"version": "1.0.0",
"version": "1.1.0",
"description": "TypeScript code generator for Apache Avro types",
"main": "./lib/index.js",
"typings": "lib/index.d.ts",
Expand All @@ -11,6 +11,7 @@
"test": "test"
},
"scripts": {
"test-update": "jest -u",
"test": "jest",
"build": "tsc"
},
Expand Down
11 changes: 7 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,26 @@ export {
Field,
isArrayType,
isEnumType,
isLogicalType,
isMapType,
isOptional,
isRecordType,
RecordType,
Type,
isLogicalType,
} from "./model";

import {
EnumType,
Field,
isArrayType,
isEnumType,
isLogicalType,
isMapType,
isOptional,
isRecordType,
RecordType,
Schema,
Type,
isLogicalType,
} from "./model";

/** Convert a primitive type from avro to TypeScript */
Expand All @@ -44,9 +45,11 @@ export function convertPrimitive(avroType: string): string {
}

/** Converts an Avro record type to a TypeScript file */
export function avroToTypeScript(recordType: RecordType): string {
export function avroToTypeScript(schema: Schema): string {
const output: string[] = [];
convertRecord(recordType, output);
if (isEnumType(schema)) convertEnum(schema, output);
else if (isRecordType(schema)) convertRecord(schema, output);
else throw "Unknown top level type " + (schema as unknown)["type"];
return output.join("\n");
}

Expand Down
2 changes: 1 addition & 1 deletion src/model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**** Contains the Interfaces and Type Guards for Avro schema */

export interface Schema {}
export type Schema = RecordType | EnumType;

export type Type = NameOrType | NameOrType[];
export type NameOrType = TypeNames | RecordType | ArrayType | NamedType | LogicalType;
Expand Down
5 changes: 5 additions & 0 deletions test/__snapshots__/avtsc.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,8 @@ export interface ExampleType {
}
"
`;

exports[`enumToTypesScript it should generate an enum 1`] = `
"export enum CompanySize { SMALL, MEDIUM, LARGE };
"
`;
20 changes: 19 additions & 1 deletion test/avtsc.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as fs from "fs";
import { avroToTypeScript, RecordType } from "../src";
import { Schema } from "../src/model";

describe("avroToTypeScript", () => {
test("it should generate an interface", () => {
Expand All @@ -9,7 +10,7 @@ describe("avroToTypeScript", () => {
});

test("it should correctly type strings with logicalType", () => {
const schema: RecordType = {
const schema: Schema = {
type: "record",
name: "datedRecord",
fields: [
Expand All @@ -22,6 +23,23 @@ describe("avroToTypeScript", () => {
},
],
};
expect(avroToTypeScript(schema)).not.toEqual(expect.stringContaining("UNKNOWN"));
});
});

describe("enumToTypesScript", () => {
test("it should generate an enum", () => {
const schemaText = fs.readFileSync(__dirname + "/just-enum.avsc", "utf-8");
const schema = JSON.parse(schemaText);
expect(avroToTypeScript(schema as RecordType)).toMatchSnapshot();
});

test("it should correctly type strings with logicalType", () => {
const schema: Schema = {
type: "enum",
name: "CompanySize",
symbols: ["SMALL", "MEDIUM", "LARGE"],
};

expect(avroToTypeScript(schema)).not.toEqual(expect.stringContaining("UNKNOWN"));
});
Expand Down
6 changes: 6 additions & 0 deletions test/just-enum.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "enum",
"name": "CompanySize",
"namespace": "com.example.avro",
"symbols": ["SMALL", "MEDIUM", "LARGE"]
}
7 changes: 0 additions & 7 deletions test/test.js

This file was deleted.

0 comments on commit 40230c1

Please sign in to comment.