forked from markphelps/optional
/
rune.go
150 lines (130 loc) · 2.8 KB
/
rune.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Code generated by go generate. DO NOT EDIT.
// This file was generated by robots at 2020-04-10 23:14:21.779221 +0000 UTC
package optional
import (
"encoding/json"
"errors"
"fmt"
"io"
)
// Rune is an optional rune.
type Rune struct {
val rune
set bool
}
// MakeRune creates an optional.Rune from a rune.
func MakeRune(v rune) Rune {
return Rune{val: v, set: true}
}
// Set sets the rune value.
func (r *Rune) Set(v rune) {
*r = MakeRune(v)
}
// Unset unsets the rune value.
func (r *Rune) Unset() {
*r = Rune{}
}
// Present returns whether or not the value is present.
func (r Rune) Present() bool {
return r.set
}
// Get returns the rune value or panics if not present.
func (r Rune) Get() rune {
if !r.Present() {
panic("value not present")
}
return r.val
}
// GetOr returns the rune value or a default value if not present.
func (r Rune) GetOr(v rune) rune {
if r.Present() {
return r.val
}
return v
}
// GetOrBool returns the rune value or false if not present.
func (r Rune) GetOrBool() (rune, bool) {
if !r.Present() {
var zero rune
return zero, false
}
return r.val, true
}
// GetOrErr returns the rune value or an error if not present.
func (r Rune) GetOrErr() (rune, error) {
if !r.Present() {
var zero rune
return zero, errors.New("value not present")
}
return r.val, nil
}
// If calls the function fn with the value if the value is present.
func (r Rune) If(fn func(rune)) {
if r.Present() {
fn(r.val)
}
}
// Map applies the function fn to the contained value (if any) and returns a new
// option value.
func (r Rune) Map(fn func(rune) rune) Rune {
if r.Present() {
return MakeRune(fn(r.val))
}
return r
}
// And returns an empty Rune option value if not present, otherwise returns
// optb.
func (r Rune) And(optb Rune) Rune {
if r.Present() {
return optb
}
return Rune{}
}
// Or returns the Rune option value if present, otherwise returns optb.
func (r Rune) Or(optb Rune) Rune {
if r.Present() {
return r
}
return optb
}
// Format implements the fmt.Formatter interface.
func (r Rune) Format(fmtS fmt.State, verb rune) {
if !r.Present() {
io.WriteString(fmtS, "none")
return
}
var format string
switch verb {
case 'v':
if fmtS.Flag('+') {
format = "some(%+v)"
} else {
format = "some(%v)"
}
case 's':
format = "some(%v)"
case 'q':
format = "\"some(%v)\""
}
fmt.Fprintf(fmtS, format, r.val)
}
// MarshalJSON implements the json.Marshaler interface.
func (r Rune) MarshalJSON() ([]byte, error) {
if r.Present() {
return json.Marshal(r.val)
}
return json.Marshal(nil)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (r *Rune) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
r.Unset()
return nil
}
var value rune
if err := json.Unmarshal(data, &value); err != nil {
return err
}
r.Set(value)
return nil
}