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 @@ -17,6 +17,7 @@
import software.amazon.polymorph.smithygo.codegen.integration.ProtocolGenerator;
import software.amazon.polymorph.smithygo.localservice.nameresolver.DafnyNameResolver;
import software.amazon.polymorph.smithygo.localservice.nameresolver.SmithyNameResolver;
import software.amazon.polymorph.smithygo.utils.Constants;
import software.amazon.polymorph.smithygo.utils.GoCodegenUtils;
import software.amazon.smithy.aws.traits.ServiceTrait;
import software.amazon.smithy.model.Model;
Expand Down Expand Up @@ -823,10 +824,7 @@ private void generateSerializerFunctions(
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

visitingMemberShape,
"ToDafny"
),
Constants.funcNameGenerator(visitingMemberShape, "ToDafny"),
inputType,
outputType,
AwsSdkToDafnyShapeVisitor.getConversionFunc(visitingMemberShape)
Expand Down Expand Up @@ -881,10 +879,7 @@ private void generateDeserializerFunctions(
func $L(input $L)($L) {
return $L
}""",
ShapeVisitorHelper.funcNameGenerator(
visitingMemberShape,
"FromDafny"
),
Constants.funcNameGenerator(visitingMemberShape, "FromDafny"),
"interface {}",
outputType,
DafnyToAwsSdkShapeVisitor.getConversionFunc(visitingMemberShape)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import software.amazon.polymorph.smithygo.codegen.GenerationContext;
import software.amazon.polymorph.smithygo.codegen.GoWriter;
import software.amazon.polymorph.smithygo.localservice.nameresolver.DafnyNameResolver;
import software.amazon.polymorph.smithygo.utils.Constants;
import software.amazon.polymorph.traits.ReferenceTrait;
import software.amazon.smithy.model.shapes.MemberShape;
import software.amazon.smithy.model.shapes.Shape;
Expand All @@ -26,25 +26,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 @@ -68,7 +49,6 @@ public static String toNativeShapeVisitorWriter(
)
.concat(")");
}
final String nextVisitorFunction;
final String funcDataSource = "input";
if (
!DafnyToAwsSdkShapeVisitor
Expand All @@ -90,9 +70,11 @@ public static String toNativeShapeVisitorWriter(
);
pointerShapesToNative.put(memberShape, isPointable);
}
final String funcName = funcNameGenerator(memberShape, "FromDafny");
nextVisitorFunction = funcName.concat("(").concat(dataSource).concat(")");
return nextVisitorFunction;
final String funcName = Constants.funcNameGenerator(
memberShape,
"FromDafny"
);
return (funcName.concat("(").concat(dataSource).concat(")"));
}

public static String toDafnyShapeVisitorWriter(
Expand All @@ -107,7 +89,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 @@ -142,11 +123,7 @@ public static String toDafnyShapeVisitorWriter(
)
);
}
final String funcName =
(memberShape.getId().toString().replaceAll("[.$#]", "_")).concat(
"_ToDafny("
);
nextVisitorFunction = funcName.concat(dataSource).concat(")");
return nextVisitorFunction;
final String funcName = Constants.funcNameGenerator(memberShape, "ToDafny");
return (funcName.concat("(").concat(dataSource).concat(")"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.Optional;
import software.amazon.polymorph.smithygo.codegen.knowledge.GoPointableIndex;
import software.amazon.polymorph.smithygo.localservice.nameresolver.SmithyNameResolver;
import software.amazon.polymorph.smithygo.utils.Constants;
import software.amazon.polymorph.smithygo.utils.GoCodegenUtils;
import software.amazon.polymorph.traits.DafnyUtf8BytesTrait;
import software.amazon.polymorph.traits.ReferenceTrait;
Expand Down Expand Up @@ -54,18 +55,6 @@ public ValidationGenerator(
this.sortedMembers = new CodegenUtils.SortedMembers(symbolProvider);
}

public static String funcNameGenerator(
final MemberShape memberShape,
final String suffix
) {
return memberShape
.getId()
.toString()
.replaceAll("[.$#]", "_")
.concat("_")
.concat(suffix);
}

public void renderValidator(
final Shape shape,
final boolean isInputStructure
Expand Down Expand Up @@ -94,7 +83,7 @@ public void writeFuncValidations(final Symbol symbol) {
writer.openBlock(
"func (input $L) $L($L) (error) {",
symbol.getName(),
funcNameGenerator(key, "validate"),
Constants.funcNameGenerator(key, "Validate"),
inputType
);
writer.write(validationFuncMap.get(key));
Expand Down Expand Up @@ -481,7 +470,10 @@ private void renderListShape(
if (
!validationFuncMap.containsKey(memberShape) && !itemValidation.isEmpty()
) {
final String funcName = funcNameGenerator(memberShape, "validate");
final String funcName = Constants.funcNameGenerator(
memberShape,
"Validate"
);
final String funcInput = dataSource.startsWith("input") ? "" : dataSource;
if (!funcInput.isEmpty()) {
var inputType = GoCodegenUtils.getType(
Expand Down Expand Up @@ -552,7 +544,7 @@ private void renderMapShape(
!validationFuncMap.containsKey(memberShape) &&
(!keyValidation.isEmpty() || !valueValidation.isEmpty())
) {
final var funcName = funcNameGenerator(memberShape, "validate");
final var funcName = Constants.funcNameGenerator(memberShape, "Validate");
final var funcInput = dataSource.startsWith("input") ? "" : dataSource;
if (!funcInput.isEmpty()) {
var inputType = GoCodegenUtils.getType(
Expand Down Expand Up @@ -597,7 +589,7 @@ private void renderUnionShape(
final StringBuilder validationCode,
final String dataSource
) {
final var funcName = funcNameGenerator(memberShape, "validate");
final var funcName = Constants.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 @@ -15,6 +15,7 @@
import software.amazon.polymorph.smithygo.localservice.shapevisitor.DafnyToSmithyShapeVisitor;
import software.amazon.polymorph.smithygo.localservice.shapevisitor.ShapeVisitorHelper;
import software.amazon.polymorph.smithygo.localservice.shapevisitor.SmithyToDafnyShapeVisitor;
import software.amazon.polymorph.smithygo.utils.Constants;
import software.amazon.polymorph.smithygo.utils.GoCodegenUtils;
import software.amazon.polymorph.traits.ExtendableTrait;
import software.amazon.polymorph.traits.LocalServiceTrait;
Expand Down Expand Up @@ -1334,10 +1335,7 @@ private void generateSerializerFunctions(
return $L
}
""",
ShapeVisitorHelper.funcNameGenerator(
visitingMemberShape,
"ToDafny"
),
Constants.funcNameGenerator(visitingMemberShape, "ToDafny"),
inputType,
outputType,
SmithyToDafnyShapeVisitor.getConversionFunc(visitingMemberShape)
Expand Down Expand Up @@ -1414,10 +1412,7 @@ private void generateDeserializerFunctions(
func $L(input interface{})($L) {
$L
}""",
ShapeVisitorHelper.funcNameGenerator(
visitingMemberShape,
"FromDafny"
),
Constants.funcNameGenerator(visitingMemberShape, "FromDafny"),
outputType,
DafnyToSmithyShapeVisitor.getConversionFunc(visitingMemberShape)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import software.amazon.polymorph.smithygo.codegen.GenerationContext;
import software.amazon.polymorph.smithygo.codegen.GoWriter;
import software.amazon.polymorph.smithygo.localservice.nameresolver.DafnyNameResolver;
import software.amazon.polymorph.smithygo.utils.Constants;
import software.amazon.polymorph.traits.ReferenceTrait;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.model.shapes.MemberShape;
Expand All @@ -19,25 +20,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 @@ -90,7 +72,6 @@ public static String toNativeShapeVisitorWriter(
);
}
}
final String nextVisitorFunction;
final String funcDataSource = "input";
if (
!DafnyToSmithyShapeVisitor
Expand All @@ -111,9 +92,11 @@ public static String toNativeShapeVisitorWriter(
)
);
}
final String funcName = funcNameGenerator(memberShape, "FromDafny");
nextVisitorFunction = funcName.concat("(").concat(dataSource).concat(")");
return nextVisitorFunction;
final String funcName = Constants.funcNameGenerator(
memberShape,
"FromDafny"
);
return (funcName.concat("(").concat(dataSource).concat(")"));
}

public static String toDafnyShapeVisitorWriter(
Expand All @@ -128,7 +111,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 @@ -163,11 +145,7 @@ public static String toDafnyShapeVisitorWriter(
)
);
}
final String funcName =
(memberShape.getId().toString().replaceAll("[.$#]", "_")).concat(
"_ToDafny("
);
nextVisitorFunction = funcName.concat(dataSource).concat(")");
return nextVisitorFunction;
final String funcName = Constants.funcNameGenerator(memberShape, "ToDafny");
return (funcName.concat("(").concat(dataSource).concat(")"));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
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);
}
}
Loading