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

feat: add enum support for js, ts #60

Merged
merged 3 commits into from
Sep 5, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion src/generators/android/android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ export const android: Generator<
generatePrimitive: async (client, schema, parentPath) => {
let type = 'Object';

if (schema.type === Type.STRING) {
if (schema.enum) {
type = schema.enum.map((e) => (typeof e === 'string' ? `'${e}'` : `${e}`)).join(' | ');
saikumarrs marked this conversation as resolved.
Show resolved Hide resolved
} else if (schema.type === Type.STRING) {
type = 'String';
} else if (schema.type === Type.BOOLEAN) {
type = 'Boolean';
Expand Down
10 changes: 7 additions & 3 deletions src/generators/gen.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JSONSchema7 } from 'json-schema';
import { parse, Schema, getPropertiesSchema, Type } from './ast.js';
import { parse, Schema, getPropertiesSchema, Type, PrimitiveTypeSchema } from './ast.js';
import { javascript } from './javascript/index.js';
import { objc } from './objc/index.js';
import { swift } from './swift/index.js';
Expand Down Expand Up @@ -100,7 +100,11 @@ export declare type Generator<
> = {
namer: NamerOptions;
setup: (options: GenOptions) => Promise<R>;
generatePrimitive: (client: GeneratorClient, schema: Schema, parentPath: string) => Promise<P>;
generatePrimitive: (
client: GeneratorClient,
schema: PrimitiveTypeSchema,
parentPath: string,
) => Promise<P>;
generateArray: (
client: GeneratorClient,
schema: Schema,
Expand Down Expand Up @@ -255,7 +259,7 @@ async function runGenerator<
let p: P;
if ([Type.ANY, Type.STRING, Type.BOOLEAN, Type.INTEGER, Type.NUMBER].includes(schema.type)) {
// Primitives are any type that doesn't require generating a "subtype".
p = await generator.generatePrimitive(client, schema, parentPath);
p = await generator.generatePrimitive(client, schema as PrimitiveTypeSchema, parentPath);
} else if (schema.type === Type.OBJECT) {
// For objects, we need to recursively generate each property first.
const properties: (P & BasePropertyContext)[] = [];
Expand Down
4 changes: 3 additions & 1 deletion src/generators/javascript/javascript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,9 @@ export const javascript: Generator<
},
generatePrimitive: async (client, schema) => {
let type = 'any';
if (schema.type === Type.STRING) {
if (schema.enum) {
type = schema.enum.map((e) => (typeof e === 'string' ? `'${e}'` : `${e}`)).join(' | ');
} else if (schema.type === Type.STRING) {
type = 'string';
} else if (schema.type === Type.BOOLEAN) {
type = 'boolean';
Expand Down
Loading