Skip to content

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Wood committed Sep 8, 2020
1 parent 6dffae1 commit 50a4942
Show file tree
Hide file tree
Showing 9 changed files with 2,558 additions and 1,073 deletions.
12 changes: 12 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"cSpell.words": [
"Allocs",
"Settl",
"adouble",
"alloc",
"amap",
"astring",
"avsc",
"datetime"
]
}
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.avroToTypeScript = void 0;
var model_1 = require("./model");
/** Convert a primitive type from avro to TypeScript */
function convertPrimitive(avroType) {
Expand Down
3 changes: 2 additions & 1 deletion lib/model.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";
/**** Contains the Interfaces and Type Guards for Avro schema */
Object.defineProperty(exports, "__esModule", { value: true });
exports.isLogicalType = exports.isOptional = exports.isUnion = exports.isEnumType = exports.isMapType = exports.isArrayType = exports.isRecordType = void 0;
function isRecordType(type) {
return type.type === "record";
}
Expand Down Expand Up @@ -31,6 +32,6 @@ function isOptional(type) {
}
exports.isOptional = isOptional;
function isLogicalType(type) {
return typeof type !== 'string' && 'logicalType' in type;
return typeof type !== "string" && "logicalType" in type;
}
exports.isLogicalType = isLogicalType;
3,193 changes: 2,324 additions & 869 deletions package-lock.json

Large diffs are not rendered by default.

96 changes: 48 additions & 48 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
{
"name": "avro-typescript",
"version": "0.0.5",
"description": "TypeSript code generator for Apache Avro types",
"main": "./lib/index.js",
"typings": "lib/index.d.ts",
"files": [
"lib"
],
"directories": {
"test": "test"
},
"scripts": {
"test": "jest",
"build": "tsc"
},
"repository": {
"type": "git",
"url": "git+https://github.com/joewood/avro-typescript.git"
},
"readme": "READMD.md",
"types": "./lib/index.d.ts",
"keywords": [
"avro",
"typescript"
],
"author": "Joe Wood",
"license": "MIT",
"bugs": {
"url": "https://github.com/joewood/avro-typescript/issues"
},
"homepage": "https://github.com/joewood/avro-typescript#readme",
"devDependencies": {
"@types/jest": "^25.1.4",
"@types/node": "^7.0.22",
"jest": "^25.2.4",
"ts-jest": "^25.2.1",
"typescript": "^3.8.3"
},
"jest": {
"transform": {
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
"name": "avro-typescript",
"version": "0.0.6",
"description": "TypeScript code generator for Apache Avro types",
"main": "./lib/index.js",
"typings": "lib/index.d.ts",
"files": [
"lib"
],
"directories": {
"test": "test"
},
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
]
}
"scripts": {
"test": "jest",
"build": "tsc"
},
"repository": {
"type": "git",
"url": "git+https://github.com/joewood/avro-typescript.git"
},
"readme": "README.md",
"types": "./lib/index.d.ts",
"keywords": [
"avro",
"typescript"
],
"author": "Joe Wood",
"license": "MIT",
"bugs": {
"url": "https://github.com/joewood/avro-typescript/issues"
},
"homepage": "https://github.com/joewood/avro-typescript#readme",
"devDependencies": {
"@types/jest": "^26.0.13",
"@types/node": "^14.6.4",
"jest": "^26.4.2",
"ts-jest": "^26.3.0",
"typescript": "^4.0.2"
},
"jest": {
"transform": {
".(ts|tsx)": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"testRegex": "(/__tests__/.*|\\.(test|spec))\\.(ts|tsx|js)$",
"moduleFileExtensions": [
"ts",
"tsx",
"js"
]
}
}
130 changes: 65 additions & 65 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,89 @@
import {
Type,
Field,
isRecordType,
isArrayType,
isEnumType,
isMapType,
RecordType,
EnumType,
isOptional,
isLogicalType
Type,
Field,
isRecordType,
isArrayType,
isEnumType,
isMapType,
RecordType,
EnumType,
isOptional,
isLogicalType,
} from "./model";
export { RecordType, Field } from "./model";
/** Convert a primitive type from avro to TypeScript */
function convertPrimitive(avroType: string): string {
switch (avroType) {
case "long":
case "int":
case "double":
case "float":
return "number";
case "bytes":
return "Buffer";
case "null":
return "null | undefined";
case "boolean":
return "boolean";
default:
return null;
}
switch (avroType) {
case "long":
case "int":
case "double":
case "float":
return "number";
case "bytes":
return "Buffer";
case "null":
return "null | undefined";
case "boolean":
return "boolean";
default:
return null;
}
}

/** Converts an Avro record type to a TypeScript file */
export function avroToTypeScript(recordType: RecordType): string {
const output: string[] = [];
convertRecord(recordType, output);
return output.join("\n");
const output: string[] = [];
convertRecord(recordType, output);
return output.join("\n");
}

/** Convert an Avro Record type. Return the name, but add the definition to the file */
function convertRecord(recordType: RecordType, fileBuffer: string[]): string {
let buffer = `export interface ${recordType.name} {\n`;
for (let field of recordType.fields) {
buffer += convertFieldDec(field, fileBuffer) + "\n";
}
buffer += "}\n";
fileBuffer.push(buffer);
return recordType.name;
let buffer = `export interface ${recordType.name} {\n`;
for (let field of recordType.fields) {
buffer += convertFieldDec(field, fileBuffer) + "\n";
}
buffer += "}\n";
fileBuffer.push(buffer);
return recordType.name;
}

/** Convert an Avro Enum type. Return the name, but add the definition to the file */
function convertEnum(enumType: EnumType, fileBuffer: string[]): string {
const enumDef = `export enum ${enumType.name} { ${enumType.symbols.join(", ")} };\n`;
fileBuffer.push(enumDef);
return enumType.name;
const enumDef = `export enum ${enumType.name} { ${enumType.symbols.join(", ")} };\n`;
fileBuffer.push(enumDef);
return enumType.name;
}

function convertType(type: Type, buffer: string[]): string {
// if it's just a name, then use that
if (typeof type === "string") {
return convertPrimitive(type) || type;
} else if (type instanceof Array) {
// array means a Union. Use the names and call recursively
return type.map(t => convertType(t, buffer)).join(" | ");
} else if (isRecordType(type)) {
//} type)) {
// record, use the name and add to the buffer
return convertRecord(type, buffer);
} else if (isArrayType(type)) {
// array, call recursively for the array element type
return convertType(type.items, buffer) + "[]";
} else if (isMapType(type)) {
// Dictionary of types, string as key
return `{ [index:string]:${convertType(type.values, buffer)} }`;
} else if (isEnumType(type)) {
// array, call recursively for the array element type
return convertEnum(type, buffer);
} else if (isLogicalType(type)) {
return convertType(type.type, buffer)
} else {
console.error("Cannot work out type", type);
return "UNKNOWN";
}
// if it's just a name, then use that
if (typeof type === "string") {
return convertPrimitive(type) || type;
} else if (type instanceof Array) {
// array means a Union. Use the names and call recursively
return type.map((t) => convertType(t, buffer)).join(" | ");
} else if (isRecordType(type)) {
//} type)) {
// record, use the name and add to the buffer
return convertRecord(type, buffer);
} else if (isArrayType(type)) {
// array, call recursively for the array element type
return convertType(type.items, buffer) + "[]";
} else if (isMapType(type)) {
// Dictionary of types, string as key
return `{ [index:string]:${convertType(type.values, buffer)} }`;
} else if (isEnumType(type)) {
// array, call recursively for the array element type
return convertEnum(type, buffer);
} else if (isLogicalType(type)) {
return convertType(type.type, buffer);
} else {
console.error("Cannot work out type", type);
return "UNKNOWN";
}
}

function convertFieldDec(field: Field, buffer: string[]): string {
// Union Type
return `\t${field.name}${isOptional(field.type) ? "?" : ""}: ${convertType(field.type, buffer)};`;
// Union Type
return `\t${field.name}${isOptional(field.type) ? "?" : ""}: ${convertType(field.type, buffer)};`;
}
56 changes: 28 additions & 28 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,74 +7,74 @@ export type NameOrType = TypeNames | RecordType | ArrayType | NamedType | Logica
export type TypeNames = "record" | "array" | "null" | "map" | string;

export interface Field {
name: string;
type: Type;
default?: string | number | null | boolean;
name: string;
type: Type;
default?: string | number | null | boolean;
}

export interface BaseType {
type: TypeNames;
type: TypeNames;
}

export interface RecordType extends BaseType {
type: "record";
name: string;
fields: Field[];
type: "record";
name: string;
fields: Field[];
}

export interface ArrayType extends BaseType {
type: "array";
items: Type;
type: "array";
items: Type;
}

export interface MapType extends BaseType {
type: "map";
values: Type;
type: "map";
values: Type;
}

export interface EnumType extends BaseType {
type: "enum";
name: string;
symbols: string[];
type: "enum";
name: string;
symbols: string[];
}

export interface NamedType extends BaseType {
type: string;
type: string;
}

export interface LogicalType extends BaseType {
logicalType: string
logicalType: string;
}

export function isRecordType(type: BaseType): type is RecordType {
return type.type === "record";
return type.type === "record";
}

export function isArrayType(type: BaseType): type is ArrayType {
return type.type === "array";
return type.type === "array";
}

export function isMapType(type: BaseType): type is MapType {
return type.type === "map";
return type.type === "map";
}

export function isEnumType(type: BaseType): type is EnumType {
return type.type === "enum";
return type.type === "enum";
}

export function isUnion(type: Type): type is NamedType[] {
return type instanceof Array;
return type instanceof Array;
}

export function isOptional(type: Type): boolean {
if (isUnion(type)) {
const t1 = type[0];
if (typeof t1 === "string") {
return t1 === "null";
}
}
if (isUnion(type)) {
const t1 = type[0];
if (typeof t1 === "string") {
return t1 === "null";
}
}
}

export function isLogicalType(type: Type): type is LogicalType {
return typeof type !== 'string' && 'logicalType' in type
return typeof type !== "string" && "logicalType" in type;
}
Loading

0 comments on commit 50a4942

Please sign in to comment.