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(golang): introduce Golang code generation #1551

Merged
merged 60 commits into from
Aug 21, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
75988da
feat(golang): introduce Golang code generation
RomainMuller Apr 15, 2020
ffbaf86
Merge remote-tracking branch 'origin/master' into golang/code-generation
RomainMuller May 14, 2020
48337ae
Merge remote-tracking branch 'origin/master' into golang/code-generation
RomainMuller May 26, 2020
3785aae
chore: do not use underscores in pkg name
SoManyHs May 29, 2020
7c0a7aa
feat: (wip) generate package and submodules
SoManyHs May 29, 2020
b904706
Start Interface Type Emission
MrArnoldPalmer Jun 10, 2020
4e8977c
Merge branch 'master' of github.com:aws/jsii into golang/code-generation
MrArnoldPalmer Jun 11, 2020
20f1e88
"Start Class Generation"
MrArnoldPalmer Jun 12, 2020
6066285
wip add iface properties as getters
SoManyHs Jun 11, 2020
e311dcd
refactor: move emit logic into interface.go, change localName
SoManyHs Jun 19, 2020
3f26997
chore: remove extra space before function return
SoManyHs Jun 19, 2020
51e7e3f
chore: remove extra space before function return
SoManyHs Jun 19, 2020
a5f7a05
Create Module/Submodule Class
MrArnoldPalmer Jun 29, 2020
121383e
Merge branch 'golang/code-generation' of github.com:aws/jsii into gol…
SoManyHs Jun 29, 2020
586d037
test: run yarn test:update
SoManyHs Jul 1, 2020
ad04878
fix: use camelcase on exported types
SoManyHs Jul 1, 2020
ef1a36b
chore: fix linting and build issues
SoManyHs Jul 28, 2020
4aff177
chore: ran yarn test:update
SoManyHs Jul 29, 2020
99b0aea
Merge branch 'master' into golang/code-generation
SoManyHs Jul 30, 2020
344be72
Merge branch 'master' into golang/code-generation
SoManyHs Aug 5, 2020
cf6927d
Merge branch 'master' into golang/code-generation
SoManyHs Aug 6, 2020
9f1583b
Merge branch 'master' into golang/code-generation
SoManyHs Aug 10, 2020
b3b3e3c
Merge branch 'master' into golang/code-generation
SoManyHs Aug 11, 2020
beff382
chore: update test snapshot
SoManyHs Aug 11, 2020
6e26d1c
Merge branch 'master' into golang/code-generation
SoManyHs Aug 13, 2020
2ce497b
chore: fix typo
SoManyHs Aug 13, 2020
abb52ec
feat(jsii-pacmak): implement enum#emit for go
SoManyHs Aug 13, 2020
0b18bca
chore: use lowercase variable for method signatures
SoManyHs Aug 13, 2020
2f56d2f
chore: use pascal case for method names
SoManyHs Aug 14, 2020
2c3e733
wip generate interfaces for classes, uppercase props
SoManyHs Aug 14, 2020
20ab90c
chore: fix case of interface methods
SoManyHs Aug 14, 2020
7d48a04
Merge branch 'master' of github.com:aws/jsii into golang/code-generation
MrArnoldPalmer Aug 14, 2020
ac5c9fd
fix breaking codemaker test
MrArnoldPalmer Aug 14, 2020
d5f0e91
update pacmak snapshot
MrArnoldPalmer Aug 14, 2020
61dc2f2
chore: update pacmak snapshot
SoManyHs Aug 15, 2020
cfe4308
Merge branch 'master' into golang/code-generation
SoManyHs Aug 17, 2020
98d1838
chore: delete unused interface file
SoManyHs Aug 17, 2020
6a8ff76
chore: remove unused test expectations
SoManyHs Aug 17, 2020
6453b80
chore: go namespace aware types
MrArnoldPalmer Aug 14, 2020
7cbb109
fix: go package compile errors
MrArnoldPalmer Aug 17, 2020
416f0b6
chore: generate datatype interfaces
SoManyHs Aug 18, 2020
0db0dbb
chore: pr feedback
MrArnoldPalmer Aug 18, 2020
251d7b1
chore: minimize interface changes
MrArnoldPalmer Aug 18, 2020
19a12b9
chore: replace `forEach` with for (const x of...)
MrArnoldPalmer Aug 18, 2020
4997976
chore: include generation of struct methods
SoManyHs Aug 18, 2020
572f151
Merge branch 'golang/code-generation' of github.com:aws/jsii into gol…
MrArnoldPalmer Aug 18, 2020
2004385
chore: update snapshot
SoManyHs Aug 19, 2020
cc350c1
chore: fix interface generation for datatypes
SoManyHs Aug 19, 2020
2234cb1
Merge branch 'master' into golang/code-generation
SoManyHs Aug 19, 2020
4784aa8
chore: constantize struct interface suffix
SoManyHs Aug 19, 2020
1b6baaf
update snapshot
MrArnoldPalmer Aug 19, 2020
34ca6ef
Merge branch 'master' into golang/code-generation
SoManyHs Aug 19, 2020
3577d61
chore: rename Submodule -> InternalPackage
SoManyHs Aug 19, 2020
cc38941
docs: add note about experimental go configuration
MrArnoldPalmer Aug 19, 2020
fe5c165
chore: rename Module -> BasePackage
SoManyHs Aug 19, 2020
dc3d70f
chore: rename goModuleName -> goPackageName
SoManyHs Aug 20, 2020
917fac9
chore: rename Module, Submodule (#1921)
SoManyHs Aug 20, 2020
0e21021
Merge branch 'golang/code-generation' of github.com:aws/jsii into gol…
SoManyHs Aug 20, 2020
4238e2a
chore: rename Package, again
SoManyHs Aug 20, 2020
541d714
fix(go): remove runtime embed
MrArnoldPalmer Aug 20, 2020
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
2 changes: 2 additions & 0 deletions packages/@jsii/kernel/test/kernel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ defineTest(
namespace: 'Amazon.JSII.Tests.CalculatorNamespace',
packageId: 'Amazon.JSII.Tests.CalculatorPackageId',
},
go: {},
java: {
package: 'software.amazon.jsii.tests.calculator',
maven: {
Expand All @@ -388,6 +389,7 @@ defineTest(
packageId: 'Amazon.JSII.Tests.CalculatorPackageId.LibPackageId',
versionSuffix: '-devpreview',
},
go: {},
java: {
package: 'software.amazon.jsii.tests.calculator.lib',
maven: {
Expand Down
2 changes: 2 additions & 0 deletions packages/@scope/jsii-calc-base-of-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"types": "lib/index.d.ts",
"scripts": {
"build": "jsii --project-references && jsii-rosetta",
"pacmak": "jsii-pacmak",
"test": "diff-test test/assembly.jsii .jsii",
"test:update": "npm run build && UPDATE_DIFF=1 npm run test"
},
Expand All @@ -40,6 +41,7 @@
"jsii": {
"outdir": "dist",
"targets": {
"go": {},
"java": {
"package": "software.amazon.jsii.tests.calculator.baseofbase",
"maven": {
Expand Down
3 changes: 2 additions & 1 deletion packages/@scope/jsii-calc-base-of-base/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseOfBaseNamespace",
"packageId": "Amazon.JSII.Tests.CalculatorPackageId.BaseOfBasePackageId"
},
"go": {},
"java": {
"maven": {
"artifactId": "calculator-base-of-base",
Expand Down Expand Up @@ -111,5 +112,5 @@
}
},
"version": "0.0.0",
"fingerprint": "v3mwSbKGQ2aa8g0DEKIeaEXh3csX7PX2MJJxHVRufhI="
"fingerprint": "QA35s5WeGGn1u/fckcbOi4pFcmI9m9oTSYs2cq+ewpw="
}
1 change: 1 addition & 0 deletions packages/@scope/jsii-calc-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"jsii": {
"outdir": "dist",
"targets": {
"go": {},
"java": {
"package": "software.amazon.jsii.tests.calculator.base",
"maven": {
Expand Down
4 changes: 3 additions & 1 deletion packages/@scope/jsii-calc-base/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseOfBaseNamespace",
"packageId": "Amazon.JSII.Tests.CalculatorPackageId.BaseOfBasePackageId"
},
"go": {},
"java": {
"maven": {
"artifactId": "calculator-base-of-base",
Expand Down Expand Up @@ -50,6 +51,7 @@
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseNamespace",
"packageId": "Amazon.JSII.Tests.CalculatorPackageId.BasePackageId"
},
"go": {},
"java": {
"maven": {
"artifactId": "calculator-base",
Expand Down Expand Up @@ -151,5 +153,5 @@
}
},
"version": "0.0.0",
"fingerprint": "ttHhQ5EzgjW0lybCNSKOfqilJ04kp4VK3arcpRl3rBM="
"fingerprint": "JCkFuFqry/njMLNi/IpfTVdkVye/esUFOCoZoFbDD+4="
}
1 change: 1 addition & 0 deletions packages/@scope/jsii-calc-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"jsii": {
"outdir": "dist",
"targets": {
"go": {},
"java": {
"package": "software.amazon.jsii.tests.calculator.lib",
"maven": {
Expand Down
5 changes: 4 additions & 1 deletion packages/@scope/jsii-calc-lib/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseNamespace",
"packageId": "Amazon.JSII.Tests.CalculatorPackageId.BasePackageId"
},
"go": {},
"java": {
"maven": {
"artifactId": "calculator-base",
Expand All @@ -40,6 +41,7 @@
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseOfBaseNamespace",
"packageId": "Amazon.JSII.Tests.CalculatorPackageId.BaseOfBasePackageId"
},
"go": {},
"java": {
"maven": {
"artifactId": "calculator-base-of-base",
Expand Down Expand Up @@ -97,6 +99,7 @@
"packageId": "Amazon.JSII.Tests.CalculatorPackageId.LibPackageId",
"versionSuffix": "-devpreview"
},
"go": {},
"java": {
"maven": {
"artifactId": "calculator-lib",
Expand Down Expand Up @@ -668,5 +671,5 @@
}
},
"version": "0.0.0",
"fingerprint": "w6/8Kda6/JitSbpMK3LGPK5JGRxJ50gflR5S4sUEHKk="
"fingerprint": "/uziR97y1c4zIKVBkWC6j4EbxYLKuCC4ePZXhOMMXBE="
}
1 change: 1 addition & 0 deletions packages/jsii-calc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"jsii": {
"outdir": "dist",
"targets": {
"go": {},
"java": {
"package": "software.amazon.jsii.tests.calculator",
"maven": {
Expand Down
6 changes: 5 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseNamespace",
"packageId": "Amazon.JSII.Tests.CalculatorPackageId.BasePackageId"
},
"go": {},
"java": {
"maven": {
"artifactId": "calculator-base",
Expand All @@ -67,6 +68,7 @@
"namespace": "Amazon.JSII.Tests.CalculatorNamespace.BaseOfBaseNamespace",
"packageId": "Amazon.JSII.Tests.CalculatorPackageId.BaseOfBasePackageId"
},
"go": {},
"java": {
"maven": {
"artifactId": "calculator-base-of-base",
Expand Down Expand Up @@ -109,6 +111,7 @@
"packageId": "Amazon.JSII.Tests.CalculatorPackageId.LibPackageId",
"versionSuffix": "-devpreview"
},
"go": {},
"java": {
"maven": {
"artifactId": "calculator-lib",
Expand Down Expand Up @@ -230,6 +233,7 @@
"namespace": "Amazon.JSII.Tests.CalculatorNamespace",
"packageId": "Amazon.JSII.Tests.CalculatorPackageId"
},
"go": {},
"java": {
"maven": {
"artifactId": "calculator",
Expand Down Expand Up @@ -13162,5 +13166,5 @@
}
},
"version": "0.0.0",
"fingerprint": "miVjqwWxNOLMY7fR23c/SVvfiGqkgqLgG+18qp4f8MM="
"fingerprint": "WdFD02mRrBZaKXT1erhd6fi+1V5/bpaeGuNZWxNuRdk="
}
4 changes: 4 additions & 0 deletions packages/jsii-pacmak/lib/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export interface GeneratorOptions {
}

export interface IGenerator {
/**
*
* @param fingerprint
*/
generate(fingerprint: boolean): void;

/**
Expand Down
96 changes: 96 additions & 0 deletions packages/jsii-pacmak/lib/targets/golang.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { CodeMaker } from 'codemaker';
import * as fs from 'fs-extra';
import { Assembly } from 'jsii-reflect';
import { Rosetta } from 'jsii-rosetta';
import * as path from 'path';
import { Package } from './golang/package';
import { IGenerator } from '../generator';
import { Target, TargetOptions } from '../target';

export class Golang extends Target {
public readonly generator: IGenerator;

public constructor(options: TargetOptions) {
super(options);

this.generator = new GolangGenerator(options.rosetta);
}

/**
* Generates a publishable artifact in `outDir`.
*
* @param sourceDir the directory where the generated source is located.
* @param outDir the directory where the publishable artifact should be placed.
*/
public async build(sourceDir: string, outDir: string): Promise<void> {
return this.copyFiles(sourceDir, outDir);
}
}

class GolangGenerator implements IGenerator {
private assembly!: Assembly;
private readonly code = new CodeMaker();

public constructor(private readonly rosetta: Rosetta) {}

public async load(_: string, assembly: Assembly): Promise<void> {
this.assembly = assembly;
return Promise.resolve();
}

public async upToDate(_outDir: string) {
return Promise.resolve(false);
}

public generate(): void {
new Package(this.assembly).emit({ code: this.code, rosetta: this.rosetta });
}

public async save(outDir: string, tarball: string): Promise<any> {
const bundledRuntimeDotGo = path.join('_jsii', 'bundled-runtime.go');
this.code.openFile(bundledRuntimeDotGo);

this.code.line(
`// Embedded data for the tarball containing the runtime of ${this.assembly.name}@${this.assembly.version}`,
);
this.code.openBlock('const tarball = []byte');
for await (const line of encodedSlices(tarball)) {
this.code.line(`${line},`);
}
this.code.closeBlock();

this.code.closeFile(bundledRuntimeDotGo);

await this.code.save(outDir);
}
}

async function* encodedSlices(path: string, sliceSize = 16) {
const slice = Buffer.alloc(sliceSize);

const fd = await fs.open(path, fs.constants.O_RDONLY);

while (true) {
// eslint-disable-next-line no-await-in-loop
const { bytesRead } = await fs.read(fd, slice, 0, slice.length, null);
if (bytesRead === 0) {
return fs.close(fd);
}
yield inGroupsOf(slice.toString('hex', 0, bytesRead - 1), 2)
.map((byte) => `0x${byte}`)
.join(', ');
}

function inGroupsOf(str: string, count: number) {
if (str.length % count !== 0) {
throw new Error(
`Expected a string with a multiple of ${count} characters, but it has ${str.length}`,
);
}
const result = new Array<string>();
for (let i = 0; i < str.length; i += count) {
result.push(str.slice(i, i + count));
}
return result;
}
}
15 changes: 15 additions & 0 deletions packages/jsii-pacmak/lib/targets/golang/documentation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Docs } from 'jsii-reflect';
import { EmitContext } from './emit-context';

export class Documentation {
public constructor(private readonly docs: Docs) {}

public emit({ code }: EmitContext): void {
// TODO: Translate code examples to Golang with Rosetta (not implemented there yet)
code.line(`// ${this.docs.summary}`);
code.line('//');
for (const line of this.docs.remarks.split('\n')) {
code.line(`// ${line}`);
}
}
}
12 changes: 12 additions & 0 deletions packages/jsii-pacmak/lib/targets/golang/emit-context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { CodeMaker } from 'codemaker';
import { Rosetta } from 'jsii-rosetta';

/**
* The context in which Golang code is emitted.
*/
export interface EmitContext {
/** A CodeMaker to write out source code. */
readonly code: CodeMaker;
/** A Rosetta stone to translate code examples. */
readonly rosetta: Rosetta;
}
Loading