forked from muesli/beehive
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaceholders.go
128 lines (114 loc) · 2.78 KB
/
placeholders.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
/*
* Copyright (C) 2017 Christian Muehlhaeuser
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Christian Muehlhaeuser <[email protected]>
*/
// Package bees is Beehive's central module system
package bees
import (
"errors"
"fmt"
"strconv"
"strings"
)
// PlaceholderSlice is an array of Placeholder.
type PlaceholderSlice []Placeholder
// Placeholder used by ins & outs of a bee.
type Placeholder struct {
Name string
Type string
Value interface{}
}
// Value retrieves a value from a Placeholder slice.
func (ph PlaceholderSlice) Value(name string) interface{} {
for _, p := range ph {
if p.Name == name {
return p.Value
}
}
return nil
}
// Bind a value from a Placeholder slice.
func (ph PlaceholderSlice) Bind(name string, dst interface{}) error {
v := ph.Value(name)
if v == nil {
return errors.New("Placeholder with name " + name + " not found")
}
return ConvertValue(v, dst)
}
// ConvertValue tries to convert v to dst.
func ConvertValue(v interface{}, dst interface{}) error {
switch d := dst.(type) {
case *string:
switch vt := v.(type) {
case string:
*d = vt
case bool:
*d = strconv.FormatBool(vt)
case int64:
*d = strconv.FormatInt(vt, 10)
case int:
*d = strconv.FormatInt(int64(vt), 10)
default:
panic(fmt.Sprintf("Unhandled type %+v", vt))
}
case *bool:
switch vt := v.(type) {
case bool:
*d = vt
case string:
vt = strings.ToLower(vt)
if vt == "true" || vt == "on" || vt == "yes" || vt == "1" {
*d = true
}
case int64:
*d = vt > 0
case int:
*d = vt > 0
case float64:
*d = vt > 0
default:
panic(fmt.Sprintf("Unhandled type %+v", vt))
}
case *float64:
switch vt := v.(type) {
case float64:
*d = vt
case string:
x, _ := strconv.Atoi(vt)
*d = float64(x)
default:
panic(fmt.Sprintf("Unhandled type %+v", vt))
}
case *int:
switch vt := v.(type) {
case int:
*d = vt
case uint:
*d = int(vt)
case float64:
*d = int(vt)
case string:
*d, _ = strconv.Atoi(vt)
default:
panic(fmt.Sprintf("Unhandled type %+v", vt))
}
default:
panic(fmt.Sprintf("Unhandled dst type %+v", dst))
}
return nil
}