(content []byte)
| 101 | } |
| 102 | |
| 103 | func parseVariableValuesFromHCL(content []byte) ([]VariableValue, error) { |
| 104 | parser := hclparse.NewParser() |
| 105 | hclFile, diags := parser.ParseHCL(content, "file.hcl") |
| 106 | if diags.HasErrors() { |
| 107 | return nil, diags |
| 108 | } |
| 109 | |
| 110 | attrs, diags := hclFile.Body.JustAttributes() |
| 111 | if diags.HasErrors() { |
| 112 | return nil, diags |
| 113 | } |
| 114 | |
| 115 | stringData := map[string]string{} |
| 116 | for _, attribute := range attrs { |
| 117 | ctyValue, diags := attribute.Expr.Value(nil) |
| 118 | if diags.HasErrors() { |
| 119 | return nil, diags |
| 120 | } |
| 121 | |
| 122 | ctyType := ctyValue.Type() |
| 123 | switch { |
| 124 | case ctyType.Equals(cty.String): |
| 125 | stringData[attribute.Name] = ctyValue.AsString() |
| 126 | case ctyType.Equals(cty.Number): |
| 127 | stringData[attribute.Name] = ctyValue.AsBigFloat().String() |
| 128 | case ctyType.IsTupleType(): |
| 129 | // In case of tuples, Coder only supports the list(string) type. |
| 130 | var items []string |
| 131 | var err error |
| 132 | _ = ctyValue.ForEachElement(func(_, val cty.Value) (stop bool) { |
| 133 | if !val.Type().Equals(cty.String) { |
| 134 | err = xerrors.Errorf("unsupported tuple item type: %s ", val.GoString()) |
| 135 | return true |
| 136 | } |
| 137 | items = append(items, val.AsString()) |
| 138 | return false |
| 139 | }) |
| 140 | if err != nil { |
| 141 | return nil, err |
| 142 | } |
| 143 | |
| 144 | m, err := json.Marshal(items) |
| 145 | if err != nil { |
| 146 | return nil, err |
| 147 | } |
| 148 | stringData[attribute.Name] = string(m) |
| 149 | default: |
| 150 | return nil, xerrors.Errorf("unsupported value type (name: %s): %s", attribute.Name, ctyType.GoString()) |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return convertMapIntoVariableValues(stringData), nil |
| 155 | } |
| 156 | |
| 157 | // parseVariableValuesFromJSON converts the .tfvars.json content into template variables. |
| 158 | // The function visits only root-level properties as template variables do not support nested |
no test coverage detected