From e821164509fd21d0f4e7a30ddd9c67a7e28722ae Mon Sep 17 00:00:00 2001 From: Tom Schindl Date: Thu, 21 Nov 2024 22:24:47 +0100 Subject: [PATCH] generate with-builders --- dsl/src/cli/java-rest-client-jdk/record.ts | 2 +- .../cli/java-server-jakarta-ws/generator.ts | 6 ++--- dsl/src/cli/java-server-jakarta-ws/record.ts | 26 ++++++++++++++++--- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/dsl/src/cli/java-rest-client-jdk/record.ts b/dsl/src/cli/java-rest-client-jdk/record.ts index bf09715..7e9a928 100644 --- a/dsl/src/cli/java-rest-client-jdk/record.ts +++ b/dsl/src/cli/java-rest-client-jdk/record.ts @@ -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) } diff --git a/dsl/src/cli/java-server-jakarta-ws/generator.ts b/dsl/src/cli/java-server-jakarta-ws/generator.ts index 1d9f56c..c3b6ef1 100644 --- a/dsl/src/cli/java-server-jakarta-ws/generator.ts +++ b/dsl/src/cli/java-server-jakarta-ws/generator.ts @@ -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) } diff --git a/dsl/src/cli/java-server-jakarta-ws/record.ts b/dsl/src/cli/java-server-jakarta-ws/record.ts index 66e72ab..b9c1635 100644 --- a/dsl/src/cli/java-server-jakarta-ws/record.ts +++ b/dsl/src/cli/java-server-jakarta-ws/record.ts @@ -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; } @@ -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); @@ -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 Builder with${toFirstUpper(p.name)}(Class clazz, ${functionType} 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)