Skip to content

Commit

Permalink
Add info to CodegenNativeType to support generators to augment native…
Browse files Browse the repository at this point in the history
… types as required
  • Loading branch information
karlvr committed Aug 22, 2024
1 parent 70ed015 commit a0d868d
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changeset/honest-starfishes-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@openapi-generator-plus/types": minor
"@openapi-generator-plus/core": minor
---

Add info to CodegenNativeType to support generators to augment native types as required
50 changes: 49 additions & 1 deletion packages/core/src/native-type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { CodegenNativeType, CodegenNativeTypeTransformers, CodegenNativeTypeComposers, CodegenNativeTypeComposer } from '@openapi-generator-plus/types'
import { CodegenNativeType, CodegenNativeTypeTransformers, CodegenNativeTypeComposers, CodegenNativeTypeComposer, CodegenNativeTypeInfo } from '@openapi-generator-plus/types'

export class CodegenNativeTypeImpl implements CodegenNativeType {

Expand All @@ -9,13 +9,15 @@ export class CodegenNativeTypeImpl implements CodegenNativeType {
public concreteType: string
public parentType: string
public componentType: CodegenNativeType | null
private infoValue: CodegenNativeTypeInfo | null

public constructor(nativeType: string, additionalTypes?: {
serializedType?: string
literalType?: string
concreteType?: string
parentType?: string
componentType?: CodegenNativeType | null
info?: CodegenNativeTypeInfo
}) {
this.nativeType = nativeType
if (additionalTypes) {
Expand All @@ -24,12 +26,23 @@ export class CodegenNativeTypeImpl implements CodegenNativeType {
this.concreteType = additionalTypes.concreteType !== undefined ? additionalTypes.concreteType : nativeType
this.parentType = additionalTypes.parentType !== undefined ? additionalTypes.parentType : nativeType
this.componentType = additionalTypes.componentType !== undefined ? additionalTypes.componentType : null
this.infoValue = additionalTypes.info !== undefined ? additionalTypes.info : null
} else {
this.serializedType = nativeType
this.literalType = nativeType
this.concreteType = nativeType
this.parentType = nativeType
this.componentType = null
this.infoValue = null
}
}

public get info() {
/* We provide this getter to ensure that our info remains immutable */
if (this.infoValue) {
return { ...this.infoValue }
} else {
return null
}
}

Expand Down Expand Up @@ -107,6 +120,16 @@ export class CodegenTransformingNativeTypeImpl implements CodegenNativeType {
}
}

public get info(): CodegenNativeTypeInfo | null {
const transformer = this.transformers.info !== undefined ? this.transformers.info : (nativeType: CodegenNativeType) => nativeType.info
if (transformer) {
/* We must create a new object to prevent modifications to the wrapped info */
return transformer(this.wrapped)
} else {
return this.wrapped.info
}
}

private get wrapped(): CodegenNativeType {
return this.actualWrapped
}
Expand Down Expand Up @@ -160,6 +183,18 @@ export class CodegenComposingNativeTypeImpl implements CodegenNativeType {
return null
}
}


/**
* The default composing of `info` is to combine left to right.
*/
public get info(): CodegenNativeTypeInfo | null {
if (this.composers.info) {
return this.composers.info(this.wrapped, defaultCodegenNativeTypeInfoComposer)
} else {
return defaultCodegenNativeTypeInfoComposer(this.wrapped)
}
}

private get wrapped(): CodegenNativeType[] {
return this.actualWrapped
Expand All @@ -179,6 +214,19 @@ export class CodegenComposingNativeTypeImpl implements CodegenNativeType {

}

function defaultCodegenNativeTypeInfoComposer(nativeTypes: CodegenNativeType[]): CodegenNativeTypeInfo | null {
let result: CodegenNativeTypeInfo | null = null
for (const nativeType of nativeTypes) {
if (nativeType.info) {
if (!result) {
result = {}
}
Object.assign(result, nativeType.info)
}
}
return result
}

function equalNativeType(a: CodegenNativeType, b: CodegenNativeType | undefined): boolean {
if (!b) {
return false
Expand Down
12 changes: 11 additions & 1 deletion packages/types/src/native-types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { CodegenNativeType } from './types'
import { CodegenNativeType, CodegenNativeTypeInfo } from './types'

export type CodegenNativeTypeStringComposer = (nativeTypeStrings: string[]) => string
export type CodegenNativeTypeComposer = (nativeTypes: CodegenNativeType[]) => string
type DefaultCodegenNativeTypeInfoComposer = (nativeTypes: CodegenNativeType[]) => CodegenNativeTypeInfo | null
export type CodegenNativeTypeInfoComposer = (nativeTypes: CodegenNativeType[], defaultComposer: DefaultCodegenNativeTypeInfoComposer) => CodegenNativeTypeInfo | null

/**
* Transform the given native type.
Expand All @@ -11,6 +13,12 @@ export type CodegenNativeTypeComposer = (nativeTypes: CodegenNativeType[]) => st
*/
export type CodegenNativeTypeTransformer = (nativeType: CodegenNativeType, nativeTypeString: string) => string

/**
* Transform the native type info
* @param nativeType the native type
*/
export type CodegenNativeTypeInfoTransformer = (nativeType: CodegenNativeType) => CodegenNativeTypeInfo | null

export interface CodegenNativeTypeTransformers {
/**
* Implement the default transformer if you don't need to know which CodegenNativeType property is being transformed,
Expand All @@ -29,6 +37,7 @@ export interface CodegenNativeTypeTransformers {
* If undefined, the component type is transformed using this set of transformers.
*/
componentType?: CodegenNativeTypeTransformers | null
info?: CodegenNativeTypeInfoTransformer | null
}

export interface CodegenTransformingNativeTypeConstructor {
Expand All @@ -42,6 +51,7 @@ export interface CodegenNativeTypeComposers {
literalType?: CodegenNativeTypeComposer
parentType?: CodegenNativeTypeComposer
concreteType?: CodegenNativeTypeComposer
info?: CodegenNativeTypeInfoComposer
}

export interface CodegenComposingNativeTypeConstructor {
Expand Down
10 changes: 10 additions & 0 deletions packages/types/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ export interface CodegenNativeTypeConstructor {
literalType?: string
concreteType?: string
componentType?: CodegenNativeType
info?: CodegenNativeTypeInfo
}): CodegenNativeType
}

Expand Down Expand Up @@ -449,12 +450,21 @@ export interface CodegenNativeType {
*/
componentType: CodegenNativeType | null

/**
* Any extra information that a generator template might provide to help with code generation.
*/
info: CodegenNativeTypeInfo | null

toString(): string

equals(other: CodegenNativeType | undefined): boolean

}

export interface CodegenNativeTypeInfo {
[key: string]: unknown
}

export enum CodegenSchemaType {
OBJECT = 'OBJECT',
INTERFACE = 'INTERFACE',
Expand Down

0 comments on commit a0d868d

Please sign in to comment.