parseVariableValuesFromJSON converts the .tfvars.json content into template variables. The function visits only root-level properties as template variables do not support nested structures.
(content []byte)
| 158 | // The function visits only root-level properties as template variables do not support nested |
| 159 | // structures. |
| 160 | func parseVariableValuesFromJSON(content []byte) ([]VariableValue, error) { |
| 161 | var data map[string]interface{} |
| 162 | err := json.Unmarshal(content, &data) |
| 163 | if err != nil { |
| 164 | return nil, err |
| 165 | } |
| 166 | |
| 167 | stringData := map[string]string{} |
| 168 | for key, value := range data { |
| 169 | switch value.(type) { |
| 170 | case string, int, bool: |
| 171 | stringData[key] = fmt.Sprintf("%v", value) |
| 172 | default: |
| 173 | m, err := json.Marshal(value) |
| 174 | if err != nil { |
| 175 | return nil, err |
| 176 | } |
| 177 | stringData[key] = string(m) |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return convertMapIntoVariableValues(stringData), nil |
| 182 | } |
| 183 | |
| 184 | func convertMapIntoVariableValues(m map[string]string) []VariableValue { |
| 185 | var parsed []VariableValue |
no test coverage detected