Skip to content

Commit

Permalink
Update mixins to use square brackets
Browse files Browse the repository at this point in the history
  • Loading branch information
JordonPhillips committed Jan 6, 2022
1 parent 00793b1 commit 44304ca
Show file tree
Hide file tree
Showing 50 changed files with 205 additions and 232 deletions.
62 changes: 27 additions & 35 deletions designs/mixins.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,18 @@ shape marked with the `@mixin` trait. Shapes can only use mixins that
are of the same shape type.

```
structure GetCityInput with CityResourceInput {
structure GetCityInput with [CityResourceInput] {
foo: String
}
```

Multiple mixins can be applied:

```
structure GetAnotherCityInput with
structure GetAnotherCityInput with [
CityResourceInput
SomeOtherMixin
{
]{
foo: String
}
```
Expand All @@ -145,11 +145,11 @@ structure MixinA {
}
@mixin
structure MixinB with MixinA {
structure MixinB with [MixinA] {
b: String
}
structure C with MixinB {
structure C with [MixinB] {
c: String
}
```
Expand Down Expand Up @@ -181,7 +181,7 @@ union UserActions {
unsubscribe: UnsubscribeAction,
}
union AdminActions with UserAction {
union AdminActions with [UserAction] {
banUser: BanUserAction,
promoteToAdmin: PromoteToAdminAction
}
Expand All @@ -204,7 +204,7 @@ structure UserInfo {
userId: String
}
structure UserSummary with UserInfo {}
structure UserSummary with [UserInfo] {}
```

Is equivalent to the following flattened structure because it inherits the
Expand All @@ -230,7 +230,7 @@ structure UserInfo {
/// Specific documentation
@tags(["replaced-tags"])
structure UserSummary with UserInfo {}
structure UserSummary with [UserInfo] {}
```

Is equivalent to the following flattened structure because it inherits the
Expand Down Expand Up @@ -268,13 +268,11 @@ structure StructB {}
/// C
@threeTrait
@mixin
structure StructC with
StructA
StructB {}
structure StructC with [StructA StructB] {}
/// D
@fourTrait
structure StructD with StructC {}
structure StructD with [StructC] {}
```

Is equivalent to the following flattened structure:
Expand Down Expand Up @@ -325,7 +323,7 @@ structure PrivateMixin {
foo: String
}
structure PublicShape with PrivateMixin {}
structure PublicShape with [PrivateMixin] {}
```

`PublicShape` is equivalent to the following flattened structure:
Expand Down Expand Up @@ -462,7 +460,7 @@ structure MyMixin {
mixinMember: String
}
structure MyStruct with MyMixin {}
structure MyStruct with [MyMixin] {}
apply MyStruct$mixinMember @documentation("Specific docs")
```

Expand All @@ -478,7 +476,7 @@ structure MyMixin {
mixinMember: String
}
structure MyStruct with MyMixin {
structure MyStruct with [MyMixin] {
/// Specific docs
mixinMember: String
}
Expand Down Expand Up @@ -533,10 +531,10 @@ Mixins MUST NOT introduce circular references. The following model is invalid:

```
@mixin
structure CycleA with CycleB {}
structure CycleA with [CycleB] {}
@mixin
structure CycleB with CycleA {}
structure CycleB with [CycleA] {}
```


Expand All @@ -557,9 +555,7 @@ structure A2 {
a: Integer
}
structure Invalid with
A1
A2 {}
structure Invalid with [A1 A2] {}
```

The following model is also invalid, but not specifically because of mixins.
Expand All @@ -577,9 +573,7 @@ structure A2 {
A: Integer
}
structure Invalid with
A1
A2 {}
structure Invalid with [A1 A2] {}
```

A shape MAY use a member name that has already defined, but if it does it MUST
Expand All @@ -599,9 +593,7 @@ structure A2 {
a: String
}
structure Valid with
A1
A2 {}
structure Valid with [A1 A2] {}
```


Expand All @@ -623,7 +615,7 @@ union_statement = "union" ws identifier ws [mixins ws] union_members
service_statement = "service" ws identifier ws [mixins ws] node_object
operation_statement = "operation" ws identifier ws [mixins ws] node_object
resource_statement = "resource" ws identifier ws [mixins ws] node_object
mixins = "with" 1*(ws shape_id)
mixins = "with" ws "[" 1*(ws shape_id) ws "]"
```


Expand Down Expand Up @@ -684,7 +676,7 @@ structure PaginatedInput {
pageSize: Integer
}
structure ListSomethingInput with PaginatedInput {
structure ListSomethingInput with [PaginatedInput] {
nameFilter: String
}
```
Expand Down Expand Up @@ -761,10 +753,10 @@ structure PaginatedInput {
pageSize: Integer
}
structure ListSomethingInput with
structure ListSomethingInput with [
PaginatedInput
FilteredByName
{
]{
sizeFilter: Integer
}
```
Expand Down Expand Up @@ -800,7 +792,7 @@ service A {
operation OperationB {}
@mixin
service B with A {
service B with [A] {
version: "B"
rename: {
"smithy.example#OperationA": "OperA"
Expand All @@ -811,7 +803,7 @@ service B with A {
operation OperationC {}
service C with B {
service C with [B] {
version: "C"
rename: {
"smithy.example#OperationA": "OpA"
Expand Down Expand Up @@ -981,7 +973,7 @@ mixin UserActions {
subscribe: SubscribeAction,
}
union AdminActions with UserAction {}
union AdminActions with [UserAction] {}
```

The `required` trait on `UserActions$subscribe` is valid, but it makes it so
Expand Down Expand Up @@ -1024,7 +1016,7 @@ structure FooMixin {
otherMember: String
}
structure ApplicationOfFooMixin with FooMixin {
structure ApplicationOfFooMixin with [FooMixin] {
// Remove the required trait from this member.
@override([omitTraits: [required]])
someMember: String
Expand All @@ -1048,7 +1040,7 @@ structure FooMixinOptional {
}
@mixin
structure FooMixinRequired with FooMixinOptional {}
structure FooMixinRequired with [FooMixinOptional] {}
apply FooMixinRequired$someMember @required
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ structure Mixin {
foo: String
}

structure UsesMixin with Mixin {
structure UsesMixin with [Mixin] {
baz: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -589,36 +589,16 @@ private void parseMixins(ShapeId id) {
}

ws();
expect('[');
ws();

do {
clearPendingDocs();
String target = ParserUtils.parseShapeId(this);
modelFile.addForwardReference(target, resolved -> modelFile.addPendingMixin(id, resolved));
ws();
} while (!peekEndWith());
}

private boolean peekEndWith() {
if (peek() == '{' || peek() == '@' || eof()) {
return true;
}

// We could make this more efficient with a prefix trie
for (String shapeType : SHAPE_TYPES) {
if (peekString(shapeType)) {
return true;
}
}

return false;
}

private boolean peekString(String possibility) {
for (int i = 0; i < possibility.length(); i++) {
if (peek(i) != possibility.charAt(i)) {
return false;
}
}
return !ParserUtils.isValidIdentifierCharacter(peek(possibility.length()));
} while (peek() != ']');
expect(']');
}

private void parseOperationStatement(ShapeId id, SourceLocation location) {
Expand Down Expand Up @@ -724,6 +704,7 @@ private void parseIdList(Consumer<ShapeId> consumer) {
private void parseServiceStatement(ShapeId id, SourceLocation location) {
ws();
parseMixins(id);
ws();
ServiceShape.Builder builder = new ServiceShape.Builder().id(id).source(location);
ObjectNode shapeNode = IdlNodeParser.parseObjectNode(this, id.toString());
LoaderUtils.checkForAdditionalProperties(shapeNode, id, SERVICE_PROPERTY_NAMES, modelFile.events());
Expand Down Expand Up @@ -754,6 +735,7 @@ private void optionalIdList(ObjectNode node, String name, Consumer<ShapeId> cons
private void parseResourceStatement(ShapeId id, SourceLocation location) {
ws();
parseMixins(id);
ws();
ResourceShape.Builder builder = ResourceShape.builder().id(id).source(location);
modelFile.onShape(builder);
ObjectNode shapeNode = IdlNodeParser.parseObjectNode(this, id.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ private static final class ShapeSerializer extends ShapeVisitor.Default<Void> {
protected Void getDefault(Shape shape) {
serializeTraits(shape);
codeWriter.writeInline("$L $L ", shape.getType(), shape.getId().getName());
writeMixins(shape, false);
writeMixins(shape);
codeWriter.write("").write("");
return null;
}
Expand All @@ -412,24 +412,22 @@ private void shapeWithMembers(Shape shape, List<MemberShape> members) {
serializeTraits(shape);
codeWriter.writeInline("$L $L ", shape.getType(), shape.getId().getName());

writeMixins(shape, !nonMixinMembers.isEmpty());
writeMixins(shape);
writeShapeMembers(nonMixinMembers);
codeWriter.write("");
applyIntroducedTraits(mixinMembers);
}

private void writeMixins(Shape shape, boolean hasNonMixinMembers) {
private void writeMixins(Shape shape) {
if (shape.getMixins().size() == 1) {
codeWriter.writeInline("with $I ", shape.getMixins().iterator().next());
codeWriter.writeInline("with [$I] ", shape.getMixins().iterator().next());
} else if (shape.getMixins().size() > 1) {
codeWriter.writeInline("with");
codeWriter.write("with [").indent();
for (ShapeId id : shape.getMixins()) {
// Trailing spaces are trimmed.
codeWriter.writeInline("\n $I ", id);
}
if (hasNonMixinMembers) {
codeWriter.write("");
codeWriter.write("$I", id);
}
codeWriter.dedent().writeInline("] ");
}
}

Expand Down Expand Up @@ -550,7 +548,7 @@ public Void unionShape(UnionShape shape) {
public Void serviceShape(ServiceShape shape) {
serializeTraits(shape);
codeWriter.writeInline("service $L ", shape.getId().getName());
writeMixins(shape, false);
writeMixins(shape);
codeWriter.openBlock("{");

ServiceShape preMixinShape = preMixinIndex.getPreMixinShape(shape).asServiceShape().get();
Expand All @@ -577,7 +575,7 @@ public Void serviceShape(ServiceShape shape) {
public Void resourceShape(ResourceShape shape) {
serializeTraits(shape);
codeWriter.writeInline("resource $L ", shape.getId().getName());
writeMixins(shape, false);
writeMixins(shape);
codeWriter.openBlock("{");
if (!shape.getIdentifiers().isEmpty()) {
codeWriter.openBlock("identifiers: {");
Expand Down Expand Up @@ -608,7 +606,7 @@ public Void operationShape(OperationShape shape) {
OperationShape preMixinShape = preMixinIndex.getPreMixinShape(shape).asOperationShape().get();
serializeTraits(shape);
codeWriter.writeInline("operation $L ", shape.getId().getName());
writeMixins(shape, false);
writeMixins(shape);
codeWriter.openBlock("{");
List<MemberShape> mixinMembers = new ArrayList<>();
mixinMembers.addAll(writeInlineableProperty("input", shape.getInputShape(), InputTrait.ID));
Expand Down Expand Up @@ -650,7 +648,7 @@ private Collection<MemberShape> writeInlineableProperty(String key, ShapeId shap
}
}

writeMixins(structure, !nonMixinMembers.isEmpty());
writeMixins(structure);
writeShapeMembers(nonMixinMembers);

if (!hasOnlyDefaultTrait(structure, defaultTrait)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ structure Foo {
bar: String
}

structure Baz with Foo {
structure Baz with [Foo] {
foo: Integer // cannot redefine mixin members!
bar: String // This is allowed because the target hasn't changed
}

@mixin
structure Bam with Foo {
structure Bam with [Foo] {
foo: Integer // cannot redefine mixin members!
bar: String // This is allowed because the target hasn't changed
}

structure Boo with Bam {
structure Boo with [Bam] {
foo: Long // cannot redefine mixin members!
bar: String // This is allowed because the target hasn't changed
}
Loading

0 comments on commit 44304ca

Please sign in to comment.