0
# Any Message Operations
1
2
Type-safe operations for protocol buffer Any messages, allowing runtime type checking and unpacking with compile-time type safety through reified generics.
3
4
## Capabilities
5
6
### Type Checking with isA
7
8
Checks if a protocol buffer Any message contains a message of a specific type.
9
10
```kotlin { .api }
11
/**
12
* Returns true if this Any contains a message of type T
13
* @param T the message type to check for (reified type parameter)
14
* @return true if this Any contains a message of type T, false otherwise
15
*/
16
inline fun <reified T : Message> ProtoAny.isA(): Boolean
17
```
18
19
**Usage Example:**
20
21
```kotlin
22
import com.google.protobuf.kotlin.isA
23
import com.google.protobuf.Any as ProtoAny
24
// Assuming you have generated message types like UserMessage, ProductMessage
25
26
val anyMessage: ProtoAny = // ... received from somewhere
27
28
// Type-safe checking
29
if (anyMessage.isA<UserMessage>()) {
30
println("This Any contains a UserMessage")
31
} else if (anyMessage.isA<ProductMessage>()) {
32
println("This Any contains a ProductMessage")
33
} else {
34
println("Unknown message type")
35
}
36
```
37
38
### Message Unpacking with unpack
39
40
Extracts and returns the message of a specific type from a protocol buffer Any.
41
42
```kotlin { .api }
43
/**
44
* Returns the message of type T encoded in this Any
45
* @param T the message type to unpack (reified type parameter)
46
* @return the unpacked message of type T
47
* @throws InvalidProtocolBufferException if this Any does not contain a T message
48
*/
49
inline fun <reified T : Message> ProtoAny.unpack(): T
50
```
51
52
**Usage Example:**
53
54
```kotlin
55
import com.google.protobuf.kotlin.unpack
56
import com.google.protobuf.kotlin.isA
57
import com.google.protobuf.Any as ProtoAny
58
import com.google.protobuf.InvalidProtocolBufferException
59
60
val anyMessage: ProtoAny = // ... received from somewhere
61
62
try {
63
// Safe unpacking with type checking
64
if (anyMessage.isA<UserMessage>()) {
65
val userMessage = anyMessage.unpack<UserMessage>()
66
println("User name: ${userMessage.name}")
67
println("User email: ${userMessage.email}")
68
}
69
} catch (e: InvalidProtocolBufferException) {
70
println("Failed to unpack message: ${e.message}")
71
}
72
```
73
74
## Complete Usage Pattern
75
76
### Safe Any Message Processing
77
78
```kotlin
79
import com.google.protobuf.kotlin.*
80
import com.google.protobuf.Any as ProtoAny
81
import com.google.protobuf.InvalidProtocolBufferException
82
83
fun processAnyMessage(anyMessage: ProtoAny) {
84
when {
85
anyMessage.isA<UserMessage>() -> {
86
val user = anyMessage.unpack<UserMessage>()
87
handleUser(user)
88
}
89
anyMessage.isA<ProductMessage>() -> {
90
val product = anyMessage.unpack<ProductMessage>()
91
handleProduct(product)
92
}
93
anyMessage.isA<OrderMessage>() -> {
94
val order = anyMessage.unpack<OrderMessage>()
95
handleOrder(order)
96
}
97
else -> {
98
println("Unknown message type: ${anyMessage.typeUrl}")
99
}
100
}
101
}
102
103
fun handleUser(user: UserMessage) {
104
println("Processing user: ${user.name}")
105
}
106
107
fun handleProduct(product: ProductMessage) {
108
println("Processing product: ${product.name}")
109
}
110
111
fun handleOrder(order: OrderMessage) {
112
println("Processing order: ${order.id}")
113
}
114
```
115
116
### Error Handling
117
118
```kotlin
119
import com.google.protobuf.kotlin.*
120
import com.google.protobuf.Any as ProtoAny
121
import com.google.protobuf.InvalidProtocolBufferException
122
123
fun safeUnpack(anyMessage: ProtoAny): UserMessage? {
124
return try {
125
if (anyMessage.isA<UserMessage>()) {
126
anyMessage.unpack<UserMessage>()
127
} else {
128
null
129
}
130
} catch (e: InvalidProtocolBufferException) {
131
println("Error unpacking UserMessage: ${e.message}")
132
null
133
}
134
}
135
136
// Usage
137
val anyMessage: ProtoAny = // ... some Any message
138
val userMessage = safeUnpack(anyMessage)
139
userMessage?.let { user ->
140
println("Successfully unpacked user: ${user.name}")
141
}
142
```
143
144
### Working with Collections of Any Messages
145
146
```kotlin
147
import com.google.protobuf.kotlin.*
148
import com.google.protobuf.Any as ProtoAny
149
150
fun processAnyMessages(messages: List<ProtoAny>) {
151
val users = messages
152
.filter { it.isA<UserMessage>() }
153
.map { it.unpack<UserMessage>() }
154
155
val products = messages
156
.filter { it.isA<ProductMessage>() }
157
.map { it.unpack<ProductMessage>() }
158
159
println("Found ${users.size} users and ${products.size} products")
160
161
users.forEach { user ->
162
println("User: ${user.name}")
163
}
164
165
products.forEach { product ->
166
println("Product: ${product.name}")
167
}
168
}
169
```
170
171
## Types
172
173
```kotlin { .api }
174
// Imported from Java protobuf library
175
import com.google.protobuf.Any as ProtoAny
176
import com.google.protobuf.Message
177
import com.google.protobuf.InvalidProtocolBufferException
178
```
179
180
## Notes
181
182
- Both `isA` and `unpack` use reified type parameters, allowing for type-safe operations at runtime
183
- These functions work with any protocol buffer message type that extends `Message`
184
- Always check with `isA` before calling `unpack` to avoid `InvalidProtocolBufferException`
185
- The `ProtoAny` type is aliased from `com.google.protobuf.Any` to avoid confusion with Kotlin's built-in `Any` type
186
- These operations are commonly used in systems that need to handle multiple message types in a type-safe manner