Skip to content

Commit 45d6da9

Browse files
add new helper methods to opt types (#37)
1 parent 5f88b1e commit 45d6da9

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

opt/opt.go

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ type Int struct {
3333
value int
3434
}
3535

36-
// NewUintNone returns a new OptUint wrapper
36+
// NewIntNone returns a new OptUint wrapper
3737
func NewIntNone() Int {
3838
return Int{
3939
exists: false,
4040
value: 0,
4141
}
4242
}
4343

44-
// UintWith returns a new OptUint wrapper with a given int
44+
// IntWith returns a new OptUint wrapper with a given int
4545
func IntWith(i int) Int {
4646
return Int{
4747
exists: true,
@@ -125,6 +125,30 @@ func (opt Uint) ValueOr(i uint64) uint64 {
125125
return i
126126
}
127127

128+
// MultUint64OrNone or will multiply the existing Uint value by a supplied uint64, and return None if either the Uint is none, or the supplied uint64 is zero.
129+
func (opt Uint) MultUint64OrNone(i uint64) Uint {
130+
if !opt.exists {
131+
return opt
132+
}
133+
if i == 0 {
134+
return Uint{exists: false}
135+
}
136+
return Uint{exists: true, value: opt.value * i}
137+
}
138+
139+
// SubtractOrNone will subtract the existing uint with the supplied uint64 value. If this would result in a value invalid for a uint (ie, a negative number), return None
140+
func (opt Uint) SubtractOrNone(i Uint) Uint {
141+
if !opt.exists || !i.Exists() {
142+
return opt
143+
}
144+
145+
if i.ValueOr(0) > opt.value {
146+
return Uint{exists: false}
147+
}
148+
149+
return Uint{exists: true, value: opt.value - i.ValueOr(0)}
150+
}
151+
128152
// SumOptUint sums a list of OptUint values
129153
func SumOptUint(opts ...Uint) uint64 {
130154
var sum uint64

0 commit comments

Comments
 (0)