-
Notifications
You must be signed in to change notification settings - Fork 0
/
params.go
125 lines (104 loc) · 2.91 KB
/
params.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
package cli
import (
"strconv"
"strings"
data "github.com/multiverse-os/cli/data"
)
// TODO: Create a New() function so we can hook in validations
type Param struct {
DataType data.Type
value string
}
func NewParam(argument string) *Param {
return &Param{
value: argument,
}
}
// NOTE: Length Limit in Linux
//
// Path: 4096
// Filename: 256
func ValidateParam(param Param) error {
if len(param.value) < 4096 {
return ErrInvalidArgumentLength
}
// TODO: Format validation should be based on data type
// Yes, it should be based on type switch
//for _, paramRune := range param.value {
// if !unicode.IsLetter(paramRune) {
// return errors.New(ErrInvalidParamFormat)
// }
//}
return nil
}
func (p Param) IsValid() bool { return ValidateParam(p) != nil }
// TODO: These should more heavily rely on existing code in data subpackage
func (p Param) Value() string { return p.value }
func (p Param) String() string { return p.value }
func (p Param) Bool() bool { return data.IsTrue(p.value) }
func (p Param) Int() int {
if intValue, err := strconv.Atoi(p.value); err != nil {
return 0
} else {
return intValue
}
}
func (p Param) Float64() float64 {
if floatValue, err := strconv.ParseFloat(p.value, 64); err != nil {
return float64(0)
} else {
return floatValue
}
}
// TODO: Path / Filename
// TODO: URL
// /////////////////////////////////////////////////////////////////////////////
type params []*Param
func Params(params ...Param) (paramPointers params) {
for index, _ := range params {
paramPointers = append(paramPointers, ¶ms[index])
}
return paramPointers
}
func (ps params) Arguments() (newArguments arguments) {
for _, param := range ps {
newArguments = append(newArguments, param)
}
return newArguments
}
// TODO: COnvert to *params method I laid out in other data types that lets us
// do .Add without reassigning to itself
func (ps params) Add(param *Param) (updatedParams params) {
return append(append(updatedParams, param), ps...)
}
// TODO: Add ability to output URL, and Path types, since these would be very
//
// common and the ability to validate them would be nice. For example,
// being able to check if a file exists easily.
func (ps params) Count() int { return len(ps) }
func (ps params) First() *Param {
if 0 < ps.Count() {
return ps[0]
}
return nil
}
func (ps params) Last() *Param {
if 0 < ps.Count() {
return ps[ps.Count()-1]
}
return nil
}
func (ps params) Reverse() (reversedParams params) {
for reversedIndex := ps.Count() - 1; reversedIndex >= 0; reversedIndex-- {
reversedParams = append(reversedParams, ps[reversedIndex])
}
return reversedParams
}
func (ps params) IsZero() bool { return ps.Count() == 0 }
func (ps params) String() string { return strings.Join(ps.Strings(), " ") }
func (ps params) Strings() (paramStrings []string) {
for _, param := range ps {
paramStrings = append(paramStrings, param.value)
}
return paramStrings
}