From 18cd949d64d186b786f836ad6e88f8dd7a9883ca Mon Sep 17 00:00:00 2001 From: Hanaasagi Date: Fri, 21 Mar 2025 12:08:09 +0900 Subject: [PATCH 1/3] feat: support zig 0.14.0 Close: https://github.com/Hanaasagi/struct-env/issues/5 --- .github/workflows/ci.yaml | 2 +- README.md | 2 +- build.zig.zon | 5 +++-- src/lib.zig | 34 +++++++++++++++++----------------- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index fe40703..de86cae 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -15,7 +15,7 @@ jobs: - uses: actions/checkout@v3 - uses: goto-bus-stop/setup-zig@v2 with: - version: 0.13.0 + version: 0.14.0 - run: zig version - run: zig env - name: Build diff --git a/README.md b/README.md index 9acf56f..aa75fda 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ [![codecov](https://codecov.io/gh/Hanaasagi/struct-env/branch/master/graph/badge.svg?token=DQQZETSCW3)](https://codecov.io/gh/Hanaasagi/struct-env) ![](https://img.shields.io/badge/language-zig-%23ec915c) -**NOTE: Supported Zig Version is 0.13.0** +**NOTE: Supported Zig Version is 0.14.0** ## What is `struct-env` diff --git a/build.zig.zon b/build.zig.zon index 92cb777..c69b918 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -1,6 +1,7 @@ .{ - .name = "struct-env", - .version = "0.3.0", + .name = .struct_env, + .fingerprint=0x1ec612b14680944c, + .version = "0.4.0", .dependencies = .{}, .paths = .{""}, } diff --git a/src/lib.zig b/src/lib.zig index 58b0081..55f2be6 100644 --- a/src/lib.zig +++ b/src/lib.zig @@ -109,7 +109,7 @@ pub const StructEnv = struct { /// Get the default value of a struct field, return null if there is no default value. fn getDefault(self: Self, comptime T: type, comptime field: Field) ?T { _ = self; - if (field.default_value) |default_value| { + if (field.default_value_ptr) |default_value| { const anyopaque_pointer: *anyopaque = @constCast(default_value); return @as(*T, @ptrCast(@alignCast(anyopaque_pointer))).*; } @@ -120,7 +120,7 @@ pub const StructEnv = struct { /// Deserialize into a value fn deserializeInto(self: *Self, ptr: anytype, comptime field: ?Field) !void { const T = @TypeOf(ptr); - comptime assert(is(.Pointer)(T)); + comptime assert(is(.pointer)(T)); const C = comptime meta.Child(T); @@ -128,16 +128,16 @@ pub const StructEnv = struct { []const u8 => try self.deserializeString(C, field.?), // ?[]const u8 => null, else => switch (@typeInfo(C)) { - .Struct => try self.deserializeStruct(C), - .Enum => try self.deserializeEnum(C, field.?), - .Optional => try self.deserializeOptional(C, field.?), - .Bool => try self.deserializeBool(C, field.?), + .@"struct" => try self.deserializeStruct(C), + .@"enum" => try self.deserializeEnum(C, field.?), + .optional => try self.deserializeOptional(C, field.?), + .bool => try self.deserializeBool(C, field.?), // .Int => |info| info.bits, - .Int => try self.deserializeInt(C, field.?), - .Float => try self.deserializeFloat(C, field.?), + .int => try self.deserializeInt(C, field.?), + .float => try self.deserializeFloat(C, field.?), // .Array => try self.deserializeArray(C), // .Vector => try self.deserializeVector(C), - .Pointer => try self.deserializePointer(C, field.?), + .pointer => try self.deserializePointer(C, field.?), // ... else => @compileError("Unsupported deserialization type " ++ @typeName(C) ++ "\n"), @@ -195,7 +195,7 @@ pub const StructEnv = struct { if (value) |v| { const C = comptime meta.Child(T); // TODO: delimiter - var it = std.mem.split(u8, v, ","); + var it = std.mem.splitSequence(u8, v, ","); var new_value = std.ArrayList(C).init(self.allocator); defer new_value.deinit(); @@ -204,9 +204,9 @@ pub const StructEnv = struct { []const u8 => try self.allocator.dupe(u8, s), // []const u8 => s, else => switch (@typeInfo(C)) { - .Bool => try utils.str2bool(self.allocator, s), - .Int => try std.fmt.parseInt(C, s, 0), - .Float => try std.fmt.parseFloat(C, s, 0), + .bool => try utils.str2bool(self.allocator, s), + .int => try std.fmt.parseInt(C, s, 0), + .float => try std.fmt.parseFloat(C, s, 0), else => @compileError("Unsupported deserialization type" ++ @typeName(C) ++ "\n"), }, }; @@ -226,9 +226,9 @@ pub const StructEnv = struct { const item = switch (C) { []const u8 => try self.allocator.dupe(u8, s), else => switch (@typeInfo(C)) { - .Bool => s, - .Int => s, - .Float => s, + .bool => s, + .int => s, + .float => s, else => @compileError("Unsupported deserialization type" ++ @typeName(C) ++ "\n"), }, }; @@ -322,7 +322,7 @@ pub fn free(allocator: std.mem.Allocator, value: anytype) void { []const u8 => allocator.free(@field(value, struct_field.name)), else => { switch (@typeInfo(struct_field.type)) { - .Pointer => { + .pointer => { const need_free = true; // const cur = @field(value, struct_field.name); From baf2080d548556085af8c35df9b577a406448e91 Mon Sep 17 00:00:00 2001 From: Hanaasagi Date: Sat, 22 Mar 2025 19:30:33 +0900 Subject: [PATCH 2/3] chore: remove kcov --- .github/workflows/ci.yaml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index de86cae..7bff8df 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -22,14 +22,14 @@ jobs: run: zig build --verbose - name: Run Tests run: zig build test - - name: Install kcov - run: sudo apt-get install -y kcov - - name: Generate coverage - run: kcov $PWD/kcov-out ./.zig-cache/o/*/test - - name: Upload coverage reports to Codecov - uses: codecov/codecov-action@v5 - with: - token: ${{ secrets.CODECOV_TOKEN }} - directory: ./kcov-out/test - verbose: true - fail_ci_if_error: true + # - name: Install kcov + # run: sudo apt-get install -y kcov + # - name: Generate coverage + # run: kcov $PWD/kcov-out ./.zig-cache/o/*/test + # - name: Upload coverage reports to Codecov + # uses: codecov/codecov-action@v5 + # with: + # token: ${{ secrets.CODECOV_TOKEN }} + # directory: ./kcov-out/test + # verbose: true + # fail_ci_if_error: true From c0c26c45b3964ab350ba08491882b16f302575fd Mon Sep 17 00:00:00 2001 From: Hanaasagi Date: Sat, 22 Mar 2025 19:38:47 +0900 Subject: [PATCH 3/3] feat: fix examples in zig 0.14 --- examples/build.zig | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/build.zig b/examples/build.zig index 3bf7fc4..109b9d7 100644 --- a/examples/build.zig +++ b/examples/build.zig @@ -9,7 +9,7 @@ const examples = .{ "prefix", }; -pub fn build(b: *std.build.Builder) void { +pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); @@ -21,14 +21,14 @@ pub fn build(b: *std.build.Builder) void { const exe = b.addExecutable(.{ .name = exe_name, - .root_source_file = .{ .path = example_path }, + .root_source_file = b.path(example_path), .target = target, .optimize = optimize, }); const mod = b.addModule("struct-env", .{ - .source_file = .{ .path = "../src/lib.zig" }, + .root_source_file = b.path("../src/lib.zig"), }); - exe.addModule("struct-env", mod); + exe.root_module.addImport("struct-env", mod); b.installArtifact(exe);