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

Remove invalid defaults from a few services #2458

Merged
merged 2 commits into from
Jan 18, 2024
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
8 changes: 8 additions & 0 deletions .changelog/24113a10aa884c7dbef8b7c9989da291.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "24113a10-aa88-4c7d-bef8-b7c9989da291",
"type": "feature",
"description": "**BREAKFIX**: Correct nullability of InitialCapacityConfig's WorkerCount field. The type of this value has changed from int64 to *int64. Due to this field being marked required, with an enforced minimum of 1, but a default of 0, the former type would result in automatic failure behavior without caller intervention. Calling code will need to be updated accordingly.",
"modules": [
"service/emrserverless"
]
}
8 changes: 8 additions & 0 deletions .changelog/d4bc92a6c4064c22bd2b44f9a82587ce.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"id": "d4bc92a6-c406-4c22-bd2b-44f9a82587ce",
"type": "feature",
"description": "**BREAKFIX**: Correct nullability of GetExperimentResultsInput's Period field. The type of this value has changed from int64 to *int64. Due to the nature of default value serialization, the former type could cause unexpected/incorrect behavior when set to its zero value. Calling code will need to be updated accordingly.",
"modules": [
"service/evidently"
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,36 @@
import software.amazon.smithy.go.codegen.GoSettings;
import software.amazon.smithy.go.codegen.integration.GoIntegration;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.shapes.AbstractShapeBuilder;
import software.amazon.smithy.model.shapes.ShapeId;
import software.amazon.smithy.model.traits.DefaultTrait;
import software.amazon.smithy.model.transform.ModelTransformer;
import software.amazon.smithy.utils.MapUtils;
import software.amazon.smithy.utils.SetUtils;
import software.amazon.smithy.utils.ToSmithyBuilder;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

public class RemoveDefaults implements GoIntegration {
private static final Map<ShapeId, Set<ShapeId>> toRemove = MapUtils.of(
ShapeId.from("com.amazonaws.s3control#AWSS3ControlServiceV20180820"), SetUtils.of(
ShapeId.from("com.amazonaws.s3control#PublicAccessBlockConfiguration$BlockPublicAcls"),
ShapeId.from("com.amazonaws.s3control#PublicAccessBlockConfiguration$IgnorePublicAcls"),
ShapeId.from("com.amazonaws.s3control#PublicAccessBlockConfiguration$BlockPublicPolicy"),
ShapeId.from("com.amazonaws.s3control#PublicAccessBlockConfiguration$RestrictPublicBuckets")
)
);
private static final Map<ShapeId, Set<ShapeId>> toRemove = MapUtils.ofEntries(
serviceToShapeIds("com.amazonaws.s3control#AWSS3ControlServiceV20180820",
"com.amazonaws.s3control#PublicAccessBlockConfiguration$BlockPublicAcls",
"com.amazonaws.s3control#PublicAccessBlockConfiguration$IgnorePublicAcls",
"com.amazonaws.s3control#PublicAccessBlockConfiguration$BlockPublicPolicy",
"com.amazonaws.s3control#PublicAccessBlockConfiguration$RestrictPublicBuckets"),
serviceToShapeIds("com.amazonaws.evidently#Evidently",
"com.amazonaws.evidently#ResultsPeriod"),
serviceToShapeIds("com.amazonaws.amplifyuibuilder#AmplifyUIBuilder",
"smithy.go.synthetic#ListPlaceIndexesInput$MaxResults",
"smithy.go.synthetic#SearchPlaceIndexForSuggestionsInput$MaxResults",
"com.amazonaws.location#PlaceIndexSearchResultLimit"),
serviceToShapeIds("com.amazonaws.paymentcryptographydata#PaymentCryptographyDataPlane",
"com.amazonaws.paymentcryptographydata#IntegerRangeBetween4And12"),
serviceToShapeIds("com.amazonaws.emrserverless#AwsToledoWebService",
"com.amazonaws.emrserverless#WorkerCounts"));

private boolean mustPreprocess(ShapeId service) {
return toRemove.containsKey(service);
Expand All @@ -38,16 +47,52 @@ public Model preprocessModel(Model model, GoSettings settings) {
}

private Model removeDefaults(Model model, Set<ShapeId> fromShapes) {
return ModelTransformer.create().mapShapes(model, it ->
fromShapes.contains(it.getId())
? withoutDefault(it)
: it
);
Set<ShapeId> removedRootDefaults = new HashSet<>();
Model removedRootDefaultsModel = ModelTransformer.create().mapShapes(model, (shape) -> {
if (shouldRemoveRootDefault(shape, fromShapes)) {
removedRootDefaults.add(shape.getId());
return withoutDefault(shape);
} else {
return shape;
}
});
return ModelTransformer.create().mapShapes(removedRootDefaultsModel, (shape) -> {
if (shouldRemoveMemberDefault(shape, removedRootDefaults, fromShapes)) {
return withoutDefault(shape);
} else {
return shape;
}
});
}

private boolean shouldRemoveRootDefault(Shape shape, Set<ShapeId> removeDefaultsFrom) {
return !shape.isMemberShape()
&& removeDefaultsFrom.contains(shape.getId())
&& shape.hasTrait(DefaultTrait.class);
}

private boolean shouldRemoveMemberDefault(
Shape shape,
Set<ShapeId> removedRootDefaults,
Set<ShapeId> removeDefaultsFrom
) {
if (!shape.isMemberShape()) {
return false;
}
MemberShape member = shape.asMemberShape().get();
return (removedRootDefaults.contains(member.getTarget()) || removeDefaultsFrom.contains(member.getId()))
&& member.hasTrait(DefaultTrait.class);
}

private Shape withoutDefault(Shape shape) {
return Shape.shapeToBuilder(shape)
.removeTrait(DefaultTrait.ID)
.build();
}

private static Map.Entry<ShapeId, Set<ShapeId>> serviceToShapeIds(String serviceId, String... shapeIds) {
return Map.entry(
ShapeId.from(serviceId),
Arrays.stream(shapeIds).map(ShapeId::from).collect(Collectors.toSet()));
}
}
2 changes: 1 addition & 1 deletion service/emrserverless/deserializers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions service/emrserverless/serializers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/emrserverless/types/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions service/emrserverless/validators.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion service/evidently/api_op_GetExperimentResults.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions service/evidently/serializers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading