forked from markphelps/optional
-
Notifications
You must be signed in to change notification settings - Fork 0
/
string.go
126 lines (107 loc) · 2.5 KB
/
string.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.954418 +0000 UTC
package optional
import (
"encoding/json"
"errors"
)
// String is an optional string.
type String struct {
val string
set bool
}
// MakeString creates an optional.String from a string.
func MakeString(v string) String {
return String{val: v, set: true}
}
// Set sets the string value.
func (s *String) Set(v string) {
*s = MakeString(v)
}
// Unset unsets the string value.
func (s *String) Unset() {
*s = String{}
}
// Present returns whether or not the value is present.
func (s String) Present() bool {
return s.set
}
// Get returns the string value or panics if not present.
func (s String) Get() string {
if !s.Present() {
panic("value not present")
}
return s.val
}
// GetOr returns the string value or a default value if not present.
func (s String) GetOr(v string) string {
if s.Present() {
return s.val
}
return v
}
// GetOrBool returns the string value or false if not present.
func (s String) GetOrBool() (string, bool) {
if !s.Present() {
var zero string
return zero, false
}
return s.val, true
}
// GetOrErr returns the string value or an error if not present.
func (s String) GetOrErr() (string, error) {
if !s.Present() {
var zero string
return zero, errors.New("value not present")
}
return s.val, nil
}
// If calls the function fn with the value if the value is present.
func (s String) If(fn func(string)) {
if s.Present() {
fn(s.val)
}
}
// Map applies the function fn to the contained value (if any) and returns a new
// option value.
func (s String) Map(fn func(string) string) String {
if s.Present() {
return MakeString(fn(s.val))
}
return s
}
// And returns an empty String option value if not present, otherwise returns
// optb.
func (s String) And(optb String) String {
if s.Present() {
return optb
}
return String{}
}
// Or returns the String option value if present, otherwise returns optb.
func (s String) Or(optb String) String {
if s.Present() {
return s
}
return optb
}
// MarshalJSON implements the json.Marshaler interface.
func (s String) MarshalJSON() ([]byte, error) {
if s.Present() {
return json.Marshal(s.val)
}
return json.Marshal(nil)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (s *String) UnmarshalJSON(data []byte) error {
if string(data) == "null" {
s.Unset()
return nil
}
var value string
if err := json.Unmarshal(data, &value); err != nil {
return err
}
s.Set(value)
return nil
}