forked from markphelps/optional
-
Notifications
You must be signed in to change notification settings - Fork 0
/
int8.go
126 lines (107 loc) · 2.4 KB
/
int8.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Code generated by go generate. DO NOT EDIT.
// This file was generated by robots at 2020-04-10 22:06:21.103682 +0000 UTC
package optional
import (
"encoding/json"
"errors"
)
// Int8 is an optional int8.
type Int8 struct {
val int8
set bool
}
// MakeInt8 creates an optional.Int8 from a int8.
func MakeInt8(v int8) Int8 {
return Int8{val: v, set: true}
}
// Set sets the int8 value.
func (i *Int8) Set(v int8) {
*i = MakeInt8(v)
}
// Unset unsets the int8 value.
func (i *Int8) Unset() {
*i = Int8{}
}
// Present returns whether or not the value is present.
func (i Int8) Present() bool {
return i.set
}
// Get returns the int8 value or panics if not present.
func (i Int8) Get() int8 {
if !i.Present() {
panic("value not present")
}
return i.val
}
// GetOr returns the int8 value or a default value if not present.
func (i Int8) GetOr(v int8) int8 {
if i.Present() {
return i.val
}
return v
}
// GetOrBool returns the int8 value or false if not present.
func (i Int8) GetOrBool() (int8, bool) {
if !i.Present() {
var zero int8
return zero, false
}
return i.val, true
}
// GetOrErr returns the int8 value or an error if not present.
func (i Int8) GetOrErr() (int8, error) {
if !i.Present() {
var zero int8
return zero, errors.New("value not present")
}
return i.val, nil
}
// If calls the function fn with the value if the value is present.
func (i Int8) If(fn func(int8)) {
if i.Present() {
fn(i.val)
}
}
// Map applies the function fn to the contained value (if any) and returns a new
// option value.
func (i Int8) Map(fn func(int8) int8) Int8 {
if i.Present() {
return MakeInt8(fn(i.val))
}
return i
}
// And returns an empty Int8 option value if not present, otherwise returns
// optb.
func (i Int8) And(optb Int8) Int8 {
if i.Present() {
return optb
}
return Int8{}
}
// Or returns the Int8 option value if present, otherwise returns optb.
func (i Int8) Or(optb Int8) Int8 {
if i.Present() {
return i
}
return optb
}
// MarshalJSON implements the json.Marshaler interface.
func (i Int8) MarshalJSON() ([]byte, error) {
if i.Present() {
return json.Marshal(i.val)
}
return json.Marshal(nil)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (i *Int8) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
i.Unset()
return nil
}
var value int8
if err := json.Unmarshal(data, &value); err != nil {
return err
}
i.Set(value)
return nil
}