Skip to content

Commit

Permalink
add runtime checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Yuan committed Apr 24, 2023
1 parent 6c2cf2a commit 6c0e937
Showing 1 changed file with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import software.amazon.smithy.diff.ModelDiff;
import software.amazon.smithy.go.codegen.AddOperationShapes;
import software.amazon.smithy.go.codegen.GoSettings;
import software.amazon.smithy.go.codegen.Synthetic;
import software.amazon.smithy.go.codegen.integration.GoIntegration;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.loader.ModelAssembler;
Expand Down Expand Up @@ -154,7 +155,7 @@ public Model preprocessModel(Model model, GoSettings settings) {
}
throw new CodegenException(sb.toString());
}
validateNullabilityExceptions(nullabilityExceptions, model);
validateNullabilityExceptions(nullabilityExceptions, model, service);
return model;
}

Expand Down Expand Up @@ -318,7 +319,7 @@ private static Model handleApiGateWayExportsNullabilityExceptions(
return ModelTransformer.create().replaceShapes(model, shapesToReplace);
}

private static void validateNullabilityExceptions(Set<ShapeId> nullabilityExceptions, Model model) {
private static void validateNullabilityExceptions(Set<ShapeId> nullabilityExceptions, Model model, ShapeId service) {
Map<ShapeId, Shape> nullabilityExceptionMap = new HashMap<>();
for (ShapeId shapeId : nullabilityExceptions) {
if (model.getShape(shapeId).isPresent()) {
Expand All @@ -327,6 +328,31 @@ private static void validateNullabilityExceptions(Set<ShapeId> nullabilityExcept
LOGGER.warning("Shape `" + shapeId + "` nullability exception is not present in the model");
}
}

for (BooleanShape shape : model.getBooleanShapesWithTrait(DefaultTrait.class)) {
ShapeId shapeId = shape.toShapeId();
String namespace = shapeId.getNamespace();
if (!namespace.equals(service.getNamespace()) && !namespace.equals(Synthetic.ID.getNamespace())) {
continue;
}
if (!nullabilityExceptions.contains(shapeId)) {
throw new CodegenException("Shape `" + shapeId + "` should be in nullability exceptions");
}
}

for (NumberShape shape : model.toSet(NumberShape.class).stream()
.filter(s -> s.hasTrait(DefaultTrait.class))
.collect(Collectors.toList())) {
ShapeId shapeId = shape.toShapeId();
String namespace = shapeId.getNamespace();
if (!namespace.equals(service.getNamespace()) && !namespace.equals(Synthetic.ID.getNamespace())) {
continue;
}
if (!nullabilityExceptions.contains(shapeId)) {
throw new CodegenException("Shape `" + shapeId + "` should be in nullability exceptions");
}
}

// Existing “defaulted” root boolean or number shapes MUST be backfilled with a
// default trait.
for (Map.Entry<ShapeId, Shape> entry : nullabilityExceptionMap.entrySet()) {
Expand Down

0 comments on commit 6c0e937

Please sign in to comment.