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

fix(go): make all type conversion shape public and delegate type conversion to dependent shape #776

Open
wants to merge 6 commits into
base: main-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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 @@ -498,8 +498,7 @@ private void renderListShape(
) {
final String funcName = Constants.funcNameGenerator(
memberShape,
"Validate",
context.model()
"Validate"
);
final String funcInput = dataSource.startsWith("input") ? "" : dataSource;
if (!funcInput.isEmpty()) {
Expand Down Expand Up @@ -593,11 +592,7 @@ private void renderMapShape(
!validationFuncMap.containsKey(memberShape) &&
(!keyValidation.isEmpty() || !valueValidation.isEmpty())
) {
final var funcName = Constants.funcNameGenerator(
memberShape,
"Validate",
context.model()
);
final var funcName = Constants.funcNameGenerator(memberShape, "Validate");
final var funcInput = dataSource.startsWith("input") ? "" : dataSource;
if (!funcInput.isEmpty()) {
final var currServiceShapeNamespace = SmithyNameResolver.shapeNamespace(
Expand Down Expand Up @@ -664,11 +659,7 @@ private void renderUnionShape(
final StringBuilder validationCode,
final String dataSource
) {
final var funcName = Constants.funcNameGenerator(
memberShape,
"Validate",
context.model()
);
final var funcName = Constants.funcNameGenerator(memberShape, "Validate");
final var funcInput = dataSource.startsWith("input") ? "" : dataSource;
var dataSourceForUnion = dataSource;
final var currServiceShapeNamespace =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,7 @@ func NewClient(clientConfig $L) (*$T, error) {
toDafnyMethod =
Constants.funcNameGenerator(
inputMemberShapeForPositional,
"ToDafny",
model
"ToDafny"
);
inputType =
", params %s".formatted(
Expand Down Expand Up @@ -313,11 +312,7 @@ func NewClient(clientConfig $L) (*$T, error) {
outputShape,
""
)
: Constants.funcNameGenerator(
postionalMemShape,
"FromDafny",
model
);
: Constants.funcNameGenerator(postionalMemShape, "FromDafny");
outputType =
SmithyNameResolver
.getSmithyType(
Expand Down Expand Up @@ -534,11 +529,7 @@ void generateShim() {
.shapeNamespace(postionalMemShape)
.concat(".")
.concat(
Constants.funcNameGenerator(
postionalMemShape,
"ToDafny",
model
)
Constants.funcNameGenerator(postionalMemShape, "ToDafny")
);
}
// this is maybe because positional trait can change this
Expand Down Expand Up @@ -604,11 +595,7 @@ void generateShim() {
.shapeNamespace(postionalMemShape)
.concat(".")
.concat(
Constants.funcNameGenerator(
postionalMemShape,
"FromDafny",
model
)
Constants.funcNameGenerator(postionalMemShape, "FromDafny")
);
final Symbol symbolForPositional = symbolProvider.toSymbol(
inputShape
Expand Down Expand Up @@ -967,8 +954,7 @@ void generateReferencedResources(final GenerationContext context) {
)
: Constants.funcNameGenerator(
postionalMemShape,
"FromDafny",
model
"FromDafny"
);

outputType =
Expand Down Expand Up @@ -1143,11 +1129,7 @@ void generateNativeResourceWrapper(
inputShape,
""
)
: Constants.funcNameGenerator(
postionalMemShape,
"FromDafny",
model
);
: Constants.funcNameGenerator(postionalMemShape, "FromDafny");
}
final var inputType = inputShape.hasTrait(UnitTypeTrait.class)
? ""
Expand Down Expand Up @@ -1217,11 +1199,7 @@ void generateNativeResourceWrapper(
outputShape,
""
)
: Constants.funcNameGenerator(
postionalMemShape,
"ToDafny",
model
);
: Constants.funcNameGenerator(postionalMemShape, "ToDafny");
deReferenceRequired = false;
}
clientResponse = "var native_response, native_error";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1997,11 +1997,7 @@ private void generateSerializerFunctions(
return $L
}
""",
Constants.funcNameGenerator(
visitingMemberShape,
"ToDafny",
context.model()
),
Constants.funcNameGenerator(visitingMemberShape, "ToDafny"),
inputType,
outputType,
SmithyToDafnyShapeVisitor.getConversionFunc(visitingMemberShape)
Expand Down Expand Up @@ -2091,11 +2087,7 @@ private void generateDeserializerFunctions(
func $L(input interface{})($L) {
$L
}""",
Constants.funcNameGenerator(
visitingMemberShape,
"FromDafny",
context.model()
),
Constants.funcNameGenerator(visitingMemberShape, "FromDafny"),
outputType,
DafnyToSmithyShapeVisitor.getConversionFunc(visitingMemberShape)
);
Expand Down
Copy link
Member Author

Choose a reason for hiding this comment

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

This contains the diff to delegate type conversion to dependent shape

Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
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.localservice.nameresolver.SmithyNameResolver;
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;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.model.shapes.Shape;

/**
Expand Down Expand Up @@ -89,6 +91,29 @@ public static String toNativeShapeVisitorWriter(
);
}
}
final String funcName = Constants.funcNameGenerator(
memberShape,
"FromDafny"
);
final var serviceShape = context
.model()
.expectShape(context.settings().getService(), ServiceShape.class);
// if the memberShape is an external shape, delegate to the type conversion in that namespace
if (
!serviceShape
.getId()
.getNamespace()
.equals(memberShape.getId().getNamespace())
) {
var memberShapeNameSpace = SmithyNameResolver.shapeNamespace(memberShape);
return (
memberShapeNameSpace
.concat(".")
.concat(funcName.concat("("))
.concat(dataSource)
.concat(")")
);
}
final String funcDataSource = "input";
if (
!DafnyToSmithyShapeVisitor
Expand All @@ -109,11 +134,6 @@ public static String toNativeShapeVisitorWriter(
)
);
}
final String funcName = Constants.funcNameGenerator(
memberShape,
"FromDafny",
context.model()
);
return (funcName.concat("(").concat(dataSource).concat(")"));
}

Expand Down Expand Up @@ -161,6 +181,25 @@ public static String toDafnyShapeVisitorWriter(
);
}
}
final String funcName = Constants.funcNameGenerator(memberShape, "ToDafny");
final var serviceShape = context
.model()
.expectShape(context.settings().getService(), ServiceShape.class);
if (
!serviceShape
.getId()
.getNamespace()
.equals(memberShape.getId().getNamespace())
) {
var memberShapeNameSpace = SmithyNameResolver.shapeNamespace(memberShape);
return (
memberShapeNameSpace
.concat(".")
.concat(funcName.concat("("))
.concat(dataSource)
.concat(")")
);
}
final String funcDataSource = "input";
if (
!SmithyToDafnyShapeVisitor
Expand All @@ -183,11 +222,6 @@ public static String toDafnyShapeVisitorWriter(
)
);
}
final String funcName = Constants.funcNameGenerator(
memberShape,
"ToDafny",
context.model()
);
return (funcName.concat("(").concat(dataSource).concat(")"));
}
}
Copy link
Member Author

@rishav-karanjit rishav-karanjit Jan 29, 2025

Choose a reason for hiding this comment

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

This file contains diff to make all type conversion functions public

Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,7 @@ public class Constants {
// TODO: Is it possible to make this function name shorter and in camelCase?
/**
* Generates a function name for memberShapes.
* Generates private function for all shape excepts memberShape whose containerShape has positional trait
*
* @param memberShape The visiting MemberShape
* @param suffix A string to be appended at the end of the generated function name
* @param model The smithy model being used
* @return A string representing the generated function name
*/
public static String funcNameGenerator(
final MemberShape memberShape,
final String suffix,
final Model model
) {
String funcName = funcNameGenerator(memberShape, suffix);
final Shape containerShape = model.expectShape(memberShape.getContainer());
// membershape inside a container shape with positional trait has to be exposed.
if (containerShape.hasTrait(PositionalTrait.class)) {
funcName = StringUtils.capitalize(funcName);
}
return funcName;
}

/**
* Generates a function name for memberShapes.
* Always generates private function for all shape
* Always generates public (exported) function for all shape
*
* @param memberShape The visiting MemberShape
* @param suffix A string to be appended at the end of the generated function name
Expand All @@ -49,11 +26,13 @@ public static String funcNameGenerator(
final MemberShape memberShape,
final String suffix
) {
return memberShape
.getId()
.toString()
.replaceAll("[.$#]", "_")
.concat("_")
.concat(suffix);
return StringUtils.capitalize(
memberShape
.getId()
.toString()
.replaceAll("[.$#]", "_")
.concat("_")
.concat(suffix)
);
}
}
Loading