-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.go
136 lines (128 loc) · 4.11 KB
/
type.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
package data
// TODO: Perhaps have built-in
type Type int
// We doon't want too offer too many options, because that goes out of scope,
// but providing the tools that will enable validation for 98% of the input is
// desirable if its not too much. Since it will enable developers too have input
// validation and consistent input across all applications, even simple scripts
// with little cost. Like MAC could be included, but its not nearly as common as
// IP or Filename as a datatype. We could do a automated survey of GNU coreutils to
// try to determine the most common types in the future with greater certainty.
const (
Bool Type = iota
Int
Float
String
Directory
Filename
Filenames // Via globbing or comma separated values
URL
Port
IPv4
IPv6
)
// TODO: This should migrate into its own package (the generic equivialence
// helpers) the idea would be to provide a collection of helpers to make all of
// our code cleaner and more expressive in the same way rails extends the
// epxressiveness of default ruby
// These may seem pointless but they will also simplify validation of values and
// provide helpers for developers to simplify validation
//
// Alias (For more expressive code)
///////////////////////////////////////////////////////////////////////////////
//var Blank = ""
//var Whitespace = " "
//var Tab = "\t"
//var NewLine = "\n"
//var True = true
//var False = false
//
// Transform
///////////////////////////////////////////////////////////////////////////////
//
// Validate
///////////////////////////////////////////////////////////////////////////////
// Encode
///////////////////////////////////////////////////////////////////////////////
// Normally convention is not to use "to" but that is regarding methods, if we
// migrate this to a more generic flexible data type for holding variable user
// input. We can migrate
//func (self Data) String() string {
// return fmt.Sprintf(self)
//}
//
//// String Subtypes
//func (self Data) Directory() string { return self.(string) }
//func (self Data) Filename() string { return self.(string) }
//func (self Data) Filenames() string { return self.(string) }
//func (self Data) URL() string { return self.(string) }
//func (self Data) IPv4() string { return self.(string) }
//func (self Data) IPv6() string { return self.(string) }
//
//func (self Data) Int() int {
// intValue, err := strconv.Atoi(self.(string))
// if err != nil {
// return 0
// } else {
// return intValue
// }
//}
//
//// Int Subtypes
//func (self Data) Port() int { return toInt(value) }
//
//func (self Data) Float() float64 {
// floatParts := strings.Split(value.(string), ".")
// if len(floatParts) > 1 {
// floatValue, err := strconv.ParseFloat(value.(string), len(floatParts[1]))
// if err != nil {
// return float64(0.00)
// } else {
// return floatValue
// }
// }
// return float64(0)
//}
//
//func (self Data) Bool() bool {
// for _, trueValue := range trueValues {
// if value.(string) == trueValue {
// return true
// }
// }
// return false
//}
//
////
//// Public Methods
/////////////////////////////////////////////////////////////////////////////////
//// TODO: Basic validation should move here, and basic conversion (which is in
//// flags since this is where it orginated, but it became clear it would also be
//// important for parameters) [BUT maybe parameters don't have a type? Just hand
//// off the string, just seems if we are dealing with flag type we should go
//// ahead and extend this to parameters, but it may be wise to just ignore both]
//func (self Data) Valid(flagType DataType) (bool, error) {
// switch flagType {
// case Bool:
// boolValues := append(trueValues, falseValues...)
// for _, boolValue := range boolValues {
// if boolValue == value {
// return true, nil
// }
// }
// return false, errors.New("[error] could not parse valid boolean value")
// //case Int:
// //case String:
// //case Directory:
// case Filename:
// _, err := os.Stat(value.(string))
// return (err == nil), nil
// //case Filenames:
// //case URL:
// //case IPv4:
// //case IPv6:
// //case Port:
// default:
// return false, errors.New("[error] failed to parse data type")
// }
//}