-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenv_struct.zig
More file actions
729 lines (624 loc) · 25 KB
/
env_struct.zig
File metadata and controls
729 lines (624 loc) · 25 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
//! Parse environment variables directly into typed Zig structs.
//!
//! Provides automatic type conversion and validation for strings, integers,
//! floats, booleans, enums, and nested structs with support for optional fields,
//! custom mapping, default values, and flexible validation patterns.
//!
//! Author: @xarunoba
//! Repository: https://github.com/xarunoba/env-struct.zig
//! License: MIT
const std = @import("std");
//==============================================================================
// Public API
//==============================================================================
/// Load configuration from system environment variables
/// Returns a wrapper that owns the EnvMap and contains the configuration
/// All string fields are borrowed slices pointing into the EnvMap (zero allocation)
pub fn load(comptime T: type) !struct {
value: T,
env_map: std.process.EnvMap,
pub fn deinit(self: *@This()) void {
self.env_map.deinit();
}
} {
var env_map = try std.process.getEnvMap(std.heap.page_allocator);
const value = try loadCore(T, &env_map, null, false);
return .{ .value = value, .env_map = env_map };
}
/// Load configuration from custom environment map
/// All string fields are borrowed slices pointing into the env_map (zero allocation)
/// env_map must outlive the returned configuration
pub fn loadMap(comptime T: type, env_map: std.process.EnvMap) !T {
return loadCore(T, &env_map, null);
}
/// Parse raw environment variable value into specified type
/// Useful for custom parsers that want to preserve default parsing with additional validation
/// For string types, returns borrowed slices (no allocation)
pub fn parseValue(comptime T: type, raw_value: []const u8) !T {
// Create a temporary EnvMap for parsing (empty, only used for nested structs)
var temp_map = std.process.EnvMap.init(std.heap.page_allocator);
defer temp_map.deinit();
return parseValueInternal(T, raw_value, &temp_map, null);
}
/// Create a validator function that combines default parsing with custom validation
/// Returns a parser function that takes raw_value and uses zero-allocation parsing
pub fn validator(comptime T: type, comptime validateFn: anytype) fn ([]const u8) anyerror!T {
return struct {
fn parse(raw_value: []const u8) !T {
const parsed = try parseValue(T, raw_value);
return validateFn(parsed);
}
}.parse;
}
//==============================================================================
// Core Utilities
//==============================================================================
fn parseBool(val: []const u8) bool {
return std.ascii.eqlIgnoreCase(val, "true") or
std.ascii.eqlIgnoreCase(val, "1") or
std.ascii.eqlIgnoreCase(val, "yes");
}
fn isStringType(comptime T: type) bool {
const info = @typeInfo(T);
return switch (info) {
.pointer => |ptr| blk: {
if (ptr.size == .one) {
const child_info = @typeInfo(ptr.child);
break :blk child_info == .array and child_info.array.child == u8;
} else {
break :blk ptr.size == .slice and ptr.child == u8;
}
},
else => false,
};
}
fn FieldMetadata(comptime T: type) type {
const type_info = @typeInfo(T);
if (type_info != .@"struct") {
@compileError("FieldMetadata can only be used with struct types");
}
comptime var fields: [type_info.@"struct".fields.len]FieldInfo(T) = undefined;
inline for (type_info.@"struct".fields, 0..) |field, i| {
fields[i] = buildFieldInfo(field.name, T);
}
return struct {
const metadata = fields;
pub fn get(comptime field_name: []const u8) FieldInfo(T) {
inline for (metadata) |meta| {
if (std.mem.eql(u8, meta.name, field_name)) {
return meta;
}
}
@compileError("Field '" ++ field_name ++ "' not found in type " ++ @typeName(T));
}
};
}
fn FieldInfo(comptime _: type) type {
return struct {
name: []const u8,
env_key: ?[]const u8,
has_parser: bool,
is_optional: bool,
field_type: type,
};
}
fn buildFieldInfo(comptime field_name: []const u8, comptime T: type) FieldInfo(T) {
const type_info = @typeInfo(T);
const field = inline for (type_info.@"struct".fields) |f| {
if (std.mem.eql(u8, f.name, field_name)) break f;
} else @compileError("Field not found");
const env_key = blk: {
if (!@hasDecl(T, "env") or !@hasField(@TypeOf(T.env), field_name)) {
break :blk field_name;
}
const env_config = @field(T.env, field_name);
const ConfigType = @TypeOf(env_config);
if (comptime isStringType(ConfigType)) {
break :blk if (std.mem.eql(u8, env_config, "-")) null else env_config;
}
if (comptime @hasField(ConfigType, "key")) {
break :blk if (std.mem.eql(u8, env_config.key, "-")) null else env_config.key;
}
break :blk field_name;
};
const has_parser = blk: {
if (!@hasDecl(T, "env")) {
break :blk false;
}
const env_type = @TypeOf(T.env);
if (!@hasField(env_type, field_name)) {
break :blk false;
}
const env_config = @field(T.env, field_name);
const ConfigType = @TypeOf(env_config);
if (comptime isStringType(ConfigType)) {
break :blk false;
}
const config_type_info = @typeInfo(ConfigType);
break :blk config_type_info == .@"struct" and @hasField(ConfigType, "parser");
};
const field_type_info = @typeInfo(field.type);
const is_optional = field_type_info == .optional;
return FieldInfo(T){
.name = field_name,
.env_key = env_key,
.has_parser = has_parser,
.is_optional = is_optional,
.field_type = field.type,
};
}
fn getEnvKey(comptime field_name: []const u8, comptime T: type) ?[]const u8 {
return FieldMetadata(T).get(field_name).env_key;
}
fn hasCustomParser(comptime field_name: []const u8, comptime T: type) bool {
return FieldMetadata(T).get(field_name).has_parser;
}
fn callCustomParser(comptime ReturnType: type, comptime field_name: []const u8, comptime T: type, raw_value: []const u8, arena: ?*std.heap.ArenaAllocator) !ReturnType {
const env_config = @field(T.env, field_name);
const parser = env_config.parser;
const ParserType = @TypeOf(parser);
const parser_info = @typeInfo(ParserType);
if (parser_info != .@"fn") {
@compileError("Parser must be a function");
}
const fn_info = parser_info.@"fn";
if (fn_info.params.len == 1) {
// Parser without allocator parameter (zero-allocation)
return parser(raw_value);
} else if (fn_info.params.len == 2) {
// Parser with allocator parameter
// Note: When arena is null (e.g., loadMap()), uses page allocator which doesn't free individual allocations
// For production use with allocating parsers, prefer load() which provides automatic cleanup
const actual_allocator = if (arena) |a| a.allocator() else std.heap.page_allocator;
return parser(raw_value, actual_allocator);
} else {
@compileError("Parser must have signature: fn([]const u8) !T or fn([]const u8, std.mem.Allocator) !T");
}
}
fn hasAnyEnvVars(comptime T: type, env_map: *const std.process.EnvMap) bool {
const type_info = @typeInfo(T);
if (type_info != .@"struct") return false;
inline for (type_info.@"struct".fields) |field| {
const meta = FieldMetadata(T).get(field.name);
const field_type_info = @typeInfo(field.type);
if (meta.env_key != null and env_map.get(meta.env_key.?) != null) {
return true;
}
if (field_type_info == .@"struct" and hasAnyEnvVars(field.type, env_map)) {
return true;
}
if (field_type_info == .optional) {
const child_type = field_type_info.optional.child;
const child_type_info = @typeInfo(child_type);
if (child_type_info == .@"struct" and hasAnyEnvVars(child_type, env_map)) {
return true;
}
}
}
return false;
}
//==============================================================================
// Core
//==============================================================================
fn loadCore(comptime T: type, env_map: *const std.process.EnvMap, arena: ?*std.heap.ArenaAllocator) !T {
const type_info = @typeInfo(T);
if (type_info != .@"struct") {
@compileError("Expected struct type, got " ++ @typeName(T));
}
var result: T = undefined;
inline for (type_info.@"struct".fields) |field| {
const env_key = getEnvKey(field.name, T);
const has_parser = comptime hasCustomParser(field.name, T);
const field_type_info = @typeInfo(field.type);
const is_optional = field_type_info == .optional;
const default_value = field.defaultValue();
if (field_type_info == .@"struct") {
@field(result, field.name) = try parseValueInternal(field.type, "", env_map, arena);
} else if (is_optional) {
const child_type = field_type_info.optional.child;
const child_type_info = @typeInfo(child_type);
if (child_type_info == .@"struct") {
if (hasAnyEnvVars(child_type, env_map)) {
@field(result, field.name) = try parseValueInternal(child_type, "", env_map, arena);
} else {
@field(result, field.name) = if (default_value) |def| def else null;
}
} else if (env_key) |key| {
if (env_map.get(key)) |val| {
@field(result, field.name) = if (has_parser)
try callCustomParser(child_type, field.name, T, val, arena)
else
try parseValueInternal(child_type, val, env_map, arena);
} else {
@field(result, field.name) = if (default_value) |def| def else null;
}
} else {
@field(result, field.name) = if (default_value) |def| def else null;
}
} else if (env_key) |key| {
if (env_map.get(key)) |val| {
@field(result, field.name) = if (has_parser)
try callCustomParser(field.type, field.name, T, val, arena)
else
try parseValueInternal(field.type, val, env_map, arena);
} else if (default_value) |def| {
@field(result, field.name) = def;
} else {
return error.MissingEnvironmentVariable;
}
} else if (default_value) |def| {
@field(result, field.name) = def;
} else {
return error.MissingEnvironmentVariable;
}
}
return result;
}
fn parseValueInternal(comptime T: type, val: []const u8, env_map: *const std.process.EnvMap, arena: ?*std.heap.ArenaAllocator) !T {
const type_info = @typeInfo(T);
if (type_info == .@"struct") {
return loadCore(T, env_map, arena);
}
return switch (T) {
[]const u8 => val,
i8, i16, i32, i64, i128, isize => std.fmt.parseInt(T, val, 10),
u8, u16, u32, u64, u128, usize => std.fmt.parseInt(T, val, 10),
f16, f32, f64, f80, f128 => std.fmt.parseFloat(T, val),
bool => parseBool(val),
else => blk: {
const inner_type_info = @typeInfo(T);
if (inner_type_info == .@"enum") {
inline for (inner_type_info.@"enum".fields) |field| {
if (std.mem.eql(u8, val, field.name)) {
break :blk @enumFromInt(field.value);
}
}
return error.InvalidEnumValue;
}
@compileError("Unsupported type: " ++ @typeName(T));
},
};
}
//==============================================================================
// Test Utilities
//==============================================================================
fn validatePort(port: u32) !u32 {
if (port > 65535) return error.InvalidPort;
return port;
}
fn parseEnum(comptime E: type) fn ([]const u8) anyerror!E {
return struct {
fn parse(raw: []const u8) !E {
inline for (@typeInfo(E).@"enum".fields) |field| {
if (std.mem.eql(u8, raw, field.name)) {
return @enumFromInt(field.value);
}
}
return error.InvalidEnumValue;
}
}.parse;
}
fn parseStringArray(raw: []const u8, allocator: std.mem.Allocator) ![][]const u8 {
if (raw.len == 0) return &[_][]const u8{};
var result = std.array_list.Managed([]const u8).init(allocator);
defer result.deinit();
var iter = std.mem.splitScalar(u8, raw, ',');
while (iter.next()) |item| {
const trimmed = std.mem.trim(u8, item, " \t");
if (trimmed.len > 0) {
const owned = try allocator.dupe(u8, trimmed);
try result.append(owned);
}
}
return result.toOwnedSlice();
}
fn createTestEnvMap(allocator: std.mem.Allocator, vars: []const struct { key: []const u8, value: []const u8 }) !std.process.EnvMap {
var env_map = std.process.EnvMap.init(allocator);
for (vars) |v| {
try env_map.put(v.key, v.value);
}
return env_map;
}
//==============================================================================
// Tests
//==============================================================================
test "basic type parsing" {
const Config = struct {
name: []const u8,
port: u32,
timeout: i32,
ratio: f32,
debug: bool,
enabled: bool,
};
const allocator = std.testing.allocator;
var env_map = try createTestEnvMap(allocator, &.{
.{ .key = "name", .value = "test-app" },
.{ .key = "port", .value = "8080" },
.{ .key = "timeout", .value = "-30" },
.{ .key = "ratio", .value = "3.14" },
.{ .key = "debug", .value = "true" },
.{ .key = "enabled", .value = "1" },
});
defer env_map.deinit();
const config = try loadMap(Config, env_map);
try std.testing.expectEqualStrings("test-app", config.name);
try std.testing.expectEqual(@as(u32, 8080), config.port);
try std.testing.expectEqual(@as(i32, -30), config.timeout);
try std.testing.expectEqual(@as(f32, 3.14), config.ratio);
try std.testing.expect(config.debug);
try std.testing.expect(config.enabled);
}
test "optional and default values" {
const Config = struct {
required: []const u8,
optional_present: ?u32,
optional_missing: ?u32,
with_default: u32 = 3000,
optional_with_default: ?u32 = 100,
const env = .{
.required = "REQUIRED",
.optional_present = "OPTIONAL_PRESENT",
.optional_missing = "OPTIONAL_MISSING",
.with_default = "WITH_DEFAULT",
.optional_with_default = "OPTIONAL_WITH_DEFAULT",
};
};
const allocator = std.testing.allocator;
var env_map = try createTestEnvMap(allocator, &.{
.{ .key = "REQUIRED", .value = "test" },
.{ .key = "OPTIONAL_PRESENT", .value = "42" },
});
defer env_map.deinit();
const config = try loadMap(Config, env_map);
try std.testing.expectEqualStrings("test", config.required);
try std.testing.expectEqual(@as(u32, 42), config.optional_present.?);
try std.testing.expectEqual(@as(?u32, null), config.optional_missing);
try std.testing.expectEqual(@as(u32, 3000), config.with_default);
try std.testing.expectEqual(@as(u32, 100), config.optional_with_default.?);
}
test "field mapping and skipping" {
const Config = struct {
mapped_field: []const u8,
skipped_field: []const u8 = "default",
normal_field: u32 = 42,
const env = .{
.mapped_field = "CUSTOM_NAME",
.skipped_field = "-",
};
};
const allocator = std.testing.allocator;
var env_map = try createTestEnvMap(allocator, &.{
.{ .key = "CUSTOM_NAME", .value = "mapped_value" },
.{ .key = "normal_field", .value = "100" },
});
defer env_map.deinit();
const config = try loadMap(Config, env_map);
try std.testing.expectEqualStrings("mapped_value", config.mapped_field);
try std.testing.expectEqualStrings("default", config.skipped_field);
try std.testing.expectEqual(@as(u32, 100), config.normal_field);
}
test "nested structs" {
const DatabaseConfig = struct {
host: []const u8,
port: u32 = 5432,
const env = .{
.host = "DB_HOST",
.port = "DB_PORT",
};
};
const Config = struct {
app_name: []const u8,
database: DatabaseConfig,
optional_db: ?DatabaseConfig,
const env = .{
.app_name = "APP_NAME",
};
};
const allocator = std.testing.allocator;
var env_map = try createTestEnvMap(allocator, &.{
.{ .key = "APP_NAME", .value = "my-app" },
.{ .key = "DB_HOST", .value = "localhost" },
.{ .key = "DB_PORT", .value = "3306" },
});
defer env_map.deinit();
const config = try loadMap(Config, env_map);
try std.testing.expectEqualStrings("my-app", config.app_name);
try std.testing.expectEqualStrings("localhost", config.database.host);
try std.testing.expectEqual(@as(u32, 3306), config.database.port);
try std.testing.expectEqualStrings("localhost", config.optional_db.?.host);
}
test "custom parsers and validators" {
const LogLevel = enum { debug, info, warn, err };
const Config = struct {
port: u32,
log_level: LogLevel,
validated_port: u32,
tags: [][]const u8,
auto_port: u32,
auto_log_level: LogLevel,
const env = .{
.port = .{
.key = "PORT",
.parser = validator(u32, validatePort),
},
.log_level = .{
.key = "LOG_LEVEL",
.parser = parseEnum(LogLevel),
},
.validated_port = .{
.key = "VALIDATED_PORT",
.parser = validator(u32, validatePort),
},
.tags = .{
.key = "TAGS",
.parser = parseStringArray,
},
.auto_port = .{
.parser = validator(u32, validatePort),
},
.auto_log_level = .{
.parser = parseEnum(LogLevel),
},
};
};
const allocator = std.testing.allocator;
var env_map = try createTestEnvMap(allocator, &.{
.{ .key = "PORT", .value = "8080" },
.{ .key = "LOG_LEVEL", .value = "info" },
.{ .key = "VALIDATED_PORT", .value = "3000" },
.{ .key = "TAGS", .value = "api, web, production" },
.{ .key = "auto_port", .value = "9000" },
.{ .key = "auto_log_level", .value = "debug" },
});
defer env_map.deinit();
const config = try loadMap(Config, env_map);
// Note: config.tags are allocated from page allocator in loadMap()
// For production use with allocating parsers, prefer load() which provides automatic cleanup
try std.testing.expectEqual(@as(u32, 8080), config.port);
try std.testing.expectEqual(LogLevel.info, config.log_level);
try std.testing.expectEqual(@as(u32, 3000), config.validated_port);
try std.testing.expectEqual(@as(usize, 3), config.tags.len);
try std.testing.expectEqualStrings("api", config.tags[0]);
try std.testing.expectEqualStrings("web", config.tags[1]);
try std.testing.expectEqualStrings("production", config.tags[2]);
try std.testing.expectEqual(@as(u32, 9000), config.auto_port);
try std.testing.expectEqual(LogLevel.debug, config.auto_log_level);
// Test parseValue utility with various types
try std.testing.expectEqual(@as(u32, 42), try parseValue(u32, "42"));
try std.testing.expectEqual(@as(f32, 3.14), try parseValue(f32, "3.14"));
try std.testing.expect(try parseValue(bool, "true"));
try std.testing.expectEqualStrings("hello", try parseValue([]const u8, "hello"));
// Test validation works with automatic key inference (using simple config to avoid memory issues)
{
const SimpleConfig = struct {
port: u32,
const env = .{
.port = .{
.parser = validator(u32, validatePort),
},
};
};
var validation_env_map = try createTestEnvMap(allocator, &.{
.{ .key = "port", .value = "70000" }, // Invalid port > 65535
});
defer validation_env_map.deinit();
try std.testing.expectError(error.InvalidPort, loadMap(SimpleConfig, validation_env_map));
}
}
test "error cases" {
const allocator = std.testing.allocator;
// Missing required field
{
const Config = struct {
required: []const u8,
};
var env_map = try createTestEnvMap(allocator, &.{});
defer env_map.deinit();
try std.testing.expectError(error.MissingEnvironmentVariable, loadMap(Config, env_map));
}
// Invalid integer
{
const Config = struct {
port: u32,
};
var env_map = try createTestEnvMap(allocator, &.{
.{ .key = "port", .value = "invalid" },
});
defer env_map.deinit();
try std.testing.expectError(error.InvalidCharacter, loadMap(Config, env_map));
}
// Custom validator error
{
const Config = struct {
port: u32,
const env = .{
.port = .{
.key = "PORT",
.parser = validator(u32, validatePort),
},
};
};
var env_map = try createTestEnvMap(allocator, &.{
.{ .key = "PORT", .value = "99999" },
});
defer env_map.deinit();
try std.testing.expectError(error.InvalidPort, loadMap(Config, env_map));
}
}
test "comprehensive real-world scenario" {
const LogLevel = enum { debug, info, warn, err };
const RedisConfig = struct {
host: []const u8 = "localhost",
port: u32 = 6379,
password: ?[]const u8,
const env = .{
.host = "REDIS_HOST",
.port = "REDIS_PORT",
.password = "REDIS_PASSWORD",
};
};
const DatabaseConfig = struct {
host: []const u8,
port: u32,
ssl: bool = false,
const env = .{
.host = "DB_HOST",
.port = .{
.key = "DB_PORT",
.parser = validator(u32, validatePort),
},
.ssl = "DB_SSL",
};
};
const Config = struct {
app_name: []const u8,
debug: bool = false,
log_level: LogLevel = .info,
features: [][]const u8,
database: DatabaseConfig,
redis: ?RedisConfig,
const env = .{
.app_name = "APP_NAME",
.debug = "DEBUG",
.log_level = .{
.key = "LOG_LEVEL",
.parser = parseEnum(LogLevel),
},
.features = .{
.key = "FEATURES",
.parser = parseStringArray,
},
};
};
const allocator = std.testing.allocator;
var env_map = try createTestEnvMap(allocator, &.{
.{ .key = "APP_NAME", .value = "production-app" },
.{ .key = "DEBUG", .value = "false" },
.{ .key = "LOG_LEVEL", .value = "warn" },
.{ .key = "FEATURES", .value = "auth, analytics, caching, monitoring" },
.{ .key = "DB_HOST", .value = "db.example.com" },
.{ .key = "DB_PORT", .value = "5432" },
.{ .key = "DB_SSL", .value = "true" },
.{ .key = "REDIS_HOST", .value = "cache.example.com" },
.{ .key = "REDIS_PORT", .value = "6380" },
});
defer env_map.deinit();
const config = try loadMap(Config, env_map);
// Note: config.features are allocated from page allocator in loadMap()
// For production use with allocating parsers, prefer load() which provides automatic cleanup
try std.testing.expectEqualStrings("production-app", config.app_name);
try std.testing.expect(!config.debug);
try std.testing.expectEqual(LogLevel.warn, config.log_level);
try std.testing.expectEqual(@as(usize, 4), config.features.len);
try std.testing.expectEqualStrings("auth", config.features[0]);
try std.testing.expectEqualStrings("analytics", config.features[1]);
try std.testing.expectEqualStrings("caching", config.features[2]);
try std.testing.expectEqualStrings("monitoring", config.features[3]);
try std.testing.expectEqualStrings("db.example.com", config.database.host);
try std.testing.expectEqual(@as(u32, 5432), config.database.port);
try std.testing.expect(config.database.ssl);
try std.testing.expectEqualStrings("cache.example.com", config.redis.?.host);
try std.testing.expectEqual(@as(u32, 6380), config.redis.?.port);
try std.testing.expectEqual(@as(?[]const u8, null), config.redis.?.password);
}