Skip to content

Commit 68c3ca4

Browse files
authored
Fix staged builders for single component records (#179)
Fixes #177
1 parent 5a75323 commit 68c3ca4

File tree

4 files changed

+63
-10
lines changed

4 files changed

+63
-10
lines changed

record-builder-processor/src/main/java/io/soabase/recordbuilder/processor/InternalRecordBuilderProcessor.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,6 @@ private void addOnceOnlySupport() {
196196
}
197197

198198
private void addStagedBuilderClasses() {
199-
if (recordComponents.size() < 2) {
200-
return;
201-
}
202-
203199
IntStream.range(0, recordComponents.size()).forEach(index -> {
204200
Optional<RecordClassType> nextComponent = ((index + 1) < recordComponents.size())
205201
? Optional.of(recordComponents.get(index + 1)) : Optional.empty();
@@ -723,10 +719,6 @@ private void addStaticDefaultBuilderMethod() {
723719
}
724720

725721
private void addStaticStagedBuilderMethod(String builderMethodName) {
726-
if (recordComponents.size() < 2) {
727-
return;
728-
}
729-
730722
/*
731723
* Adds the staged builder method similar to:
732724
*
@@ -757,11 +749,12 @@ private void addStaticStagedBuilderMethod(String builderMethodName) {
757749
codeBlock.addStatement(")");
758750
}
759751

752+
var returnType = stagedBuilderType(recordComponents.isEmpty() ? builderClassType : recordComponents.get(0));
753+
760754
var methodSpec = MethodSpec.methodBuilder(builderMethodName)
761755
.addJavadoc("Return the first stage of a staged builder\n")
762756
.addModifiers(Modifier.PUBLIC, Modifier.STATIC).addAnnotation(generatedRecordBuilderAnnotation)
763-
.addTypeVariables(typeVariables).returns(stagedBuilderType(recordComponents.get(0)).typeName())
764-
.addCode(codeBlock.build()).build();
757+
.addTypeVariables(typeVariables).returns(returnType.typeName()).addCode(codeBlock.build()).build();
765758
builder.addMethod(methodSpec);
766759
}
767760

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright 2019 The original author or authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.soabase.recordbuilder.test.staged;
17+
18+
import io.soabase.recordbuilder.core.RecordBuilder;
19+
20+
@RecordBuilder
21+
@RecordBuilder.Options(builderMode = RecordBuilder.BuilderMode.STAGED)
22+
public record NoFieldsStaged() {
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2019 The original author or authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.soabase.recordbuilder.test.staged;
17+
18+
import io.soabase.recordbuilder.core.RecordBuilder;
19+
20+
import java.time.Instant;
21+
22+
@RecordBuilder
23+
@RecordBuilder.Options(builderMode = RecordBuilder.BuilderMode.STAGED)
24+
public record SingleFieldStaged(int i) {
25+
}

record-builder-test/src/test/java/io/soabase/recordbuilder/test/staged/TestStagedBuilder.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,16 @@ void testGenericCombined() {
6161
.aT(new GenericStaged<>("other", builder.build(), BigInteger.TEN)).theUThing(BigDecimal.ONE).build();
6262
assertEquals(obj1, obj2);
6363
}
64+
65+
@Test
66+
void testSingleField() {
67+
SingleFieldStaged obj = SingleFieldStagedBuilder.builder().i(1).build();
68+
assertEquals(new SingleFieldStaged(1), obj);
69+
}
70+
71+
@Test
72+
void testNoFields() {
73+
NoFieldsStaged obj = NoFieldsStagedBuilder.builder().build();
74+
assertEquals(new NoFieldsStaged(), obj);
75+
}
6476
}

0 commit comments

Comments
 (0)