-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.go
91 lines (81 loc) · 1.86 KB
/
load.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
package config
type property interface {
required() bool
}
type propertyBase struct {
Property string
_Required bool
}
func (pb *propertyBase) required() bool {
return pb._Required
}
// StringProperty holds a string mapping
type StringProperty struct {
propertyBase
mapTo *string
}
// StrProp constructs StringProperty
func StrProp(property string, mapTo *string, required bool) *StringProperty {
return &StringProperty{
propertyBase{
property,
required,
},
mapTo,
}
}
// IntProperty holds an int mapping
type IntProperty struct {
propertyBase
mapTo *int
}
// IntProp constructs IntProperty
func IntProp(property string, mapTo *int, required bool) *IntProperty {
return &IntProperty{
propertyBase{
property,
required,
},
mapTo,
}
}
// LoadProperties will try load properties from source into variable reference
func LoadProperties(typedSource *TypedSource, properties ...property) error {
for _, property := range properties {
switch v := property.(type) {
case *StringProperty:
if err := mapStringProperty(typedSource, v); err != nil {
return err
}
case *IntProperty:
if err := mapIntProperty(typedSource, v); err != nil {
return err
}
default:
return NewErrUnknownPropertyType(property)
}
}
return nil
}
func mapStringProperty(typedSource *TypedSource, property *StringProperty) error {
result, err := typedSource.GetString(property.Property)
if err == nil {
*property.mapTo = result
return nil
}
if _, notPresent := err.(*ErrPropertyNotSet); notPresent && !property.required() {
return nil
}
return err
}
func mapIntProperty(typedSource *TypedSource, property *IntProperty) error {
result, err := typedSource.GetInt(property.Property)
if err == nil {
*property.mapTo = result
return nil
}
if _, notPresent := err.(*ErrPropertyNotSet); notPresent && !property.required() {
return nil
}
return err
}