Skip to content

Refactoring for JSON marshalling/unmarshalling, support for JSON valu… #727

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

Merged
merged 9 commits into from
Oct 11, 2018
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 @@ -162,7 +162,9 @@ private MemberModel generateMemberModel(String c2jMemberName, Member c2jMemberDe
.withVariable(new VariableModel(variableName, variableType, variableDeclarationType)
.withDocumentation(c2jMemberDefinition.getDocumentation()))
.withSetterModel(new VariableModel(variableName, variableType, variableDeclarationType))
.withGetterModel(new ReturnTypeModel(variableType));
.withGetterModel(new ReturnTypeModel(variableType))
.withTimestampFormat(resolveTimestampFormat(c2jMemberDefinition, c2jShape))
.withJsonValue(c2jMemberDefinition.getJsonValue());
memberModel.setDocumentation(c2jMemberDefinition.getDocumentation());
memberModel.setDeprecated(c2jMemberDefinition.isDeprecated());
memberModel
Expand Down Expand Up @@ -205,6 +207,11 @@ private MemberModel generateMemberModel(String c2jMemberName, Member c2jMemberDe
return memberModel;
}

private String resolveTimestampFormat(Member c2jMemberDefinition, Shape c2jShape) {
return c2jMemberDefinition.getTimestampFormat() != null ?
c2jMemberDefinition.getTimestampFormat() : c2jShape.getTimestampFormat();
}

private ParameterHttpMapping generateParameterHttpMapping(Shape parentShape,
String memberName,
Member member,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static software.amazon.awssdk.codegen.AddMetadata.constructMetadata;
import static software.amazon.awssdk.codegen.RemoveUnusedShapes.removeUnusedShapes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -90,7 +89,7 @@ private List<IntermediateModelShapeProcessor> createShapeProcessors() {
return processors;
}

public IntermediateModel build() throws IOException {
public IntermediateModel build() {
// Note: This needs to come before any pre/post processing of the
// models, as the transformer must have access to the original shapes,
// before any customizations have been applied (which modifies them).
Expand Down Expand Up @@ -119,7 +118,7 @@ public IntermediateModel build() throws IOException {

IntermediateModel fullModel = new IntermediateModel(
constructMetadata(service, codeGenConfig, customConfig), operations, shapes,
customConfig, examples, authorizers, paginators.getPaginators());
customConfig, examples, authorizers, paginators.getPaginators(), namingStrategy);

customization.postprocess(fullModel);

Expand All @@ -135,7 +134,8 @@ public IntermediateModel build() throws IOException {
fullModel.getCustomizationConfig(),
fullModel.getExamples(),
fullModel.getCustomAuthorizers(),
fullModel.getPaginators());
fullModel.getPaginators(),
namingStrategy);

linkMembersToShapes(trimmedModel);
linkOperationsToInputOutputShapes(trimmedModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import software.amazon.awssdk.codegen.model.intermediate.Metadata;
import software.amazon.awssdk.codegen.model.intermediate.ShapeModel;
import software.amazon.awssdk.codegen.model.intermediate.ShapeType;
import software.amazon.awssdk.codegen.poet.transform.JsonModelMarshallerSpec;
import software.amazon.awssdk.codegen.poet.transform.MarshallerSpec;
import software.amazon.awssdk.utils.ImmutableMap;

Expand All @@ -48,7 +47,7 @@ public MarshallerGeneratorTasks(GeneratorTaskParams dependencies) {
}

@Override
protected List<GeneratorTask> createTasks() throws Exception {
protected List<GeneratorTask> createTasks() {
info("Emitting marshaller classes");
return model.getShapes().entrySet().stream()
.filter(e -> shouldGenerate(e.getValue()))
Expand All @@ -70,9 +69,8 @@ private boolean shouldGenerate(ShapeModel shapeModel) {
private Stream<GeneratorTask> createTask(String javaShapeName, ShapeModel shapeModel) throws Exception {
if (metadata.isJsonProtocol()) {
return ShapeType.Request == shapeModel.getShapeType() ?
Stream.of(createPoetGeneratorTask(new JsonModelMarshallerSpec(model, shapeModel, "ModelMarshaller")),
createPoetGeneratorTask(new MarshallerSpec(model, shapeModel))) :
Stream.of(createPoetGeneratorTask(new JsonModelMarshallerSpec(model, shapeModel, "Marshaller")));
Stream.of(createPoetGeneratorTask(new MarshallerSpec(model, shapeModel))) :
Stream.empty();
}

return Stream.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static software.amazon.awssdk.utils.FunctionalUtils.safeFunction;

import freemarker.template.Template;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -43,6 +44,9 @@ public UnmarshallerGeneratorTasks(GeneratorTaskParams dependencies) {
@Override
protected List<GeneratorTask> createTasks() throws Exception {
info("Emitting unmarshaller classes");
if (metadata.isJsonProtocol()) {
return Collections.emptyList();
}
return model.getShapes().entrySet().stream()
.filter(e -> shouldGenerate(e.getValue()))
.map(safeFunction(e -> createTask(e.getKey(), e.getValue())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package software.amazon.awssdk.codegen.model.config.customization;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import software.amazon.awssdk.codegen.model.config.templates.CodeGenTemplatesConfig;
Expand Down Expand Up @@ -126,7 +127,7 @@ public class CustomizationConfig {
private String sdkResponseBaseClassName;
private String defaultExceptionUnmarshaller;

private Map<String, String> modelMarshallerDefaultValueSupplier;
private Map<String, String> modelMarshallerDefaultValueSupplier = new HashMap<>();

private boolean useAutoConstructList = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import software.amazon.awssdk.codegen.internal.Utils;
import software.amazon.awssdk.codegen.model.config.customization.CustomizationConfig;
import software.amazon.awssdk.codegen.model.service.PaginatorDefinition;
import software.amazon.awssdk.codegen.naming.NamingStrategy;
import software.amazon.awssdk.utils.IoUtils;

public final class IntermediateModel {
Expand All @@ -56,6 +57,9 @@ public final class IntermediateModel {
@JsonIgnore
private final Map<String, PaginatorDefinition> paginators;

@JsonIgnore
private final NamingStrategy namingStrategy;

@JsonCreator
public IntermediateModel(
@JsonProperty("metadata") Metadata metadata,
Expand All @@ -64,7 +68,7 @@ public IntermediateModel(
@JsonProperty("customizationConfig") CustomizationConfig customizationConfig,
@JsonProperty("serviceExamples") ServiceExamples examples) {

this(metadata, operations, shapes, customizationConfig, examples, Collections.emptyMap(), Collections.emptyMap());
this(metadata, operations, shapes, customizationConfig, examples, Collections.emptyMap(), Collections.emptyMap(), null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can pass in DefaultNamingStrategy instead of null

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DefaultNamingStrategy requires the c2j models and we only have intermediate at this point.

}

public IntermediateModel(
Expand All @@ -74,14 +78,16 @@ public IntermediateModel(
CustomizationConfig customizationConfig,
ServiceExamples examples,
Map<String, AuthorizerModel> customAuthorizers,
Map<String, PaginatorDefinition> paginators) {
Map<String, PaginatorDefinition> paginators,
NamingStrategy namingStrategy) {
this.metadata = metadata;
this.operations = operations;
this.shapes = shapes;
this.customizationConfig = customizationConfig;
this.examples = examples;
this.customAuthorizers = customAuthorizers;
this.paginators = paginators;
this.namingStrategy = namingStrategy;
}

public Metadata getMetadata() {
Expand Down Expand Up @@ -116,6 +122,10 @@ public Map<String, PaginatorDefinition> getPaginators() {
return paginators;
}

public NamingStrategy getNamingStrategy() {
return namingStrategy;
}

/**
* @return Exception unmarshaller implementation to use. Currently only needed by XML based
* protocols.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
import static software.amazon.awssdk.codegen.internal.DocumentationUtils.defaultSetter;
import static software.amazon.awssdk.codegen.internal.DocumentationUtils.defaultSetterParam;
import static software.amazon.awssdk.codegen.internal.DocumentationUtils.stripHtmlTags;
import static software.amazon.awssdk.utils.StringUtils.upperCase;

import com.fasterxml.jackson.annotation.JsonIgnore;
import java.util.List;
import java.util.Map;
import software.amazon.awssdk.codegen.internal.TypeUtils;
import software.amazon.awssdk.core.SdkBytes;
import software.amazon.awssdk.core.protocol.SdkField;
import software.amazon.awssdk.core.runtime.transform.PathMarshaller;
import software.amazon.awssdk.utils.StringUtils;

Expand Down Expand Up @@ -74,6 +74,10 @@ public class MemberModel extends DocumentationModel {

private String beanStyleSetterName;

private boolean isJsonValue;

private String timestampFormat;

private boolean eventPayload;

private boolean eventHeader;
Expand Down Expand Up @@ -475,8 +479,9 @@ public boolean getIsBinary() {
/**
* @return Implementation of {@link PathMarshaller} to use if this member is bound the the URI.
* @throws IllegalStateException If this member is not bound to the URI. Templates should first check
* {@link ParameterHttpMapping#isUri()} first.
* {@link ParameterHttpMapping#isUri()} first.
*/
// TODO remove when rest XML marshaller refactor is merged
@JsonIgnore
public String getPathMarshaller() {
if (!http.isUri()) {
Expand All @@ -492,13 +497,30 @@ public String getPathMarshaller() {
}
}

/**
* Used for JSON services. Name of the field containing the {@link software.amazon.awssdk.core.protocol.MarshallingInfo} for
* this member.
*/
@JsonIgnore
public String getMarshallerBindingFieldName() {
return upperCase(this.name) + "_BINDING";
public boolean isJsonValue() {
return isJsonValue;
}

public void setJsonValue(boolean jsonValue) {
isJsonValue = jsonValue;
}

public MemberModel withJsonValue(boolean jsonValue) {
setJsonValue(jsonValue);
return this;
}

public String getTimestampFormat() {
return timestampFormat;
}

public void setTimestampFormat(String timestampFormat) {
this.timestampFormat = timestampFormat;
}

public MemberModel withTimestampFormat(String timestampFormat) {
setTimestampFormat(timestampFormat);
return this;
}

@JsonIgnore
Expand All @@ -518,9 +540,7 @@ public boolean isSdkBytesType() {
}

/**
* Currently used only for JSON services.
*
* @return Marshalling type to use when creating a {@link software.amazon.awssdk.core.protocol.MarshallingInfo}. Must be a
* @return Marshalling type to use when creating a {@link SdkField}. Must be a
* field of {@link software.amazon.awssdk.core.protocol.MarshallingType}.
*/
public String getMarshallingType() {
Expand All @@ -529,29 +549,12 @@ public String getMarshallingType() {
} else if (isMap()) {
return "MAP";
} else if (!isSimple()) {
return "STRUCTURED";
return "SDK_POJO";
} else {
return TypeUtils.getMarshallingType(variable.getSimpleType());
}
}

/**
* Currently used only for JSON services.
*
* @return The target class a marshalling type is bound to.
*/
public String getMarshallingTargetClass() {
if (isList()) {
return "List";
} else if (isMap()) {
return "Map";
} else if (!isSimple()) {
return "StructuredPojo";
} else {
return variable.getVariableType();
}
}

@JsonIgnore
public ShapeModel getShape() {
return shape;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ public MarshallLocation getMarshallLocation() {
switch (location) {
default:
return MarshallLocation.PAYLOAD;
case STATUS_CODE:
return MarshallLocation.STATUS_CODE;
case HEADER:
case HEADERS:
return MarshallLocation.HEADER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ public MemberModel getExplicitEventPayloadMember() {
}

return members.stream()
.filter(m -> m.isEventPayload())
.filter(MemberModel::isEventPayload)
.findFirst()
.orElse(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public class Member {

private boolean deprecated;

@JsonProperty("jsonvalue")
private boolean jsonValue;

private String timestampFormat;

@JsonProperty(value = "eventpayload")
private boolean eventPayload;

Expand Down Expand Up @@ -135,6 +140,22 @@ public void setDeprecated(boolean deprecated) {
this.deprecated = deprecated;
}

public boolean getJsonValue() {
return jsonValue;
}

public void setJsonValue(boolean jsonValue) {
this.jsonValue = jsonValue;
}

public String getTimestampFormat() {
return timestampFormat;
}

public void setTimestampFormat(String timestampFormat) {
this.timestampFormat = timestampFormat;
}

public boolean isEventPayload() {
return eventPayload;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ public class Shape {
@JsonProperty(value = "event")
private boolean isEvent;

private String timestampFormat;

public boolean isFault() {
return fault;
}
Expand Down Expand Up @@ -238,4 +240,12 @@ public boolean isEvent() {
public void setIsEvent(boolean event) {
isEvent = event;
}

public String getTimestampFormat() {
return timestampFormat;
}

public void setTimestampFormat(String timestampFormat) {
this.timestampFormat = timestampFormat;
}
}
Loading