diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceTypeConversionProtocol.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceTypeConversionProtocol.java index d2a1dd25d..e1f68a0c0 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceTypeConversionProtocol.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/localservice/DafnyLocalServiceTypeConversionProtocol.java @@ -446,11 +446,7 @@ public void generateOrphanShapeSerializer( final Set alreadyVisited, ServiceShape serviceShape ) { - if (alreadyVisited.contains(shape.toShapeId())) { - return; - } - // We don't need type conversion for operation shape and resource shape - if (shape.isOperationShape() || shape.isResourceShape()) { + if (GoCodegenUtils.shapeShouldHaveConversionFunction(shape) == false || alreadyVisited.contains(shape.toShapeId())) { return; } if (shape.hasTrait(UnitTypeTrait.class)) { @@ -952,10 +948,7 @@ public void generateOrphanShapeDeserializer( final Set alreadyVisited, ServiceShape serviceShape ) { - if (alreadyVisited.contains(shape.toShapeId())) { - return; - } - if (shape.isOperationShape() || shape.isResourceShape()) { + if (GoCodegenUtils.shapeShouldHaveConversionFunction(shape) == false || alreadyVisited.contains(shape.toShapeId())) { return; } if (shape.hasTrait(UnitTypeTrait.class)) { diff --git a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/utils/GoCodegenUtils.java b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/utils/GoCodegenUtils.java index 00fc41ffa..9b56d02ed 100644 --- a/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/utils/GoCodegenUtils.java +++ b/codegen/smithy-dafny-codegen/src/main/java/software/amazon/polymorph/smithygo/utils/GoCodegenUtils.java @@ -6,6 +6,8 @@ import software.amazon.polymorph.smithygo.codegen.SymbolUtils; import software.amazon.polymorph.smithygo.localservice.nameresolver.DafnyNameResolver; import software.amazon.polymorph.smithygo.localservice.nameresolver.SmithyNameResolver; +import software.amazon.polymorph.smithypython.awssdk.nameresolver.AwsSdkNameResolver; +import software.amazon.smithy.model.traits.ErrorTrait; import software.amazon.polymorph.traits.PositionalTrait; import software.amazon.polymorph.traits.ReferenceTrait; import software.amazon.smithy.aws.traits.ServiceTrait; @@ -188,4 +190,34 @@ private static String getInputOrOutputName( ); } } + + /** + * Returns true if a conversion function should be written for the shape, false otherwise. + * Conversion functions are only written for "complex" shapes: + * - StructureShapes ("complex" because StructureShapes can be recursive) + * - except for non-AWS SDK StructureShapes with ErrorTrait; these aren't "complex" + * - UnionShapes ("complex" because the conversion is not a one-liner) + * - EnumShapes or StringShapes with EnumTrait ("complex" because the conversion is not a one-liner) + * @param shape + * @return + */ + public static boolean shapeShouldHaveConversionFunction(Shape shape) { + if (shape.isStructureShape()) { + if ( + !SmithyNameResolver.isShapeFromAWSSDK(shape) && + shape.hasTrait(ErrorTrait.class) + ) { + return false; + } + return true; + } else if (shape.isUnionShape()) { + return true; + } else if ( + (shape.isStringShape() && shape.hasTrait(EnumTrait.class)) || + shape.isEnumShape() + ) { + return true; + } + return false; + } }