Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dcb6 committed Oct 2, 2024
1 parent c1834bc commit 7e2043a
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 52 deletions.
16 changes: 8 additions & 8 deletions generators/php/codegen/src/ast/Class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export declare namespace Class {
/* The class to inherit from if any */
parentClassReference?: AstNode;
/* The traits that this class uses, if any */
usedTraits?: ClassReference[];
traits?: ClassReference[];
}

interface Constructor {
Expand All @@ -42,20 +42,20 @@ export class Class extends AstNode {
public readonly abstract: boolean;
public readonly docs: string | undefined;
public readonly parentClassReference: AstNode | undefined;
public readonly usedTraits: ClassReference[];
public readonly traits: ClassReference[];

public readonly fields: Field[] = [];
public readonly methods: Method[] = [];
private constructor_: Class.Constructor | undefined;

constructor({ name, namespace, abstract, docs, parentClassReference, usedTraits }: Class.Args) {
constructor({ name, namespace, abstract, docs, parentClassReference, traits }: Class.Args) {
super();
this.name = name;
this.namespace = namespace;
this.abstract = abstract ?? false;
this.docs = docs;
this.parentClassReference = parentClassReference;
this.usedTraits = usedTraits ?? [];
this.traits = traits ?? [];
}

public addConstructor(constructor: Class.Constructor): void {
Expand All @@ -70,8 +70,8 @@ export class Class extends AstNode {
this.methods.push(method);
}

public addUsedTrait(traitClassReference: ClassReference): void {
this.usedTraits.push(traitClassReference);
public addTrait(traitClassReference: ClassReference): void {
this.traits.push(traitClassReference);
}

public write(writer: Writer): void {
Expand All @@ -87,9 +87,9 @@ export class Class extends AstNode {
writer.newLine();
writer.writeLine("{");
writer.indent();
if (this.usedTraits.length > 0) {
if (this.traits.length > 0) {
writer.write("use ");
this.usedTraits.forEach((trait, index) => {
this.traits.forEach((trait, index) => {
if (index > 0) {
writer.write(",");
}
Expand Down
8 changes: 4 additions & 4 deletions generators/php/codegen/src/ast/DataClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ export class DataClass extends AstNode {
public readonly namespace: string;
private class_: Class;

constructor({ name, namespace, abstract, docs, parentClassReference, usedTraits }: DataClass.Args) {
constructor({ name, namespace, abstract, docs, parentClassReference, traits }: DataClass.Args) {
super();
this.name = name;
this.namespace = namespace;
this.class_ = new Class({ name, namespace, abstract, docs, parentClassReference, usedTraits });
this.class_ = new Class({ name, namespace, abstract, docs, parentClassReference, traits });
}

public addField(field: Field): void {
Expand All @@ -37,8 +37,8 @@ export class DataClass extends AstNode {
public addMethod(method: Method): void {
this.class_.addMethod(method);
}
public addUsedTrait(traitClassRefeference: ClassReference): void {
this.class_.addUsedTrait(traitClassRefeference);
public addTrait(traitClassRefeference: ClassReference): void {
this.class_.addTrait(traitClassRefeference);
}

public write(writer: Writer): void {
Expand Down
12 changes: 6 additions & 6 deletions generators/php/codegen/src/ast/Trait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ export declare namespace Trait {
/* Docs associated with the trait */
docs?: string;
/* The traits that this trait uses, if any */
usedTraits?: ClassReference[];
traits?: ClassReference[];
}
}

export class Trait extends AstNode {
public readonly name: string;
public readonly namespace: string;
public readonly docs: string | undefined;
public readonly usedTraits: ClassReference[];
public readonly traits: ClassReference[];

public readonly fields: Field[] = [];
public readonly methods: Method[] = [];

constructor({ name, namespace, docs, usedTraits }: Trait.Args) {
constructor({ name, namespace, docs, traits }: Trait.Args) {
super();
this.name = name;
this.namespace = namespace;
this.docs = docs;
this.usedTraits = usedTraits ?? [];
this.traits = traits ?? [];
}

public addField(field: Field): void {
Expand All @@ -51,9 +51,9 @@ export class Trait extends AstNode {
writer.writeLine("{");
writer.indent();

if (this.usedTraits.length > 0) {
if (this.traits.length > 0) {
writer.write("use ");
this.usedTraits.forEach((trait, index) => {
this.traits.forEach((trait, index) => {
if (index > 0) {
writer.write(",");
}
Expand Down
18 changes: 8 additions & 10 deletions generators/php/codegen/src/context/AbstractPhpGeneratorContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,8 @@ export abstract class AbstractPhpGeneratorContext<
}
}

public getUnderlyingObjectTypeDeclaration(typeReference: TypeReference): ObjectTypeDeclaration | undefined {
public getUnderlyingObjectTypeDeclaration(typeReference: TypeReference): ObjectTypeDeclaration {
switch (typeReference.type) {
case "primitive":
case "unknown":
case "container":
return undefined;
case "named": {
const declaration = this.getTypeDeclarationOrThrow(typeReference.typeId);
if (declaration.shape.type === "alias") {
Expand All @@ -299,21 +295,23 @@ export abstract class AbstractPhpGeneratorContext<
if (declaration.shape.type === "object") {
return declaration.shape;
}
return undefined;
throw new Error("Type is not an object type");
}
case "primitive":
case "unknown":
case "container":
}
throw new Error("Type is not an object type");
}

public getUnderlyingObjectTypeDeclarationFromTypeDeclaration(
typeDeclaration: TypeDeclaration
): ObjectTypeDeclaration | undefined {
public getUnderlyingObjectTypeDeclarationOrThrow(typeDeclaration: TypeDeclaration): ObjectTypeDeclaration {
if (typeDeclaration.shape.type === "alias") {
return this.getUnderlyingObjectTypeDeclaration(typeDeclaration.shape.aliasOf);
}
if (typeDeclaration.shape.type === "object") {
return typeDeclaration.shape;
}
return undefined;
throw new Error("Type is not an object type");
}

public maybeLiteral(typeReference: TypeReference): Literal | undefined {
Expand Down
28 changes: 11 additions & 17 deletions generators/php/model/src/generateTraits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@ import { ModelGeneratorContext } from "./ModelGeneratorContext";
import { TraitGenerator } from "./trait/TraitGenerator";

export function generateTraits(context: ModelGeneratorContext): void {
const extendedTypeIdsFromTypes: Set<string> = new Set(
Object.values(context.ir.types).flatMap((typeDeclaration) =>
typeDeclaration.shape._visit<string[]>({
alias: () => [],
enum: () => [],
object: (objectDeclaration) =>
objectDeclaration.extends.map((declaredTypeName) => declaredTypeName.typeId),
undiscriminatedUnion: () => [],
union: () => [],
_other: () => []
})
)
const extendedTypeIdsFromTypes = Object.values(context.ir.types).flatMap((typeDeclaration) =>
typeDeclaration.shape._visit<string[]>({
alias: () => [],
enum: () => [],
object: (objectDeclaration) => objectDeclaration.extends.map((declaredTypeName) => declaredTypeName.typeId),
undiscriminatedUnion: () => [],
union: () => [],
_other: () => []
})
);
const extendedTypeIdsFromInlinedRequests = Object.values(context.ir.services)
.flatMap((service) => service.endpoints)
Expand All @@ -22,12 +19,9 @@ export function generateTraits(context: ModelGeneratorContext): void {
? endpoint.requestBody.extends.map((declaredTypeName) => declaredTypeName.typeId)
: [];
});
for (const typeId of [...extendedTypeIdsFromTypes, ...extendedTypeIdsFromInlinedRequests]) {
for (const typeId of new Set([...extendedTypeIdsFromTypes, ...extendedTypeIdsFromInlinedRequests])) {
const typeDeclaration = context.getTypeDeclarationOrThrow(typeId);
const objectTypeDeclaration = context.getUnderlyingObjectTypeDeclarationFromTypeDeclaration(typeDeclaration);
if (objectTypeDeclaration == null) {
throw new Error("Unexpected no object type declaration");
}
const objectTypeDeclaration = context.getUnderlyingObjectTypeDeclarationOrThrow(typeDeclaration);
const file = new TraitGenerator(context, typeDeclaration, objectTypeDeclaration).generate();
context.project.addSourceFiles(file);
}
Expand Down
6 changes: 3 additions & 3 deletions generators/php/model/src/object/ObjectGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ export class ObjectGenerator extends FileGenerator<PhpFile, ModelCustomConfigSch
...this.classReference,
docs: this.typeDeclaration.docs,
parentClassReference: this.context.getSerializableTypeClassReference(),
usedTraits: this.objectDeclaration.extends.map((declaredTypeName) =>
traits: this.objectDeclaration.extends.map((declaredTypeName) =>
this.context.phpTypeMapper.convertToTraitClassReference(declaredTypeName)
)
});

for (const property of this.objectDeclaration.properties) {
clazz.addField(this.toField({ property, inherited: false }));
clazz.addField(this.toField({ property }));
}
for (const property of this.objectDeclaration.extendedProperties ?? []) {
clazz.addField(this.toField({ property, inherited: true }));
Expand All @@ -44,7 +44,7 @@ export class ObjectGenerator extends FileGenerator<PhpFile, ModelCustomConfigSch
});
}

private toField({ property, inherited }: { property: ObjectProperty; inherited: boolean }): php.Field {
private toField({ property, inherited }: { property: ObjectProperty; inherited?: boolean }): php.Field {
const convertedType = this.context.phpTypeMapper.convert({ reference: property.valueType });
return php.field({
type: convertedType,
Expand Down
2 changes: 1 addition & 1 deletion generators/php/model/src/trait/TraitGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class TraitGenerator extends FileGenerator<PhpFile, ModelCustomConfigSche
const clazz = php.trait({
...this.classReference,
docs: this.typeDeclaration.docs,
usedTraits: this.objectDeclaration.extends.map((declaredTypeName) =>
traits: this.objectDeclaration.extends.map((declaredTypeName) =>
this.context.phpTypeMapper.convertToTraitClassReference(declaredTypeName)
)
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ export class WrappedEndpointRequestGenerator extends FileGenerator<
},
inlinedRequestBody: (request) => {
for (const property of request.properties) {
clazz.addField(this.toField({ property, inherited: false }));
clazz.addField(this.toField({ property }));
}
for (const property of request.extendedProperties ?? []) {
clazz.addField(this.toField({ property, inherited: true }));
}
for (const declaredTypeName of request.extends) {
clazz.addUsedTrait(this.context.phpTypeMapper.convertToTraitClassReference(declaredTypeName));
clazz.addTrait(this.context.phpTypeMapper.convertToTraitClassReference(declaredTypeName));
}
},
fileUpload: () => undefined,
Expand All @@ -106,7 +106,7 @@ export class WrappedEndpointRequestGenerator extends FileGenerator<
});
}

private toField({ property, inherited }: { property: InlinedRequestBodyProperty; inherited: boolean }): php.Field {
private toField({ property, inherited }: { property: InlinedRequestBodyProperty; inherited?: boolean }): php.Field {
const convertedType = this.context.phpTypeMapper.convert({ reference: property.valueType });
return php.field({
type: convertedType,
Expand Down

0 comments on commit 7e2043a

Please sign in to comment.