Skip to content

Commit cb48d1d

Browse files
authored
feat: add externalUnionsAsInterfaces config (#13)
1 parent b50dafb commit cb48d1d

File tree

18 files changed

+25
-6
lines changed

18 files changed

+25
-6
lines changed

src/config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export const configSchema = object({
3434
* @description If `MyType` depends on `MyDependentType1` and `MyDependentType2`, we can allow `MyDependentType2` to be imported externally by including its import in `extraImports` and omitting it in the `dependentTypesInScope` list: `["MyType", "MyDependentType1"]`
3535
*/
3636
dependentTypesInScope: optional(array(string())),
37+
/**
38+
* Denotes Kotlin classes representing union types to be treated as interfaces rather than annotation classes.
39+
* This should be used for types outside `dependentTypesInScope` that are not generated by the plugin.
40+
*/
41+
externalUnionsAsInterfaces: optional(array(string())),
3742
/**
3843
* Additional imports to add to the generated file.
3944
* @example ["com.example.additional.import.*"]

src/helpers/build-annotations.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ export function buildAnnotations({
5555
const directiveAnnotations = definitionNode
5656
? buildDirectiveAnnotations(definitionNode, config, description)
5757
: "";
58-
const unionAnnotation = resolvedType?.annotation
59-
? `@${resolvedType.annotation}\n`
58+
const unionAnnotation = resolvedType?.unionAnnotation
59+
? `@${resolvedType.unionAnnotation}\n`
6060
: "";
6161

6262
const annotations = [

src/helpers/build-type-metadata.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { CodegenConfig } from "../plugin";
2525

2626
export interface TypeMetadata {
2727
typeName: string;
28-
annotation?: string;
28+
unionAnnotation?: string;
2929
defaultValue: string;
3030
isNullable: boolean;
3131
}
@@ -59,10 +59,17 @@ export function buildTypeMetadata(
5959
typeName: buildListType(typeNode, scalarTypeName),
6060
};
6161
} else if (isUnionType(schemaType)) {
62+
const shouldTreatUnionAsInterface =
63+
config.externalUnionsAsInterfaces?.includes(schemaType.name);
6264
return {
6365
...commonMetadata,
64-
annotation: schemaType.name,
65-
typeName: buildListType(typeNode, "Any"),
66+
unionAnnotation: shouldTreatUnionAsInterface
67+
? undefined
68+
: schemaType.name,
69+
typeName: buildListType(
70+
typeNode,
71+
shouldTreatUnionAsInterface ? schemaType.name : "Any",
72+
),
6673
};
6774
} else {
6875
return {

0 commit comments

Comments
 (0)