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(python): idiomatic capitalization for structs #586

Merged
merged 11 commits into from
Jul 7, 2019
4 changes: 2 additions & 2 deletions packages/codemaker/lib/case-utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { default as camelcase } from 'camelcase';
import * as decamelize from 'decamelize';
import decamelize = require('decamelize');

export function toCamelCase(...args: string[]) {
return camelcase(args);
Expand All @@ -11,4 +11,4 @@ export function toPascalCase(...args: string[]) {

export function toSnakeCase(s: string, sep = '_') {
return decamelize(s, sep);
}
}
9 changes: 9 additions & 0 deletions packages/codemaker/test/test.case-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,14 @@ export = nodeunit.testCase({
test.equal(caseUtils.toPascalCase('EXAMPLE_VALUE'), 'ExampleValue');
test.equal(caseUtils.toPascalCase('example', 'value'), 'ExampleValue');
test.done();
},

toSnakeCase(test: nodeunit.Test) {
test.equal(caseUtils.toSnakeCase('EXAMPLE_VALUE'), 'example_value');
test.equal(caseUtils.toSnakeCase('exampleValue'), 'example_value');
test.equal(caseUtils.toSnakeCase('ExampleValue'), 'example_value');
test.equal(caseUtils.toSnakeCase('EPSConduit'), 'eps_conduit');
test.equal(caseUtils.toSnakeCase('SomeEBSVolume'), 'some_ebs_volume');
test.done();
}
});
43 changes: 43 additions & 0 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1741,3 +1741,46 @@ export class DataRenderer {
return JSON.stringify(map, null, 2);
}
}

export interface TopLevelStruct {
/**
* This is a required field
*/
readonly required: string;

/**
* You don't have to pass this
*/
readonly optional?: string;

/**
* A union to really stress test our serialization
*/
readonly secondLevel: SecondLevelStruct | number;
}

export interface SecondLevelStruct {
/**
* It's long and required
*/
readonly deeperRequiredProp: string;

/**
* It's long, but you'll almost never pass it.
*/
readonly deeperOptionalProp?: string;
}

export class StructPassing {
public static roundTrip(_positional: number, input: TopLevelStruct): TopLevelStruct {
return {
required: input.required,
optional: input.optional,
secondLevel: input.secondLevel,
};
}

public static howManyVarArgsDidIPass(_positional: number, inputs: TopLevelStruct[]): number {
return inputs.length;
}
}
206 changes: 205 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -7150,6 +7150,55 @@
],
"name": "RuntimeTypeChecking"
},
"jsii-calc.SecondLevelStruct": {
"assembly": "jsii-calc",
"datatype": true,
"docs": {
"stability": "experimental"
},
"fqn": "jsii-calc.SecondLevelStruct",
"kind": "interface",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1762
},
"name": "SecondLevelStruct",
"properties": [
{
"abstract": true,
"docs": {
"stability": "experimental",
"summary": "It's long and required."
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1766
},
"name": "deeperRequiredProp",
"type": {
"primitive": "string"
}
},
{
"abstract": true,
"docs": {
"stability": "experimental",
"summary": "It's long, but you'll almost never pass it."
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1771
},
"name": "deeperOptionalProp",
"optional": true,
"type": {
"primitive": "string"
}
}
]
},
"jsii-calc.SingleInstanceTwoTypes": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -7756,6 +7805,87 @@
}
]
},
"jsii-calc.StructPassing": {
"assembly": "jsii-calc",
"docs": {
"stability": "experimental"
},
"fqn": "jsii-calc.StructPassing",
"initializer": {},
"kind": "class",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1774
},
"methods": [
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1783
},
"name": "howManyVarArgsDidIPass",
"parameters": [
{
"name": "_positional",
"type": {
"primitive": "number"
}
},
{
"name": "inputs",
"type": {
"collection": {
"elementtype": {
"fqn": "jsii-calc.TopLevelStruct"
},
"kind": "array"
}
}
}
],
"returns": {
"type": {
"primitive": "number"
}
},
"static": true
},
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1775
},
"name": "roundTrip",
"parameters": [
{
"name": "_positional",
"type": {
"primitive": "number"
}
},
{
"name": "input",
"type": {
"fqn": "jsii-calc.TopLevelStruct"
}
}
],
"returns": {
"type": {
"fqn": "jsii-calc.TopLevelStruct"
}
},
"static": true
}
],
"name": "StructPassing"
},
"jsii-calc.Sum": {
"assembly": "jsii-calc",
"base": "jsii-calc.composition.CompositeOperation",
Expand Down Expand Up @@ -8104,6 +8234,80 @@
],
"name": "Thrower"
},
"jsii-calc.TopLevelStruct": {
"assembly": "jsii-calc",
"datatype": true,
"docs": {
"stability": "experimental"
},
"fqn": "jsii-calc.TopLevelStruct",
"kind": "interface",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1745
},
"name": "TopLevelStruct",
"properties": [
{
"abstract": true,
"docs": {
"stability": "experimental",
"summary": "This is a required field."
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1749
},
"name": "required",
"type": {
"primitive": "string"
}
},
{
"abstract": true,
"docs": {
"stability": "experimental",
"summary": "A union to really stress test our serialization."
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1759
},
"name": "secondLevel",
"type": {
"union": {
"types": [
{
"primitive": "number"
},
{
"fqn": "jsii-calc.SecondLevelStruct"
}
]
}
}
},
{
"abstract": true,
"docs": {
"stability": "experimental",
"summary": "You don't have to pass this."
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1754
},
"name": "optional",
"optional": true,
"type": {
"primitive": "string"
}
}
]
},
"jsii-calc.UnaryOperation": {
"abstract": true,
"assembly": "jsii-calc",
Expand Down Expand Up @@ -8851,5 +9055,5 @@
}
},
"version": "0.13.4",
"fingerprint": "T+Xf0Qtu6HVsykkz6vGH8OhMtRQbt7Jzq4O7Rsv5SeM="
"fingerprint": "7VlL0zBte+Qeew8qdhvaeS0ZtQtbbT8m3wjT7mDtAoE="
}
10 changes: 8 additions & 2 deletions packages/jsii-pacmak/bin/jsii-pacmak.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env node
import fs = require('fs-extra');
import reflect = require('jsii-reflect');
import spec = require('jsii-spec');
import os = require('os');
import path = require('path');
Expand Down Expand Up @@ -136,6 +137,10 @@ import { VERSION_DESC } from '../lib/version';
const tarball = await timers.recordAsync('npm pack', () => {
return npmPack(packageDir, tmpdir);
});

const ts = new reflect.TypeSystem();
const assembly = await ts.loadModule(packageDir);

for (const targetName of targets) {
// if we are targeting a single language, output to outdir, otherwise outdir/<target>
const targetOutputDir = (targets.length > 1 || forceSubdirectory)
Expand All @@ -144,7 +149,7 @@ import { VERSION_DESC } from '../lib/version';
logging.debug(`Building ${pkg.name}/${targetName}: ${targetOutputDir}`);

await timers.recordAsync(targetName.toString(), () =>
generateTarget(packageDir, targetName.toString(), targetOutputDir, tarball)
generateTarget(assembly, packageDir, targetName.toString(), targetOutputDir, tarball)
);
}
} finally {
Expand All @@ -159,7 +164,7 @@ import { VERSION_DESC } from '../lib/version';
logging.info(`Packaged. ${timers.display()}`);
}

async function generateTarget(packageDir: string, targetName: string, targetOutputDir: string, tarball: string) {
async function generateTarget(assembly: reflect.Assembly, packageDir: string, targetName: string, targetOutputDir: string, tarball: string) {
// ``argv.target`` is guaranteed valid by ``yargs`` through the ``choices`` directive.
const targetConstructor = targetConstructors[targetName];
if (!targetConstructor) {
Expand All @@ -169,6 +174,7 @@ import { VERSION_DESC } from '../lib/version';
const target = new targetConstructor({
targetName,
packageDir,
assembly,
fingerprint: argv.fingerprint,
force: argv.force,
arguments: argv
Expand Down
Loading