awslabs / aws-sdk-swift

Apache License 2.0
402 stars 80 forks source link

Correct build warnings for unneeded default values #1769

Closed jbelkins closed 1 month ago

jbelkins commented 1 month ago

Describe the bug

When building the SDK, the following warnings are thrown:

/Users/jbelkins/Code/Xcode/aws-sdk-swift/Sources/Services/AWSConnect/Sources/AWSConnect/Models.swift:37022:72: warning: left side of nil coalescing operator '??' has non-optional type 'Double', so the right side is never used
37020 |                 return .stringvalue(try reader["StringValue"].read())
37021 |             case "NumericValue":
37022 |                 return .numericvalue(try reader["NumericValue"].read() ?? 0)
      |                                                                        `- warning: left side of nil coalescing operator '??' has non-optional type 'Double', so the right side is never used
37023 |             case "NotApplicable":
37024 |                 return .notapplicable(try reader["NotApplicable"].read() ?? false)

/Users/jbelkins/Code/Xcode/aws-sdk-swift/Sources/Services/AWSConnect/Sources/AWSConnect/Models.swift:37024:74: warning: left side of nil coalescing operator '??' has non-optional type 'Bool', so the right side is never used
37022 |                 return .numericvalue(try reader["NumericValue"].read() ?? 0)
37023 |             case "NotApplicable":
37024 |                 return .notapplicable(try reader["NotApplicable"].read() ?? false)
      |                                                                          `- warning: left side of nil coalescing operator '??' has non-optional type 'Bool', so the right side is never used
37025 |             default:
37026 |                 return .sdkUnknown(name ?? "")

Correct codegen so that deserialization code is not rendered with unnecessary defaults.

Expected Behavior

SDK should build without warnings

Current Behavior

SDK builds with the warnings shown above

Reproduction Steps

Build AWSConnect service Observe compiler output

Possible Solution

No response

Additional Information/Context

No response

AWS SWIFT SDK version used

1.0.3 / latest

Compiler and Version used

Xcode 16

Operating System and version

macOS 14

jbelkins commented 1 month ago

The warnings are caused by union member targets that have default values. Union members are non-optional, but we are rendering the code to fill them with the default value applied.

For .numericvalue:

        "com.amazonaws.connect#EvaluationAnswerDataNumericValue": {
            "type": "double",
            "traits": {
                "smithy.api#default": 0
            }
        },

For .notapplicable:

        "com.amazonaws.connect#Boolean": {
            "type": "boolean",
            "traits": {
                "smithy.api#default": false
            }
        },
jbelkins commented 1 month ago

Fixed in https://github.com/smithy-lang/smithy-swift/pull/840