forked from pufferpanel/pufferpanel
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvariable.go
More file actions
61 lines (54 loc) · 1.31 KB
/
variable.go
File metadata and controls
61 lines (54 loc) · 1.31 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
package pufferpanel
import (
"encoding/json"
"github.com/spf13/cast"
)
type Variable struct {
Type
Value interface{} `json:"value"`
Display string `json:"display,omitempty"`
Description string `json:"desc,omitempty"`
Required bool `json:"required"`
Internal bool `json:"internal,omitempty"`
UserEditable bool `json:"userEdit"`
Options []VariableOption `json:"options,omitempty"`
} //@name Variable
type variableAlias Variable
type VariableOption struct {
Value interface{} `json:"value"`
Display string `json:"display"`
} //@name VariableOption
func (v *Variable) UnmarshalJSON(data []byte) (err error) {
aux := variableAlias{}
if err = json.Unmarshal(data, &aux); err != nil {
return
}
if aux.Type.Type == "" {
aux.Type = Type{Type: "string"}
}
//default any null value to empty string
if aux.Value == nil {
aux.Value = ""
}
//convert variable to correct typing
switch aux.Type.Type {
case "integer":
{
aux.Value, err = cast.ToIntE(aux.Value)
if err != nil {
var str string
if str, err = cast.ToStringE(aux.Value); err == nil {
if str == "" {
aux.Value = 0
}
}
}
}
case "boolean":
{
aux.Value, err = cast.ToBoolE(aux.Value)
}
}
*v = Variable(aux)
return
}