github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/resources/environment_variables_resource.go (about)

     1  package resources
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     7  	"code.cloudfoundry.org/cli/types"
     8  )
     9  
    10  type EnvironmentVariables map[string]types.FilteredString
    11  
    12  func (variables EnvironmentVariables) MarshalJSON() ([]byte, error) {
    13  	ccEnvVars := struct {
    14  		Var map[string]types.FilteredString `json:"var"`
    15  	}{
    16  		Var: variables,
    17  	}
    18  
    19  	return json.Marshal(ccEnvVars)
    20  }
    21  
    22  func (variables *EnvironmentVariables) UnmarshalJSON(data []byte) error {
    23  	var ccEnvVars struct {
    24  		Var map[string]types.FilteredInterface `json:"var"`
    25  	}
    26  
    27  	err := cloudcontroller.DecodeJSON(data, &ccEnvVars)
    28  
    29  	*variables = EnvironmentVariables{}
    30  
    31  	for envVarName, envVarValue := range ccEnvVars.Var {
    32  		var valueAsString string
    33  		if str, ok := envVarValue.Value.(string); ok {
    34  			valueAsString = str
    35  		} else {
    36  			bytes, err := json.Marshal(envVarValue.Value)
    37  			if err != nil {
    38  				return err
    39  			}
    40  			valueAsString = string(bytes)
    41  		}
    42  
    43  		(*variables)[envVarName] = types.FilteredString{Value: valueAsString, IsSet: true}
    44  	}
    45  
    46  	return err
    47  }