-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joe Wood
committed
Sep 8, 2020
1 parent
6dffae1
commit 50a4942
Showing
9 changed files
with
2,558 additions
and
1,073 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"cSpell.words": [ | ||
"Allocs", | ||
"Settl", | ||
"adouble", | ||
"alloc", | ||
"amap", | ||
"astring", | ||
"avsc", | ||
"datetime" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)};`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.