{"id":19998906,"url":"https://github.com/hanaasagi/struct-env","last_synced_at":"2025-05-04T14:31:13.511Z","repository":{"id":170061049,"uuid":"644712820","full_name":"Hanaasagi/struct-env","owner":"Hanaasagi","description":"deserialize env vars into typesafe structs","archived":false,"fork":false,"pushed_at":"2025-03-22T10:39:52.000Z","size":23,"stargazers_count":17,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-02T22:44:11.312Z","etag":null,"topics":["environment-variables","zig"],"latest_commit_sha":null,"homepage":"","language":"Zig","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Hanaasagi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-05-24T05:24:24.000Z","updated_at":"2025-04-03T19:41:08.000Z","dependencies_parsed_at":"2023-10-03T10:28:54.472Z","dependency_job_id":"302955b9-e9ac-4b6f-aeb1-d681279b270c","html_url":"https://github.com/Hanaasagi/struct-env","commit_stats":{"total_commits":24,"total_committers":1,"mean_commits":24.0,"dds":0.0,"last_synced_commit":"aeebd4b19f75b7caf77f44f6418c298a4fe7d385"},"previous_names":["hanaasagi/struct-env"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hanaasagi%2Fstruct-env","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hanaasagi%2Fstruct-env/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hanaasagi%2Fstruct-env/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Hanaasagi%2Fstruct-env/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Hanaasagi","download_url":"https://codeload.github.com/Hanaasagi/struct-env/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252349442,"owners_count":21733833,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["environment-variables","zig"],"created_at":"2024-11-13T05:09:51.721Z","updated_at":"2025-05-04T14:31:13.505Z","avatar_url":"https://github.com/Hanaasagi.png","language":"Zig","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e struct-env 🌱 \u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e 𝒉𝒂𝒏𝒅𝒍𝒊𝒏𝒈 𝒆𝒏𝒗𝒊𝒓𝒐𝒏𝒎𝒆𝒏𝒕 𝒗𝒂𝒓𝒊𝒂𝒃𝒍𝒆𝒔 𝒊𝒏 𝒂 𝒕𝒚𝒑𝒆-𝒔𝒂𝒇𝒆 𝒘𝒂𝒚. \u003c/p\u003e\n\n[![CI](https://github.com/Hanaasagi/struct-env/actions/workflows/ci.yaml/badge.svg)](https://github.com/Hanaasagi/struct-env/actions/workflows/ci.yaml)\n[![codecov](https://codecov.io/gh/Hanaasagi/struct-env/branch/master/graph/badge.svg?token=DQQZETSCW3)](https://codecov.io/gh/Hanaasagi/struct-env)\n![](https://img.shields.io/badge/language-zig-%23ec915c)\n\n**NOTE: Supported Zig Version is 0.14.0**\n\n## What is `struct-env`\n\n`struct-env` provides a way to handle environment variables using struct fields.\nIts advantage is the automatic deserialization of environment variables into the specified types.\nFor example, instead of using `std.mem.eql(u8, foo, \"true\")` to determine the truth value of an env-var,\n`struct-env` allows us to simply use `foo: bool` to deserialize it into a boolean type.\n\n## Quick Start\n\nBelow is a basic example:\n\n```zig\nconst std = @import(\"std\");\nconst struct_env = @import(\"struct-env\");\n\nconst MyEnv = struct {\n    home: []const u8,\n    foo: ?[]const u8,\n    bar: []const u8 = \"bar\",\n};\n\npub fn main() !void {\n    const allocator = std.heap.page_allocator;\n\n    const env = try struct_env.fromEnv(allocator, MyEnv);\n    defer struct_env.free(allocator, env);\n\n    std.debug.print(\"HOME is {s}\\n\", .{env.home});\n    std.debug.print(\"FOO  is {any}\\n\", .{env.foo == null});\n    std.debug.print(\"BAR  is {s}\\n\", .{env.bar});\n}\n```\n\nHere are some examples of this program's output.\nYou can find more examples in the `examples` directory.\n\n```\n$ zig run [file]\nHOME is /home/username\nFOO  is true\nBAR  is bar\n```\n\n```\n$ FOO=\"foo\" BAR=\"bar\" zig run [file]\nHOME is /home/username\nFOO  is false\nBAR  is bar\n```\n\n`struct-env` assumes that there is an environment variable corresponding to each struct field, with the same name in all uppercase letters.\nFor instance, a struct field `foo_bar` would be expected to have an environment variable named `FOO_BAR`.\n\nStructs with fields of type Optional(`?` prefix) can be successfully deserialized even if their associated environment variable is not present.\n\nOf course, if the variable does not exist, you can set a default value.\n\n`struct-env` also supports deserializing slice from comma separated env var values.\n\n## Env-var with common prefix\n\nThe common pattern for prefixeing env var names for a specific app is supported using the `fromPrefixedEnv`.\nAsumming your env vars are prefixed with `APP_`, the example may look like\n\n```zig\nconst MyEnv = struct {\n    // APP_NAME\n    name : []const u8,\n};\n\nconst env = try struct_env.fromPrefixedEnv(allocator, MyEnv, \"APP_\");\ndefer struct_env.free(allocator, env);\n```\n\n## Supported types:\n\n- Built-in types, such as `[]const u8`, `i32`\n- Optional types, such as `?u32`\n- Slice types, such as `[][]const u8`\n\n## License\n\nMIT\n\n\u003chr\u003e\n\nThanks to those who have helped me on Reddit and Stack Overflow.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhanaasagi%2Fstruct-env","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhanaasagi%2Fstruct-env","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhanaasagi%2Fstruct-env/lists"}