{"id":16792255,"url":"https://github.com/leaanthony/u","last_synced_at":"2025-03-17T03:30:43.298Z","repository":{"id":195756795,"uuid":"693595081","full_name":"leaanthony/u","owner":"leaanthony","description":"Adding \"unset\" state to Go's types","archived":false,"fork":false,"pushed_at":"2023-10-29T21:25:40.000Z","size":17,"stargazers_count":81,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-27T17:16:51.506Z","etag":null,"topics":["go","golang"],"latest_commit_sha":null,"homepage":"","language":"Go","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/leaanthony.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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-09-19T10:39:22.000Z","updated_at":"2024-11-09T22:21:55.000Z","dependencies_parsed_at":"2023-09-24T09:44:09.667Z","dependency_job_id":"5415e70c-08f6-4152-9f19-846cfa3b834b","html_url":"https://github.com/leaanthony/u","commit_stats":null,"previous_names":["leaanthony/u"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leaanthony%2Fu","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leaanthony%2Fu/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leaanthony%2Fu/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/leaanthony%2Fu/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/leaanthony","download_url":"https://codeload.github.com/leaanthony/u/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243841204,"owners_count":20356441,"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":["go","golang"],"created_at":"2024-10-13T08:45:09.982Z","updated_at":"2025-03-17T03:30:43.014Z","avatar_url":"https://github.com/leaanthony.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# u\n\nu provides a simple way to create variables that are \"unset\" by default.\n\nu is dependency free and has 100% test coverage.\n\n## Why?\n\nGo's default values are great most of the time, but sometimes you want to know if a value has been set or not. This is especially true when you want to know if a value has been set to its zero value or not.\n\nFor example, let's say you had a preferences struct like this:\n\n```go\ntype Preferences struct {\n    UseFeatureX bool\n    Threshold int\n}\n\nfunc processPreferences(prefs Preferences) {\n    // Uh oh...we're in a heap of trouble here...\n    thirdPartyLibrary.EnableFeatureX(prefs.UseFeatureX)\n    thirdPartyLibrary.SetThreshold(prefs.Threshold)\n}\n\nfunc main() {\n    var prefs Preferences\n    processPreferences(prefs)\n}\n```\n\nThere is no real way to know if `UseFeatureX` or `Threshold` have been set or not.\n\nUsing this library we can *eliminate* this problem:\n\n```go\ntype Preferences struct {\n    UseFeatureX u.Bool\n    Threshold u.Int\n}\n\nfunc processPreferences(prefs Preferences) {\n    // #winning\n    if prefs.UseFeatureX.IsSet() {\n        thirdPartyLibrary.EnableFeatureX(prefs.UseFeatureX.Get())\n    }\n    if prefs.Threshold.IsSet() {\n        thirdPartyLibrary.SetThreshold(prefs.Threshold.Get())\n    }\n}\n\nfunc main() {\n    var prefs Preferences\n    processPreferences(prefs)\n}\n\n```\n\n## Why should any of this matter? \n\nIt matters when you are working with third party libraries or frameworks that have default values that may not be the same as the Zero Go values. \nPerhaps the default value for a preference is `true` and you only want to set it if an explicit value has been specified.\n\n## Usage\n\n### Basic Usage\n\n```go\nvar myVar u.Int\n\n// Set the value\nmyVar.Set(10)\n\n// Get the value\nfmt.Println(myVar.Get()) // 10\n\n// Check if the value has been set\nfmt.Println(myVar.IsSet()) // true\n\n// Unset the value\nmyVar.Unset()\n\n// Check if the value has been set\nfmt.Println(myVar.IsSet()) // false\n```\n\n### Structs\n\n`New` methods are provided for setting values in structs.\n\n```go\n    type MyStruct struct {\n        MyVar u.Int\n        MyOption u.Bool\n    }\n\t\n    myStruct := MyStruct{\n        MyVar: u.NewInt(42),\n        MyOption: u.True,\n    }\n```\n\n## Why not use pointer values?\n\nYes, that's one way you can solve this issue. Personally, I try to avoid having pointer values as it increases the chance of dereferencing errors. It also feels like a hacky approach to the problem which is one missed test away from a runtime error. \n\n## Supported types\n\n- `u.Bool`\n- `u.Int`\n- `u.Int8`\n- `u.Int16`\n- `u.Int32`\n- `u.Int64`\n- `u.Uint`\n- `u.Uint8`\n- `u.Uint16`\n- `u.Uint32`\n- `u.Uint64`\n- `u.Float32`\n- `u.Float64`\n- `u.Complex64`\n- `u.Complex128`\n- `u.String`\n- `u.Byte`\n- `u.Rune`\n\n## Values\n\n- `u.True`\n- `u.False`\n\n## Custom types\n\nAny type can be used with this library by creating a `u.Var` of that type. For example:\n\n```go\n\ntype MyCustomType struct {\n    value string\n}\n\nvar myVar u.Var[MyCustomType]\n\n// Set the value\nmyVar.Set(MyCustomType{value: \"hello\"})\n```\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleaanthony%2Fu","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fleaanthony%2Fu","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fleaanthony%2Fu/lists"}