Skip to content

Commit

Permalink
updated to generics version
Browse files Browse the repository at this point in the history
  • Loading branch information
pmorelli92 committed Mar 23, 2022
1 parent 13618d4 commit cb8f922
Show file tree
Hide file tree
Showing 14 changed files with 124 additions and 973 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.18

- name: Build
run: go build -v ./...
Expand All @@ -27,7 +27,7 @@ jobs:

- name: Test
run: go test -race -covermode atomic -coverprofile=covprofile ./...

- name: Send coverage
env:
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
46 changes: 19 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@ Maybe is a library that adds an [Option data type](https://en.wikipedia.org/wiki

### What does it offer:

The types exported by this library are immutable and thread safe. The json serialization and deserialization works in the same way as with the native types. Using this library will free you up from using pointers and possible panics.
The `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 native types. Using this library will free you up from using pointers and possible panics.

It 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 Option eliminates the usage of null replacing it with `HasValue`.
It 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`:

- `salary.Value != 0` has a paid job.
- `salary.Value == 0 && salary.HasValue` has an unpaid internship.
- `salary.HasValue` does not have a job, this is serialized as `null` but you don't have to care about pointers.

### When should I use it:

It can be used for transport layer (as it has json capabilities) but it could also be used on the domain layer.

### Examples:

**Marshal of String Option without value**
**Marshal of Maybe[string] without value**

```go
package main
Expand All @@ -32,8 +35,8 @@ import (
)

type Person struct {
Name maybe.String `json:"name"`
Age int `json:"age"`
Name Maybe[string] `json:"name"`
Age int `json:"age"`
}

func main() {
Expand All @@ -43,7 +46,7 @@ func main() {
}
```

**Marshal of String Option with value**
**Marshal of Maybe[string] with value**

```go
package main
Expand All @@ -56,18 +59,18 @@ import (
)

type Person struct {
Name maybe.String `json:"name"`
Age int `json:"age"`
Name Maybe[string] `json:"name"`
Age int `json:"age"`
}

func main() {
p := Person{Age: 28, Name: maybe.SetString("Pablo")}
p := Person{Age: 28, Name: maybe.Set("Pablo")}
bytes, _ := json.Marshal(p)
fmt.Println(string(bytes)) // {"name":"Pablo","age":28}
}
```

**Unmarshal of String Option without value**
**Unmarshal of Maybe[string] without value**

```go
package main
Expand All @@ -80,8 +83,8 @@ import (
)

type Person struct {
Name maybe.String `json:"name"`
Age int `json:"age"`
Name Maybe[string] `json:"name"`
Age int `json:"age"`
}

func main() {
Expand All @@ -92,7 +95,7 @@ func main() {
```


**Unmarshal of String Option with value**
**Unmarshal of Maybe[string] with value**

```go
package main
Expand All @@ -105,8 +108,8 @@ import (
)

type Person struct {
Name maybe.String `json:"name"`
Age int `json:"age"`
Name Maybe[string] `json:"name"`
Age int `json:"age"`
}

func main() {
Expand All @@ -119,15 +122,4 @@ func main() {

### Types supported:

- bool
- string
- float
- int
- time

If this library is not supporting certain type, feel free to do a pull request or add an issue asking for it.

### Generics

Go does not support generics as of now, but the draft was recently approved. When they become available on Go 1.18 this library will be updated and only a generic struct will remain.
The library will look like this: [go2playgrounds](https://go2goplay.golang.org/p/YBqR5GX7N6m).
`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.
54 changes: 0 additions & 54 deletions bool.go

This file was deleted.

52 changes: 0 additions & 52 deletions float.go

This file was deleted.

Loading

0 comments on commit cb8f922

Please sign in to comment.