Skip to content

Allow member to inherit from more than one mixin#1715

Merged
sugmanue merged 2 commits intosmithy-lang:mainfrom
sugmanue:members-can-inherit-from-multiple-mixins
Apr 5, 2023
Merged

Allow member to inherit from more than one mixin#1715
sugmanue merged 2 commits intosmithy-lang:mainfrom
sugmanue:members-can-inherit-from-multiple-mixins

Conversation

@sugmanue
Copy link
Copy Markdown
Contributor

@sugmanue sugmanue commented Apr 3, 2023

Description of changes:

Allow multiple mixins to override the same member if they all target the same shape. This will allow the following example from the docs

@mixin
structure A1 {
    @private
    a: String
}

@mixin
structure A2 {
    @required
    a: String
}

structure Valid with [A1, A2] {}

To actually work as documented. This will also bring parity to the way mixins behave for top-level shapes, it will allow multiple mixins to override the same member provided that they all agree on the same target and will override traits with a last introduced wins policy.

Traits attached to each member will be overwritten by the last mixin or by the corresponding member in the shape. For instance, the following model

@mixin
structure A1  {
    @tags(["a"])
    a: String
}

@mixin
structure A2  {
    @tags(["b"])
    a: String
}

@mixin
structure Valid with [A1 A2]  {
    @tags(["c"])
    a: String
}

Will be flattened to the following shape:

structure Valid with [A1 A2]  {
    @tags(["c"])
    a: String
}

Notice that the tags trait in the Valid structure will supersede the tags introduced by the mixin members.

And the following model:

@mixin
structure A1  {
    @tags(["a"])
    a: String
}

@mixin
structure A2  {
    @tags(["b"])
    a: String
}

@mixin
structure Valid with [A1 A2]  {
    a: String
}

Will be flattened to the following shape:

structure Valid with [A1 A2]  {
    @tags(["b"])
    a: String
}

Overriding the @tags in the order the mixins are processed, last wins.

This change also forbids following the docs overriding using a different target, for instance

$version: "2.0"

namespace com.amazon.example

@mixin
structure A1 {
    a: Integer
}

@mixin
structure A2 {
    a: String
}

structure Valid with [A1 A2] {
    a: Integer
    c: Integer
}

Will emit a validation error like:

Member conflicts with an inherited mixin member: `com.amazon.example#A2$a`

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@sugmanue sugmanue requested a review from a team as a code owner April 3, 2023 20:50
*
* @return Returns the optional member with the given name.
*/
public Optional<MemberShape> getMember(String memberName) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

list and map should override this too I think

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Totally, let me update it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants