Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience method to get IntEnumShape from GenerateIntEnumDirective #2033

Merged
merged 3 commits into from
Nov 7, 2023
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
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);
});
}
}