-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproptagconfig.go
More file actions
59 lines (48 loc) · 1.56 KB
/
Copy pathproptagconfig.go
File metadata and controls
59 lines (48 loc) · 1.56 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
package proptagconfig
import (
"reflect"
"errors"
"strconv"
"github.com/magiconair/properties"
"github.com/Experticity/tagconfig"
)
// PropTagConfig implements the TagValueGetter to allow for populating a
// struct via "prop" struct tagconfig. It also implements TagValueSetter which
// allows for the struct to populate properties, which is basically the inverse
// of the TagValueGetter interface.
type PropTagConfig struct {
*properties.Properties
}
var ErrUnsupportedSetType = errors.New("Received an unsupported type to set")
func (pv *PropTagConfig) TagName() string {
return "props"
}
func (pv *PropTagConfig) Get(key string, _ reflect.StructField) string {
return pv.Properties.GetString(key, "")
}
// Set is used to set properties based on values provided by the struct. For
// this iteration, it only supports a couple of major types, but if there's a
// need for this to grow, we can make this more sophisticated.
func (pv *PropTagConfig) Set(key string, value interface{}, _ reflect.StructField) error {
var s string
switch v := value.(type) {
case string:
s = v
case bool:
s = strconv.FormatBool(v)
case int:
s = strconv.Itoa(v)
default:
return ErrUnsupportedSetType
}
_, _, err := pv.Properties.Set(key, s)
return err
}
// PopulatePropertiesFromStruct is used to create *properties.Properties based
// off of fields/data within a struct.
func PopulatePropertiesFromStruct(value interface{}) (*properties.Properties, error) {
p := properties.NewProperties()
pt := &PropTagConfig{p}
err := tagconfig.PopulateExternalSource(pt, value)
return p, err
}