Skip to content

Commit e29b8ca

Browse files
fix(go): make all type conversion shape public and delegate type conversion to dependent shape (#776)
1 parent bdafaaa commit e29b8ca

File tree

5 files changed

+66
-92
lines changed

5 files changed

+66
-92
lines changed

codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/codegen/ValidationGenerator.java

+3-12
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,7 @@ private void renderListShape(
498498
) {
499499
final String funcName = Constants.funcNameGenerator(
500500
memberShape,
501-
"Validate",
502-
context.model()
501+
"Validate"
503502
);
504503
final String funcInput = dataSource.startsWith("input") ? "" : dataSource;
505504
if (!funcInput.isEmpty()) {
@@ -593,11 +592,7 @@ private void renderMapShape(
593592
!validationFuncMap.containsKey(memberShape) &&
594593
(!keyValidation.isEmpty() || !valueValidation.isEmpty())
595594
) {
596-
final var funcName = Constants.funcNameGenerator(
597-
memberShape,
598-
"Validate",
599-
context.model()
600-
);
595+
final var funcName = Constants.funcNameGenerator(memberShape, "Validate");
601596
final var funcInput = dataSource.startsWith("input") ? "" : dataSource;
602597
if (!funcInput.isEmpty()) {
603598
final var currServiceShapeNamespace = SmithyNameResolver.shapeNamespace(
@@ -664,11 +659,7 @@ private void renderUnionShape(
664659
final StringBuilder validationCode,
665660
final String dataSource
666661
) {
667-
final var funcName = Constants.funcNameGenerator(
668-
memberShape,
669-
"Validate",
670-
context.model()
671-
);
662+
final var funcName = Constants.funcNameGenerator(memberShape, "Validate");
672663
final var funcInput = dataSource.startsWith("input") ? "" : dataSource;
673664
var dataSourceForUnion = dataSource;
674665
final var currServiceShapeNamespace =

codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceGenerator.java

+7-29
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,7 @@ func NewClient(clientConfig $L) (*$T, error) {
257257
toDafnyMethod =
258258
Constants.funcNameGenerator(
259259
inputMemberShapeForPositional,
260-
"ToDafny",
261-
model
260+
"ToDafny"
262261
);
263262
inputType =
264263
", params %s".formatted(
@@ -313,11 +312,7 @@ func NewClient(clientConfig $L) (*$T, error) {
313312
outputShape,
314313
""
315314
)
316-
: Constants.funcNameGenerator(
317-
postionalMemShape,
318-
"FromDafny",
319-
model
320-
);
315+
: Constants.funcNameGenerator(postionalMemShape, "FromDafny");
321316
outputType =
322317
SmithyNameResolver
323318
.getSmithyType(
@@ -534,11 +529,7 @@ void generateShim() {
534529
.shapeNamespace(postionalMemShape)
535530
.concat(".")
536531
.concat(
537-
Constants.funcNameGenerator(
538-
postionalMemShape,
539-
"ToDafny",
540-
model
541-
)
532+
Constants.funcNameGenerator(postionalMemShape, "ToDafny")
542533
);
543534
}
544535
// this is maybe because positional trait can change this
@@ -604,11 +595,7 @@ void generateShim() {
604595
.shapeNamespace(postionalMemShape)
605596
.concat(".")
606597
.concat(
607-
Constants.funcNameGenerator(
608-
postionalMemShape,
609-
"FromDafny",
610-
model
611-
)
598+
Constants.funcNameGenerator(postionalMemShape, "FromDafny")
612599
);
613600
final Symbol symbolForPositional = symbolProvider.toSymbol(
614601
inputShape
@@ -967,8 +954,7 @@ void generateReferencedResources(final GenerationContext context) {
967954
)
968955
: Constants.funcNameGenerator(
969956
postionalMemShape,
970-
"FromDafny",
971-
model
957+
"FromDafny"
972958
);
973959

974960
outputType =
@@ -1143,11 +1129,7 @@ void generateNativeResourceWrapper(
11431129
inputShape,
11441130
""
11451131
)
1146-
: Constants.funcNameGenerator(
1147-
postionalMemShape,
1148-
"FromDafny",
1149-
model
1150-
);
1132+
: Constants.funcNameGenerator(postionalMemShape, "FromDafny");
11511133
}
11521134
final var inputType = inputShape.hasTrait(UnitTypeTrait.class)
11531135
? ""
@@ -1217,11 +1199,7 @@ void generateNativeResourceWrapper(
12171199
outputShape,
12181200
""
12191201
)
1220-
: Constants.funcNameGenerator(
1221-
postionalMemShape,
1222-
"ToDafny",
1223-
model
1224-
);
1202+
: Constants.funcNameGenerator(postionalMemShape, "ToDafny");
12251203
deReferenceRequired = false;
12261204
}
12271205
clientResponse = "var native_response, native_error";

codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceTypeConversionProtocol.java

+2-10
Original file line numberDiff line numberDiff line change
@@ -1997,11 +1997,7 @@ private void generateSerializerFunctions(
19971997
return $L
19981998
}
19991999
""",
2000-
Constants.funcNameGenerator(
2001-
visitingMemberShape,
2002-
"ToDafny",
2003-
context.model()
2004-
),
2000+
Constants.funcNameGenerator(visitingMemberShape, "ToDafny"),
20052001
inputType,
20062002
outputType,
20072003
SmithyToDafnyShapeVisitor.getConversionFunc(visitingMemberShape)
@@ -2091,11 +2087,7 @@ private void generateDeserializerFunctions(
20912087
func $L(input interface{})($L) {
20922088
$L
20932089
}""",
2094-
Constants.funcNameGenerator(
2095-
visitingMemberShape,
2096-
"FromDafny",
2097-
context.model()
2098-
),
2090+
Constants.funcNameGenerator(visitingMemberShape, "FromDafny"),
20992091
outputType,
21002092
DafnyToSmithyShapeVisitor.getConversionFunc(visitingMemberShape)
21012093
);

codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/shapevisitor/ShapeVisitorHelper.java

+44-10
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import software.amazon.polymorph.smithygo.codegen.GenerationContext;
66
import software.amazon.polymorph.smithygo.codegen.GoWriter;
77
import software.amazon.polymorph.smithygo.localservice.nameresolver.DafnyNameResolver;
8+
import software.amazon.polymorph.smithygo.localservice.nameresolver.SmithyNameResolver;
89
import software.amazon.polymorph.smithygo.utils.Constants;
910
import software.amazon.polymorph.traits.ReferenceTrait;
1011
import software.amazon.smithy.codegen.core.Symbol;
1112
import software.amazon.smithy.model.shapes.MemberShape;
13+
import software.amazon.smithy.model.shapes.ServiceShape;
1214
import software.amazon.smithy.model.shapes.Shape;
1315

1416
/**
@@ -89,6 +91,29 @@ public static String toNativeShapeVisitorWriter(
8991
);
9092
}
9193
}
94+
final String funcName = Constants.funcNameGenerator(
95+
memberShape,
96+
"FromDafny"
97+
);
98+
final var serviceShape = context
99+
.model()
100+
.expectShape(context.settings().getService(), ServiceShape.class);
101+
// if the memberShape is an external shape, delegate to the type conversion in that namespace
102+
if (
103+
!serviceShape
104+
.getId()
105+
.getNamespace()
106+
.equals(memberShape.getId().getNamespace())
107+
) {
108+
var memberShapeNameSpace = SmithyNameResolver.shapeNamespace(memberShape);
109+
return (
110+
memberShapeNameSpace
111+
.concat(".")
112+
.concat(funcName.concat("("))
113+
.concat(dataSource)
114+
.concat(")")
115+
);
116+
}
92117
final String funcDataSource = "input";
93118
if (
94119
!DafnyToSmithyShapeVisitor
@@ -109,11 +134,6 @@ public static String toNativeShapeVisitorWriter(
109134
)
110135
);
111136
}
112-
final String funcName = Constants.funcNameGenerator(
113-
memberShape,
114-
"FromDafny",
115-
context.model()
116-
);
117137
return (funcName.concat("(").concat(dataSource).concat(")"));
118138
}
119139

@@ -161,6 +181,25 @@ public static String toDafnyShapeVisitorWriter(
161181
);
162182
}
163183
}
184+
final String funcName = Constants.funcNameGenerator(memberShape, "ToDafny");
185+
final var serviceShape = context
186+
.model()
187+
.expectShape(context.settings().getService(), ServiceShape.class);
188+
if (
189+
!serviceShape
190+
.getId()
191+
.getNamespace()
192+
.equals(memberShape.getId().getNamespace())
193+
) {
194+
var memberShapeNameSpace = SmithyNameResolver.shapeNamespace(memberShape);
195+
return (
196+
memberShapeNameSpace
197+
.concat(".")
198+
.concat(funcName.concat("("))
199+
.concat(dataSource)
200+
.concat(")")
201+
);
202+
}
164203
final String funcDataSource = "input";
165204
if (
166205
!SmithyToDafnyShapeVisitor
@@ -183,11 +222,6 @@ public static String toDafnyShapeVisitorWriter(
183222
)
184223
);
185224
}
186-
final String funcName = Constants.funcNameGenerator(
187-
memberShape,
188-
"ToDafny",
189-
context.model()
190-
);
191225
return (funcName.concat("(").concat(dataSource).concat(")"));
192226
}
193227
}

codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/utils/Constants.java

+10-31
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,8 @@ public class Constants {
1515

1616
// TODO: Is it possible to make this function name shorter and in camelCase?
1717
/**
18-
* Generates a function name for memberShapes.
19-
* Generates private function for all shape excepts memberShape whose containerShape has positional trait
20-
*
21-
* @param memberShape The visiting MemberShape
22-
* @param suffix A string to be appended at the end of the generated function name
23-
* @param model The smithy model being used
24-
* @return A string representing the generated function name
25-
*/
26-
public static String funcNameGenerator(
27-
final MemberShape memberShape,
28-
final String suffix,
29-
final Model model
30-
) {
31-
String funcName = funcNameGenerator(memberShape, suffix);
32-
final Shape containerShape = model.expectShape(memberShape.getContainer());
33-
// membershape inside a container shape with positional trait has to be exposed.
34-
if (containerShape.hasTrait(PositionalTrait.class)) {
35-
funcName = StringUtils.capitalize(funcName);
36-
}
37-
return funcName;
38-
}
39-
40-
/**
41-
* Generates a function name for memberShapes.
42-
* Always generates private function for all shape
18+
* Generates a function name used for type conversion of memberShapes.
19+
* Always generates public (exported) function for all shape
4320
*
4421
* @param memberShape The visiting MemberShape
4522
* @param suffix A string to be appended at the end of the generated function name
@@ -49,11 +26,13 @@ public static String funcNameGenerator(
4926
final MemberShape memberShape,
5027
final String suffix
5128
) {
52-
return memberShape
53-
.getId()
54-
.toString()
55-
.replaceAll("[.$#]", "_")
56-
.concat("_")
57-
.concat(suffix);
29+
return StringUtils.capitalize(
30+
memberShape
31+
.getId()
32+
.toString()
33+
.replaceAll("[.$#]", "_")
34+
.concat("_")
35+
.concat(suffix)
36+
);
5837
}
5938
}

0 commit comments

Comments
 (0)