Skip to content

Commit

Permalink
generate with-builders
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsontom committed Nov 21, 2024
1 parent 150c6e1 commit e821164
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dsl/src/cli/java-rest-client-jdk/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ function generateBuilderWith(node: IndentNode, property: MProperty, model: MReso
block.append('throw new IllegalArgumentException();', NL);
});
methodBody.append('}',NL)
methodBody.append(`$builder.add("${property.name}", ((${property.type}DTOImpl)block.apply((T) b)).data);`, NL)
methodBody.append(`$builder.add("${property.name}", ((${property.type}DTOImpl)block.apply(clazz.cast(b))).data);`, NL)
} else {
methodBody.append(`// Could not find union-type "${property.type}"`, NL)
}
Expand Down
6 changes: 3 additions & 3 deletions dsl/src/cli/java-server-jakarta-ws/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ export function generate(model: MResolvedRSDModel, generatorConfig: ArtifactGene
return [];
}

const result = model.elements.map( e => generateType(e, artifactConfig) ).filter(isDefined)
const result = model.elements.map( e => generateType(e, model, artifactConfig) ).filter(isDefined)
result.push(...model.services.flatMap( e => generateService(e, artifactConfig)))

return result;
}

function generateType(t: MResolvedUserType, artifactConfig: JavaServerJakartaWSGeneratorConfig): Artifact | undefined {
function generateType(t: MResolvedUserType, model: MResolvedRSDModel, artifactConfig: JavaServerJakartaWSGeneratorConfig): Artifact | undefined {
if( isMEnumType(t) ) {

} else if( isMRecordType(t) ) {
return generateRecord(t, artifactConfig);
return generateRecord(t, model, artifactConfig);
} else if( isMUnionType(t) ) {
return generateUnion(t, artifactConfig)
}
Expand Down
26 changes: 22 additions & 4 deletions dsl/src/cli/java-server-jakarta-ws/record.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CompositeGeneratorNode, IndentNode, NL, toString } from "langium/generate";
import { Artifact } from "../artifact-generator.js";
import { builtinToJavaType, generateCompilationUnit, JavaImportsCollector, JavaServerJakartaWSGeneratorConfig, resolveObjectType, resolveType, toPath } from "../java-gen-utils.js";
import { allRecordProperties, isMKeyProperty, isMProperty, isMRevisionProperty, MBaseProperty, MResolvedRecordType } from "../model.js";
import { allRecordProperties, isMKeyProperty, isMProperty, isMRevisionProperty, isMUnionType, MBaseProperty, MResolvedRecordType, MResolvedRSDModel } from "../model.js";
import { toFirstUpper } from "../util.js";

export function generateRecord(t: MResolvedRecordType, artifactConfig: JavaServerJakartaWSGeneratorConfig): Artifact | undefined {
export function generateRecord(t: MResolvedRecordType, model: MResolvedRSDModel, artifactConfig: JavaServerJakartaWSGeneratorConfig): Artifact | undefined {
if( t.resolved.unions.length === 1 ) {
return undefined;
}
Expand All @@ -15,12 +15,12 @@ export function generateRecord(t: MResolvedRecordType, artifactConfig: JavaServe

return {
name: `${t.name}DTOImpl.java`,
content: toString(generateCompilationUnit(packageName, importCollector, generateRecordContent(t, artifactConfig, fqn))),
content: toString(generateCompilationUnit(packageName, importCollector, generateRecordContent(t, artifactConfig, fqn, model))),
path: toPath(artifactConfig.targetFolder, packageName)
};
}

export function generateRecordContent(t: MResolvedRecordType, artifactConfig: JavaServerJakartaWSGeneratorConfig, fqn: (type: string) => string): CompositeGeneratorNode {
export function generateRecordContent(t: MResolvedRecordType, artifactConfig: JavaServerJakartaWSGeneratorConfig, fqn: (type: string) => string, model: MResolvedRSDModel): CompositeGeneratorNode {
const node = new CompositeGeneratorNode();

const allProps = allRecordProperties(t);
Expand Down Expand Up @@ -89,12 +89,30 @@ export function generateRecordContent(t: MResolvedRecordType, artifactConfig: Ja
if( p.variant === 'record' ) {
param.append(`public Builder with${toFirstUpper(p.name)}(${functionType}<${iType}.Builder, ${iType}> block) {`,NL)
param.indent( methodBody => {
methodBody.append(`this.${p.name} = (${p.type}DTOImpl)block.apply(${p.type}DTOImpl.builder());`)
methodBody.append('return this;',NL)
})
param.append('}', NL)
} else {
param.append(`public <T extends ${iType}.Builder> Builder with${toFirstUpper(p.name)}(Class<T> clazz, ${functionType}<T, ${iType}> block) {`,NL)
param.indent( methodBody => {
const t = model.elements.find( m => m.name === p.type )
if( isMUnionType(t) ) {
methodBody.append(`${p.type}DTOImpl.Builder b;`, NL)
t.resolved.records.forEach( (r, idx) => {
methodBody.append(`${idx > 0 ? ' else ' : ''}if( clazz == ${p.type}DTO.${r.name}DTO.Builder.class ) {`, NL);
methodBody.indent( block => {
block.append(`b = new ${p.type}DTOImpl.${r.name}DTOImpl.BuilderImpl();`, NL)
});
methodBody.append('}')
});
methodBody.append(' else {', NL);
methodBody.indent( block => {
block.append('throw new IllegalArgumentException();', NL);
});
methodBody.append('}',NL)
methodBody.append(`this.${p.name} = (${p.type}DTOImpl)block.apply(clazz.cast(b));`, NL)
}
methodBody.append('return this;',NL)
})
param.append('}', NL)
Expand Down

0 comments on commit e821164

Please sign in to comment.