Skip to content

Commit

Permalink
Ensure SourceLocations are on all StringListTrait traits
Browse files Browse the repository at this point in the history
  • Loading branch information
mtdowling committed Sep 15, 2020
1 parent a1d629e commit 42444bc
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ public Provider() {

@Override
public Builder toBuilder() {
ConditionKeysTrait.Builder builder = builder().sourceLocation(getSourceLocation());
getValues().forEach(builder::addValue);
return builder;
return builder().sourceLocation(getSourceLocation()).values(getValues());
}

public static final class Builder extends StringListTrait.Builder<ConditionKeysTrait, Builder> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ public static Builder builder() {

@Override
public Builder toBuilder() {
Builder builder = builder().sourceLocation(getSourceLocation());
getValues().forEach(builder::addValue);
return builder;
return builder().sourceLocation(getSourceLocation()).values(getValues());
}

public static final class Builder extends StringListTrait.Builder<RequiredActionsTrait, Builder> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ public StringListTrait(ShapeId id, List<String> values, FromSourceLocation sourc

@Override
protected final Node createNode() {
return ArrayNode.fromStrings(values);
List<Node> nodes = new ArrayList<>(values.size());
for (String value : values) {
nodes.add(Node.from(value));
}
return new ArrayNode(nodes, getSourceLocation());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Provider() {

@Override
public Builder toBuilder() {
return builder().values(getValues());
return builder().sourceLocation(getSourceLocation()).values(getValues());
}

public static Builder builder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@

import java.util.Optional;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.model.SourceLocation;
import software.amazon.smithy.model.node.ArrayNode;
import software.amazon.smithy.model.node.Node;
import software.amazon.smithy.model.node.StringNode;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.utils.ListUtils;

public class TagsTraitTest {
@Test
Expand All @@ -40,4 +44,18 @@ public void loadsTrait() {
assertThat(tagsTrait.getValues(), contains("experimental"));
assertThat(tagsTrait.toNode(), equalTo(node));
}

@Test
public void hasSourceLocation() {
SourceLocation location1 = new SourceLocation("/foo.smithy", 1, 1);
SourceLocation location2 = new SourceLocation("/foo.smithy", 1, 2);
ArrayNode node = new ArrayNode(ListUtils.of(new StringNode("a", location1)), location2);
ShapeId id = ShapeId.from("ns.qux#foo");
TraitFactory provider = TraitFactory.createServiceFactory();
Optional<Trait> trait = provider.createTrait(ShapeId.from("smithy.api#tags"), id, node);

assertTrue(trait.isPresent());
assertThat(trait.get().getSourceLocation(), equalTo(location2));
assertThat(trait.get().toNode().getSourceLocation(), equalTo(location2));
}
}

0 comments on commit 42444bc

Please sign in to comment.