Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions docs/source-2.0/guides/smithy-build-json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1518,6 +1518,28 @@ key is not in the provided ``keys`` list.
}
}

.. _flattenAndRemoveMixins:

flattenAndRemoveMixins
----------------------

Flattens :ref:`mixins <Mixins>` out of the model and into their local shapes.

.. code-block:: json

{
"version": "1.0",
"projections": {
"exampleProjection": {
"transforms": [
{
"name": "flattenAndRemoveMixins"
}
]
}
}
}

.. _flattenNamespaces:

flattenNamespaces
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.build.transforms;

import software.amazon.smithy.build.ProjectionTransformer;
import software.amazon.smithy.build.TransformContext;
import software.amazon.smithy.model.Model;

/**
* {@code flattenAndRemoveMixins} flattens mixins out of the model.
*/
public final class FlattenAndRemoveMixins implements ProjectionTransformer {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: doesn't flatten=removing for mixings? if not, then naming seems ok

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm I see the transformer uses that name


@Override
public String getName() {
return "flattenAndRemoveMixins";
}

@Override
public Model transform(TransformContext context) {
Model model = context.getModel();
return context.getTransformer().flattenAndRemoveMixins(model);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.build.transforms;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.shapes.StructureShape;
import software.amazon.smithy.model.traits.MixinTrait;
import software.amazon.smithy.model.transform.ModelTransformer;

public class FlattenAndRemoveMixinsTest {

@Test
void compareTransform() {
Model before = Model.assembler()
.addImport(FlattenAndRemoveMixinsTest.class.getResource("flatten-and-remove-mixins.smithy"))
.assemble()
.unwrap();
Model result = ModelTransformer.create().flattenAndRemoveMixins(before);

assertTrue(result.getShapesWithTrait(MixinTrait.class).isEmpty());
assertThat(result.expectShape(ShapeId.from("smithy.example#Foo"), StructureShape.class).getMemberNames(),
contains("bar", "foo"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
$version: "2.0"

namespace smithy.example

@mixin
structure FooMixin {
bar: String
}

structure Foo with [FooMixin] {
foo: String
}
Loading