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

Add ignore namespace option #153

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
23 changes: 14 additions & 9 deletions packages/avro-ts/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ import { fullName, firstUpperCase, nameParts, convertName } from './helpers';
import * as ts from 'typescript';
import { convertNamedType, isNamedType } from './types/named-type';

export const addRef = (type: schema.RecordType | schema.EnumType, context: Context): Context => ({
...context,
namespace: type.namespace ?? context.namespace,
refs: {
...context.refs,
[fullName(context, type)]: { ...type, namespace: type.namespace ?? context.namespace },
},
});
export const addRef = (type: schema.RecordType | schema.EnumType, context: Context): Context => {
if (context.ignoreNamespaces) {
delete type.namespace;
}
return {
...context,
namespace: type.namespace ?? context.namespace,
refs: {
...context.refs,
[fullName(context, type)]: { ...type, namespace: type.namespace ?? context.namespace },
},
};
};

export const collectRefs = (type: Schema, context: Context): Context => {
if (isUnion(type)) {
Expand Down Expand Up @@ -63,7 +68,7 @@ export const convertType: Convert = (context, type) => {
return convertNamedType(context, type);
} else if (typeof type === 'string') {
const [name, nameNamespace] = nameParts(type);
const namespace = nameNamespace ?? context.namespace;
const namespace = !context.ignoreNamespaces ? nameNamespace ?? context.namespace : undefined;

if (namespace && context.external && !context.refs?.[type]) {
for (const module in context.external) {
Expand Down
1 change: 1 addition & 0 deletions packages/avro-ts/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface Context extends DocumentContext {
external?: { [file: string]: { [key: string]: Schema } };
defaultsAsOptional?: boolean;
withTypescriptEnums?: boolean;
ignoreNamespaces?: boolean;
}

export type Convert<TSchema = Schema, TType = ts.TypeNode> = (
Expand Down
Loading