-
Notifications
You must be signed in to change notification settings - Fork 13
/
obj_to_struct.go
41 lines (34 loc) · 931 Bytes
/
obj_to_struct.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
package types
import (
"github.com/cloudfoundry/bosh-utils/errors"
"gopkg.in/yaml.v2"
)
func stringInArray(key string, list []string) bool {
for _, value := range list {
if key == value {
return true
}
}
return false
}
func objToStruct(input interface{}, str interface{}, supportedParameters []string) error {
valBytes, err := yaml.Marshal(input)
if err != nil {
return errors.WrapErrorf(err, "Expected input to be serializable")
}
parametersMap := make(map[string]interface{})
err = yaml.Unmarshal(valBytes, parametersMap)
if err != nil {
return errors.WrapErrorf(err, "Expected input to be deserializable")
}
for key := range parametersMap {
if !stringInArray(key, supportedParameters) {
return errors.Errorf("Unsupported parameter '%s'", key)
}
}
err = yaml.Unmarshal(valBytes, str)
if err != nil {
return errors.WrapErrorf(err, "Expected input to be deserializable")
}
return nil
}