Skip to content

Commit

Permalink
fix(rosetta): incorrect transliteration for selective imports (#3508)
Browse files Browse the repository at this point in the history
Go does nto support "selective" imports, and has limited symbol aliasing
support. Instead of attempting to transliterate selective imports with
a one-to-one semantic mapping, resolve the imported symbols and emit the
correct qualified original name instead.

This results in more idiomatic (and likely more correct) go code.

For example:

```ts
import { Foo as RenamedFoo, Bar } from 'baz';

const foo = new RenamedFoo();
Bar.baz(foo);
```

Should transliterate to something looking like:

```go
import "github.com/aws-samples/dummy/baz"

foo := baz.NewFoo()
Bar_baz(foo)
```

---

Bonus content:
- Now emits README.md for submodules, too (was previously ignored).
- Missing newlines between go import statements are now inserted.

---

By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license].

[Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
  • Loading branch information
RomainMuller authored May 6, 2022
1 parent 3648117 commit 8eec086
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 98 deletions.
16 changes: 16 additions & 0 deletions packages/jsii-pacmak/lib/targets/go/documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ export class Documentation {
}
}

public emitReadme(moduleFqn: string, readme: string, directory: string) {
const goReadme = this.rosetta.translateSnippetsInMarkdown(
{ api: 'moduleReadme', moduleFqn },
readme,
TargetLanguage.GO,
false,
);

const readmeFile = `${directory}/README.md`;
this.code.openFile(readmeFile);
for (const line of goReadme.split('\n')) {
this.code.line(line);
}
this.code.closeFile(readmeFile);
}

private emitComment(text = '') {
const lines = text.trim().split('\n');

Expand Down
53 changes: 22 additions & 31 deletions packages/jsii-pacmak/lib/targets/go/package.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { CodeMaker } from 'codemaker';
import { Assembly, Type, Submodule as JsiiSubmodule } from 'jsii-reflect';
import {
Assembly,
ModuleLike as JsiiModuleLike,
Type,
Submodule as JsiiSubmodule,
} from 'jsii-reflect';
import { basename, dirname, join } from 'path';
import * as semver from 'semver';

Expand Down Expand Up @@ -42,10 +47,10 @@ export abstract class Package {
public readonly types: GoType[];

private readonly embeddedTypes = new Map<string, EmbeddedType>();
private readonly readmeFile?: ReadmeFile;

public constructor(
private readonly typeSpec: readonly Type[],
private readonly submoduleSpec: readonly JsiiSubmodule[],
private readonly jsiiModule: JsiiModuleLike,
public readonly packageName: string,
public readonly filePath: string,
public readonly moduleName: string,
Expand All @@ -56,11 +61,11 @@ export abstract class Package {
this.directory = filePath;
this.file = `${this.directory}/${packageName}.go`;
this.root = root ?? this;
this.submodules = this.submoduleSpec.map(
this.submodules = this.jsiiModule.submodules.map(
(sm) => new InternalPackage(this.root, this, sm),
);

this.types = this.typeSpec.map((type: Type): GoType => {
this.types = this.jsiiModule.types.map((type: Type): GoType => {
if (type.isInterfaceType() && type.datatype) {
return new Struct(this, type);
} else if (type.isInterfaceType()) {
Expand All @@ -74,6 +79,14 @@ export abstract class Package {
`Type: ${type.name} with kind ${type.kind} is not a supported type`,
);
});

if (this.jsiiModule.readme?.markdown) {
this.readmeFile = new ReadmeFile(
this.jsiiModule.fqn,
this.jsiiModule.readme.markdown,
this.directory,
);
}
}

/*
Expand Down Expand Up @@ -121,6 +134,8 @@ export abstract class Package {
this.emitTypes(context);
code.closeFile(this.file);

this.readmeFile?.emit(context);

this.emitGoInitFunction(context);
this.emitSubmodules(context);

Expand Down Expand Up @@ -318,7 +333,6 @@ export abstract class Package {
export class RootPackage extends Package {
public readonly assembly: Assembly;
public readonly version: string;
private readonly readme?: ReadmeFile;
private readonly versionFile: VersionFile;

// This cache of root packages is shared across all root packages derived created by this one (via dependencies).
Expand All @@ -334,34 +348,19 @@ export class RootPackage extends Package {
const moduleName = goConfig.moduleName ?? '';
const version = `${assembly.version}${goConfig.versionSuffix ?? ''}`;

super(
assembly.types,
assembly.submodules,
packageName,
filePath,
moduleName,
version,
);
super(assembly, packageName, filePath, moduleName, version);

this.rootPackageCache = rootPackageCache;
this.rootPackageCache.set(assembly.name, this);

this.assembly = assembly;
this.version = version;
this.versionFile = new VersionFile(this.version);

if (this.assembly.readme?.markdown) {
this.readme = new ReadmeFile(
this.packageName,
this.assembly.readme.markdown,
);
}
}

public emit(context: EmitContext): void {
super.emit(context);
this.emitJsiiPackage(context);
this.readme?.emit(context);

this.emitGomod(context.code);
this.versionFile.emit(context.code);
Expand Down Expand Up @@ -517,15 +516,7 @@ export class InternalPackage extends Package {
const filePath =
parent === root ? packageName : `${parent.filePath}/${packageName}`;

super(
assembly.types,
assembly.submodules,
packageName,
filePath,
root.moduleName,
root.version,
root,
);
super(assembly, packageName, filePath, root.moduleName, root.version, root);

this.parent = parent;
}
Expand Down
38 changes: 5 additions & 33 deletions packages/jsii-pacmak/lib/targets/go/readme-file.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,16 @@
import { join } from 'path';

import { EmitContext } from './emit-context';
// import { Translation } from 'jsii-rosetta';
// import { INCOMPLETE_DISCLAIMER_COMPILING, INCOMPLETE_DISCLAIMER_NONCOMPILING } from '..';

export class ReadmeFile {
public constructor(
private readonly packageName: string,
private readonly document: string,
private readonly directory: string,
) {}

public emit({ code /*, rosetta */ }: EmitContext) {
const nameParts = this.packageName.split('.');
const file = join(
...nameParts.slice(0, nameParts.length - 11),
'README.md',
);

code.openFile(file);
const translated = this.document; // rosetta.translateSnippetsInMarkdown(this.document, 'go', prependDisclaimer);
for (const line of translated.split('\n')) {
code.line(line);
}
code.closeFile(file);
}

/*
function prependDisclaimer(translation: Translation) {
const source = addDisclaimerIfNeeded();
return { language: translation.language, source };
function addDisclaimerIfNeeded(): string {
if (translation.didCompile && INCOMPLETE_DISCLAIMER_COMPILING) {
return `// ${INCOMPLETE_DISCLAIMER_COMPILING}\n\n${translation.source}`;
}
if (!translation.didCompile && INCOMPLETE_DISCLAIMER_NONCOMPILING) {
return `// ${INCOMPLETE_DISCLAIMER_NONCOMPILING}\n\n${translation.source}`;
}
return translation.source;
public emit({ documenter }: EmitContext) {
if (!this.document) {
return;
}
documenter.emitReadme(this.packageName, this.document, this.directory);
}
*/
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8eec086

Please sign in to comment.