0
# Field Annotations
1
2
Annotations for marking generated message fields with encoding metadata and serialization behavior. These annotations control how Wire generates code and how fields are handled during encoding and decoding.
3
4
## Capabilities
5
6
### WireField Annotation
7
8
Primary annotation for protocol buffer field metadata used by Wire's code generator.
9
10
```kotlin { .api }
11
/**
12
* Annotates generated Message fields with metadata for serialization and deserialization
13
*/
14
@Target(AnnotationTarget.FIELD)
15
@Retention(AnnotationRetention.RUNTIME)
16
annotation class WireField(
17
/** The tag number used to store the field's value */
18
val tag: Int,
19
20
/** Reference to ProtoAdapter for keys (maps only) */
21
val keyAdapter: String = "",
22
23
/** Reference to ProtoAdapter for values */
24
val adapter: String,
25
26
/** Field label (OPTIONAL, REQUIRED, REPEATED, etc.) */
27
val label: Label = Label.OPTIONAL,
28
29
/** Redacted fields omitted from toString() */
30
val redacted: Boolean = false,
31
32
/** Original proto field name if different from generated */
33
val declaredName: String = "",
34
35
/** JSON field name if different from proto name */
36
val jsonName: String = "",
37
38
/** Oneof group name if field is part of oneof */
39
val oneofName: String = "",
40
41
/** Declaration order in proto schema */
42
val schemaIndex: Int = -1
43
)
44
```
45
46
### Field Labels
47
48
Enum defining the protocol buffer field behavior and encoding rules.
49
50
```kotlin { .api }
51
enum class Label {
52
/** Required field (proto2 only) */
53
REQUIRED,
54
55
/** Optional field (default) */
56
OPTIONAL,
57
58
/** Repeated field */
59
REPEATED,
60
61
/** Oneof field */
62
ONE_OF,
63
64
/** Packed repeated field (more efficient for primitives) */
65
PACKED,
66
67
/** Proto3 field omitted if identity value */
68
OMIT_IDENTITY;
69
70
/** True if field is repeated */
71
val isRepeated: Boolean
72
73
/** True if field uses packed encoding */
74
val isPacked: Boolean
75
76
/** True if field is part of oneof */
77
val isOneOf: Boolean
78
}
79
```
80
81
**Usage Examples:**
82
83
```kotlin
84
// Generated message class with field annotations
85
class Person(
86
@field:WireField(
87
tag = 1,
88
adapter = "com.squareup.wire.ProtoAdapter#STRING"
89
)
90
val name: String,
91
92
@field:WireField(
93
tag = 2,
94
adapter = "com.squareup.wire.ProtoAdapter#INT32",
95
label = WireField.Label.OPTIONAL
96
)
97
val age: Int,
98
99
@field:WireField(
100
tag = 3,
101
adapter = "com.squareup.wire.ProtoAdapter#STRING",
102
label = WireField.Label.REPEATED
103
)
104
val emails: List<String>,
105
106
@field:WireField(
107
tag = 4,
108
adapter = "com.example.AddressAdapter#ADAPTER"
109
)
110
val address: Address?,
111
112
unknownFields: ByteString = ByteString.EMPTY
113
) : Message<Person, Person.Builder>(ADAPTER, unknownFields)
114
```
115
116
### Other Annotations
117
118
Additional annotations for specialized use cases.
119
120
```kotlin { .api }
121
/** Annotation for enum constants */
122
@Target(AnnotationTarget.FIELD)
123
@Retention(AnnotationRetention.RUNTIME)
124
annotation class WireEnumConstant(
125
val declaredName: String = ""
126
)
127
128
/** Annotation for RPC methods */
129
@Target(AnnotationTarget.FUNCTION)
130
@Retention(AnnotationRetention.RUNTIME)
131
annotation class WireRpc(
132
val path: String,
133
val requestAdapter: String,
134
val responseAdapter: String
135
)
136
```