-
Notifications
You must be signed in to change notification settings - Fork 4
/
nonNegativeInteger.go
69 lines (59 loc) · 1.96 KB
/
nonNegativeInteger.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
package values
import (
"github.com/echocat/caretakerd/errors"
"strconv"
)
// NonNegativeInteger represents an int with more features as the primitive type and that could not be negative.
// @inline
type NonNegativeInteger int
func (i NonNegativeInteger) String() string {
result, err := i.CheckedString()
if err != nil {
panic(err)
}
return result
}
// CheckedString is like String but also returns also an optional error if there are any
// validation errors.
func (i NonNegativeInteger) CheckedString() (string, error) {
return strconv.Itoa(int(i)), nil
}
// Set sets the given string to current object from a string.
// Returns an error object if there are any problems while transforming the string.
func (i *NonNegativeInteger) Set(value string) error {
valueAsInt, err := strconv.Atoi(value)
if err != nil {
return errors.New("Illegal integer value: " + value)
}
return i.SetFromInt(valueAsInt)
}
// SetFromInt tries to set the given int value to this instance.
// Returns an error object if there are any problems while transforming the plain int.
func (i *NonNegativeInteger) SetFromInt(value int) error {
if value < 0 {
return errors.New("This intger value should not be negative. But got: %v", value)
}
*i = NonNegativeInteger(value)
return nil
}
// MarshalYAML is used until yaml marshalling. Do not call this method directly.
func (i NonNegativeInteger) MarshalYAML() (interface{}, error) {
return int(i), nil
}
// UnmarshalYAML is used until yaml unmarshalling. Do not call this method directly.
func (i *NonNegativeInteger) UnmarshalYAML(unmarshal func(interface{}) error) error {
var value int
if err := unmarshal(&value); err != nil {
return err
}
return i.SetFromInt(value)
}
// Int returns this value as int.
func (i NonNegativeInteger) Int() int {
return int(i)
}
// Validate validates actions on this object and returns an error object if there are any.
func (i NonNegativeInteger) Validate() error {
_, err := i.CheckedString()
return err
}