Skip to content

Commit

Permalink
Bring function name generator in a constant utils file
Browse files Browse the repository at this point in the history
  • Loading branch information
rishav-karanjit committed Oct 17, 2024
1 parent 0249fa0 commit f7340ed
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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 @@ -597,7 +599,7 @@ func Error_ToDafny(err error)($L.Error) {
}
""",
DafnyNameResolver.dafnyTypesNamespace(serviceShape),
writer.consumer(w -> {
writer.consumer(w -> {
for (final var error : errorShapes) {
w.write(
"""
Expand Down Expand Up @@ -806,7 +808,7 @@ private void generateSerializerFunctions(final GenerationContext context, final
return $L
}
""",
ShapeVisitorHelper.funcNameGenerator(
funcNameGenerator(
visitingMemberShape,
"ToDafny"
),
Expand Down Expand Up @@ -856,7 +858,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 @@ -34,17 +36,17 @@ public static boolean isToNativeShapePointable(final MemberShape shape) {
* @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 funcNameGenerator(
// final MemberShape memberShape,
// final String suffix
// ) {
// return memberShape
// .getId()
// .toString()
// .replaceAll("[.$#]", "_")
// .concat("_")
// .concat(suffix);
// }

public static String toNativeShapeVisitorWriter(
final MemberShape memberShape,
Expand Down Expand Up @@ -135,11 +137,8 @@ public static String toDafnyShapeVisitorWriter(
)
);
}
final String funcName =
(memberShape.getId().toString().replaceAll("[.$#]", "_")).concat(
"_ToDafny("
);
nextVisitorFunction = funcName.concat(dataSource).concat(")");
final String funcName = funcNameGenerator(memberShape, "ToDafny");
nextVisitorFunction = funcName.concat("(").concat(dataSource).concat(")");
return nextVisitorFunction;
}
}
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 @@ -1383,7 +1385,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 @@ -146,11 +129,8 @@ public static String toDafnyShapeVisitorWriter(
)
);
}
final String funcName =
(memberShape.getId().toString().replaceAll("[.$#]", "_")).concat(
"_ToDafny("
);
nextVisitorFunction = funcName.concat(dataSource).concat(")");
final String funcName = funcNameGenerator(memberShape, "ToDafny");
nextVisitorFunction = funcName.concat("(").concat(dataSource).concat(")");
return nextVisitorFunction;
}
}
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";

// 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);
}
}

0 comments on commit f7340ed

Please sign in to comment.