@@ -448,22 +448,21 @@ pub const Value = union(enum) {
448
448
bool = > return .{ .bool = object },
449
449
else = > {},
450
450
}
451
- if (comptime std .meta .trait .isZigString (Type ))
452
- return .{ .string = object };
453
- if (comptime std .meta .trait .isTuple (Type )) {
454
- var array = std .ArrayListUnmanaged (Value ){};
455
- inline for (object ) | item | try array .append (allocator , try init (allocator , item ));
456
- return .{ .array = array };
457
- }
458
- if (comptime std .meta .trait .isIndexable (Type )) {
459
- var array = std .ArrayListUnmanaged (Value ){};
460
- for (object ) | item | try array .append (allocator , try init (allocator , item ));
461
- return .{ .array = array };
462
- }
463
451
switch (@typeInfo (Type )) {
464
- .Struct = > | the_struct | {
452
+ .Array = > | array_type | return initArray (allocator , object , array_type .child ),
453
+ .Pointer = > | pointer_type | return if (pointer_type .size == .Slice )
454
+ initArray (allocator , object , pointer_type .child )
455
+ else switch (@typeInfo (pointer_type .child )) {
456
+ .Array = > | array_type | initArray (allocator , object , array_type .child ),
457
+ else = > @compileError ("invalid pointer type: " ++ @typeName (Type )),
458
+ },
459
+ .Struct = > | struct_type | if (struct_type .is_tuple ) {
460
+ var array = std .ArrayListUnmanaged (Value ){};
461
+ inline for (object ) | item | try array .append (allocator , try init (allocator , item ));
462
+ return .{ .array = array };
463
+ } else {
465
464
var dict = std .StringHashMapUnmanaged (Value ){};
466
- inline for (the_struct .fields ) | field | {
465
+ inline for (struct_type .fields ) | field | {
467
466
const field_value = try init (allocator , @field (object , field .name ));
468
467
try dict .put (allocator , field .name , field_value );
469
468
}
@@ -472,6 +471,13 @@ pub const Value = union(enum) {
472
471
else = > @compileError ("invalid type: " ++ @typeName (Type )),
473
472
}
474
473
}
474
+
475
+ fn initArray (allocator : Allocator , object : anytype , comptime ItemType : type ) ! Value {
476
+ if (ItemType == u8 ) return .{ .string = object };
477
+ var array = std .ArrayListUnmanaged (Value ){};
478
+ for (object ) | item | try array .append (allocator , try init (allocator , item ));
479
+ return .{ .array = array };
480
+ }
475
481
};
476
482
477
483
test "value" {
0 commit comments