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

chore: refactor to put function name generator in constant #641

Merged
merged 12 commits into from
Oct 23, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import software.amazon.smithy.model.traits.ErrorTrait;
import software.amazon.smithy.model.traits.UnitTypeTrait;

import static software.amazon.polymorph.smithygo.utils.Constants.funcNameGenerator;

public class DafnyAwsSdkClientTypeConversionProtocol
implements ProtocolGenerator {

Expand Down Expand Up @@ -598,7 +600,7 @@ func Error_ToDafny(err error)($L.Error) {
}
""",
DafnyNameResolver.dafnyTypesNamespace(serviceShape),
writer.consumer(w -> {
writer.consumer(w -> {
Copy link
Author

Choose a reason for hiding this comment

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

Not sure how this came in diff but make format_java will fix this. So, Ignoring this for now.

Copy link
Contributor

Choose a reason for hiding this comment

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

Personally I'm not a fan of our java formatter - it keeps line too short and I think uses 2 white-spaces for indentation.

for (final var error : errorShapes) {
w.write(
"""
Expand Down Expand Up @@ -807,7 +809,7 @@ private void generateSerializerFunctions(final GenerationContext context, final
return $L
}
""",
ShapeVisitorHelper.funcNameGenerator(
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I'd prefer fully-qualified-name / FQN over static imports. Improves readability / reasoning around where this function belongs.

Copy link
Author

Choose a reason for hiding this comment

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

Done

funcNameGenerator(
visitingMemberShape,
"ToDafny"
),
Expand Down Expand Up @@ -871,7 +873,7 @@ private void generateDeserializerFunctions(final GenerationContext context, fina
func $L(input $L)($L) {
return $L
}""",
ShapeVisitorHelper.funcNameGenerator(
funcNameGenerator(
visitingMemberShape,
"FromDafny"
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import software.amazon.smithy.model.shapes.Shape;
import software.amazon.smithy.model.traits.EnumTrait;

import static software.amazon.polymorph.smithygo.utils.Constants.funcNameGenerator;

public class ShapeVisitorHelper {

private static final Map<MemberShape, Boolean> optionalShapesToDafny =
Expand All @@ -27,25 +29,6 @@ public static boolean isToNativeShapePointable(final MemberShape shape) {
return pointerShapesToNative.get(shape);
}

/**
* Generates functions Name for To Dafny and To Native conversion.
*
* @param memberShape MemberShape to generate function name for.
* @param suffix Suffix to add to the function. As of this writing, we only put FromDafny or ToNative suffix.
* @return the function Name
*/
public static String funcNameGenerator(
final MemberShape memberShape,
final String suffix
) {
return memberShape
.getId()
.toString()
.replaceAll("[.$#]", "_")
.concat("_")
.concat(suffix);
}

public static String toNativeShapeVisitorWriter(
final MemberShape memberShape,
final GenerationContext context,
Expand All @@ -69,7 +52,6 @@ public static String toNativeShapeVisitorWriter(
)
.concat(")");
}
final String nextVisitorFunction;
final String funcDataSource = "input";
if (!DafnyToAwsSdkShapeVisitor.getAllShapesRequiringConversionFunc().contains(memberShape)) {
DafnyToAwsSdkShapeVisitor.putShapesWithConversionFunc(memberShape, "");
Expand All @@ -88,8 +70,7 @@ public static String toNativeShapeVisitorWriter(
pointerShapesToNative.put(memberShape, isPointable);
}
final String funcName = funcNameGenerator(memberShape, "FromDafny");
nextVisitorFunction = funcName.concat("(").concat(dataSource).concat(")");
return nextVisitorFunction;
return(funcName.concat("(").concat(dataSource).concat(")"));
}

public static String toDafnyShapeVisitorWriter(
Expand All @@ -104,7 +85,6 @@ public static String toDafnyShapeVisitorWriter(
final Shape targetShape = context
.model()
.expectShape(memberShape.getTarget());
final String nextVisitorFunction;
if (targetShape.hasTrait(ReferenceTrait.class)) {
return targetShape.accept(
new AwsSdkToDafnyShapeVisitor(
Expand Down Expand Up @@ -135,11 +115,7 @@ public static String toDafnyShapeVisitorWriter(
)
);
}
final String funcName =
(memberShape.getId().toString().replaceAll("[.$#]", "_")).concat(
"_ToDafny("
);
nextVisitorFunction = funcName.concat(dataSource).concat(")");
return nextVisitorFunction;
final String funcName = funcNameGenerator(memberShape, "ToDafny");
return(funcName.concat("(").concat(dataSource).concat(")"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import software.amazon.smithy.model.traits.RangeTrait;
import software.amazon.smithy.model.traits.RequiredTrait;
import software.amazon.smithy.model.traits.StreamingTrait;
import software.amazon.smithy.utils.CaseUtils;
import software.amazon.polymorph.smithygo.utils.GoCodegenUtils;

// Renders constraint validation
Expand Down Expand Up @@ -54,15 +55,26 @@ public ValidationGenerator(
this.sortedMembers = new CodegenUtils.SortedMembers(symbolProvider);
}

/**
* Generates a function name to validate constraints.
*
* This method creates a unique function name by combining:
* 1. The namespace of the MemberShape (with dots removed and converted to camelCase)
* 2. The name of the MemberShape
* 3. A provided suffix
*
* @param memberShape The visiting MemberShape
* @param suffix A string to be appended at the end of the generated function name
* @return A string representing the generated function name
*/
public static String funcNameGenerator(
final MemberShape memberShape,
final String suffix
) {
return memberShape
.getId()
.toString()
.replaceAll("[.$#]", "_")
.concat("_")
final var memberNameSpace = memberShape.getId().getNamespace().replaceAll("[.]", "_");
final var memberNameSpaceCamelCase = CaseUtils.toCamelCase(memberNameSpace);
return memberNameSpaceCamelCase
.concat(memberShape.getId().getName())
.concat(suffix);
}

Expand Down Expand Up @@ -94,7 +106,7 @@ public void writeFuncValidations(final Symbol symbol) {
writer.openBlock(
"func (input $L) $L($L) (error) {",
symbol.getName(),
funcNameGenerator(key, "validate"),
funcNameGenerator(key, "Validate"),
inputType
);
writer.write(validationFuncMap.get(key));
Expand Down Expand Up @@ -448,7 +460,7 @@ private void renderListShape(final ListShape currentShape, final MemberShape me
renderValidatorForEachShape(model.expectShape(itemMember.getTarget()), itemMember, false, LIST_ITEM, itemValidation);
// If the validation function is not created and the list shape does have some constraints
if (!validationFuncMap.containsKey(memberShape) && !itemValidation.isEmpty()) {
final String funcName = funcNameGenerator(memberShape, "validate");
final String funcName = funcNameGenerator(memberShape, "Validate");
final String funcInput = dataSource.startsWith("input") ? "" : dataSource;
if (!funcInput.isEmpty()) {
var inputType = GoCodegenUtils.getType(symbolProvider.toSymbol(currentShape), currentShape);
Expand Down Expand Up @@ -497,7 +509,7 @@ private void renderMapShape(final MapShape currentShape, final MemberShape memb
final var maybeMapValue = valueValidation.isEmpty() ? "_" : MAP_VALUE;
// If the validation function is not created and the map shape does have some constraints in its key and value
if (!validationFuncMap.containsKey(memberShape) && (!keyValidation.isEmpty() || !valueValidation.isEmpty())) {
final var funcName = funcNameGenerator(memberShape, "validate");
final var funcName = funcNameGenerator(memberShape, "Validate");
final var funcInput = dataSource.startsWith("input") ? "" : dataSource;
if (!funcInput.isEmpty()) {
var inputType = GoCodegenUtils.getType(symbolProvider.toSymbol(currentShape), currentShape);
Expand Down Expand Up @@ -535,7 +547,7 @@ private void renderMapShape(final MapShape currentShape, final MemberShape memb
}

private void renderUnionShape(final UnionShape currentShape, final MemberShape memberShape, final StringBuilder validationCode, final String dataSource) {
final var funcName = funcNameGenerator(memberShape, "validate");
final var funcName = funcNameGenerator(memberShape, "Validate");
final var funcInput = dataSource.startsWith("input") ? "" : dataSource;
var dataSourceForUnion = dataSource;
if (!funcInput.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import software.amazon.smithy.model.traits.UnitTypeTrait;
import software.amazon.polymorph.smithygo.utils.GoCodegenUtils;

import static software.amazon.polymorph.smithygo.utils.Constants.funcNameGenerator;

public class DafnyLocalServiceTypeConversionProtocol
implements ProtocolGenerator {

Expand Down Expand Up @@ -1313,7 +1315,7 @@ private void generateSerializerFunctions(final GenerationContext context, final
return $L
}
""",
ShapeVisitorHelper.funcNameGenerator(
funcNameGenerator(
visitingMemberShape,
"ToDafny"
),
Expand Down Expand Up @@ -1382,7 +1384,7 @@ private void generateDeserializerFunctions(final GenerationContext context, fina
func $L(input interface{})($L) {
$L
}""",
ShapeVisitorHelper.funcNameGenerator(
funcNameGenerator(
visitingMemberShape,
"FromDafny"
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.Shape;

import static software.amazon.polymorph.smithygo.utils.Constants.funcNameGenerator;

public class ShapeVisitorHelper {

private static final Map<MemberShape, Boolean> optionalShapesToDafny =
Expand All @@ -19,25 +21,6 @@ public static boolean isToDafnyShapeOptional(final MemberShape shape) {
return optionalShapesToDafny.get(shape);
}

/**
* Generates functions Name for To Dafny and To Native conversion.
*
* @param memberShape MemberShape to generate function name for.
* @param suffix Suffix to add to the function. As of this writing, we only put FromDafny or ToNative suffix.
* @return the function Name
*/
public static String funcNameGenerator(
final MemberShape memberShape,
final String suffix
) {
return memberShape
.getId()
.toString()
.replaceAll("[.$#]", "_")
.concat("_")
.concat(suffix);
}

public static String toNativeShapeVisitorWriter(
final MemberShape memberShape,
final GenerationContext context,
Expand Down Expand Up @@ -81,7 +64,6 @@ public static String toNativeShapeVisitorWriter(
);
}
}
final String nextVisitorFunction;
final String funcDataSource = "input";
if (!DafnyToSmithyShapeVisitor.getAllShapesRequiringConversionFunc().contains(memberShape)) {
DafnyToSmithyShapeVisitor.putShapesWithConversionFunc(memberShape, "");
Expand All @@ -99,8 +81,7 @@ public static String toNativeShapeVisitorWriter(
);
}
final String funcName = funcNameGenerator(memberShape, "FromDafny");
nextVisitorFunction = funcName.concat("(").concat(dataSource).concat(")");
return nextVisitorFunction;
return (funcName.concat("(").concat(dataSource).concat(")"));
}

public static String toDafnyShapeVisitorWriter(
Expand All @@ -115,7 +96,6 @@ public static String toDafnyShapeVisitorWriter(
final Shape targetShape = context
.model()
.expectShape(memberShape.getTarget());
final String nextVisitorFunction;
if (targetShape.hasTrait(ReferenceTrait.class)) {
return targetShape.accept(
new SmithyToDafnyShapeVisitor(
Expand Down Expand Up @@ -146,11 +126,7 @@ public static String toDafnyShapeVisitorWriter(
)
);
}
final String funcName =
(memberShape.getId().toString().replaceAll("[.$#]", "_")).concat(
"_ToDafny("
);
nextVisitorFunction = funcName.concat(dataSource).concat(")");
return nextVisitorFunction;
final String funcName = funcNameGenerator(memberShape, "ToDafny");
return (funcName.concat("(").concat(dataSource).concat(")"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package software.amazon.polymorph.smithygo.utils;

import software.amazon.smithy.model.shapes.MemberShape;

public class Constants {
public static final String DAFNY_RUNTIME_GO_LIBRARY_MODULE = "github.com/dafny-lang/DafnyRuntimeGo/v4";

// TODO: Is it possible to make this function name shorter and in camelCase?
/**
* Generates a function name for shape visitors for AWS SDK and localservice.
*
* @param memberShape The visiting MemberShape
* @param suffix A string to be appended at the end of the generated function name
* @return A string representing the generated function name
*/
public static String funcNameGenerator(
final MemberShape memberShape,
final String suffix
) {
return memberShape
.getId()
.toString()
.replaceAll("[.$#]", "_")
.concat("_")
.concat(suffix);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why is this different than one in the ValidationGenerator ?

Copy link
Author

Choose a reason for hiding this comment

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

I thought the name in ValidationGenerator won't collide and will look good but the name collided (Caught in CI in 8738b87) So, I made ValidationGenerator to use same function name.

}
}
Loading