Skip to content

Commit 24af34c

Browse files
committed
Add sourceLocation setting in fromNode() in trait codegen
1 parent 8159e00 commit 24af34c

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

smithy-trait-codegen/src/it/java/software/amazon/smithy/traitcodegen/test/CreatesTraitTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
public class CreatesTraitTest {
7272
private static final ShapeId DUMMY_ID = ShapeId.from("ns.foo#foo");
7373
private final TraitFactory provider = TraitFactory.createServiceFactory();
74+
private static final SourceLocation testLocation = new SourceLocation("test.smithy", 1, 2);
7475

7576
static Stream<Arguments> createTraitTests() {
7677
return Stream.of(
@@ -236,4 +237,44 @@ void createsTraitFromNode(ShapeId traitId, Node fromNode) {
236237
assertEquals(SourceLocation.NONE, trait.getSourceLocation());
237238
assertEquals(trait, provider.createTrait(traitId, DUMMY_ID, trait.toNode()).orElseThrow(RuntimeException::new));
238239
}
240+
241+
static Stream<Arguments> createSourceLocationTests() {
242+
return Stream.of(
243+
Arguments.of(NumberListTrait.ID,
244+
ArrayNode.builder()
245+
.withValue(Node.from(1))
246+
.withValue(Node.from(2))
247+
.withValue(Node.from(3))
248+
.sourceLocation(testLocation)
249+
.build()
250+
.toNode()),
251+
Arguments.of(NumberSetTrait.ID,
252+
ArrayNode.builder()
253+
.withValue(Node.from(1))
254+
.withValue(Node.from(2))
255+
.withValue(Node.from(3))
256+
.sourceLocation(testLocation)
257+
.build()
258+
.toNode()),
259+
Arguments.of(NestedMapTrait.ID,
260+
NestedMapTrait.builder()
261+
.putValues("1", MapUtils.of("1", MapUtils.of("2", "3")))
262+
.sourceLocation(testLocation)
263+
.build()
264+
.toNode()),
265+
Arguments.of(StructWithListOfMapTrait.ID,
266+
StructWithListOfMapTrait.builder()
267+
.addItems(MapUtils.of("1", "2"))
268+
.addItems(MapUtils.of("3", "4"))
269+
.sourceLocation(testLocation)
270+
.build()
271+
.toNode()));
272+
}
273+
274+
@ParameterizedTest
275+
@MethodSource("createSourceLocationTests")
276+
void sourceLocationTest(ShapeId traitId, Node fromNode) {
277+
Trait trait = provider.createTrait(traitId, DUMMY_ID, fromNode).orElseThrow(RuntimeException::new);
278+
assertEquals(testLocation, trait.getSourceLocation());
279+
}
239280
}

smithy-trait-codegen/src/main/java/software/amazon/smithy/traitcodegen/generators/FromNodeGenerator.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import software.amazon.smithy.model.shapes.UnionShape;
3838
import software.amazon.smithy.model.traits.IdRefTrait;
3939
import software.amazon.smithy.model.traits.TimestampFormatTrait;
40+
import software.amazon.smithy.model.traits.TraitDefinition;
4041
import software.amazon.smithy.model.traits.UniqueItemsTrait;
4142
import software.amazon.smithy.traitcodegen.SymbolProperties;
4243
import software.amazon.smithy.traitcodegen.TraitCodegenUtils;
@@ -83,7 +84,8 @@ public Void listShape(ListShape shape) {
8384
symbol,
8485
Node.class,
8586
() -> {
86-
writer.writeWithNoFormatting("Builder builder = builder();");
87+
writer.writeWithNoFormatting("Builder builder = builder()" +
88+
(shape.hasTrait(TraitDefinition.ID) ? ".sourceLocation(node);" : ";"));
8789
shape.accept(new FromNodeMapperVisitor(writer, model, "node", symbolProvider));
8890
writer.write("builder.values(value0);");
8991
writer.writeWithNoFormatting("return builder.build();");
@@ -101,7 +103,8 @@ public Void mapShape(MapShape shape) {
101103
symbol,
102104
Node.class,
103105
() -> {
104-
writer.writeWithNoFormatting("Builder builder = builder();");
106+
writer.writeWithNoFormatting("Builder builder = builder()" +
107+
(shape.hasTrait(TraitDefinition.ID) ? ".sourceLocation(node);" : ";"));
105108
shape.accept(new FromNodeMapperVisitor(writer, model, "node", symbolProvider));
106109
writer.writeWithNoFormatting("return builder.build();");
107110
});
@@ -159,7 +162,8 @@ public Void structureShape(StructureShape shape) {
159162
writeFromNodeJavaDoc();
160163
writer.write("public static $T fromNode($T node) {", symbol, Node.class);
161164
writer.indent();
162-
writer.write("Builder builder = builder();");
165+
writer.write("Builder builder = builder()" +
166+
(shape.hasTrait(TraitDefinition.ID) ? ".sourceLocation(node);" : ";"));
163167
// If the shape has no members (i.e. is an annotation trait) then there will be no member setters, and we
164168
// need to terminate the line.
165169
writer.putContext("isEmpty", shape.members().isEmpty());

0 commit comments

Comments
 (0)