Skip to content

Commit

Permalink
improved patch generation for unions
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsontom committed Dec 30, 2024
1 parent 176e8e5 commit b7f9fca
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
9 changes: 1 addition & 8 deletions dsl/src/cli/java-client-api/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
import { isMProperty, MResolvedUnionType } from '../model.js';
import { Artifact } from '../artifact-generator.js';
import { generateBuilderProperty, generateProperty } from './shared.js';
import { generateRecordPatch } from './record.js';
import { toFirstUpper } from '../util.js';

export function generateUnion(
Expand Down Expand Up @@ -57,13 +56,7 @@ export function generateUnionContent(

if (t.resolved.records.find((r) => r.patchable)) {
node.indent((child) => {
generateRecordPatch(
child,
t.resolved.sharedProps,
artifactConfig,
fqn,
[]
);
child.append('public interface Patch {}', NL);
});
}

Expand Down
34 changes: 21 additions & 13 deletions dsl/src/cli/java-server-jakarta-ws/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,32 @@ function generateRecordPatch(
const dtoInterface = fqn(
`${artifactConfig.rootPackageName}.service.dto.${t.name}DTO`
);

const allProps = allRecordProperties(t);

const node = new CompositeGeneratorNode();

node.append(
`public class ${t.name}PatchDTOImpl implements ${dtoInterface}.Patch {`,
NL
);
if (t.resolved.unions.length == 1) {
node.append(
`public class ${t.name}PatchDTOImpl extends ${t.resolved.unions[0].name}PatchDTOImpl implements ${dtoInterface}.Patch {`,
NL
);
} else {
node.append(
`public class ${t.name}PatchDTOImpl implements ${dtoInterface}.Patch {`,
NL
);
}
node.indent((classBody) => {
classBody.append(generateFields(allProps, artifactConfig, fqn));
classBody.appendNewLine();
classBody.append(`public ${t.name}PatchDTOImpl() {}`, NL);
classBody.appendNewLine();
classBody.append('@Override', NL);
classBody.append('public boolean isSet(Props prop) {', NL);
classBody.append(
`public boolean isSet(${dtoInterface}.Patch.Props prop) {`,
NL
);
classBody.indent((mBody) => {
mBody.append('return dataSet.contains(prop);', NL);
});
Expand Down Expand Up @@ -126,13 +137,13 @@ function generateFields(
);
} else {
if (property.variant === 'union' || property.variant === 'record') {
const type = fqn(
`${artifactConfig.rootPackageName}.service.dto.${property.type}DTO`
);
if (property.array) {
// TODO
} else {
node.append(`private ${type}.Patch ${property.name};`, NL);
node.append(
`private ${property.type}PatchDTOImpl ${property.name};`,
NL
);
}
} else if (typeof property.type === 'string') {
if (property.array) {
Expand Down Expand Up @@ -200,10 +211,7 @@ function generateAccessors(
} else {
let type: string = '';
if (property.variant === 'union' || property.variant === 'record') {
type =
fqn(
`${artifactConfig.rootPackageName}.service.dto.${property.type}DTO`
) + '.Patch';
type = `${property.type}PatchDTOImpl`;
} else if (typeof property.type === 'string') {
type = resolveType(
property.type,
Expand Down

0 comments on commit b7f9fca

Please sign in to comment.