Skip to content

Commit 08cbf80

Browse files
committed
Fixing glacier marshalling
1 parent f67a509 commit 08cbf80

File tree

7 files changed

+92
-50
lines changed

7 files changed

+92
-50
lines changed

codegen/src/main/java/software/amazon/awssdk/codegen/model/config/customization/CustomizationConfig.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package software.amazon.awssdk.codegen.model.config.customization;
1717

1818
import java.util.ArrayList;
19+
import java.util.HashMap;
1920
import java.util.List;
2021
import java.util.Map;
2122
import software.amazon.awssdk.codegen.model.config.templates.CodeGenTemplatesConfig;
@@ -126,7 +127,7 @@ public class CustomizationConfig {
126127
private String sdkResponseBaseClassName;
127128
private String defaultExceptionUnmarshaller;
128129

129-
private Map<String, String> modelMarshallerDefaultValueSupplier;
130+
private Map<String, String> modelMarshallerDefaultValueSupplier = new HashMap<>();
130131

131132
private boolean useAutoConstructList = true;
132133

codegen/src/main/java/software/amazon/awssdk/codegen/poet/model/AwsServiceModel.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ public AwsServiceModel(IntermediateModel intermediateModel, ShapeModel shapeMode
7373
this.shapeModelSpec = new ShapeModelSpec(this.shapeModel,
7474
typeProvider,
7575
poetExtensions,
76-
intermediateModel.getNamingStrategy());
76+
intermediateModel.getNamingStrategy(),
77+
intermediateModel.getCustomizationConfig());
7778
this.modelMethodOverrides = new ModelMethodOverrides(this.poetExtensions);
7879
this.modelBuilderSpecs = new ModelBuilderSpecs(intermediateModel, this.shapeModel, this.typeProvider);
7980
}

codegen/src/main/java/software/amazon/awssdk/codegen/poet/model/ShapeModelSpec.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.List;
2727
import java.util.stream.Collectors;
2828
import javax.lang.model.element.Modifier;
29+
import software.amazon.awssdk.codegen.model.config.customization.CustomizationConfig;
2930
import software.amazon.awssdk.codegen.model.intermediate.MemberModel;
3031
import software.amazon.awssdk.codegen.model.intermediate.ShapeModel;
3132
import software.amazon.awssdk.codegen.model.intermediate.ShapeType;
@@ -34,7 +35,7 @@
3435
import software.amazon.awssdk.core.protocol.MarshallLocation;
3536
import software.amazon.awssdk.core.protocol.MarshallingType;
3637
import software.amazon.awssdk.core.protocol.SdkField;
37-
import software.amazon.awssdk.core.protocol.traits.IdempotencyTrait;
38+
import software.amazon.awssdk.core.protocol.traits.DefaultValueTrait;
3839
import software.amazon.awssdk.core.protocol.traits.JsonValueTrait;
3940
import software.amazon.awssdk.core.protocol.traits.ListTrait;
4041
import software.amazon.awssdk.core.protocol.traits.LocationTrait;
@@ -51,15 +52,18 @@ class ShapeModelSpec {
5152
private final TypeProvider typeProvider;
5253
private final PoetExtensions poetExtensions;
5354
private final NamingStrategy namingStrategy;
55+
private final CustomizationConfig customizationConfig;
5456

5557
ShapeModelSpec(ShapeModel shapeModel,
5658
TypeProvider typeProvider,
5759
PoetExtensions poetExtensions,
58-
NamingStrategy namingStrategy) {
60+
NamingStrategy namingStrategy,
61+
CustomizationConfig customizationConfig) {
5962
this.shapeModel = shapeModel;
6063
this.typeProvider = typeProvider;
6164
this.poetExtensions = poetExtensions;
6265
this.namingStrategy = namingStrategy;
66+
this.customizationConfig = customizationConfig;
6367
}
6468

6569
ClassName className() {
@@ -155,6 +159,10 @@ private CodeBlock traits(MemberModel m) {
155159
if (m.isIdempotencyToken()) {
156160
traits.add(createIdempotencyTrait());
157161
}
162+
String customDefaultValueSupplier = customizationConfig.getModelMarshallerDefaultValueSupplier().get(m.getC2jName());
163+
if (customDefaultValueSupplier != null) {
164+
traits.add(createDefaultValueTrait(customDefaultValueSupplier));
165+
}
158166
if (m.getTimestampFormat() != null) {
159167
traits.add(createTimestampFormatTrait(m));
160168
}
@@ -187,7 +195,14 @@ private CodeBlock createLocationTrait(MemberModel m) {
187195

188196
private CodeBlock createIdempotencyTrait() {
189197
return CodeBlock.builder()
190-
.add("$T.create()", ClassName.get(IdempotencyTrait.class))
198+
.add("$T.idempotencyToken()", ClassName.get(DefaultValueTrait.class))
199+
.build();
200+
}
201+
202+
private CodeBlock createDefaultValueTrait(String customDefaultValueSupplier) {
203+
return CodeBlock.builder()
204+
.add("$T.create($T.getInstance())", ClassName.get(DefaultValueTrait.class),
205+
ClassName.bestGuess(customDefaultValueSupplier))
191206
.build();
192207
}
193208

core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/protocol/json/JsonProtocolMarshaller.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import software.amazon.awssdk.core.protocol.SdkField;
3131
import software.amazon.awssdk.core.protocol.SdkPojo;
3232
import software.amazon.awssdk.core.protocol.json.StructuredJsonGenerator;
33-
import software.amazon.awssdk.core.protocol.traits.IdempotencyTrait;
33+
import software.amazon.awssdk.core.protocol.traits.DefaultValueTrait;
3434
import software.amazon.awssdk.core.protocol.traits.PayloadTrait;
3535
import software.amazon.awssdk.core.util.UriResourcePathUtils;
3636

@@ -172,8 +172,8 @@ public Request<OrigRequestT> marshall(SdkPojo pojo) {
172172
}
173173

174174
private Object resolveValue(Object val, SdkField<?> marshallingInfo) {
175-
IdempotencyTrait trait = marshallingInfo.getTrait(IdempotencyTrait.class);
176-
return trait == null ? val : trait.resolveValue((String) val);
175+
DefaultValueTrait trait = marshallingInfo.getTrait(DefaultValueTrait.class);
176+
return trait == null ? val : trait.resolveValue(val);
177177
}
178178

179179
private Request<OrigRequestT> finishMarshalling() {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.core.protocol.traits;
17+
18+
import java.util.function.Supplier;
19+
import software.amazon.awssdk.annotations.SdkProtectedApi;
20+
import software.amazon.awssdk.core.util.IdempotentUtils;
21+
22+
/**
23+
* Trait that supplies a default value when none is present for a given field.
24+
*/
25+
@SdkProtectedApi
26+
public final class DefaultValueTrait implements Trait {
27+
28+
private final Supplier<?> defaultValueSupplier;
29+
30+
private DefaultValueTrait(Supplier<?> defaultValueSupplier) {
31+
this.defaultValueSupplier = defaultValueSupplier;
32+
}
33+
34+
/**
35+
* If the value is null then the default value supplier is used to get a default
36+
* value for the field. Otherwise 'val' is returned.
37+
*
38+
* @param val Value to resolve.
39+
* @return Resolved value.
40+
*/
41+
public Object resolveValue(Object val) {
42+
return val != null ? val : defaultValueSupplier.get();
43+
}
44+
45+
/**
46+
* Creates a new {@link DefaultValueTrait} with a custom {@link Supplier}.
47+
*
48+
* @param supplier Supplier of default value for the field.
49+
* @return New trait instance.
50+
*/
51+
public static DefaultValueTrait create(Supplier<?> supplier) {
52+
return new DefaultValueTrait(supplier);
53+
}
54+
55+
/**
56+
* Creates a precanned {@link DefaultValueTrait} using the idempotency token generation which
57+
* creates a new UUID if a field is null. This is used when the 'idempotencyToken' trait in the service
58+
* model is present.
59+
*
60+
* @return New trait instance.
61+
*/
62+
public static DefaultValueTrait idempotencyToken() {
63+
return new DefaultValueTrait(IdempotentUtils.getGenerator());
64+
}
65+
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/protocol/traits/IdempotencyTrait.java

Lines changed: 0 additions & 41 deletions
This file was deleted.

services/glacier/src/main/resources/codegen-resources/customization.config

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
23
"customErrorCodeFieldName": "code",
34

45
"shapeModifiers" : {
@@ -34,6 +35,6 @@
3435
}
3536
},
3637
"modelMarshallerDefaultValueSupplier": {
37-
"AccountId" : "software.amazon.awssdk.services.glacier.transform.DefaultAccountIdSupplier.getInstance()"
38+
"accountId" : "software.amazon.awssdk.services.glacier.transform.DefaultAccountIdSupplier"
3839
}
3940
}

0 commit comments

Comments
 (0)