Skip to content

Commit

Permalink
Version 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Wood committed Sep 8, 2020
1 parent 50a4942 commit e7093c8
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 180 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ logs
npm-debug.log*
yarn-debug.log*
yarn-error.log*
/lib
.idea

# Runtime data
pids
Expand Down
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/src
/test
/.vscode
tsconfig.json
4 changes: 0 additions & 4 deletions lib/index.d.ts

This file was deleted.

84 changes: 0 additions & 84 deletions lib/index.js

This file was deleted.

45 changes: 0 additions & 45 deletions lib/model.d.ts

This file was deleted.

37 changes: 0 additions & 37 deletions lib/model.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "avro-typescript",
"version": "0.0.6",
"version": "1.0.0",
"description": "TypeScript code generator for Apache Avro types",
"main": "./lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
31 changes: 22 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import {
Type,
export {
EnumType,
Field,
isRecordType,
isArrayType,
isEnumType,
isMapType,
isOptional,
isRecordType,
RecordType,
Type,
isLogicalType,
} from "./model";

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

/** Convert a primitive type from avro to TypeScript */
function convertPrimitive(avroType: string): string {
export function convertPrimitive(avroType: string): string {
switch (avroType) {
case "long":
case "int":
Expand All @@ -38,7 +51,7 @@ export function avroToTypeScript(recordType: RecordType): string {
}

/** Convert an Avro Record type. Return the name, but add the definition to the file */
function convertRecord(recordType: RecordType, fileBuffer: string[]): string {
export 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";
Expand All @@ -49,13 +62,13 @@ function convertRecord(recordType: RecordType, fileBuffer: string[]): string {
}

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

function convertType(type: Type, buffer: string[]): string {
export function convertType(type: Type, buffer: string[]): string {
// if it's just a name, then use that
if (typeof type === "string") {
return convertPrimitive(type) || type;
Expand Down Expand Up @@ -83,7 +96,7 @@ function convertType(type: Type, buffer: string[]): string {
}
}

function convertFieldDec(field: Field, buffer: string[]): string {
export function convertFieldDec(field: Field, buffer: string[]): string {
// Union Type
return `\t${field.name}${isOptional(field.type) ? "?" : ""}: ${convertType(field.type, buffer)};`;
}

0 comments on commit e7093c8

Please sign in to comment.