Skip to content

Commit

Permalink
remove interfaces from union
Browse files Browse the repository at this point in the history
  • Loading branch information
tomsontom committed Dec 29, 2024
1 parent 01720d7 commit d5363c7
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 64 deletions.
65 changes: 31 additions & 34 deletions dsl/src/cli/java-client-api/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ import {
IndentNode,
NL,
toString,
} from "langium/generate";
import { Artifact } from "../artifact-generator.js";
} from 'langium/generate';
import { Artifact } from '../artifact-generator.js';
import {
JavaImportsCollector,
JavaClientAPIGeneratorConfig,
generateCompilationUnit,
toPath,
resolveObjectType,
} from "../java-gen-utils.js";
} from '../java-gen-utils.js';
import {
MBaseProperty,
MResolvedRecordType,
Expand All @@ -20,18 +20,15 @@ import {
isMKeyProperty,
isMProperty,
isMRevisionProperty,
} from "../model.js";
import { generateInlineEnum } from "./enum.js";
import { toFirstUpper } from "../util.js";
import { generateBuilderProperty, generateProperty } from "./shared.js";
} from '../model.js';
import { generateInlineEnum } from './enum.js';
import { toFirstUpper } from '../util.js';
import { generateBuilderProperty, generateProperty } from './shared.js';

export function generateRecord(
t: MResolvedRecordType,
artifactConfig: JavaClientAPIGeneratorConfig
): Artifact | undefined {
if (t.resolved.unions.length === 1) {
return undefined;
}
const packageName = `${artifactConfig.rootPackageName}.dto`;

const importCollector = new JavaImportsCollector(packageName);
Expand Down Expand Up @@ -61,16 +58,16 @@ export function generateRecordPatch(
child.append(
`public interface Patch extends ${superPatchTypes
.map((t) => `${t}.Patch`)
.join(", ")} {`,
.join(', ')} {`,
NL
);
} else {
child.append("public interface Patch {", NL);
child.append('public interface Patch {', NL);
}

child.indent((patchBody) => {
if (allProps.find((p) => !isMKeyProperty(p) && !isMRevisionProperty(p))) {
patchBody.append("public enum Props {", NL);
patchBody.append('public enum Props {', NL);
patchBody.indent((enumBody) => {
allProps
.filter((p) => !isMKeyProperty(p) && !isMRevisionProperty(p))
Expand All @@ -79,9 +76,9 @@ export function generateRecordPatch(
});
});

patchBody.append("}", NL, NL);
patchBody.append('}', NL, NL);

patchBody.append("public boolean isSet(Props prop);", NL, NL);
patchBody.append('public boolean isSet(Props prop);', NL, NL);
}

allProps.forEach((p) => {
Expand All @@ -98,10 +95,10 @@ export function generateRecordPatch(
p.nullable === false &&
p.array === false
) {
const Consumer = fqn("java.util.function.Consumer");
const Function = fqn("java.util.function.Function");
if (p.variant === "union" || p.variant === "record") {
} else if (typeof p.type === "string") {
const Consumer = fqn('java.util.function.Consumer');
const Function = fqn('java.util.function.Function');
if (p.variant === 'union' || p.variant === 'record') {
} else if (typeof p.type === 'string') {
patchBody.append(
`public static void if${toFirstUpper(
p.name
Expand All @@ -120,9 +117,9 @@ export function generateRecordPatch(
mBody.indent((block) => {
block.append(`consumer.accept(dto.${p.name}());`, NL);
});
mBody.append("}", NL);
mBody.append('}', NL);
});
patchBody.append("}", NL);
patchBody.append('}', NL);

patchBody.append(
`public static <T> T if${toFirstUpper(
Expand All @@ -142,17 +139,17 @@ export function generateRecordPatch(
mBody.indent((block) => {
block.append(`return consumer.apply(dto.${p.name}());`, NL);
});
mBody.append("}", NL);
mBody.append('}', NL);

mBody.append("return defaultValue;", NL);
mBody.append('return defaultValue;', NL);
});

patchBody.append("}", NL);
patchBody.append('}', NL);
}
}
});
});
child.append("}", NL);
child.append('}', NL);
}

export function generateRecordContent(
Expand All @@ -168,15 +165,15 @@ export function generateRecordContent(
...t.resolved.unions.map((u) => `${u.name}DTO`),
...t.mixins.map((m) => `Mixin${m}DTO`),
]
: ["BaseDTO", ...t.mixins.map((m) => `Mixin${m}DTO`)];
: ['BaseDTO', ...t.mixins.map((m) => `Mixin${m}DTO`)];

const superPatchTypes =
t.resolved.unions.length > 0
? [...t.resolved.unions.map((u) => `${u.name}DTO`)]
: [];

node.append(
`public interface ${t.name}DTO extends ${superTypes.join(", ")} {`,
`public interface ${t.name}DTO extends ${superTypes.join(', ')} {`,
NL
);

Expand All @@ -185,7 +182,7 @@ export function generateRecordContent(
const allProps = allRecordProperties(t);
allProps
.filter(isMProperty)
.filter((p) => p.variant === "inline-enum")
.filter((p) => p.variant === 'inline-enum')
.filter((p) => !sharedProps.includes(p))
.forEach((p) => {
const inlineEnum = p.type;
Expand All @@ -205,7 +202,7 @@ export function generateRecordContent(
child.append(
`public interface Builder extends ${superTypes
.map((s) => `${s}.Builder`)
.join(", ")} {`,
.join(', ')} {`,
NL
);
child.indent((builderChild) => {
Expand All @@ -214,10 +211,10 @@ export function generateRecordContent(
);
allProps
.filter(isMProperty)
.filter((p) => p.variant === "union" || p.variant === "record")
.filter((p) => p.variant === 'union' || p.variant === 'record')
.forEach((p) => {
const functionType = fqn("java.util.function.Function");
if (p.variant === "record") {
const functionType = fqn('java.util.function.Function');
if (p.variant === 'record') {
builderChild.append(
`public Builder with${toFirstUpper(p.name)}(${functionType}<${
p.type
Expand All @@ -237,7 +234,7 @@ export function generateRecordContent(
});
builderChild.append(`public ${t.name}DTO build();`, NL);
});
child.append("}", NL);
child.append('}', NL);
if (t.patchable) {
generateRecordPatch(
child,
Expand All @@ -248,6 +245,6 @@ export function generateRecordContent(
);
}
});
node.append("}", NL);
node.append('}', NL);
return node;
}
32 changes: 10 additions & 22 deletions dsl/src/cli/java-client-api/union.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { CompositeGeneratorNode, NL, toString } from "langium/generate";
import { CompositeGeneratorNode, NL, toString } from 'langium/generate';

import {
JavaImportsCollector,
JavaClientAPIGeneratorConfig,
generateCompilationUnit,
toPath,
} from "../java-gen-utils.js";
import { isMProperty, MResolvedUnionType } from "../model.js";
import { Artifact } from "../artifact-generator.js";
import { generateBuilderProperty, generateProperty } from "./shared.js";
import { generateRecordContent, generateRecordPatch } from "./record.js";
import { toFirstUpper } from "../util.js";
} from '../java-gen-utils.js';
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(
t: MResolvedUnionType,
Expand Down Expand Up @@ -43,7 +43,7 @@ export function generateUnionContent(
const node = new CompositeGeneratorNode();
t.resolved.sharedProps
.filter(isMProperty)
.filter((p) => p.variant === "inline-enum")
.filter((p) => p.variant === 'inline-enum')
.forEach((p) => {
const m = t.resolved.records
.flatMap((r) => r.resolved.mixins)
Expand Down Expand Up @@ -82,22 +82,10 @@ export function generateUnionContent(
);
child.append(`public ${t.name}DTO build();`, NL);
});
child.append("}", NL);
child.append('}', NL);
});
}

const childRecords = t.resolved.records.filter(
(r) => r.resolved.unions.length === 1
);
if (childRecords.length > 0) {
node.indent((child) => {
childRecords.forEach((r) => {
child.appendNewLine();
child.append(generateRecordContent(r, artifactConfig, fqn));
});
});
}

node.append("}", NL);
node.append('}', NL);
return node;
}
13 changes: 5 additions & 8 deletions dsl/src/cli/java-server/record.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
import { toString } from "langium/generate";
import { Artifact } from "../artifact-generator.js";
import { toString } from 'langium/generate';
import { Artifact } from '../artifact-generator.js';
import {
generateCompilationUnit,
JavaImportsCollector,
JavaServerGeneratorConfig,
toPath,
} from "../java-gen-utils.js";
import { MResolvedRecordType, MResolvedRSDModel } from "../model.js";
import { generateRecordContent } from "../java-client-api/record.js";
} from '../java-gen-utils.js';
import { MResolvedRecordType, MResolvedRSDModel } from '../model.js';
import { generateRecordContent } from '../java-client-api/record.js';

export function generateRecord(
t: MResolvedRecordType,
model: MResolvedRSDModel,
artifactConfig: JavaServerGeneratorConfig
): Artifact | undefined {
if (t.resolved.unions.length === 1) {
return undefined;
}
const packageName = `${artifactConfig.rootPackageName}.service.dto`;

const importCollector = new JavaImportsCollector(packageName);
Expand Down

0 comments on commit d5363c7

Please sign in to comment.