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

chore: noStreams skips tally headers in generated source #727

Merged
merged 2 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 5 additions & 5 deletions packages/sdk-codegen-scripts/src/sdkGen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,20 +103,20 @@ export const writeCodeFile = (fileName: string, content: string): string => {
if (gen.useFunctions) {
log(`generating ${api} functions ...`)
const s = new FunctionGenerator(apiModel, gen)
const output = s.render(gen.indentStr)
const output = s.render(gen.indentStr, noStreams)
writeCodeFile(gen.sdkFileName(`funcs`), output)
}

if (gen.useInterfaces) {
log(`generating ${api} interfaces ...`)
const s = new InterfaceGenerator(apiModel, gen)
const output = s.render(gen.indentStr)
const output = s.render(gen.indentStr, noStreams)
writeCodeFile(gen.sdkFileName(`methodsInterface`), output)
}

// Generate standard method declarations
const sdk = new MethodGenerator(apiModel, gen)
let output = sdk.render(gen.indentStr)
let output = sdk.render(gen.indentStr, noStreams)
writeCodeFile(gen.sdkFileName(`methods`), output)

if (gen.willItStream) {
Expand All @@ -126,14 +126,14 @@ export const writeCodeFile = (fileName: string, content: string): string => {
// Generate streaming method declarations
log(`generating ${api} streaming methods ...`)
const s = new StreamGenerator(apiModel, gen)
const output = s.render(gen.indentStr)
const output = s.render(gen.indentStr, noStreams)
writeCodeFile(gen.sdkFileName(`streams`), output)
}
}

log(`generating ${api} models ...`)
const types = new TypeGenerator(apiModel, gen)
output = types.render('')
output = types.render('', noStreams)
writeCodeFile(gen.sdkFileName(`models`), output)
if (api === lastApi) {
formatter.versionStamp(gen)
Expand Down
63 changes: 32 additions & 31 deletions packages/sdk-codegen-scripts/src/sdkGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,27 +53,27 @@ export abstract class Generator<T extends Models.IModel> {
this.codeFormatter = formatter
}

// convenience function that calls render for each item in the list
// and collects their output in the buffer
each<K extends Models.IModel>(
list: Array<K>,
ctor: IGeneratorCtor<K>,
indent = '',
delimiter?: string
): this {
const strs = list.map((model) => {
// eslint-disable-next-line new-cap
return new ctor(model, this.codeFormatter).render(indent)
})
if (delimiter) {
this.p(strs.join(delimiter))
} else {
this.p(strs)
}
return this
}
// // convenience function that calls render for each item in the list
// // and collects their output in the buffer
// each<K extends Models.IModel>(
// list: Array<K>,
// ctor: IGeneratorCtor<K>,
// indent = '',
// delimiter?: string
// ): this {
// const strs = list.map((model) => {
// // eslint-disable-next-line new-cap
// return new ctor(model, this.codeFormatter).render(indent)
// })
// if (delimiter) {
// this.p(strs.join(delimiter))
// } else {
// this.p(strs)
// }
// return this
// }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dead code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I'll remove it in an update


abstract render(indent: string): string
abstract render(indent: string, noStreams: boolean): string

// Add one or more strings to the internal buffer
// if the string is not empty or undefined
Expand All @@ -84,13 +84,6 @@ export abstract class Generator<T extends Models.IModel> {
return this
}

// pIf(expr: any, str?: string | string[]): this {
// if (expr) {
// this.p(str)
// }
// return this
// }

toString(indent: string): string {
return indent + this.buf.join('\n' + indent)
}
Expand All @@ -111,7 +104,7 @@ export class MethodGenerator extends Generator<Models.IApiModel> {
return this.codeFormatter.methodsEpilogue(indent)
}

render(indent: string) {
render(indent: string, noStreams = false) {
this.codeFormatter.reset()
const items: string[] = []
// reset refcounts for ALL types so dynamic import statement will work
Expand Down Expand Up @@ -145,8 +138,12 @@ export class MethodGenerator extends Generator<Models.IApiModel> {
)
}
})
const tally = `${items.length - tagCount} API methods`
let tally = `${items.length - tagCount} API methods`
success(tally)
if (noStreams) {
// Minimize code changes, don't include the tally when not streaming
tally = ''
}
return this.p(this.codeFormatter.commentHeader('', licenseText, noComment))
.p(this.codeFormatter.commentHeader('', tally))
.p(this.prologue(indent))
Expand Down Expand Up @@ -199,7 +196,7 @@ export class FunctionGenerator extends MethodGenerator {
}

export class TypeGenerator extends Generator<Models.IApiModel> {
render(indent: string) {
render(indent: string, noStreams = false) {
this.codeFormatter.reset()
const items: string[] = []
Object.values(this.model.types).forEach((type) => {
Expand All @@ -214,8 +211,12 @@ export class TypeGenerator extends Generator<Models.IApiModel> {
})

const counts = this.typeTally(this.model.types)
const tally = `${counts.total} API models: ${counts.standard} Spec, ${counts.request} Request, ${counts.write} Write, ${counts.enums} Enum`
let tally = `${counts.total} API models: ${counts.standard} Spec, ${counts.request} Request, ${counts.write} Write, ${counts.enums} Enum`
success(tally)
if (noStreams) {
// Minimize code changes, don't include the tally when not streaming
tally = ''
}
return this.p(this.codeFormatter.commentHeader('', licenseText, noComment))
.p(this.codeFormatter.commentHeader('', tally))
.p(this.codeFormatter.modelsPrologue(indent))
Expand Down