{"id":18310123,"url":"https://github.com/kazhuravlev/optional","last_synced_at":"2026-03-13T18:24:01.571Z","repository":{"id":218296491,"uuid":"746032653","full_name":"kazhuravlev/optional","owner":"kazhuravlev","description":"Provides a type for working with optional variables, fields in structures, functions and database models.","archived":false,"fork":false,"pushed_at":"2024-08-14T16:32:50.000Z","size":16,"stargazers_count":7,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T16:16:18.868Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/kazhuravlev.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":"2024-01-20T21:44:50.000Z","updated_at":"2024-08-31T00:28:52.000Z","dependencies_parsed_at":"2024-01-21T03:21:59.078Z","dependency_job_id":"ab4924b7-c0b3-4d29-a37a-e0cb71ef1819","html_url":"https://github.com/kazhuravlev/optional","commit_stats":null,"previous_names":["kazhuravlev/optional"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kazhuravlev%2Foptional","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kazhuravlev%2Foptional/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kazhuravlev%2Foptional/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kazhuravlev%2Foptional/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kazhuravlev","download_url":"https://codeload.github.com/kazhuravlev/optional/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247375851,"owners_count":20929120,"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":[],"created_at":"2024-11-05T16:13:19.028Z","updated_at":"2026-03-13T18:23:56.521Z","avatar_url":"https://github.com/kazhuravlev.png","language":"Go","readme":"# Optional values for Go projects\n\n[![Go Reference](https://pkg.go.dev/badge/github.com/kazhuravlev/optional.svg)](https://pkg.go.dev/github.com/kazhuravlev/optional)\n[![License](https://img.shields.io/github/license/kazhuravlev/optional?color=blue)](https://github.com/kazhuravlev/optional/blob/master/LICENSE)\n[![Build Status](https://github.com/kazhuravlev/optional/actions/workflows/tests.yml/badge.svg?branch=master)](https://github.com/kazhuravlev/optional/actions/workflows/tests.yml?query=branch%3Amaster)\n[![Go Report Card](https://goreportcard.com/badge/github.com/kazhuravlev/optional)](https://goreportcard.com/report/github.com/kazhuravlev/optional)\n[![codecov](https://codecov.io/gh/kazhuravlev/optional/graph/badge.svg?token=B7ILMGURZW)](https://codecov.io/gh/kazhuravlev/optional)\n\nWe often need tools that help us work with optional values. For example, for optional fields in requests/responses from\nAPIs (JSON), optional values in YAML/JSON configs, or nullable columns in some tables. Typically, we use a pointer for\ncertain data types, but this does not work well for some scenarios.\n\nThis package offers a simple way to define optional fields and provides some helpers for marshalling and unmarshalling\ndata.\n\n## Features\n\n- JSON marshal/unmarshal\n- YAML marshal/unmarshal\n- SQL driver value/scan\n- Useful functions to create and handle optional values\n\n## Installation\n\n```shell\ngo get -u github.com/kazhuravlev/optional\n```\n\n## Usage\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"encoding/json\"\n\t\"github.com/kazhuravlev/optional\"\n)\n\ntype User struct {\n\tAvatarURL optional.Val[string] `json:\"avatar_url\"`\n\t// some fields...\n}\n\nfunc main() {\n\treq := []byte(`{\"avatar_url\": \"https://example.com/img.webp\"}`)\n\tvar user User\n\tif err := json.Unmarshal(req, \u0026user); err != nil {\n\t\tpanic(err)\n\t}\n\n\t// Check the presence of value and use it.\n\tif avatar, ok := user.AvatarURL.Get(); ok {\n\t\tfmt.Println(\"User have avatar\", avatar)\n\t} else {\n\t\tfmt.Println(\"User have no avatar\")\n\t}\n\n\t// Just check that value presented.\n\tif !user.AvatarURL.HasVal() {\n\t\tfmt.Println(\"User have no avatar\")\n\t}\n\n\t// Use default value in case of it did not presented.\n\tfmt.Println(\"Avatar of this user is:\", user.AvatarURL.ValDefault(\"https://example.com/default.webp\"))\n\n\t// Use this api to adapt value to pointer. It will return nil when value not provided.\n\tavatarPtr := user.AvatarURL.AsPointer()\n\tfmt.Printf(\"Pointer to avatar: %#v\\n\", avatarPtr)\n}\n```","funding_links":[],"categories":["Utilities","公用事业公司"],"sub_categories":["Utility/Miscellaneous","实用程序/Miscellaneous"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkazhuravlev%2Foptional","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkazhuravlev%2Foptional","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkazhuravlev%2Foptional/lists"}