Skip to content

Commit 279e433

Browse files
committed
Add flattenAndRemoveMixins build transform
1 parent 494ddef commit 279e433

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

docs/source-2.0/guides/smithy-build-json.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1518,6 +1518,28 @@ key is not in the provided ``keys`` list.
15181518
}
15191519
}
15201520
1521+
.. _flattenAndRemoveMixins:
1522+
1523+
flattenAndRemoveMixins
1524+
----------------------
1525+
1526+
Flattens :ref:`mixins <Mixins>` out of the model and into their local shapes.
1527+
1528+
.. code-block:: json
1529+
1530+
{
1531+
"version": "1.0",
1532+
"projections": {
1533+
"exampleProjection": {
1534+
"transforms": [
1535+
{
1536+
"name": "flattenAndRemoveMixins"
1537+
}
1538+
]
1539+
}
1540+
}
1541+
}
1542+
15211543
.. _flattenNamespaces:
15221544

15231545
flattenNamespaces
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package software.amazon.smithy.build.transforms;
6+
7+
import software.amazon.smithy.build.ProjectionTransformer;
8+
import software.amazon.smithy.build.TransformContext;
9+
import software.amazon.smithy.model.Model;
10+
11+
/**
12+
* {@code flattenAndRemoveMixins} flattens mixins out of the model.
13+
*/
14+
public final class FlattenAndRemoveMixins implements ProjectionTransformer {
15+
16+
@Override
17+
public String getName() {
18+
return "flattenAndRemoveMixins";
19+
}
20+
21+
@Override
22+
public Model transform(TransformContext context) {
23+
Model model = context.getModel();
24+
return context.getTransformer().flattenAndRemoveMixins(model);
25+
}
26+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package software.amazon.smithy.build.transforms;
6+
7+
import static org.hamcrest.MatcherAssert.assertThat;
8+
import static org.hamcrest.Matchers.contains;
9+
import static org.junit.jupiter.api.Assertions.assertTrue;
10+
11+
import org.junit.jupiter.api.Test;
12+
import software.amazon.smithy.model.Model;
13+
import software.amazon.smithy.model.shapes.ShapeId;
14+
import software.amazon.smithy.model.shapes.StructureShape;
15+
import software.amazon.smithy.model.traits.MixinTrait;
16+
import software.amazon.smithy.model.transform.ModelTransformer;
17+
18+
public class FlattenAndRemoveMixinsTest {
19+
20+
@Test
21+
void compareTransform() {
22+
Model before = Model.assembler()
23+
.addImport(FlattenAndRemoveMixinsTest.class.getResource("flatten-and-remove-mixins.smithy"))
24+
.assemble()
25+
.unwrap();
26+
Model result = ModelTransformer.create().flattenAndRemoveMixins(before);
27+
28+
assertTrue(result.getShapesWithTrait(MixinTrait.class).isEmpty());
29+
assertThat(result.expectShape(ShapeId.from("smithy.example#Foo"), StructureShape.class).getMemberNames(),
30+
contains("bar", "foo"));
31+
}
32+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
$version: "2.0"
2+
3+
namespace smithy.example
4+
5+
@mixin
6+
structure FooMixin {
7+
bar: String
8+
}
9+
10+
structure Foo with [FooMixin] {
11+
foo: String
12+
}

0 commit comments

Comments
 (0)