Skip to content

Commit 34ca331

Browse files
committed
Move the validation from ModelTransformer.java to RemoveShapes.java.
1 parent bbb21c4 commit 34ca331

File tree

3 files changed

+34
-26
lines changed

3 files changed

+34
-26
lines changed

docs/source-2.0/spec/mixins.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ Alternatively, the member can be redefined if it targets the same shape:
300300
301301
.. warning::
302302

303-
Excluding copied mixin members explicitly through transforms will result in undefined behavior and an inconsistent model.
303+
Removing mixed in members through transforms will result in an inconsistent model.
304304

305305
Mixins are an implementation detail of the model
306306
================================================

smithy-model/src/main/java/software/amazon/smithy/model/transform/ModelTransformer.java

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import java.util.function.Function;
1919
import java.util.function.Predicate;
2020
import java.util.function.Supplier;
21-
import java.util.logging.Logger;
2221
import software.amazon.smithy.model.Model;
2322
import software.amazon.smithy.model.loader.ModelAssembler;
2423
import software.amazon.smithy.model.neighbor.UnreferencedShapes;
@@ -42,7 +41,6 @@
4241
public final class ModelTransformer {
4342
private final List<ModelTransformerPlugin> plugins;
4443

45-
private static final Logger LOGGER = Logger.getLogger(ModelTransformer.class.getName());
4644

4745
private ModelTransformer(List<ModelTransformerPlugin> plugins) {
4846
this.plugins = ListUtils.copyOf(plugins);
@@ -119,32 +117,10 @@ public Model removeShapes(Model model, Collection<? extends Shape> shapes) {
119117
if (shapes.isEmpty()) {
120118
return model;
121119
}
122-
validateMembersFromMixin(model, shapes);
120+
123121
return new RemoveShapes(shapes, plugins).transform(this, model);
124122
}
125123

126-
private void validateMembersFromMixin(Model model, Collection<? extends Shape> shapesToExclude) {
127-
for (Shape shape : shapesToExclude) {
128-
// Only validate MemberShapes
129-
if (!shape.isMemberShape()) {
130-
continue;
131-
}
132-
MemberShape memberShape = shape.asMemberShape().get();
133-
Optional<Shape> container = model.getShape(memberShape.getContainer());
134-
if (container.isPresent()) {
135-
for (ShapeId mixinId : container.get().getMixins()) {
136-
Shape mixinShape = model.expectShape(mixinId);
137-
if (mixinShape.getMemberNames().contains(shape.getId().getMember().get())) {
138-
LOGGER.warning(format("Excluding copied mixin member `%s` from mixin shape `%s` explicitly "
139-
+ "in `%s` will result in undefined behavior and an inconsistent model!",
140-
memberShape.getMemberName(),
141-
mixinShape.getId(),
142-
container.get().getId().getName()));
143-
}
144-
}
145-
}
146-
}
147-
}
148124

149125
/**
150126
* Removes shapes from the model that match the given predicate.

smithy-model/src/main/java/software/amazon/smithy/model/transform/RemoveShapes.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,15 @@
77
import java.util.Collection;
88
import java.util.HashSet;
99
import java.util.List;
10+
import java.util.Optional;
1011
import java.util.Set;
12+
import java.util.logging.Logger;
1113
import software.amazon.smithy.model.Model;
14+
import software.amazon.smithy.model.shapes.MemberShape;
1215
import software.amazon.smithy.model.shapes.Shape;
16+
import software.amazon.smithy.model.shapes.ShapeId;
17+
18+
import static java.lang.String.format;
1319

1420
/**
1521
* Removes shapes from a model while ensuring that relationships to/from
@@ -27,6 +33,8 @@ final class RemoveShapes {
2733
private final Collection<? extends Shape> toRemove;
2834
private final List<ModelTransformerPlugin> plugins;
2935

36+
private static final Logger LOGGER = Logger.getLogger(RemoveShapes.class.getName());
37+
3038
RemoveShapes(Collection<? extends Shape> toRemove, List<ModelTransformerPlugin> plugins) {
3139
this.toRemove = toRemove;
3240
this.plugins = plugins;
@@ -37,6 +45,7 @@ Model transform(ModelTransformer transformer, Model model) {
3745

3846
// Iteratively add each shape that needs to be removed from the index using multiple rounds.
3947
Set<Shape> removed = new HashSet<>(toRemove);
48+
validateMembersFromMixin(model, removed);
4049
for (Shape removedShape : toRemove) {
4150
builder.removeShape(removedShape.getId());
4251
// We don't need to remove members from the builder since
@@ -52,4 +61,27 @@ Model transform(ModelTransformer transformer, Model model) {
5261

5362
return result;
5463
}
64+
65+
private void validateMembersFromMixin(Model model, Collection<? extends Shape> shapesToExclude) {
66+
for (Shape shape : shapesToExclude) {
67+
// Only validate MemberShapes
68+
if (!shape.isMemberShape()) {
69+
continue;
70+
}
71+
MemberShape memberShape = shape.asMemberShape().get();
72+
Optional<Shape> container = model.getShape(memberShape.getContainer());
73+
if (container.isPresent()) {
74+
for (ShapeId mixinId : container.get().getMixins()) {
75+
Shape mixinShape = model.expectShape(mixinId);
76+
if (mixinShape.getMemberNames().contains(shape.getId().getMember().get())) {
77+
LOGGER.warning(format("Removing mixed in member `%s` from mixin shape `%s` "
78+
+ "in `%s` will result in an inconsistent model.",
79+
memberShape.getMemberName(),
80+
mixinShape.getId(),
81+
container.get().getId().getName()));
82+
}
83+
}
84+
}
85+
}
86+
}
5587
}

0 commit comments

Comments
 (0)