Skip to content

Commit

Permalink
Fix staged builders for single component records (#179)
Browse files Browse the repository at this point in the history
Fixes #177
  • Loading branch information
Randgalt authored Apr 11, 2024
1 parent 5a75323 commit 68c3ca4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,6 @@ private void addOnceOnlySupport() {
}

private void addStagedBuilderClasses() {
if (recordComponents.size() < 2) {
return;
}

IntStream.range(0, recordComponents.size()).forEach(index -> {
Optional<RecordClassType> nextComponent = ((index + 1) < recordComponents.size())
? Optional.of(recordComponents.get(index + 1)) : Optional.empty();
Expand Down Expand Up @@ -723,10 +719,6 @@ private void addStaticDefaultBuilderMethod() {
}

private void addStaticStagedBuilderMethod(String builderMethodName) {
if (recordComponents.size() < 2) {
return;
}

/*
* Adds the staged builder method similar to:
*
Expand Down Expand Up @@ -757,11 +749,12 @@ private void addStaticStagedBuilderMethod(String builderMethodName) {
codeBlock.addStatement(")");
}

var returnType = stagedBuilderType(recordComponents.isEmpty() ? builderClassType : recordComponents.get(0));

var methodSpec = MethodSpec.methodBuilder(builderMethodName)
.addJavadoc("Return the first stage of a staged builder\n")
.addModifiers(Modifier.PUBLIC, Modifier.STATIC).addAnnotation(generatedRecordBuilderAnnotation)
.addTypeVariables(typeVariables).returns(stagedBuilderType(recordComponents.get(0)).typeName())
.addCode(codeBlock.build()).build();
.addTypeVariables(typeVariables).returns(returnType.typeName()).addCode(codeBlock.build()).build();
builder.addMethod(methodSpec);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright 2019 The original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.soabase.recordbuilder.test.staged;

import io.soabase.recordbuilder.core.RecordBuilder;

@RecordBuilder
@RecordBuilder.Options(builderMode = RecordBuilder.BuilderMode.STAGED)
public record NoFieldsStaged() {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2019 The original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.soabase.recordbuilder.test.staged;

import io.soabase.recordbuilder.core.RecordBuilder;

import java.time.Instant;

@RecordBuilder
@RecordBuilder.Options(builderMode = RecordBuilder.BuilderMode.STAGED)
public record SingleFieldStaged(int i) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,16 @@ void testGenericCombined() {
.aT(new GenericStaged<>("other", builder.build(), BigInteger.TEN)).theUThing(BigDecimal.ONE).build();
assertEquals(obj1, obj2);
}

@Test
void testSingleField() {
SingleFieldStaged obj = SingleFieldStagedBuilder.builder().i(1).build();
assertEquals(new SingleFieldStaged(1), obj);
}

@Test
void testNoFields() {
NoFieldsStaged obj = NoFieldsStagedBuilder.builder().build();
assertEquals(new NoFieldsStaged(), obj);
}
}

0 comments on commit 68c3ca4

Please sign in to comment.