forked from rbaliyan/config
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
120 lines (101 loc) · 2.6 KB
/
types.go
File metadata and controls
120 lines (101 loc) · 2.6 KB
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
package config
import "fmt"
// Type represents the type of a configuration value.
type Type int
const (
// TypeUnknown indicates an unknown or unsupported type.
TypeUnknown Type = iota
// TypeInt represents an integer value.
TypeInt
// TypeFloat represents a floating-point value.
TypeFloat
// TypeString represents a string value.
TypeString
// TypeBool represents a boolean value.
TypeBool
// TypeMapStringInt represents a map[string]int value.
TypeMapStringInt
// TypeMapStringFloat represents a map[string]float64 value.
TypeMapStringFloat
// TypeMapStringString represents a map[string]string value.
TypeMapStringString
// TypeListInt represents a []int value.
TypeListInt
// TypeListFloat represents a []float64 value.
TypeListFloat
// TypeListString represents a []string value.
TypeListString
// TypeCustom represents a custom type that requires Unmarshal.
TypeCustom
)
// String returns the string representation of the type.
func (t Type) String() string {
switch t {
case TypeInt:
return "int"
case TypeFloat:
return "float"
case TypeString:
return "string"
case TypeBool:
return "bool"
case TypeMapStringInt:
return "map[string]int"
case TypeMapStringFloat:
return "map[string]float64"
case TypeMapStringString:
return "map[string]string"
case TypeListInt:
return "[]int"
case TypeListFloat:
return "[]float64"
case TypeListString:
return "[]string"
case TypeCustom:
return "custom"
default:
return fmt.Sprintf("unknown(%d)", t)
}
}
// ParseType parses a string into a Type.
// Returns TypeUnknown for unrecognized strings.
func ParseType(s string) Type {
switch s {
case "int":
return TypeInt
case "float":
return TypeFloat
case "string":
return TypeString
case "bool":
return TypeBool
case "map[string]int":
return TypeMapStringInt
case "map[string]float64":
return TypeMapStringFloat
case "map[string]string":
return TypeMapStringString
case "[]int":
return TypeListInt
case "[]float64":
return TypeListFloat
case "[]string":
return TypeListString
case "custom":
return TypeCustom
default:
return TypeUnknown
}
}
// IsPrimitive returns true if the type is a primitive (int, float, string, bool).
func (t Type) IsPrimitive() bool {
return t == TypeInt || t == TypeFloat || t == TypeString || t == TypeBool
}
// IsMap returns true if the type is a map type.
func (t Type) IsMap() bool {
return t == TypeMapStringInt || t == TypeMapStringFloat || t == TypeMapStringString
}
// IsList returns true if the type is a list type.
func (t Type) IsList() bool {
return t == TypeListInt || t == TypeListFloat || t == TypeListString
}