Skip to content

Commit

Permalink
refactor to suffix attributes and remove resource names
Browse files Browse the repository at this point in the history
  • Loading branch information
moofish32 committed May 4, 2019
1 parent af39691 commit 3ff28ad
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 38 deletions.
33 changes: 20 additions & 13 deletions tools/cfn2ts/lib/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const CONSTRUCT_CLASS = `${CORE}.Construct`;
const TAG_TYPE = `${CORE}.TagType`;
const TAG_MANAGER = `${CORE}.TagManager`;

const REF_DOC_LINK = 'https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-ref.html';

interface Dictionary<T> { [key: string]: T; }

/**
Expand Down Expand Up @@ -234,26 +236,31 @@ export default class CodeGenerator {
this.code.line();

this.docLink(undefined, `@cloudformationAttribute ${attributeName}`);
const attr = genspec.attributeDefinition(resourceName, attributeName, attributeSpec);
const attr = genspec.attributeDefinition(attributeName, attributeSpec);

this.code.line(`public readonly ${attr.propertyName}: ${attr.attributeType};`);

attributes.push(attr);
}
this.code.line();
const refAttr = genspec.refAttributeDefinition();
this.docLink(spec.Documentation || REF_DOC_LINK, `@cloudformationReference Ref`);
this.code.line(`public readonly ${refAttr.propertyName}: ${refAttr.attributeType};`);
attributes.push(refAttr);
}

// //
// // Ref attribute
// //
// if (spec.RefKind !== schema.SpecialRefKind.None) {
// const refAttribute = genspec.refAttributeDefinition(resourceName, spec.RefKind!);
//
// Ref attribute
//
if (spec.RefKind !== schema.SpecialRefKind.None) {
const refAttribute = genspec.refAttributeDefinition(resourceName, spec.RefKind!);

// If there's already an attribute with the same name, ref is not needed
if (!attributes.some(a => a.propertyName === refAttribute.propertyName)) {
this.code.line(`public readonly ${refAttribute.propertyName}: ${refAttribute.attributeType};`);
attributes.push(refAttribute);
}
}
// // If there's already an attribute with the same name, ref is not needed
// if (!attributes.some(a => a.propertyName === refAttribute.propertyName)) {
// this.code.line(`public readonly ${refAttribute.propertyName}Attr: ${refAttribute.attributeType};`);
// attributes.push(refAttribute);
// }
// }

// set class properties to match CloudFormation Properties spec

Expand Down Expand Up @@ -534,7 +541,7 @@ export default class CodeGenerator {
if (props.propName === 'Tags' && schema.isTagProperty(props.spec)) {
this.code.line(`public readonly tags: ${TAG_MANAGER};`);
} else {
this.code.line(`public ${javascriptPropertyName}Prop${line}`);
this.code.line(`public ${javascriptPropertyName}${line}`);
}
}
return javascriptPropertyName;
Expand Down
49 changes: 24 additions & 25 deletions tools/cfn2ts/lib/genspec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,9 @@ export function validatorName(typeName: CodeName): CodeName {
* - The type we will generate for the attribute, including its base class and docs.
* - The property name we will use to refer to the attribute.
*/
export function attributeDefinition(resourceName: CodeName, attributeName: string, spec: schema.Attribute): Attribute {
const descriptiveName = descriptiveAttributeName(resourceName, attributeName); // "BucketArn"
const propertyName = cloudFormationToScriptName(descriptiveName); // "bucketArn"
export function attributeDefinition(attributeName: string, spec: schema.Attribute): Attribute {
const descriptiveName = attributeName.replace(/\./g, '');
const propertyName = `${cloudFormationToScriptName(descriptiveName)}Attr`; // "bucketArn"

let attrType: string;
if ('PrimitiveType' in spec && spec.PrimitiveType === 'String') {
Expand All @@ -189,12 +189,11 @@ export function attributeDefinition(resourceName: CodeName, attributeName: strin
/**
* Return an attribute definition name for the RefKind for this class
*/
export function refAttributeDefinition(resourceName: CodeName, refKind: string): Attribute {
const propertyName = codemaker.toCamelCase(descriptiveAttributeName(resourceName, refKind));
export function refAttributeDefinition(): Attribute {

const constructorArguments = 'this.ref';

return new Attribute(propertyName, 'string', constructorArguments);
return new Attribute('refAttr', 'string', constructorArguments);
}

/**
Expand All @@ -203,25 +202,25 @@ export function refAttributeDefinition(resourceName: CodeName, refKind: string):
* collisions with base class properties, but also to allow certain constructs to expose multiple attributes
* of different sub-resources using the same names (i.e. 'bucketArn' and 'topicArn' can co-exist while 'arn' and 'arn' cannot).
*/
function descriptiveAttributeName(resourceName: CodeName, attributeName: string): string {
// remove '.'s
attributeName = attributeName.replace(/\./g, '');

const resName = resourceName.specName!.resourceName;

// special case (someone was smart)
if (resName === 'SecurityGroup' && attributeName === 'GroupId') {
attributeName = 'SecurityGroupId';
}

// if property name already starts with the resource name, then just use it as-is
// otherwise, prepend the resource name
if (!attributeName.toLowerCase().startsWith(resName.toLowerCase())) {
attributeName = `${resName}${codemaker.toPascalCase(attributeName)}`;
}

return attributeName;
}
// function descriptiveAttributeName(resourceName: CodeName, attributeName: string): string {
// // remove '.'s
// attributeName = attributeName.replace(/\./g, '');
//
// const resName = resourceName.specName!.resourceName;
//
// // special case (someone was smart)
// if (resName === 'SecurityGroup' && attributeName === 'GroupId') {
// attributeName = 'SecurityGroupId';
// }
//
// // if property name already starts with the resource name, then just use it as-is
// // otherwise, prepend the resource name
// if (!attributeName.toLowerCase().startsWith(resName.toLowerCase())) {
// attributeName = `${resName}${codemaker.toPascalCase(attributeName)}`;
// }
//
// return attributeName;
// }

/**
* Convert a CloudFormation name to a nice TypeScript name
Expand Down

0 comments on commit 3ff28ad

Please sign in to comment.