Skip to content
Open
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 @@ -42,6 +42,7 @@
import software.amazon.awssdk.codegen.model.service.ServiceModel;
import software.amazon.awssdk.codegen.model.service.Shape;
import software.amazon.awssdk.codegen.naming.NamingStrategy;
import software.amazon.awssdk.codegen.naming.ShapeContext;
import software.amazon.awssdk.codegen.utils.ProtocolUtils;
import software.amazon.awssdk.codegen.validation.ModelInvalidException;
import software.amazon.awssdk.codegen.validation.ValidationEntry;
Expand Down Expand Up @@ -158,7 +159,9 @@ private MemberModel generateMemberModel(String c2jMemberName, Member c2jMemberDe
Map<String, Shape> allC2jShapes) {
String c2jShapeName = c2jMemberDefinition.getShape();
Shape shape = allC2jShapes.get(c2jShapeName);
String variableName = getNamingStrategy().getVariableName(c2jMemberName, parentShape);
ShapeContext parentCtx = ShapeContext.ofC2j(parentShape, allC2jShapes);
ShapeContext shapeCtx = ShapeContext.ofC2j(shape, allC2jShapes);
String variableName = getNamingStrategy().getVariableName(c2jMemberName, parentCtx);
String variableType = getTypeUtils().getJavaDataType(allC2jShapes, c2jShapeName);
String variableDeclarationType = getTypeUtils().getJavaDataType(allC2jShapes, c2jShapeName);

Expand Down Expand Up @@ -187,13 +190,13 @@ private MemberModel generateMemberModel(String c2jMemberName, Member c2jMemberDe
memberModel.setDeprecatedMessage(c2jMemberDefinition.getDeprecatedMessage());
memberModel.setSensitive(isSensitiveShapeOrContainer(c2jMemberDefinition, allC2jShapes));
memberModel
.withFluentGetterMethodName(namingStrategy.getFluentGetterMethodName(c2jMemberName, parentShape, shape))
.withFluentEnumGetterMethodName(namingStrategy.getFluentEnumGetterMethodName(c2jMemberName, parentShape, shape))
.withFluentSetterMethodName(namingStrategy.getFluentSetterMethodName(c2jMemberName, parentShape, shape))
.withFluentEnumSetterMethodName(namingStrategy.getFluentEnumSetterMethodName(c2jMemberName, parentShape, shape))
.withExistenceCheckMethodName(namingStrategy.getExistenceCheckMethodName(c2jMemberName, parentShape))
.withBeanStyleGetterMethodName(namingStrategy.getBeanStyleGetterMethodName(c2jMemberName, parentShape, shape))
.withBeanStyleSetterMethodName(namingStrategy.getBeanStyleSetterMethodName(c2jMemberName, parentShape, shape));
.withFluentGetterMethodName(namingStrategy.getFluentGetterMethodName(c2jMemberName, parentCtx, shapeCtx))
.withFluentEnumGetterMethodName(namingStrategy.getFluentEnumGetterMethodName(c2jMemberName, parentCtx, shapeCtx))
.withFluentSetterMethodName(namingStrategy.getFluentSetterMethodName(c2jMemberName, parentCtx, shapeCtx))
.withFluentEnumSetterMethodName(namingStrategy.getFluentEnumSetterMethodName(c2jMemberName, parentCtx, shapeCtx))
.withExistenceCheckMethodName(namingStrategy.getExistenceCheckMethodName(c2jMemberName, parentCtx))
.withBeanStyleGetterMethodName(namingStrategy.getBeanStyleGetterMethodName(c2jMemberName, parentCtx, shapeCtx))
.withBeanStyleSetterMethodName(namingStrategy.getBeanStyleSetterMethodName(c2jMemberName, parentCtx, shapeCtx));
memberModel.setIdempotencyToken(c2jMemberDefinition.isIdempotencyToken());
memberModel.setEventPayload(c2jMemberDefinition.isEventpayload());
memberModel.setEventHeader(c2jMemberDefinition.isEventheader());
Expand Down Expand Up @@ -229,11 +232,11 @@ private MemberModel generateMemberModel(String c2jMemberName, Member c2jMemberDe

memberModel.setDeprecatedName(deprecatedName);
memberModel.setDeprecatedFluentGetterMethodName(
namingStrategy.getFluentGetterMethodName(deprecatedName, parentShape, shape));
namingStrategy.getFluentGetterMethodName(deprecatedName, parentCtx, shapeCtx));
memberModel.setDeprecatedFluentSetterMethodName(
namingStrategy.getFluentSetterMethodName(deprecatedName, parentShape, shape));
namingStrategy.getFluentSetterMethodName(deprecatedName, parentCtx, shapeCtx));
memberModel.setDeprecatedBeanStyleSetterMethodName(
namingStrategy.getBeanStyleSetterMethodName(deprecatedName, parentShape, shape));
namingStrategy.getBeanStyleSetterMethodName(deprecatedName, parentCtx, shapeCtx));
}

ParameterHttpMapping httpMapping = generateParameterHttpMapping(parentShape,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import software.amazon.awssdk.codegen.model.intermediate.MemberModel;
import software.amazon.awssdk.codegen.model.intermediate.Metadata;
import software.amazon.awssdk.codegen.model.service.ServiceModel;
import software.amazon.awssdk.codegen.model.service.Shape;
import software.amazon.awssdk.codegen.validation.ModelInvalidException;
import software.amazon.awssdk.codegen.validation.ValidationEntry;
import software.amazon.awssdk.codegen.validation.ValidationErrorId;
Expand Down Expand Up @@ -300,7 +299,7 @@ public String getVariableName(String name) {
}

@Override
public String getVariableName(String name, Shape parentShape) {
public String getVariableName(String name, ShapeContext parentShape) {
if (isJavaKeyword(name) ||
isDisallowedNameForShape(unCapitalize(name), parentShape)) {
return unCapitalize(name + CONFLICTING_NAME_SUFFIX);
Expand Down Expand Up @@ -342,15 +341,15 @@ public String getShapeClassName(String shapeName) {
}

@Override
public String getFluentGetterMethodName(String memberName, Shape parentShape, Shape shape) {
public String getFluentGetterMethodName(String memberName, ShapeContext parentShape, ShapeContext shape) {
String getterMethodName = Utils.unCapitalize(memberName);

getterMethodName = rewriteInvalidMemberName(getterMethodName, parentShape);

if (Utils.isOrContainsEnumShape(shape, serviceModel.getShapes())) {
if (shape.isOrContainsEnum()) {
getterMethodName += "AsString";

if (Utils.isListShape(shape) || Utils.isMapShape(shape)) {
if (shape.isList() || shape.isMap()) {
getterMethodName += "s";
}
}
Expand All @@ -359,8 +358,8 @@ public String getFluentGetterMethodName(String memberName, Shape parentShape, Sh
}

@Override
public String getFluentEnumGetterMethodName(String memberName, Shape parentShape, Shape shape) {
if (!Utils.isOrContainsEnumShape(shape, serviceModel.getShapes())) {
public String getFluentEnumGetterMethodName(String memberName, ShapeContext parentShape, ShapeContext shape) {
if (!shape.isOrContainsEnum()) {
return null;
}

Expand All @@ -370,48 +369,46 @@ public String getFluentEnumGetterMethodName(String memberName, Shape parentShape
}

@Override
public String getExistenceCheckMethodName(String memberName, Shape parentShape) {
public String getExistenceCheckMethodName(String memberName, ShapeContext parentShape) {
String existenceCheckMethodName = Utils.unCapitalize(memberName);
existenceCheckMethodName = rewriteInvalidMemberName(existenceCheckMethodName, parentShape);
return String.format("has%s", Utils.capitalize(existenceCheckMethodName));
}

@Override
public String getBeanStyleGetterMethodName(String memberName, Shape parentShape, Shape c2jShape) {
public String getBeanStyleGetterMethodName(String memberName, ShapeContext parentShape, ShapeContext shape) {
String fluentGetterMethodName;
if (Utils.isOrContainsEnumShape(c2jShape, serviceModel.getShapes())) {
if (shape.isOrContainsEnum()) {
// Use the enum (modeled) name for bean-style getters
fluentGetterMethodName = getFluentEnumGetterMethodName(memberName, parentShape, c2jShape);
fluentGetterMethodName = getFluentEnumGetterMethodName(memberName, parentShape, shape);
} else {
fluentGetterMethodName = getFluentGetterMethodName(memberName, parentShape, c2jShape);
fluentGetterMethodName = getFluentGetterMethodName(memberName, parentShape, shape);
}
return String.format("get%s", Utils.capitalize(fluentGetterMethodName));
}

@Override
public String getBeanStyleSetterMethodName(String memberName, Shape parentShape, Shape c2jShape) {
String beanStyleGetter = getBeanStyleGetterMethodName(memberName, parentShape, c2jShape);
public String getBeanStyleSetterMethodName(String memberName, ShapeContext parentShape, ShapeContext shape) {
String beanStyleGetter = getBeanStyleGetterMethodName(memberName, parentShape, shape);
return String.format("set%s", beanStyleGetter.substring("get".length()));
}

@Override
public String getFluentSetterMethodName(String memberName, Shape parentShape, Shape shape) {
public String getFluentSetterMethodName(String memberName, ShapeContext parentShape, ShapeContext shape) {
String setterMethodName = Utils.unCapitalize(memberName);

setterMethodName = rewriteInvalidMemberName(setterMethodName, parentShape);

if (Utils.isOrContainsEnumShape(shape, serviceModel.getShapes()) &&
(Utils.isListShape(shape) || Utils.isMapShape(shape))) {

if (shape.isOrContainsEnum() && (shape.isList() || shape.isMap())) {
setterMethodName += "WithStrings";
}

return setterMethodName;
}

@Override
public String getFluentEnumSetterMethodName(String memberName, Shape parentShape, Shape shape) {
if (!Utils.isOrContainsEnumShape(shape, serviceModel.getShapes())) {
public String getFluentEnumSetterMethodName(String memberName, ShapeContext parentShape, ShapeContext shape) {
if (!shape.isOrContainsEnum()) {
return null;
}

Expand All @@ -430,15 +427,15 @@ public String getUnionEnumTypeName(MemberModel memberModel) {
return screamCase(memberModel.getName());
}

private String rewriteInvalidMemberName(String memberName, Shape parentShape) {
private String rewriteInvalidMemberName(String memberName, ShapeContext parentShape) {
if (isJavaKeyword(memberName) || isDisallowedNameForShape(memberName, parentShape)) {
return Utils.unCapitalize(memberName + CONFLICTING_NAME_SUFFIX);
}

return memberName;
}

private boolean isDisallowedNameForShape(String name, Shape parentShape) {
private boolean isDisallowedNameForShape(String name, ShapeContext parentShape) {
// Reserve the name "type" for union shapes.
if (parentShape.isUnion() && "type".equals(name)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.model.intermediate.MemberModel;
import software.amazon.awssdk.codegen.model.service.Shape;
import software.amazon.awssdk.core.SdkField;

/**
Expand Down Expand Up @@ -129,7 +128,7 @@ public interface NamingStrategy {
* @param parentShape The shape containing the member, used to check for shape-specific reserved names.
* @return Appropriate name to use for a Java variable or field.
*/
String getVariableName(String name, Shape parentShape);
String getVariableName(String name, ShapeContext parentShape);

/**
* @param enumValue Enum value as defined in the service model used to derive the java name.
Expand All @@ -145,42 +144,45 @@ public interface NamingStrategy {

/**
* @param memberName Member name to name getter for.
* @param parentShape Shape containing the member.
* @param shape The shape associated with the member.
* @return Name of the getter method for a model class member.
*/
String getFluentGetterMethodName(String memberName, Shape parentShape, Shape shape);
String getFluentGetterMethodName(String memberName, ShapeContext parentShape, ShapeContext shape);

/**
* @param memberName The full member to get the name for.
* @param parentShape Shape containing the member.
* @param shape The shape associated with the member.
* @return Name of the getter method for an enum model class member.
*/
String getFluentEnumGetterMethodName(String memberName, Shape parentShape, Shape shape);
String getFluentEnumGetterMethodName(String memberName, ShapeContext parentShape, ShapeContext shape);

/**
* @param memberName Member name to name getter for.
* @return Name of the JavaBean getter method for model class member.
*/
String getBeanStyleGetterMethodName(String memberName, Shape parentShape, Shape c2jShape);
String getBeanStyleGetterMethodName(String memberName, ShapeContext parentShape, ShapeContext shape);

/**
* @param memberName Member name to name setter for.
* @return Name of the JavaBean setter method for model class member.
*/
String getBeanStyleSetterMethodName(String memberName, Shape parentShape, Shape c2jShape);
String getBeanStyleSetterMethodName(String memberName, ShapeContext parentShape, ShapeContext shape);

/**
* @param memberName Member name to name fluent setter for.
* @return Appropriate name to use for fluent setter method (i.e. withFoo) for a model class member.
*/
String getFluentSetterMethodName(String memberName, Shape parentShape, Shape shape);
String getFluentSetterMethodName(String memberName, ShapeContext parentShape, ShapeContext shape);

/**
* @param memberName The full member to get the name for.
* @param parentShape Shape containing the member.
* @param shape The shape associated with the member.
* @return Name of the getter method for an enum model class member.
*/
String getFluentEnumSetterMethodName(String memberName, Shape parentShape, Shape shape);
String getFluentEnumSetterMethodName(String memberName, ShapeContext parentShape, ShapeContext shape);

/**
* Stuttering is intentional, returns the name of the {@link SdkField} field.
Expand All @@ -205,7 +207,7 @@ public interface NamingStrategy {
* @param parentShape The shape containing the member.
* @return Name of an existence check method.
*/
String getExistenceCheckMethodName(String memberName, Shape parentShape);
String getExistenceCheckMethodName(String memberName, ShapeContext parentShape);

/**
* Retrieve the service's signing name that should be used based on the model.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.codegen.naming;

import java.util.Map;
import software.amazon.awssdk.codegen.internal.Utils;
import software.amazon.awssdk.codegen.model.service.Shape;

/**
* A read-only view of a service model shape, exposing the predicates
* ({@link #isUnion()}, {@link #isException()}, {@link #isList()},
* {@link #isMap()}, {@link #isOrContainsEnum()}) that {@link NamingStrategy}
* consults when deriving Java names for members and accessors.
*
* <p>Adapters for specific model formats are provided as static factories;
* see {@link #ofC2j(Shape, Map)}.
*/
public interface ShapeContext {

/**
* @return true if the shape is a union (member named {@code "type"} is reserved).
*/
boolean isUnion();

/**
* @return true if the shape is an exception (exception-reserved method names apply).
*/
boolean isException();

/**
* @return true if the shape is or transitively contains an enum shape. For
* list and map shapes, this recurses into member targets.
*/
boolean isOrContainsEnum();

/**
* @return true if the shape is a list.
*/
boolean isList();

/**
* @return true if the shape is a map.
*/
boolean isMap();

/**
* Adapts a C2J {@link Shape} into a {@link ShapeContext}. Uses the same
* predicates {@link Utils} exposes today, so behavior is unchanged from
* pre-refactor call sites.
*
* @param shape the C2J shape (must not be null when the caller intends to
* query any of the shape-relative predicates).
* @param allShapes the service's full shape map, needed for the
* {@code isOrContainsEnum} recursion into list/map targets.
*/
static ShapeContext ofC2j(Shape shape, Map<String, Shape> allShapes) {
return new ShapeContext() {
@Override
public boolean isUnion() {
return shape.isUnion();
}

@Override
public boolean isException() {
return shape.isException();
}

@Override
public boolean isOrContainsEnum() {
return Utils.isOrContainsEnumShape(shape, allShapes);
}

@Override
public boolean isList() {
return Utils.isListShape(shape);
}

@Override
public boolean isMap() {
return Utils.isMapShape(shape);
}
};
}
}
Loading
Loading