Skip to content

Commit f2a6e81

Browse files
committed
grpc: Address PR comments
1 parent 18e0f78 commit f2a6e81

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

grpc/grpc-core/src/commonMain/kotlin/kotlinx/rpc/grpc/GrpcMetadata.kt

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ public expect class GrpcMetadata constructor()
5252
/**
5353
* Returns the last metadata entry added with the given [key], or `null` if there are no entries.
5454
*
55-
* @param key the name of the metadata entry to retrieve (case-insensitive). Must not end with `-bin`.
55+
* @param key the name of the metadata entry (case-insensitive). Must contain only digits (0-9),
56+
* lowercase letters (a-z), and special characters (`-`, `_`, `.`). Must not end with `-bin`.
5657
* @return the last value associated with the key, or `null` if no values exist
57-
* @throws IllegalArgumentException if the key ends with `-bin`
58+
* @throws IllegalArgumentException if the key ends with `-bin` or contains invalid characters
5859
*/
5960
public expect operator fun GrpcMetadata.get(key: String): String?
6061

@@ -63,18 +64,22 @@ public expect operator fun GrpcMetadata.get(key: String): String?
6364
*
6465
* Binary keys must end with the "-bin" suffix according to gRPC specification.
6566
*
66-
* @param key the name of the binary metadata entry to retrieve (case-insensitive). Must end with `-bin`.
67+
* @param key the name of the metadata entry (case-insensitive).
68+
* Must contain only digits (0-9), lowercase letters (a-z), and special characters (`-`, `_`, `.`).
69+
* Must end with `-bin`.
6770
* @return the last binary value associated with the key, or `null` if no values exist
68-
* @throws IllegalArgumentException if the key does not end with `-bin`
71+
* @throws IllegalArgumentException if the key does not end with `-bin` or contains invalid characters
6972
*/
7073
public expect fun GrpcMetadata.getBinary(key: String): ByteArray?
7174

7275
/**
7376
* Returns all metadata entries with the given [key], in the order they were added.
7477
*
75-
* @param key the name of the metadata entries to retrieve (case-insensitive). Must not end with `-bin`.
78+
* @param key the name of the metadata entry (case-insensitive).
79+
* Must contain only digits (0-9), lowercase letters (a-z), and special characters (`-`, `_`, `.`).
80+
* Must not end with `-bin`.
7681
* @return a list of all values associated with the key, or an empty list if no values exist
77-
* @throws IllegalArgumentException if the key ends with `-bin`
82+
* @throws IllegalArgumentException if the key ends with `-bin` or contains invalid characters
7883
*/
7984
public expect fun GrpcMetadata.getAll(key: String): List<String>
8085

@@ -83,9 +88,11 @@ public expect fun GrpcMetadata.getAll(key: String): List<String>
8388
*
8489
* Binary keys must end with the "-bin" suffix according to gRPC specification.
8590
*
86-
* @param key the name of the binary metadata entries to retrieve (case-insensitive). Must end with `-bin`.
91+
* @param key the name of the metadata entry (case-insensitive).
92+
* Must contain only digits (0-9), lowercase letters (a-z), and special characters (`-`, `_`, `.`).
93+
* Must end with `-bin`.
8794
* @return a list of all binary values associated with the key, or an empty list if no values exist
88-
* @throws IllegalArgumentException if the key does not end with `-bin`
95+
* @throws IllegalArgumentException if the key does not end with `-bin` or contains invalid characters
8996
*/
9097
public expect fun GrpcMetadata.getAllBinary(key: String): List<ByteArray>
9198

@@ -102,7 +109,8 @@ public expect fun GrpcMetadata.keys(): Set<String>
102109
/**
103110
* Returns `true` if this metadata contains one or more values for the specified [key].
104111
*
105-
* @param key the key whose presence is to be tested (case-insensitive)
112+
* @param key the key whose presence is to be tested (case-insensitive).
113+
* Must contain only digits (0-9), lowercase letters (a-z), and special characters (`-`, `_`, `.`).
106114
* @return `true` if this metadata contains the key, `false` otherwise
107115
*/
108116
public expect operator fun GrpcMetadata.contains(key: String): Boolean
@@ -137,10 +145,12 @@ public expect fun GrpcMetadata.appendBinary(key: String, value: ByteArray)
137145
/**
138146
* Removes the first occurrence of the specified [value] for the given [key].
139147
*
140-
* @param key the name of the metadata entry (case-insensitive). Must not end with `-bin`.
148+
* @param key the name of the metadata entry (case-insensitive).
149+
* Must contain only digits (0-9), lowercase letters (a-z), and special characters (`-`, `_`, `.`).
150+
* Must not end with `-bin`.
141151
* @param value the value to remove
142152
* @return `true` if the value was found and removed, `false` if the value was not present
143-
* @throws IllegalArgumentException if the key ends with `-bin`
153+
* @throws IllegalArgumentException if the key ends with `-bin` or contains invalid characters
144154
*/
145155
public expect fun GrpcMetadata.remove(key: String, value: String): Boolean
146156

@@ -149,7 +159,9 @@ public expect fun GrpcMetadata.remove(key: String, value: String): Boolean
149159
*
150160
* Binary keys must end with the "-bin" suffix according to gRPC specification.
151161
*
152-
* @param key the name of the binary metadata entry (case-insensitive). Must end with `-bin`.
162+
* @param key the name of the binary metadata entry (case-insensitive).
163+
* Must contain only digits (0-9), lowercase letters (a-z), and special characters (`-`, `_`, `.`).
164+
* Must end with `-bin`.
153165
* @param value the binary value to remove
154166
* @return `true` if the value was found and removed, `false` if the value was not present
155167
* @throws IllegalArgumentException if the key does not end with `-bin`
@@ -159,7 +171,9 @@ public expect fun GrpcMetadata.removeBinary(key: String, value: ByteArray): Bool
159171
/**
160172
* Removes all values for the given [key] and returns them.
161173
*
162-
* @param key the name of the metadata entries to remove (case-insensitive). Must not end with `-bin`.
174+
* @param key the name of the metadata entries to remove (case-insensitive).
175+
* Must contain only digits (0-9), lowercase letters (a-z), and special characters (`-`, `_`, `.`).
176+
* Must not end with `-bin`.
163177
* @return a list of all values that were removed, or an empty list if there were no values
164178
* @throws IllegalArgumentException if the key ends with `-bin`
165179
*/
@@ -170,7 +184,9 @@ public expect fun GrpcMetadata.removeAll(key: String): List<String>
170184
*
171185
* Binary keys must end with the "-bin" suffix according to gRPC specification.
172186
*
173-
* @param key the name of the binary metadata entries to remove (case-insensitive). Must end with `-bin`.
187+
* @param key the name of the binary metadata entries to remove (case-insensitive).
188+
* Must contain only digits (0-9), lowercase letters (a-z), and special characters (`-`, `_`, `.`).
189+
* Must end with `-bin`.
174190
* @return a list of all binary values that were removed, or an empty list if there were no values
175191
* @throws IllegalArgumentException if the key does not end with `-bin`
176192
*/

grpc/grpc-core/src/nativeMain/kotlin/kotlinx/rpc/grpc/GrpcMetadata.native.kt

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ import libkgrpc.grpc_slice_unref
2727
import libkgrpc.kgrpc_metadata_array_append
2828
import kotlin.experimental.ExperimentalNativeApi
2929

30-
private value class GrpcKey private constructor(val name: String) {
30+
private value class GrpcMetadataKey private constructor(val name: String) {
3131
val isBinary get() = name.endsWith("-bin")
3232

33-
companion object {
34-
fun binary(name: String): GrpcKey {
35-
val key = GrpcKey(validateName(name.lowercase()))
33+
companion object Companion {
34+
fun binary(name: String): GrpcMetadataKey {
35+
val key = GrpcMetadataKey(validateName(name.lowercase()))
3636
require(key.isBinary) { "Binary header is named ${key.name}. It must end with '-bin'" }
3737
return key
3838
}
3939

40-
fun string(name: String): GrpcKey {
41-
val key = GrpcKey(validateName(name.lowercase()))
40+
fun string(name: String): GrpcMetadataKey {
41+
val key = GrpcMetadataKey(validateName(name.lowercase()))
4242
require(!key.isBinary) { "String header is named ${key.name}. It must not end with '-bin'" }
4343
return key
4444
}
@@ -97,19 +97,19 @@ public actual class GrpcMetadata actual constructor() {
9797
}
9898

9999
public actual operator fun GrpcMetadata.get(key: String): String? {
100-
return map[GrpcKey.string(key).name]?.lastOrNull()?.toAsciiString()
100+
return map[GrpcMetadataKey.string(key).name]?.lastOrNull()?.toAsciiString()
101101
}
102102

103103
public actual fun GrpcMetadata.getBinary(key: String): ByteArray? {
104-
return map[GrpcKey.binary(key).name]?.lastOrNull()
104+
return map[GrpcMetadataKey.binary(key).name]?.lastOrNull()
105105
}
106106

107107
public actual fun GrpcMetadata.getAll(key: String): List<String> {
108-
return map[GrpcKey.string(key).name]?.map { it.toAsciiString() } ?: emptyList()
108+
return map[GrpcMetadataKey.string(key).name]?.map { it.toAsciiString() } ?: emptyList()
109109
}
110110

111111
public actual fun GrpcMetadata.getAllBinary(key: String): List<ByteArray> {
112-
return map[GrpcKey.binary(key).name]?.map { it } ?: emptyList()
112+
return map[GrpcMetadataKey.binary(key).name]?.map { it } ?: emptyList()
113113
}
114114

115115
public actual operator fun GrpcMetadata.contains(key: String): Boolean {
@@ -121,35 +121,35 @@ public actual fun GrpcMetadata.keys(): Set<String> {
121121
}
122122

123123
public actual fun GrpcMetadata.append(key: String, value: String) {
124-
val k = GrpcKey.string(key) // non-bin key
124+
val k = GrpcMetadataKey.string(key) // non-bin key
125125
map.getOrPut(k.name) { mutableListOf() }.add(value.toAsciiBytes())
126126
}
127127

128128
public actual fun GrpcMetadata.appendBinary(key: String, value: ByteArray) {
129-
val k = GrpcKey.binary(key)
129+
val k = GrpcMetadataKey.binary(key)
130130
map.getOrPut(k.name) { mutableListOf() }.add(value)
131131
}
132132

133133
public actual fun GrpcMetadata.remove(key: String, value: String): Boolean {
134134
val index = getAll(key).indexOf(value)
135135
if (index == -1) return false
136-
map[GrpcKey.string(key).name]!!.removeAt(index)
136+
map[GrpcMetadataKey.string(key).name]!!.removeAt(index)
137137
return true
138138
}
139139

140140
public actual fun GrpcMetadata.removeBinary(key: String, value: ByteArray): Boolean {
141141
val index = getAllBinary(key).indexOf(value)
142142
if (index == -1) return false
143-
map[GrpcKey.binary(key).name]!!.removeAt(index)
143+
map[GrpcMetadataKey.binary(key).name]!!.removeAt(index)
144144
return true
145145
}
146146

147147
public actual fun GrpcMetadata.removeAll(key: String): List<String> {
148-
return map.remove(GrpcKey.string(key).name)?.map { it.toAsciiString() } ?: emptyList()
148+
return map.remove(GrpcMetadataKey.string(key).name)?.map { it.toAsciiString() } ?: emptyList()
149149
}
150150

151151
public actual fun GrpcMetadata.removeAllBinary(key: String): List<ByteArray> {
152-
return map.remove(GrpcKey.binary(key).name) ?: emptyList()
152+
return map.remove(GrpcMetadataKey.binary(key).name) ?: emptyList()
153153
}
154154

155155
public actual fun GrpcMetadata.merge(other: GrpcMetadata) {
@@ -201,7 +201,7 @@ private val VALID_KEY_CHARS by lazy {
201201
}
202202

203203
@OptIn(ObsoleteNativeApi::class)
204-
private fun GrpcKey.Companion.validateName(name: String): String {
204+
private fun GrpcMetadataKey.Companion.validateName(name: String): String {
205205
require(!name.startsWith("grpc-")) { "Header is named $name. It must not start with 'grpc-' as it is reserved for internal use." }
206206

207207
for (char in name) {

0 commit comments

Comments
 (0)