Skip to content

Commit

Permalink
feat: Better .NET type conversion encapsulation
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
seebees committed Nov 20, 2024
1 parent 180ad67 commit 884b6bc
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,30 @@ private Set<ShapeId> findInitialShapeIdsToConvert() {
.map(unionShape -> unionShape.getId())
.collect(Collectors.toSet());

// Collect map shapes
final Set<ShapeId> mapShapes = model
.getMapShapes()
.stream()
.map(Shape::getId)
.filter(this::isInServiceNamespace)
.collect(Collectors.toSet());

// Collect list shapes
final Set<ShapeId> listShapes = model
.getListShapes()
.stream()
.map(Shape::getId)
.filter(this::isInServiceNamespace)
.collect(Collectors.toSet());

// Collect structure shapes
final Set<ShapeId> structureShapes = model
.getStructureShapes()
.stream()
.map(Shape::getId)
.filter(this::isInServiceNamespace)
.collect(Collectors.toSet());

// TODO add smithy v2 Enums
// Collect enum shapes
final Set<ShapeId> enumShapes = model
Expand All @@ -258,6 +282,9 @@ private Set<ShapeId> findInitialShapeIdsToConvert() {
orderedSet.addAll(unionShapes);
orderedSet.addAll(errorStructures);
orderedSet.addAll(enumShapes);
orderedSet.addAll(mapShapes);
orderedSet.addAll(listShapes);
orderedSet.addAll(structureShapes);
return orderedSet;
}

Expand Down Expand Up @@ -1984,7 +2011,7 @@ protected TypeConverter buildConverterFromMethodBodies(
// This is more controlled than exposing
// the NativeWrapper and the Dafny wrapped type.
final boolean isDependantModuleType =
ModelUtils.isReferenceDependantModuleType(
ModelUtils.isDependantModuleType(
shape,
nameResolver.namespaceForService()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,19 @@ public static Boolean isReferenceDependantModuleType(
final String namespace
) {
if (shape.hasTrait(ReferenceTrait.class)) {
return !namespace.equalsIgnoreCase(shape.getId().getNamespace());
return isDependantModuleType(shape, namespace);
} else {
return false;
}
}

public static Boolean isDependantModuleType(
final Shape shape,
final String namespace
) {
final String shapeNamespace = shape.getId().getNamespace();
if (!shapeNamespace.toLowerCase().startsWith("smithy.api") && !namespace.equalsIgnoreCase(shapeNamespace)) {
return true;
} else {
return false;
}
Expand Down

0 comments on commit 884b6bc

Please sign in to comment.