Skip to content

Commit 884b6bc

Browse files
committed
feat: Better .NET type conversion encapsulation
Dafny has optional parameters. Smithy-Dafny uses this feature to produce Dafny types that default to `:= None`. However, the translated Dafny does *not* support optional parameters. This means that these optional parameters, are _not_ interoperable in target runtimes. .NET already deferred type conversions of wrapped resources to the target module. But it assumed that Dafny was overly interoperable. This change to .NET defers all type conversion to the module that owns the type. This brings .NET more in line with other runtimes. This change is not completely backwards compatible. There are a few edge cases with orphaned types as well as already published versions.
1 parent 180ad67 commit 884b6bc

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithydotnet/TypeConversionCodegen.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,30 @@ private Set<ShapeId> findInitialShapeIdsToConvert() {
236236
.map(unionShape -> unionShape.getId())
237237
.collect(Collectors.toSet());
238238

239+
// Collect map shapes
240+
final Set<ShapeId> mapShapes = model
241+
.getMapShapes()
242+
.stream()
243+
.map(Shape::getId)
244+
.filter(this::isInServiceNamespace)
245+
.collect(Collectors.toSet());
246+
247+
// Collect list shapes
248+
final Set<ShapeId> listShapes = model
249+
.getListShapes()
250+
.stream()
251+
.map(Shape::getId)
252+
.filter(this::isInServiceNamespace)
253+
.collect(Collectors.toSet());
254+
255+
// Collect structure shapes
256+
final Set<ShapeId> structureShapes = model
257+
.getStructureShapes()
258+
.stream()
259+
.map(Shape::getId)
260+
.filter(this::isInServiceNamespace)
261+
.collect(Collectors.toSet());
262+
239263
// TODO add smithy v2 Enums
240264
// Collect enum shapes
241265
final Set<ShapeId> enumShapes = model
@@ -258,6 +282,9 @@ private Set<ShapeId> findInitialShapeIdsToConvert() {
258282
orderedSet.addAll(unionShapes);
259283
orderedSet.addAll(errorStructures);
260284
orderedSet.addAll(enumShapes);
285+
orderedSet.addAll(mapShapes);
286+
orderedSet.addAll(listShapes);
287+
orderedSet.addAll(structureShapes);
261288
return orderedSet;
262289
}
263290

@@ -1984,7 +2011,7 @@ protected TypeConverter buildConverterFromMethodBodies(
19842011
// This is more controlled than exposing
19852012
// the NativeWrapper and the Dafny wrapped type.
19862013
final boolean isDependantModuleType =
1987-
ModelUtils.isReferenceDependantModuleType(
2014+
ModelUtils.isDependantModuleType(
19882015
shape,
19892016
nameResolver.namespaceForService()
19902017
);

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,19 @@ public static Boolean isReferenceDependantModuleType(
558558
final String namespace
559559
) {
560560
if (shape.hasTrait(ReferenceTrait.class)) {
561-
return !namespace.equalsIgnoreCase(shape.getId().getNamespace());
561+
return isDependantModuleType(shape, namespace);
562+
} else {
563+
return false;
564+
}
565+
}
566+
567+
public static Boolean isDependantModuleType(
568+
final Shape shape,
569+
final String namespace
570+
) {
571+
final String shapeNamespace = shape.getId().getNamespace();
572+
if (!shapeNamespace.toLowerCase().startsWith("smithy.api") && !namespace.equalsIgnoreCase(shapeNamespace)) {
573+
return true;
562574
} else {
563575
return false;
564576
}

0 commit comments

Comments
 (0)