forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment_variables_resource.go
47 lines (36 loc) · 1.01 KB
/
environment_variables_resource.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
package resources
import (
"encoding/json"
"code.cloudfoundry.org/cli/api/cloudcontroller"
"code.cloudfoundry.org/cli/types"
)
type EnvironmentVariables map[string]types.FilteredString
func (variables EnvironmentVariables) MarshalJSON() ([]byte, error) {
ccEnvVars := struct {
Var map[string]types.FilteredString `json:"var"`
}{
Var: variables,
}
return json.Marshal(ccEnvVars)
}
func (variables *EnvironmentVariables) UnmarshalJSON(data []byte) error {
var ccEnvVars struct {
Var map[string]types.FilteredInterface `json:"var"`
}
err := cloudcontroller.DecodeJSON(data, &ccEnvVars)
*variables = EnvironmentVariables{}
for envVarName, envVarValue := range ccEnvVars.Var {
var valueAsString string
if str, ok := envVarValue.Value.(string); ok {
valueAsString = str
} else {
bytes, err := json.Marshal(envVarValue.Value)
if err != nil {
return err
}
valueAsString = string(bytes)
}
(*variables)[envVarName] = types.FilteredString{Value: valueAsString, IsSet: true}
}
return err
}