@zio.dev/zio-blocks 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.md +426 -0
- package/package.json +6 -0
- package/path-interpolator.md +645 -0
- package/reference/binding.md +364 -0
- package/reference/chunk.md +576 -0
- package/reference/context.md +157 -0
- package/reference/docs.md +524 -0
- package/reference/dynamic-value.md +823 -0
- package/reference/formats.md +640 -0
- package/reference/json-schema.md +626 -0
- package/reference/json.md +979 -0
- package/reference/modifier.md +276 -0
- package/reference/optics.md +1613 -0
- package/reference/patch.md +631 -0
- package/reference/reflect-transform.md +387 -0
- package/reference/reflect.md +521 -0
- package/reference/registers.md +282 -0
- package/reference/schema-evolution.md +540 -0
- package/reference/schema.md +619 -0
- package/reference/syntax.md +409 -0
- package/reference/type-class-derivation-internals.md +632 -0
- package/reference/typeid.md +900 -0
- package/reference/validation.md +458 -0
- package/scope.md +627 -0
- package/sidebars.js +30 -0
|
@@ -0,0 +1,276 @@
|
|
|
1
|
+
---
|
|
2
|
+
id: modifier
|
|
3
|
+
title: "Modifier"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
`Modifier` is a sealed trait that provides a mechanism to attach metadata and configuration to schema elements. Modifiers serve as annotations for record fields, variant cases, and reflect values, enabling format-specific customization without polluting domain types.
|
|
7
|
+
|
|
8
|
+
Unlike Scala annotations, modifiers are **pure data** values that can be serialized, making them ideal for runtime introspection and cross-process schema exchange.
|
|
9
|
+
|
|
10
|
+
```scala
|
|
11
|
+
sealed trait Modifier extends StaticAnnotation
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Modifier Hierarchy
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
┌────────────────────────────────────────────────────────────────┐
|
|
18
|
+
│ Modifier │
|
|
19
|
+
├────────────────────────────────────────────────────────────────┤
|
|
20
|
+
│ ┌─────────────────────────────────────────────────────────┐ │
|
|
21
|
+
│ │ Modifier.Term │ │
|
|
22
|
+
│ │ (annotates record fields and variant cases) │ │
|
|
23
|
+
│ │ │ │
|
|
24
|
+
│ │ - transient() : exclude from serialization │ │
|
|
25
|
+
│ │ - rename(name) : change serialized name │ │
|
|
26
|
+
│ │ - alias(name) : add alternative name │ │
|
|
27
|
+
│ │ - config(key, val) : attach key-value metadata │ │
|
|
28
|
+
│ └─────────────────────────────────────────────────────────┘ │
|
|
29
|
+
│ ┌─────────────────────────────────────────────────────────┐ │
|
|
30
|
+
│ │ Modifier.Reflect │ │
|
|
31
|
+
│ │ (annotates reflect values / types) │ │
|
|
32
|
+
│ │ │ │
|
|
33
|
+
│ │ - config(key, val) : attach key-value metadata │ │
|
|
34
|
+
│ └─────────────────────────────────────────────────────────┘ │
|
|
35
|
+
└────────────────────────────────────────────────────────────────┘
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## Term Modifiers
|
|
39
|
+
|
|
40
|
+
Term modifiers annotate record fields and variant cases. They are used during schema derivation to customize how fields are serialized and deserialized.
|
|
41
|
+
|
|
42
|
+
### transient
|
|
43
|
+
|
|
44
|
+
The `transient` modifier marks a field as transient, meaning it will be excluded from serialization. This is useful for computed fields, caches, or sensitive data that shouldn't be persisted.
|
|
45
|
+
|
|
46
|
+
```scala mdoc:compile-only
|
|
47
|
+
import zio.blocks.schema._
|
|
48
|
+
|
|
49
|
+
case class User(
|
|
50
|
+
id: Long,
|
|
51
|
+
name: String,
|
|
52
|
+
@Modifier.transient cache: Map[String, String] = Map.empty
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
object User {
|
|
56
|
+
implicit val schema: Schema[User] = Schema.derived
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
When encoding a `User` to JSON, the `cache` field will be omitted:
|
|
61
|
+
|
|
62
|
+
```json
|
|
63
|
+
{"id": 1, "name": "Alice"}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
During decoding, transient fields use their default values. Therefore, **transient fields must have default values** defined in the case class.
|
|
67
|
+
|
|
68
|
+
### rename
|
|
69
|
+
|
|
70
|
+
The `rename` modifier changes the serialized name of a field or variant case. This is useful when the field name in your Scala code differs from the expected name in the serialized format.
|
|
71
|
+
|
|
72
|
+
```scala mdoc:compile-only
|
|
73
|
+
import zio.blocks.schema._
|
|
74
|
+
|
|
75
|
+
case class Person(
|
|
76
|
+
@Modifier.rename("user_name") name: String,
|
|
77
|
+
@Modifier.rename("user_age") age: Int
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
object Person {
|
|
81
|
+
implicit val schema: Schema[Person] = Schema.derived
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
When encoding a `Person` to JSON:
|
|
86
|
+
|
|
87
|
+
```json
|
|
88
|
+
{"user_name": "Alice", "user_age": 30}
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
You can also use `rename` on variant cases to customize the discriminator value:
|
|
92
|
+
|
|
93
|
+
```scala mdoc:compile-only
|
|
94
|
+
import zio.blocks.schema._
|
|
95
|
+
|
|
96
|
+
sealed trait PaymentMethod
|
|
97
|
+
|
|
98
|
+
object PaymentMethod {
|
|
99
|
+
@Modifier.rename("credit_card")
|
|
100
|
+
case class CreditCard(number: String, cvv: String) extends PaymentMethod
|
|
101
|
+
|
|
102
|
+
@Modifier.rename("bank_transfer")
|
|
103
|
+
case class BankTransfer(iban: String) extends PaymentMethod
|
|
104
|
+
|
|
105
|
+
implicit val schema: Schema[PaymentMethod] = Schema.derived
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
### alias
|
|
110
|
+
|
|
111
|
+
The `alias` modifier provides an alternative name for a term during decoding. This is useful for supporting multiple names during schema evolution or data migration.
|
|
112
|
+
|
|
113
|
+
```scala mdoc:compile-only
|
|
114
|
+
import zio.blocks.schema._
|
|
115
|
+
|
|
116
|
+
@Modifier.rename("NewName")
|
|
117
|
+
@Modifier.alias("OldName")
|
|
118
|
+
@Modifier.alias("LegacyName")
|
|
119
|
+
case class MyClass(value: String)
|
|
120
|
+
|
|
121
|
+
object MyClass {
|
|
122
|
+
implicit val schema: Schema[MyClass] = Schema.derived
|
|
123
|
+
}
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
With this configuration:
|
|
127
|
+
- **Encoding** always uses the `rename` value: `"NewName"`
|
|
128
|
+
- **Decoding** accepts any of: `"NewName"`, `"OldName"`, or `"LegacyName"`
|
|
129
|
+
|
|
130
|
+
This pattern is particularly useful when migrating data formats without breaking compatibility with existing data.
|
|
131
|
+
|
|
132
|
+
### config
|
|
133
|
+
|
|
134
|
+
The `config` modifier attaches arbitrary key-value metadata to a term. The convention for keys is `<format>.<property>`, allowing format-specific configuration.
|
|
135
|
+
|
|
136
|
+
```scala mdoc:compile-only
|
|
137
|
+
import zio.blocks.schema._
|
|
138
|
+
|
|
139
|
+
case class Event(
|
|
140
|
+
@Modifier.config("protobuf.field-id", "1") id: Long,
|
|
141
|
+
@Modifier.config("protobuf.field-id", "2") name: String
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
object Event {
|
|
145
|
+
implicit val schema: Schema[Event] = Schema.derived
|
|
146
|
+
}
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
The `config` modifier extends both `Term` and `Reflect`, making it usable on both fields and types.
|
|
150
|
+
|
|
151
|
+
## Reflect Modifiers
|
|
152
|
+
|
|
153
|
+
Reflect modifiers annotate reflect values (types themselves). Currently, only `config` is a reflect modifier.
|
|
154
|
+
|
|
155
|
+
### Using config on Types
|
|
156
|
+
|
|
157
|
+
You can attach configuration to the type itself using the `Schema#modifier` method:
|
|
158
|
+
|
|
159
|
+
```scala mdoc:compile-only
|
|
160
|
+
import zio.blocks.schema._
|
|
161
|
+
|
|
162
|
+
case class Person(name: String, age: Int)
|
|
163
|
+
|
|
164
|
+
object Person {
|
|
165
|
+
implicit val schema: Schema[Person] = Schema.derived
|
|
166
|
+
.modifier(Modifier.config("db.table-name", "person_table"))
|
|
167
|
+
.modifier(Modifier.config("schema.version", "v2"))
|
|
168
|
+
}
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Or add multiple modifiers at once:
|
|
172
|
+
|
|
173
|
+
```scala mdoc:compile-only
|
|
174
|
+
import zio.blocks.schema._
|
|
175
|
+
|
|
176
|
+
case class Person(name: String, age: Int)
|
|
177
|
+
|
|
178
|
+
object Person {
|
|
179
|
+
implicit val schema: Schema[Person] = Schema.derived
|
|
180
|
+
.modifiers(
|
|
181
|
+
Seq(
|
|
182
|
+
Modifier.config("db.table-name", "person_table"),
|
|
183
|
+
Modifier.config("schema.version", "v2")
|
|
184
|
+
)
|
|
185
|
+
)
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## Programmatic Modifier Access
|
|
190
|
+
|
|
191
|
+
You can access modifiers programmatically through the `Reflect` structure:
|
|
192
|
+
|
|
193
|
+
```scala mdoc:compile-only
|
|
194
|
+
import zio.blocks.schema._
|
|
195
|
+
|
|
196
|
+
case class Person(
|
|
197
|
+
@Modifier.rename("full_name") name: String,
|
|
198
|
+
@Modifier.transient cache: String = ""
|
|
199
|
+
)
|
|
200
|
+
|
|
201
|
+
object Person {
|
|
202
|
+
implicit val schema: Schema[Person] = Schema.derived
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Access field modifiers through the reflect
|
|
206
|
+
val reflect = Schema[Person].reflect
|
|
207
|
+
reflect match {
|
|
208
|
+
case record: Reflect.Record[_, _] =>
|
|
209
|
+
record.fields.foreach { field =>
|
|
210
|
+
println(s"Field: ${field.name}")
|
|
211
|
+
println(s"Modifiers: ${field.modifiers}")
|
|
212
|
+
}
|
|
213
|
+
case _ => ()
|
|
214
|
+
}
|
|
215
|
+
```
|
|
216
|
+
|
|
217
|
+
## Built-in Schema Support
|
|
218
|
+
|
|
219
|
+
All modifier types have built-in `Schema` instances, enabling them to be serialized and deserialized:
|
|
220
|
+
|
|
221
|
+
```scala mdoc:compile-only
|
|
222
|
+
import zio.blocks.schema._
|
|
223
|
+
|
|
224
|
+
// Schema instances for individual modifiers
|
|
225
|
+
Schema[Modifier.transient]
|
|
226
|
+
Schema[Modifier.rename]
|
|
227
|
+
Schema[Modifier.alias]
|
|
228
|
+
Schema[Modifier.config]
|
|
229
|
+
|
|
230
|
+
// Schema instances for modifier traits
|
|
231
|
+
Schema[Modifier.Term]
|
|
232
|
+
Schema[Modifier.Reflect]
|
|
233
|
+
Schema[Modifier]
|
|
234
|
+
```
|
|
235
|
+
|
|
236
|
+
This enables scenarios like:
|
|
237
|
+
- Serializing schema metadata across process boundaries
|
|
238
|
+
- Storing schema configuration in databases
|
|
239
|
+
- Building schema registries with full modifier information
|
|
240
|
+
|
|
241
|
+
## Format Support
|
|
242
|
+
|
|
243
|
+
Different serialization formats interpret modifiers according to their semantics:
|
|
244
|
+
|
|
245
|
+
| Modifier | JSON | BSON | Avro | Protobuf |
|
|
246
|
+
|-------------|------|------|------|----------|
|
|
247
|
+
| `transient` | Field omitted | Field omitted | Field omitted | Field omitted |
|
|
248
|
+
| `rename` | Custom field name | Custom field name | Custom field name | Custom field name |
|
|
249
|
+
| `alias` | Accepts alternatives | Accepts alternatives | - | - |
|
|
250
|
+
| `config` | Format-specific | Format-specific | Format-specific | Format-specific |
|
|
251
|
+
|
|
252
|
+
## Best Practices
|
|
253
|
+
|
|
254
|
+
1. **Use `rename` for external APIs**: When integrating with external systems that use different naming conventions (snake_case vs camelCase), use `rename` to match the expected format.
|
|
255
|
+
|
|
256
|
+
2. **Use `alias` for migrations**: When evolving your data model, add `alias` modifiers to support reading old data while writing with new names.
|
|
257
|
+
|
|
258
|
+
3. **Use `transient` sparingly**: Only mark fields as transient when they are truly derived or temporary. Remember that transient fields need default values.
|
|
259
|
+
|
|
260
|
+
4. **Use namespaced keys for `config`**: Follow the `<format>.<property>` convention to avoid conflicts between different formats or tools.
|
|
261
|
+
|
|
262
|
+
5. **Combine modifiers**: You can apply multiple modifiers to the same field:
|
|
263
|
+
|
|
264
|
+
```scala mdoc:compile-only
|
|
265
|
+
import zio.blocks.schema._
|
|
266
|
+
|
|
267
|
+
case class Document(
|
|
268
|
+
@Modifier.rename("doc_id")
|
|
269
|
+
@Modifier.config("protobuf.field-id", "1")
|
|
270
|
+
id: String
|
|
271
|
+
)
|
|
272
|
+
|
|
273
|
+
object Document {
|
|
274
|
+
implicit val schema: Schema[Document] = Schema.derived
|
|
275
|
+
}
|
|
276
|
+
```
|