Skip to content

Commit

Permalink
born
Browse files Browse the repository at this point in the history
  • Loading branch information
pmorelli92 committed Apr 28, 2021
1 parent 49a954f commit 686abcd
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/pmorelli92/maybe

go 1.16
34 changes: 34 additions & 0 deletions int.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package maybe

import "encoding/json"

type Int struct {
Value int
HasValue bool
}

func (ms *Int) UnmarshalJSON(data []byte) error {
var s *int
if err := json.Unmarshal(data, &s); err != nil {
return err
}

if s != nil {
*ms = Int{
Value: *s,
HasValue: true,
}
}

return nil
}

func (ms Int) MarshalJSON() ([]byte, error) {
var s *int

if ms.HasValue {
s = &ms.Value
}

return json.Marshal(s)
}
34 changes: 34 additions & 0 deletions string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package maybe

import "encoding/json"

type String struct {
Value string
HasValue bool
}

func (ms *String) UnmarshalJSON(data []byte) error {
var s *string
if err := json.Unmarshal(data, &s); err != nil {
return err
}

if s != nil {
*ms = String{
Value: *s,
HasValue: true,
}
}

return nil
}

func (ms String) MarshalJSON() ([]byte, error) {
var s *string

if ms.HasValue {
s = &ms.Value
}

return json.Marshal(s)
}

0 comments on commit 686abcd

Please sign in to comment.