Skip to content

Commit

Permalink
Merge pull request #58 from southworks/issue_57
Browse files Browse the repository at this point in the history
Issue 57: GO private constructors must be cammelized
  • Loading branch information
agustinseifert authored Oct 14, 2022
2 parents b378af1 + a27afb6 commit cb41de5
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
8 changes: 2 additions & 6 deletions src/shared/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ import { ElementFactory, Factory, FactoryParams } from "./types/factory";
import { SourceElement } from "./types/source-element";

export abstract class Element<K extends NamedDeclaration> implements SourceElement<K> {
private parent!: SourceElement;
private elementFactory: ElementFactory;
private values: ElementValues = new ElementValues();

public name!: string;
public kind!: ElementKind;
public parent!: SourceElement;

protected constructor(params: FactoryParams) {
this.elementFactory = params.elementFactory;
}

private addElementAndParse(kind: ElementKind, node: Declaration): void {
let element = this.createElement(kind);
element.setParent(this);
element.parent = this;
if (node) {
element.parse(node);
}
Expand Down Expand Up @@ -65,8 +65,4 @@ export abstract class Element<K extends NamedDeclaration> implements SourceEleme
public parse(node: K): void {
this.name = (node.name as Identifier)?.escapedText ?? "";
}

public setParent(element: SourceElement): void {
this.parent = element;
}
}
2 changes: 1 addition & 1 deletion src/shared/types/source-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type SourceElementNamed<T extends Declaration = Declaration> = Parseable<

export interface SourceElement<T extends Declaration = Declaration> extends SourceElementNamed<T> {
kind: ElementKind;
setParent(element: SourceElement): void;
parent: SourceElement;
};

export interface VisibilitySourceElement<T extends Declaration = Declaration> extends SourceElement<T> {
Expand Down
6 changes: 3 additions & 3 deletions src/templating/go/go-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,11 @@ export function getGoHelpers(helpers: TemplateHelper & GoHelpers): GoHelpers {
let type = helpers.mapType(p);
return `${p.name} ${type}`;
}).join(", ");

let result = `func New${helpers.capitalize(v.name)}(${params}) *${helpers.fixName(v)} {`;
let newPrefix = v.visibility == "private" ? "new" : "New";
let result = `func ${newPrefix}${helpers.capitalize(v.name)}(${params}) *${helpers.fixName(v.parent as VisibilitySourceElement)} {`;

const firstLetter = v.name.charAt(0).toLowerCase();
const init = `${firstLetter} := ${helpers.fixName(v)}{}`;
const init = `${firstLetter} := ${helpers.fixName(v.parent as VisibilitySourceElement)}{}`;
result = `${result}\n\t${init}`;
result = `${result}${helpers.printMethodBody(v)}`;
result = `${result}\n\treturn &${firstLetter}`;
Expand Down
26 changes: 26 additions & 0 deletions src/test/go/constructor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,30 @@ describe("GO: constructor", () => {

expect(strWritter.getString()).toBe(expected.getString());
});

test("private", () => {
const code = `
export class Test {
private constructor() {
}
}`;
let compilationResult = compileTypeScriptCode(code, "test.ts");

const strWritter = new StringWritter();
printFile(compilationResult, new GoGenerator(), strWritter);

const expected = new StringWritter();
expected.write(`package test`);
expected.write("");
expected.write("type Test struct {");
expected.write("}");
expected.write("");
expected.write("func newTest() *Test {");
expected.write("\tt := Test{}");
expected.write("\treturn &t");
expected.write("}");
expected.write("");

expect(strWritter.getString()).toBe(expected.getString());
});
});

0 comments on commit cb41de5

Please sign in to comment.