Skip to content

Commit 8209f96

Browse files
authored
fix: empty* comparison (#1102)
1 parent 677be9b commit 8209f96

File tree

10 files changed

+73
-8
lines changed

10 files changed

+73
-8
lines changed

codegen/smithy-kotlin-codegen/src/main/kotlin/software/amazon/smithy/kotlin/codegen/rendering/endpoints/DefaultEndpointProviderTestGenerator.kt

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ class DefaultEndpointProviderTestGenerator(
6161
fun render() {
6262
writer.addImport("*", namespace = "kotlin.test")
6363
writer.withBlock("public class #L {", "}", CLASS_NAME) {
64-
writer.write("")
6564
writer.withBlock(
6665
"private fun expectEqualEndpoints(expected: #1T, actual: #1T) {",
6766
"}",
@@ -79,19 +78,15 @@ class DefaultEndpointProviderTestGenerator(
7978
RuntimeTypes.Core.Collections.toMutableAttributes,
8079
)
8180
writer.write(
82-
"if (actual.attributes.contains(#1T)) newActualAttributes.remove(#1T)",
81+
"newActualAttributes.remove(#T)",
8382
RuntimeTypes.Core.BusinessMetrics.ServiceEndpointOverride,
8483
)
8584
writer.write(
86-
"if (actual.attributes.contains(#1T)) newActualAttributes.remove(#1T)",
85+
"newActualAttributes.remove(#T)",
8786
RuntimeTypes.Core.BusinessMetrics.AccountIdBasedEndpointAccountId,
8887
)
8988
writer.write(
90-
"val newActualAttributesOrEmpty = if (newActualAttributes.isEmpty) #T() else newActualAttributes",
91-
RuntimeTypes.Core.Collections.emptyAttributes,
92-
)
93-
writer.write(
94-
"val newActual = #T(actual.uri, actual.headers, newActualAttributesOrEmpty)",
89+
"val newActual = #T(actual.uri, actual.headers, newActualAttributes)",
9590
RuntimeTypes.SmithyClient.Endpoints.Endpoint,
9691
)
9792
writer.write("assertEquals(expected, newActual)")

runtime/protocol/http/api/http.api

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public final class aws/smithy/kotlin/runtime/http/HeadersBuilder : aws/smithy/ko
5252

5353
public abstract class aws/smithy/kotlin/runtime/http/HttpBody {
5454
public static final field Companion Laws/smithy/kotlin/runtime/http/HttpBody$Companion;
55+
public fun equals (Ljava/lang/Object;)Z
5556
public fun getContentLength ()Ljava/lang/Long;
5657
public fun isDuplex ()Z
5758
public fun isOneShot ()Z
@@ -74,6 +75,7 @@ public final class aws/smithy/kotlin/runtime/http/HttpBody$Companion {
7475

7576
public final class aws/smithy/kotlin/runtime/http/HttpBody$Empty : aws/smithy/kotlin/runtime/http/HttpBody {
7677
public static final field INSTANCE Laws/smithy/kotlin/runtime/http/HttpBody$Empty;
78+
public fun equals (Ljava/lang/Object;)Z
7779
public fun getContentLength ()Ljava/lang/Long;
7880
public final fun isOneShot ()Z
7981
}

runtime/protocol/http/common/src/aws/smithy/kotlin/runtime/http/DeferredHeaders.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ private object EmptyDeferredHeaders : DeferredHeaders {
3535
override fun entries(): Set<Map.Entry<String, List<Deferred<String>>>> = emptySet()
3636
override fun contains(name: String): Boolean = false
3737
override fun isEmpty(): Boolean = true
38+
override fun equals(other: Any?): Boolean = other is DeferredHeaders && other.isEmpty()
3839
}
3940

4041
/**

runtime/protocol/http/common/src/aws/smithy/kotlin/runtime/http/Headers.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ private object EmptyHeaders : Headers {
3232
override fun entries(): Set<Map.Entry<String, List<String>>> = emptySet()
3333
override fun contains(name: String): Boolean = false
3434
override fun isEmpty(): Boolean = true
35+
override fun equals(other: Any?): Boolean = other is Headers && other.isEmpty()
3536
}
3637

3738
/**

runtime/protocol/http/common/src/aws/smithy/kotlin/runtime/http/HttpBody.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,19 @@ public sealed class HttpBody {
3535
*/
3636
public open val isDuplex: Boolean = false
3737

38+
override fun equals(other: Any?): Boolean =
39+
other is HttpBody &&
40+
other.contentLength == contentLength &&
41+
other.isOneShot == isOneShot &&
42+
other.isDuplex == isDuplex
43+
3844
/**
3945
* Variant of a [HttpBody] without a payload
4046
*/
4147
public object Empty : HttpBody() {
4248
final override val isOneShot: Boolean = false
4349
override val contentLength: Long = 0
50+
override fun equals(other: Any?): Boolean = other is HttpBody && other.contentLength == contentLength
4451
}
4552

4653
/**
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package aws.smithy.kotlin.runtime.http
6+
7+
import kotlin.test.Test
8+
import kotlin.test.assertEquals
9+
10+
class DeferredHeadersTest {
11+
@Test
12+
fun testEmptyEquals() {
13+
val explicitlyEmpty = DeferredHeaders.Empty
14+
val implicitlyEmpty = DeferredHeadersBuilder().build()
15+
16+
assertEquals(implicitlyEmpty, explicitlyEmpty)
17+
assertEquals(explicitlyEmpty, implicitlyEmpty)
18+
19+
assertEquals(explicitlyEmpty, explicitlyEmpty)
20+
assertEquals(implicitlyEmpty, implicitlyEmpty)
21+
}
22+
}

runtime/protocol/http/common/test/aws/smithy/kotlin/runtime/http/HeadersTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,16 @@ class HeadersTest {
5555
assertEquals(firstExpected.entries, first.entries())
5656
assertEquals(secondExpected.entries, second.entries())
5757
}
58+
59+
@Test
60+
fun testEmptyEquals() {
61+
val explicitlyEmpty = Headers.Empty
62+
val implicitlyEmpty = HeadersBuilder().build()
63+
64+
assertEquals(implicitlyEmpty, explicitlyEmpty)
65+
assertEquals(explicitlyEmpty, implicitlyEmpty)
66+
67+
assertEquals(explicitlyEmpty, explicitlyEmpty)
68+
assertEquals(implicitlyEmpty, implicitlyEmpty)
69+
}
5870
}

runtime/protocol/http/common/test/aws/smithy/kotlin/runtime/http/HttpBodyTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,16 @@ class HttpBodyTest {
7979

8080
assertEquals(expected, body.readAll()!!.decodeToString())
8181
}
82+
83+
@Test
84+
fun testEmptyEquals() {
85+
val explicitlyEmpty = HttpBody.Empty
86+
val implicitlyEmpty = HttpBody.fromBytes(byteArrayOf())
87+
88+
assertEquals(implicitlyEmpty, explicitlyEmpty)
89+
assertEquals(explicitlyEmpty, implicitlyEmpty)
90+
91+
assertEquals(implicitlyEmpty, implicitlyEmpty)
92+
assertEquals(explicitlyEmpty, explicitlyEmpty)
93+
}
8294
}

runtime/runtime-core/common/src/aws/smithy/kotlin/runtime/collections/Attributes.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ private object EmptyAttributes : Attributes {
172172
override val keys: Set<AttributeKey<*>> = emptySet()
173173
override fun contains(key: AttributeKey<*>): Boolean = false
174174
override fun <T : Any> getOrNull(key: AttributeKey<T>): T? = null
175+
override fun equals(other: Any?): Boolean = other is Attributes && other.isEmpty
175176
}
176177

177178
/**

runtime/runtime-core/common/test/aws/smithy/kotlin/runtime/collections/AttributesTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,16 @@ class AttributesTest {
125125
assertEquals("foo", attrs[attr1])
126126
assertEquals(57, attrs[attr2])
127127
}
128+
129+
@Test
130+
fun testEmptyEquals() {
131+
val explicitlyEmpty = emptyAttributes()
132+
val implicitlyEmpty = AttributesBuilder().attributes
133+
134+
assertEquals(implicitlyEmpty, explicitlyEmpty)
135+
assertEquals(explicitlyEmpty, implicitlyEmpty)
136+
137+
assertEquals(explicitlyEmpty, explicitlyEmpty)
138+
assertEquals(implicitlyEmpty, implicitlyEmpty)
139+
}
128140
}

0 commit comments

Comments
 (0)