Skip to content

Commit

Permalink
Add convenience method to get IntEnumShape from GenerateIntEnumDirect…
Browse files Browse the repository at this point in the history
…ive (#2033)

Add a method to GenerateIntEnumDirective to give directed codegen implementers access to the contained shape as an IntEnum.
  • Loading branch information
hpmellema authored Nov 7, 2023
1 parent 3ea5215 commit 78f336f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package software.amazon.smithy.codegen.core.directed;

import software.amazon.smithy.codegen.core.CodegenContext;
import software.amazon.smithy.model.node.ExpectationNotMetException;
import software.amazon.smithy.model.shapes.IntEnumShape;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;

Expand All @@ -29,6 +31,19 @@
public final class GenerateIntEnumDirective<C extends CodegenContext<S, ?, ?>, S> extends ShapeDirective<Shape, C, S> {

GenerateIntEnumDirective(C context, ServiceShape service, Shape shape) {
super(context, service, shape);
super(context, service, validateShape(shape));
}

private static Shape validateShape(Shape shape) {
if (shape.isIntEnumShape()) {
return shape;
}
throw new IllegalArgumentException("GenerateIntEnum requires an IntEnum shape");
}


public IntEnumShape expectIntEnumShape() {
return shape().asIntEnumShape().orElseThrow(() -> new ExpectationNotMetException(
"Expected an IntEnum shape, but found " + shape(), shape()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package software.amazon.smithy.codegen.core.directed;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import software.amazon.smithy.model.shapes.BooleanShape;

public class GenerateIntEnumDirectiveTest {
@Test
public void validatesShapeType() {
BooleanShape shape = BooleanShape.builder().id("smithy.example#Foo").build();

Assertions.assertThrows(IllegalArgumentException.class, () -> {
new GenerateIntEnumDirective<TestContext, TestSettings>(null, null, shape);
});
}
}

0 comments on commit 78f336f

Please sign in to comment.