Commit e104cb7
committed
feat: Implement automatic nested struct loading
Implements automatic loading for nested configuration structs marked with
#[gonfig(nested)] attribute. Nested structs now load automatically by calling
their own from_gonfig() method, enabling clean hierarchical configuration.
How it works:
- When from_gonfig() is called on a parent struct, it automatically loads
all nested fields by calling their from_gonfig() methods
- Each nested struct uses its own env_prefix for loading
- Regular fields load from parent's prefix
- Nested fields load from their own prefix
Requirements:
- Nested field types must derive Gonfig
- Nested field types must implement Default
- Parent struct must mark nested fields with #[serde(default)]
Example:
#[derive(Gonfig, Default)]
#[Gonfig(env_prefix = "APP")]
#[serde(default)]
struct Config {
#[gonfig(nested)]
#[serde(default)]
server: ServerConfig,
}
#[derive(Gonfig, Default)]
#[Gonfig(env_prefix = "SERVER")]
#[serde(default)]
struct ServerConfig {
host: String,
}
// Automatic loading - just works!
let config = Config::from_gonfig()?;
Environment variable mapping:
- APP_ENVIRONMENT -> Config.environment
- SERVER_HOST -> ServerConfig.host (loaded via nested from_gonfig)
- SERVER_PORT -> ServerConfig.port
Implementation details:
- Detect nested fields during macro expansion
- Generate code to call from_gonfig() for each nested field
- Deserialize regular fields from config builder
- Replace nested field defaults with loaded values
Testing:
- Added 2 test files with 6 new tests
- Updated issue #23 test to demonstrate automatic loading
- Tests basic nesting, deep nesting (3 levels), multiple nested fields
- All existing tests pass (63 total)
Fixes #251 parent 36dc786 commit e104cb7
4 files changed
Lines changed: 311 additions & 29 deletions
File tree
- gonfig_derive/src
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
154 | 154 | | |
155 | 155 | | |
156 | 156 | | |
157 | | - | |
| 157 | + | |
158 | 158 | | |
159 | | - | |
160 | | - | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
161 | 167 | | |
162 | 168 | | |
163 | 169 | | |
164 | | - | |
165 | | - | |
166 | | - | |
167 | | - | |
168 | | - | |
169 | | - | |
| 170 | + | |
| 171 | + | |
170 | 172 | | |
171 | | - | |
| 173 | + | |
172 | 174 | | |
| 175 | + | |
173 | 176 | | |
| 177 | + | |
174 | 178 | | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
175 | 182 | | |
176 | 183 | | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
182 | 188 | | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
183 | 204 | | |
184 | 205 | | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
185 | 211 | | |
186 | 212 | | |
187 | 213 | | |
| |||
286 | 312 | | |
287 | 313 | | |
288 | 314 | | |
289 | | - | |
| 315 | + | |
290 | 316 | | |
291 | 317 | | |
| 318 | + | |
| 319 | + | |
292 | 320 | | |
293 | 321 | | |
294 | 322 | | |
295 | 323 | | |
| 324 | + | |
296 | 325 | | |
297 | | - | |
298 | | - | |
| 326 | + | |
299 | 327 | | |
| 328 | + | |
| 329 | + | |
300 | 330 | | |
301 | 331 | | |
302 | 332 | | |
| 333 | + | |
| 334 | + | |
303 | 335 | | |
304 | 336 | | |
305 | 337 | | |
| |||
335 | 367 | | |
336 | 368 | | |
337 | 369 | | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
338 | 375 | | |
339 | 376 | | |
340 | 377 | | |
| |||
411 | 448 | | |
412 | 449 | | |
413 | 450 | | |
414 | | - | |
415 | | - | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
416 | 481 | | |
417 | 482 | | |
418 | 483 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
20 | | - | |
| 21 | + | |
21 | 22 | | |
| 23 | + | |
22 | 24 | | |
23 | 25 | | |
24 | 26 | | |
| |||
51 | 53 | | |
52 | 54 | | |
53 | 55 | | |
54 | | - | |
55 | | - | |
56 | | - | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
57 | 66 | | |
58 | | - | |
59 | | - | |
60 | | - | |
61 | | - | |
62 | | - | |
| 67 | + | |
63 | 68 | | |
| 69 | + | |
64 | 70 | | |
| 71 | + | |
65 | 72 | | |
66 | 73 | | |
67 | 74 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
0 commit comments