|
| 1 | +# Types and Values |
| 2 | + |
| 3 | +How OpenDecree's schema field types map to Python types, and how to read and write |
| 4 | +typed values — including atomic multi-field writes with `set_many`. |
| 5 | + |
| 6 | +## Go-to-Python type mapping |
| 7 | + |
| 8 | +The server stores every config value as a string and validates it against a |
| 9 | +schema-defined field type. The SDK converts between that wire representation and |
| 10 | +native Python types at the boundary — both when reading with a typed `get()` and |
| 11 | +(for `watch()`) when registering a field. |
| 12 | + |
| 13 | +| Schema field type | Python type | Notes | |
| 14 | +|-------------------|-------------|-------| |
| 15 | +| `integer` | `int` | Decimal string, e.g. `"42"`, `"-1"` | |
| 16 | +| `number` | `float` | Decimal string, e.g. `"3.14"`, `"-99.9"` | |
| 17 | +| `string` | `str` | Free-form text | |
| 18 | +| `bool` | `bool` | `"true"`/`"1"` → `True`, `"false"`/`"0"` → `False` | |
| 19 | +| `time` | `datetime` | RFC 3339 string, parsed with `datetime.fromisoformat` | |
| 20 | +| `duration` | `timedelta` | Go-style duration string, e.g. `"24h"`, `"30m"`, `"500ms"`, `"1h30m"` | |
| 21 | +| `url` | `str` (`opendecree.URL`) | `URL` is a type alias for `str` — semantically distinct, converted identically | |
| 22 | +| `json` | `dict` / `list` | JSON-decoded; the result must match the requested container type | |
| 23 | + |
| 24 | +`opendecree.URL` exists so you can express intent in your own type annotations |
| 25 | +(e.g. `watcher.field("webhooks.endpoint", URL)`) — at runtime it behaves exactly |
| 26 | +like `str`. |
| 27 | + |
| 28 | +## Supported `get()` types |
| 29 | + |
| 30 | +Pass the target type as the third positional argument to `get()` to receive a |
| 31 | +converted value instead of the raw string: |
| 32 | + |
| 33 | +```python |
| 34 | +from datetime import datetime, timedelta |
| 35 | + |
| 36 | +from opendecree import URL, ConfigClient |
| 37 | + |
| 38 | +with ConfigClient("localhost:9090", subject="myapp") as client: |
| 39 | + name: str = client.get("tenant-id", "service.name") |
| 40 | + retries: int = client.get("tenant-id", "payments.retries", int) |
| 41 | + fee: float = client.get("tenant-id", "payments.fee", float) |
| 42 | + enabled: bool = client.get("tenant-id", "payments.enabled", bool) |
| 43 | + deploy_at: datetime = client.get("tenant-id", "release.deploy_at", datetime) |
| 44 | + timeout: timedelta = client.get("tenant-id", "payments.timeout", timedelta) |
| 45 | + webhook: URL = client.get("tenant-id", "webhooks.endpoint", URL) |
| 46 | + metadata: dict = client.get("tenant-id", "service.metadata", dict) |
| 47 | + tags: list = client.get("tenant-id", "service.tags", list) |
| 48 | +``` |
| 49 | + |
| 50 | +Supported types: `str` (default), `int`, `float`, `bool`, `datetime`, `timedelta`, |
| 51 | +`dict`, `list`. `URL` is an alias for `str`. |
| 52 | + |
| 53 | +For `dict`/`list`, the SDK JSON-decodes the raw value and checks that the decoded |
| 54 | +result is an instance of the requested container type — decoding `{"a": 1}` as |
| 55 | +`list` (or `[1, 2]` as `dict`) raises `TypeMismatchError`. |
| 56 | + |
| 57 | +```python |
| 58 | +try: |
| 59 | + metadata = client.get("tenant-id", "service.metadata", dict) |
| 60 | +except TypeMismatchError: |
| 61 | + print("value is not a JSON object") |
| 62 | +``` |
| 63 | + |
| 64 | +`AsyncConfigClient.get()` supports the same set of types — `await` the call instead. |
| 65 | + |
| 66 | +### Nullable reads |
| 67 | + |
| 68 | +Pass `nullable=True` to get `None` back for unset/null fields instead of raising |
| 69 | +`NotFoundError`: |
| 70 | + |
| 71 | +```python |
| 72 | +description = client.get("tenant-id", "service.description", str, nullable=True) |
| 73 | +if description is None: |
| 74 | + print("not set") |
| 75 | +``` |
| 76 | + |
| 77 | +## Writing values |
| 78 | + |
| 79 | +`set`, `set_many`, and `set_null` all send the `value` argument as a **string** |
| 80 | +on the wire — there's no separate typed-write API. The server validates the |
| 81 | +written value against the field's declared schema type. |
| 82 | + |
| 83 | +!!! warning "Currently string-typed fields only" |
| 84 | + The SDK always sends writes as a string-valued `TypedValue`. The server |
| 85 | + requires the value's wire representation to match the field's declared type |
| 86 | + exactly (no coercion), so `set`/`set_many`/`set_null` only work against |
| 87 | + `string`-typed fields today — writing to a `bool`, `integer`, `number`, |
| 88 | + `time`, `duration`, `url`, or `json` field raises `InvalidArgumentError` |
| 89 | + (e.g. `"expected bool value"`). This is a known SDK limitation, not |
| 90 | + something to work around in your own code. |
| 91 | + |
| 92 | +```python |
| 93 | +client.set("tenant-id", "service.name", "checkout-api") |
| 94 | +client.set("tenant-id", "payments.currency", "EUR") |
| 95 | +``` |
| 96 | + |
| 97 | +## Bulk writes with `set_many` and `FieldUpdate` |
| 98 | + |
| 99 | +`set_many` atomically applies a batch of field updates in a single version — either |
| 100 | +all of them succeed, or none do. Each update is a `FieldUpdate`: |
| 101 | + |
| 102 | +```python |
| 103 | +@dataclass(frozen=True, slots=True) |
| 104 | +class FieldUpdate: |
| 105 | + field_path: str |
| 106 | + value: str |
| 107 | + expected_checksum: str | None = None |
| 108 | + value_description: str | None = None |
| 109 | +``` |
| 110 | + |
| 111 | +| Field | Type | Meaning | |
| 112 | +|-------|------|---------| |
| 113 | +| `field_path` | `str` | Dot-separated field path, e.g. `"payments.fee"` | |
| 114 | +| `value` | `str` | The value as a string (same wire format as `set`) | |
| 115 | +| `expected_checksum` | `str \| None` | Optional per-field optimistic-concurrency check — the whole batch is rejected with `ChecksumMismatchError` if any field's current checksum doesn't match | |
| 116 | +| `value_description` | `str \| None` | Optional description stored with this specific value | |
| 117 | + |
| 118 | +```python |
| 119 | +from opendecree import ConfigClient, FieldUpdate |
| 120 | + |
| 121 | +with ConfigClient("localhost:9090", subject="myapp") as client: |
| 122 | + client.set_many( |
| 123 | + "tenant-id", |
| 124 | + [ |
| 125 | + FieldUpdate("service.name", "checkout-api"), |
| 126 | + FieldUpdate("payments.currency", "EUR"), |
| 127 | + FieldUpdate( |
| 128 | + "service.region", |
| 129 | + "us-east-1", |
| 130 | + expected_checksum="abc123", |
| 131 | + value_description="moved for latency", |
| 132 | + ), |
| 133 | + ], |
| 134 | + description="tune service settings", |
| 135 | + ) |
| 136 | +``` |
| 137 | + |
| 138 | +Like `set`, `set_many` is currently limited to `string`-typed fields — see the |
| 139 | +warning above. |
| 140 | + |
| 141 | +`set_many` accepts the same `description` and `idempotency_key` keyword arguments |
| 142 | +as `set` (see [Connecting → Retry](connect.md#retry) for retry/idempotency |
| 143 | +semantics — bulk writes are subject to the same write-safety rules). |
| 144 | + |
| 145 | +`AsyncConfigClient.set_many` works identically with `await`. |
| 146 | + |
| 147 | +## `watch()` field types |
| 148 | + |
| 149 | +`ConfigWatcher.field()` and `AsyncConfigWatcher.field()` support a smaller set of |
| 150 | +types than `get()` — see [Watching → Supported field types](watch.md#supported-field-types) |
| 151 | +for the list and suggested defaults. |
0 commit comments