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: enabled data tags, description preset for Go, add -i flag #2123

Open
wants to merge 4 commits into
base: next
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
18 changes: 16 additions & 2 deletions modelina-cli/src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,23 @@ export default class Models extends ModelinaCommand {
try {
document = await readFile(file, 'utf8');
} catch {
throw new Error('Unable to read input file content.');
if (flags.input) {
try {
let fname = ""
if (flags.input) {
fname = flags.input
}

document = await readFile(fname, 'utf8');
} catch {
throw new Error(`Unable to read input file content: ${flags.input}`);
}
}
else {
throw new Error(`Unable to read input file content: ${file}`);
}
}

const logger = {
info: (message: string) => {
this.log(message);
Expand Down
5 changes: 5 additions & 0 deletions modelina-cli/src/helpers/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ export const ModelinaFlags = {
description: 'The output directory where the models should be written to. Omitting this flag will write the models to `stdout`.',
required: false
}),
input: Flags.string({
char: 'i',
description: 'The input file with the API definition.',
required: false
}),
/**
* Go and Java specific package name to use for the generated models
*/
Expand Down
21 changes: 16 additions & 5 deletions modelina-cli/src/helpers/go.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { GoFileGenerator } from "@asyncapi/modelina";
import { GO_DESCRIPTION_PRESET, GO_COMMON_PRESET, GoCommonPresetOptions, GoFileGenerator } from "@asyncapi/modelina";
import { BuilderReturnType } from "./generate";
import { Flags } from "@oclif/core";

export const GoOclifFlags = { }
export const GoOclifFlags = {
goIncludeComments: Flags.boolean({
description: 'Golang specific, if enabled add comments while generating models.',
required: false,
default: false,
}),
}

/**
* This function builds all the relevant information for the main generate command
Expand All @@ -10,13 +17,17 @@ export const GoOclifFlags = { }
* @returns
*/
export function buildGoGenerator(flags: any): BuilderReturnType {
const { packageName } = flags;
const { packageName, goIncludeComments } = flags;

if (packageName === undefined) {
throw new Error('In order to generate models to Go, we need to know which package they are under. Add `--packageName=PACKAGENAME` to set the desired package name.');
}

const fileGenerator = new GoFileGenerator();
const presets = []
const options: GoCommonPresetOptions = { addJsonTag: true };
presets.push({ preset: GO_COMMON_PRESET, options })
if (goIncludeComments) { presets.push(GO_DESCRIPTION_PRESET); }
const fileGenerator = new GoFileGenerator({ presets });
const fileOptions = {
packageName
};
Expand Down
3 changes: 3 additions & 0 deletions src/generators/go/presets/CommonPreset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ function renderJSONTag({
) {
return `json:"-,omitempty"`;
}
if (field.required) {
return `json:"${field.unconstrainedPropertyName}" binding:"required"`;
}
return `json:"${field.unconstrainedPropertyName},omitempty"`;
}

Expand Down
42 changes: 42 additions & 0 deletions src/generators/go/presets/DescriptionPreset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { ConstrainedMetaModel } from '../../../models';
import { GoPreset } from '../GoPreset';
import { GoRenderer } from '../GoRenderer';

const renderDescription = ({
renderer,
content,
item
}: {
renderer: GoRenderer<ConstrainedMetaModel>;
content: string;
item: ConstrainedMetaModel;
}): string => {
const desc = item.originalInput.description?.trim();
let formattedDesc = '';
if (desc) {
formattedDesc = renderer.renderComments(desc);
formattedDesc += '\n';
}
return formattedDesc + content;
};

/**
* Preset which adds descriptions
*
* @implements {GoPreset}
*/
export const GO_DESCRIPTION_PRESET: GoPreset = {
struct: {
self({ renderer, model, content }) {
return renderDescription({ renderer, content, item: model });
},
field({ renderer, field, content }) {
return renderDescription({ renderer, content, item: field.property });
}
},
enum: {
self({ renderer, model, content }) {
return renderDescription({ renderer, content, item: model });
}
}
};
1 change: 1 addition & 0 deletions src/generators/go/presets/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './CommonPreset';
export * from './DescriptionPreset';
2 changes: 1 addition & 1 deletion src/generators/go/renderers/EnumRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { GoOptions } from '../GoGenerator';
*/
export class EnumRenderer extends GoRenderer<ConstrainedEnumModel> {
public async defaultSelf(): Promise<string> {
const doc = this.renderCommentForEnumType(this.model.name, this.model.type);
const doc = '';
const enumValues = await this.renderItems();
const valuesToEnumMap = this.model.values.map((value) => {
return `${this.model.name}Values[${value.key}]: ${value.key},`;
Expand Down
22 changes: 8 additions & 14 deletions src/generators/go/renderers/StructRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,21 @@ export class StructRenderer extends GoRenderer<ConstrainedObjectModel> {
await this.renderFields(),
await this.runAdditionalContentPreset()
];

const doc = this.renderComments(
`${this.model.name} represents a ${this.model.name} model.`
);

const doc = '';
Copy link
Member

Choose a reason for hiding this comment

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

You can remove it completely if it's no longer in use 🙂

let discriminator = '';

if (this.model.options.parents?.length) {
discriminator = await this.runDiscriminatorFuncPreset();
}

return `${doc}
type ${this.model.name} struct {
${this.indent(this.renderBlock(content, 2))}
}${
discriminator &&
type ${this.model.name} struct {
${this.indent(this.renderBlock(content, 2))}
}${discriminator &&
`

${discriminator}
`
}`;
${discriminator}
`
}`;
}

async renderFields(): Promise<string> {
Expand Down
5 changes: 1 addition & 4 deletions src/generators/go/renderers/UnionRenderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ const unionIncludesDiscriminator = (model: ConstrainedUnionModel): boolean => {
*/
export class UnionRenderer extends GoRenderer<ConstrainedUnionModel> {
public async defaultSelf(): Promise<string> {
const doc = this.renderComments(
`${this.model.name} represents a ${this.model.name} model.`
);

const doc = '';
if (unionIncludesDiscriminator(this.model)) {
const content: string[] = [await this.runDiscriminatorAccessorPreset()];

Expand Down
Loading