/
config_props.go
40 lines (32 loc) · 1.05 KB
/
config_props.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
package format
import (
"errors"
"github.com/containrrr/shoutrrr/pkg/types"
r "reflect"
)
// GetConfigPropFromString deserializes a config property from a string representation using the ConfigProp interface
func GetConfigPropFromString(structType r.Type, value string) (r.Value, error) {
valuePtr := r.New(structType)
configProp, ok := valuePtr.Interface().(types.ConfigProp)
if !ok {
return r.Value{}, errors.New("struct field cannot be used as a prop")
}
if err := configProp.SetFromProp(value); err != nil {
return r.Value{}, err
}
return valuePtr, nil
}
// GetConfigPropString serializes a config property to a string representation using the ConfigProp interface
func GetConfigPropString(propPtr r.Value) (string, error) {
if propPtr.Kind() != r.Ptr {
propVal := propPtr
propPtr = r.New(propVal.Type())
propPtr.Elem().Set(propVal)
}
if propPtr.CanInterface() {
if configProp, ok := propPtr.Interface().(types.ConfigProp); ok {
return configProp.GetPropValue()
}
}
return "", errors.New("struct field cannot be used as a prop")
}