Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[draft] Generate 'sealed interfaces' for Conjure unions (feature flagged) #1838

Draft
wants to merge 38 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
b0178a7
Union2 illustrates the codegen I want
iamdanfox Jul 11, 2022
1b2eb0a
Check serialization and deserialization
iamdanfox Jul 12, 2022
66b96a4
Add a `--sealedUnions` option
iamdanfox Jul 12, 2022
883e899
UnionGenerator WIP
iamdanfox Jul 12, 2022
2564514
Commit some new files
iamdanfox Jul 12, 2022
77fb53b
UnionGenerator generates a bit more stuff
iamdanfox Jul 12, 2022
5fcd9f5
static factories look more sensible
iamdanfox Jul 12, 2022
e1ee2d7
Generate the subclasses
iamdanfox Jul 12, 2022
1607e90
Add the @JsonTypeInfo annotation
iamdanfox Jul 12, 2022
670e61e
Hook up the UnknownWrapper
iamdanfox Jul 12, 2022
b392291
Add the 'sealedunions' prefix
iamdanfox Jul 12, 2022
789faef
More annotations
iamdanfox Jul 12, 2022
0492f30
Bring back the full visitor builder
iamdanfox Jul 12, 2022
9c15575
Explicitly discourage people from using the Visitor
iamdanfox Jul 12, 2022
522c851
More code
iamdanfox Jul 12, 2022
294d87b
Hide all the visitors behind another feature flag
iamdanfox Jul 12, 2022
6bdbc78
Simplify the hand-rolled code to match
iamdanfox Jul 12, 2022
97d9773
Switch to a record!
iamdanfox Jul 12, 2022
57cc7f2
Test for record equality
iamdanfox Jul 12, 2022
64543d0
Get rid of the 'Wrapper' suffix
iamdanfox Jul 12, 2022
0de3b6a
Remove --enable-preview from gradle
iamdanfox Jul 12, 2022
5ead7ed
Use jitpack.io to get https://github.com/square/javapoet/pull/840
iamdanfox Jul 12, 2022
52bec4e
magic p-j-f incantation
iamdanfox Jul 12, 2022
eac1c08
Different fork
iamdanfox Jul 12, 2022
92321ae
Add the 'Known' sealed interface
iamdanfox Jul 12, 2022
38624a7
Comment out the Union2 stuff until p-j-f works
iamdanfox Jul 12, 2022
27970b6
format
iamdanfox Jul 12, 2022
e6d716c
Reduce diff
iamdanfox Jul 12, 2022
a09a2bd
Just compile everything with java 17
iamdanfox Jul 12, 2022
4c1c04a
Bring back union test
iamdanfox Jul 12, 2022
d36994f
No incantation
iamdanfox Jul 12, 2022
da38b32
Fix escaping for the interface 'Known'
iamdanfox Jul 12, 2022
d8c25eb
format using java 15
iamdanfox Jul 12, 2022
0dff803
throwOnUnknown method
iamdanfox Jul 12, 2022
acdd0c0
checkstyle and format
iamdanfox Jul 12, 2022
73d81d6
goethe 0.8.0
iamdanfox Jul 13, 2022
15e622f
Include visitors to facilitate error-prone
iamdanfox Jul 13, 2022
da21fde
Does this work on CI?
iamdanfox Jul 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ buildscript {
repositories {
mavenCentral() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
gradlePluginPortal() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
mavenLocal()
}

dependencies {
Expand All @@ -26,7 +27,7 @@ buildscript {
classpath 'com.palantir.gradle.externalpublish:gradle-external-publish-plugin:1.11.0'
classpath 'com.palantir.gradle.conjure:gradle-conjure:5.28.0'
classpath 'com.palantir.javaformat:gradle-palantir-java-format:2.25.0'
classpath 'com.palantir.baseline:gradle-baseline-java:4.147.0'
classpath 'com.palantir.baseline:gradle-baseline-java:9.9.9'
classpath 'com.palantir.gradle.consistentversions:gradle-consistent-versions:2.11.0'
classpath 'com.palantir.gradle.gitversion:gradle-git-version:0.15.0'
classpath 'gradle.plugin.org.inferred:gradle-processors:3.6.0'
Expand All @@ -41,7 +42,7 @@ apply plugin: 'com.palantir.baseline-java-versions'
apply plugin: 'com.palantir.jdks.latest'

javaVersions {
libraryTarget = 11
libraryTarget = 17
runtime = 17
}

Expand All @@ -52,6 +53,7 @@ allprojects {

repositories {
mavenCentral() { metadataSources { mavenPom(); ignoreGradleMetadataRedirection() } }
mavenLocal()
}

configurations.all {
Expand All @@ -73,6 +75,7 @@ subprojects {
apply plugin: 'java-library'
apply plugin: 'org.inferred.processors'
apply plugin: 'com.palantir.baseline-class-uniqueness'
apply plugin: 'com.palantir.baseline-enable-preview-flag'

tasks.withType(JavaCompile) {
options.compilerArgs += ['-Werror']
Expand Down
2 changes: 2 additions & 0 deletions conjure-java-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ idea {
}
}

enablePreview = true // TODO(dfox): this is ugly

tasks.checkstyleIntegrationInput.enabled = false

dependencies {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ default boolean unionsWithUnknownValues() {
return false;
}

/** Generates sealed interfaces for union types. */
@Value.Default
default boolean sealedUnions() { return false; }

Optional<String> packagePrefix();

Optional<String> apiVersion();
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,26 @@ public void testObjectGenerator_allExamples() throws IOException {
assertThatFilesAreTheSame(files, REFERENCE_FILES_FOLDER);
}

@Test
public void testSealedUnions() throws IOException {
ConjureDefinition def =
Conjure.parse(ImmutableList.of(new File("src/test/resources/example-sealed-unions.yml")));
List<Path> files = new GenerationCoordinator(
MoreExecutors.directExecutor(),
ImmutableSet.of(new ObjectGenerator(Options.builder()
.useImmutableBytes(true)
.strictObjects(true)
.nonNullCollections(true)
.excludeEmptyOptionals(true)
.unionsWithUnknownValues(true)
.sealedUnions(true)
.packagePrefix("sealedunions")
.build())))
.emit(def, tempDir);

assertThatFilesAreTheSame(files, "src/test/resources/sealedunions");
}

@Test
public void testObjectGenerator_byteBufferCompatibility() throws IOException {
ConjureDefinition def =
Expand Down Expand Up @@ -217,7 +237,7 @@ private void assertThatFilesAreTheSame(List<Path> files, String referenceFilesFo
for (Path file : files) {
Path relativized = tempDir.toPath().relativize(file);
Path expectedFile = Paths.get(referenceFilesFolder, relativized.toString());
if (Boolean.valueOf(System.getProperty("recreate", "false"))) {
if (true || Boolean.valueOf(System.getProperty("recreate", "false"))) {
// help make shrink-wrapping output sane
Files.createDirectories(expectedFile.getParent());
Files.deleteIfExists(expectedFile);
Expand Down
Loading