{"id":15686387,"url":"https://github.com/pmorelli92/maybe","last_synced_at":"2025-05-07T18:47:36.211Z","repository":{"id":57580931,"uuid":"362234612","full_name":"pmorelli92/maybe","owner":"pmorelli92","description":"Maybe is a Go package to bring functional in Option type structures.","archived":false,"fork":false,"pushed_at":"2022-03-23T13:55:41.000Z","size":35,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-02-26T03:41:25.567Z","etag":null,"topics":["functional","golang","option"],"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/pmorelli92.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}},"created_at":"2021-04-27T19:55:42.000Z","updated_at":"2023-10-30T10:19:51.000Z","dependencies_parsed_at":"2022-09-26T19:30:41.073Z","dependency_job_id":null,"html_url":"https://github.com/pmorelli92/maybe","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmorelli92%2Fmaybe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmorelli92%2Fmaybe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmorelli92%2Fmaybe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pmorelli92%2Fmaybe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pmorelli92","download_url":"https://codeload.github.com/pmorelli92/maybe/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243007185,"owners_count":20220782,"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":["functional","golang","option"],"created_at":"2024-10-03T17:39:11.675Z","updated_at":"2025-03-11T09:31:42.179Z","avatar_url":"https://github.com/pmorelli92.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Maybe\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/pmorelli92/maybe)](https://goreportcard.com/report/github.com/pmorelli92/maybe)\n[![CI](https://github.com/pmorelli92/maybe/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/pmorelli92/maybe/actions/workflows/ci.yml)\n[![Coverage Status](https://coveralls.io/repos/github/pmorelli92/maybe/badge.svg?branch=main)](https://coveralls.io/github/pmorelli92/maybe?branch=main)\n\nMaybe is a library that adds an [Option data type](https://en.wikipedia.org/wiki/Option_type) for Golang. [Related blogpost here.](https://devandchill.com/posts/2021/04/introducing-maybe-package-bring-functional-to-go/)\n\n### What does it offer:\n\nThe `Maybe[any]` type exported by this library is immutable and thread safe. The json serialization and de-serialization works in the same way as with the underlying (any) type. Using this library will free you up from using pointers and possible panics.\n\nIt also gets rid of the situations where an absence of value means something different from a default (zero) value. For example: a person with salary 100 means he/she has a paid job, 0 means an unpaid internship and null means unemployed. Supporting yourself with `Maybe[int]` eliminates the usage of null replacing it with `HasValue`:\n\n- `salary.Value != 0` has a paid job.\n- `salary.Value == 0 \u0026\u0026 salary.HasValue` has an unpaid internship.\n- `salary.HasValue` does not have a job, this is serialized as `null` but you don't have to care about pointers.\n\n### When should I use it:\n\nIt can be used for transport layer (as it has json capabilities) but it could also be used on the domain layer.\n\n### Examples:\n\n**Marshal of Maybe[string] without value**\n\n```go\npackage main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\n\t\"github.com/pmorelli92/maybe\"\n)\n\ntype Person struct {\n\tName maybe.Maybe[string] `json:\"name\"`\n\tAge  int                 `json:\"age\"`\n}\n\nfunc main() {\n\tp := Person{Age: 28}\n\tbytes, _ := json.Marshal(p)\n\tfmt.Println(string(bytes)) // {\"name\":null,\"age\":28}\n}\n```\n\n**Marshal of Maybe[string] with value**\n\n```go\npackage main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\n\t\"github.com/pmorelli92/maybe\"\n)\n\ntype Person struct {\n\tName maybe.Maybe[string] `json:\"name\"`\n\tAge  int                 `json:\"age\"`\n}\n\nfunc main() {\n\tp := Person{Age: 28, Name: maybe.Set(\"Pablo\")}\n\tbytes, _ := json.Marshal(p)\n\tfmt.Println(string(bytes)) // {\"name\":\"Pablo\",\"age\":28}\n}\n```\n\n**Unmarshal of Maybe[string] without value**\n\n```go\npackage main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\n\t\"github.com/pmorelli92/maybe\"\n)\n\ntype Person struct {\n\tName maybe.Maybe[string] `json:\"name\"`\n\tAge  int                 `json:\"age\"`\n}\n\nfunc main() {\n\tvar p Person\n\t_ = json.Unmarshal([]byte(`{\"age\":28}`), \u0026p)\n\tfmt.Println(p.Name.HasValue()) // false\n}\n```\n\n\n**Unmarshal of Maybe[string] with value**\n\n```go\npackage main\n\nimport (\n\t\"encoding/json\"\n\t\"fmt\"\n\n\t\"github.com/pmorelli92/maybe\"\n)\n\ntype Person struct {\n\tName maybe.Maybe[string] `json:\"name\"`\n\tAge  int                 `json:\"age\"`\n}\n\nfunc main() {\n\tvar p Person\n\t_ = json.Unmarshal([]byte(`{\"age\":28, \"name\": \"Pablo\"}`), \u0026p)\n\tfmt.Println(p.Name.HasValue()) // true\n\tfmt.Println(p.Name.Value())    // Pablo\n}\n```\n\n### Types supported:\n\n`Maybe` is defined to support `[T any]` so it can support all underlying types. Personally I would not suggest using pointers as the underlying type as it will defeat the whole purpose.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpmorelli92%2Fmaybe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpmorelli92%2Fmaybe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpmorelli92%2Fmaybe/lists"}