-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit aa4b787
Showing
17 changed files
with
800 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
jobs: | ||
build: | ||
executor: | ||
name: go/default | ||
tag: '1.21.1' | ||
steps: | ||
- checkout | ||
- go/load-cache | ||
- go/mod-download | ||
- go/save-cache | ||
- go/test: | ||
covermode: atomic | ||
failfast: true | ||
race: true | ||
coverprofile: coverage.txt | ||
- run: | ||
name: Coverage Report | ||
command: | | ||
bash <(curl -s https://codecov.io/bash) | ||
orbs: | ||
go: circleci/go@1.9.0 | ||
version: 2.1 | ||
workflows: | ||
main: | ||
jobs: | ||
- build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
name: Commit validation | ||
|
||
on: | ||
[pull_request] | ||
|
||
jobs: | ||
commitsar: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v1 | ||
|
||
- name: Commitsar Action | ||
uses: outillage/commitsar@v0.13.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: golangci-lint | ||
on: | ||
push: | ||
tags: | ||
- v* | ||
branches: | ||
- master | ||
pull_request: | ||
jobs: | ||
golangci: | ||
name: lint | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- name: golangci-lint | ||
uses: golangci/golangci-lint-action@v2 | ||
with: | ||
# Required: the version of golangci-lint is required and must be specified without patch version: we always use the latest patch version. | ||
version: v1.29 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
name: Release | ||
|
||
on: | ||
pull_request: | ||
branches: | ||
- main | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
release: | ||
name: release | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
|
||
- name: Action For Semantic Release | ||
uses: cycjimmy/semantic-release-action@v2.3.0 | ||
id: semantic | ||
with: | ||
semantic_version: 17 | ||
extra_plugins: | | ||
conventional-changelog-conventionalcommits | ||
@semantic-release/changelog | ||
@semantic-release/git | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }} | ||
|
||
- name: Push updates to branch for major version | ||
if: steps.semantic.outputs.new_release_published == 'true' | ||
run: "git push https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git HEAD:refs/heads/v${{steps.semantic.outputs.new_release_major_version}}" | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# If you prefer the allow list template instead of the deny list, see community template: | ||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore | ||
# | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# Go workspace file | ||
go.work | ||
|
||
coverage.txt | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 Maksim Martianov | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
[![CircleCI](https://dl.circleci.com/status-badge/img/gh/maksimru/go-option/tree/master.svg?style=svg)](https://dl.circleci.com/status-badge/redirect/gh/maksimru/go-option/tree/master) | ||
[![codecov](https://codecov.io/gh/maksimru/go-option/graph/badge.svg?token=NQICPHBEUQ)](https://codecov.io/gh/maksimru/go-option) | ||
[![PkgGoDev](https://pkg.go.dev/badge/github.com/maksimru/go-option)](https://pkg.go.dev/github.com/maksimru/go-option) | ||
[![Go Report Card](https://goreportcard.com/badge/github.com/maksimru/go-option)](https://goreportcard.com/report/github.com/maksimru/go-option) | ||
|
||
# Go Option | ||
|
||
The idea is based on Scala Option and Java Optional. The package allows to create optional values in Golang | ||
|
||
## Functions | ||
|
||
Set an non-empty value: | ||
``` | ||
option.Some[T](v) | ||
``` | ||
|
||
Set an empty value: | ||
``` | ||
v := option.None[T]() | ||
``` | ||
|
||
Set an empty value if v is nil, otherwise set non-empty value | ||
``` | ||
v := option.NewOption[T](v) | ||
``` | ||
|
||
Remap one option to another option | ||
``` | ||
import "github.com/maksimru/go-option" | ||
type Car struct { | ||
name string | ||
plateNumber option.Option[string] | ||
} | ||
carOpt := option.Some[Car]( | ||
Car { | ||
name: "bmw" | ||
}, | ||
) | ||
// get car name as option | ||
carNameOpt := option.Map[Car, string](carOpt, func(c Car) string { | ||
return c.name | ||
}) | ||
``` | ||
|
||
Option composition | ||
``` | ||
import "github.com/maksimru/go-option" | ||
type Car struct { | ||
name string | ||
plateNumber option.Option[string] | ||
} | ||
type User struct { | ||
name string | ||
car option.Option[Car] | ||
} | ||
u := User{ | ||
name: "jake", | ||
car: option.Some[Car]( | ||
Car{ | ||
name: "bmw", | ||
plateNumber: option.Some[string]("X723"), | ||
}, | ||
), | ||
} | ||
// get car plate as option | ||
carPlateOpt := option.FlatMap[Car, string](u.car, func(c Car) option.Option[string] { | ||
return c.plateNumber | ||
}) | ||
``` | ||
|
||
## Methods of Option | ||
|
||
| Method | Description | | ||
|--------|:-----------------------------------------------:| | ||
| Get() | gets underlying value (unsafe*) | | ||
| GetOrElse(fallback) | gets underlying value or returns fallback value | | ||
| OrElse(fallbackOpt) | returns fallback option if option is empty | | ||
| Empty() | checks if value is empty | | ||
| NonEmpty() | checks if value is set | | ||
| String() | string representation | | ||
`* - empty value will panic` | ||
|
||
--- | ||
## Testing | ||
|
||
To run all tests in this module: | ||
|
||
``` | ||
go test ./... | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package option |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module github.com/maksimru/go-option | ||
|
||
go 1.21.1 | ||
|
||
require github.com/stretchr/testify v1.8.4 | ||
|
||
require ( | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
gopkg.in/yaml.v3 v3.0.1 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= | ||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= | ||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= | ||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package option | ||
|
||
type optNone[T any] struct { | ||
} | ||
|
||
func (n optNone[T]) Get() T { | ||
panic("called Get on optNone value") | ||
} | ||
|
||
func (n optNone[T]) GetOrElse(v T) T { | ||
return v | ||
} | ||
|
||
func (n optNone[T]) OrElse(opt Option[T]) Option[T] { | ||
return opt | ||
} | ||
|
||
func (n optNone[T]) Empty() bool { | ||
return true | ||
} | ||
|
||
func (n optNone[T]) NonEmpty() bool { | ||
return false | ||
} | ||
|
||
func (n optNone[T]) String() string { | ||
return "None" | ||
} |
Oops, something went wrong.