| 1 | package hcl |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "github.com/hashicorp/hcl/v2" |
| 7 | "github.com/hashicorp/hcl/v2/gohcl" |
| 8 | "github.com/hashicorp/hcl/v2/hclsyntax" |
| 9 | "github.com/tomwright/dasel/v3/model" |
| 10 | "github.com/tomwright/dasel/v3/parsing" |
| 11 | "github.com/zclconf/go-cty/cty" |
| 12 | ) |
| 13 | |
| 14 | func newHCLReader(options parsing.ReaderOptions) (parsing.Reader, error) { |
| 15 | return &hclReader{ |
| 16 | alwaysReadLabelsToSlice: options.Ext["hcl-block-format"] == "array", |
| 17 | }, nil |
| 18 | } |
| 19 | |
| 20 | type hclReader struct { |
| 21 | alwaysReadLabelsToSlice bool |
| 22 | } |
| 23 | |
| 24 | // Read reads a value from a byte slice. |
| 25 | // Reads the HCL data into a model that follows the HCL JSON spec. |
| 26 | // See https://github.com/hashicorp/hcl/blob/main/json%2Fspec.md |
| 27 | func (r *hclReader) Read(data []byte) (*model.Value, error) { |
| 28 | f, _ := hclsyntax.ParseConfig(data, "input", hcl.InitialPos) |
| 29 | |
| 30 | body, ok := f.Body.(*hclsyntax.Body) |
| 31 | if !ok { |
| 32 | return nil, fmt.Errorf("failed to assert file body type") |
| 33 | } |
| 34 | |
| 35 | return r.decodeHCLBody(body) |
| 36 | } |
| 37 | |
| 38 | func (r *hclReader) decodeHCLBody(body *hclsyntax.Body) (*model.Value, error) { |
| 39 | res := model.NewMapValue() |
| 40 | var err error |
| 41 | |
| 42 | for _, attr := range body.Attributes { |
| 43 | val, err := r.decodeHCLExpr(attr.Expr) |
| 44 | if err != nil { |
| 45 | return nil, fmt.Errorf("failed to decode attr %q: %w", attr.Name, err) |
| 46 | } |
| 47 | |
| 48 | if err := res.SetMapKey(attr.Name, val); err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | res, err = r.decodeHCLBodyBlocks(body, res) |
| 54 | if err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | |
| 58 | return res, nil |
| 59 | } |
| 60 | |
| 61 | func (r *hclReader) decodeHCLBodyBlocks(body *hclsyntax.Body, res *model.Value) (*model.Value, error) { |
| 62 | for _, block := range body.Blocks { |
| 63 | if err := r.decodeHCLBlock(block, res); err != nil { |
| 64 | return nil, err |
| 65 | } |
| 66 | } |
| 67 | return res, nil |
| 68 | } |
| 69 | |
| 70 | func (r *hclReader) decodeHCLBlock(block *hclsyntax.Block, res *model.Value) error { |
| 71 | key := block.Type |
| 72 | v := res |
| 73 | for _, label := range block.Labels { |
| 74 | exists, err := v.MapKeyExists(key) |
| 75 | if err != nil { |
| 76 | return err |
| 77 | } |
| 78 | |
| 79 | if exists { |
| 80 | keyV, err := v.GetMapKey(key) |
| 81 | if err != nil { |
| 82 | return err |
| 83 | } |
| 84 | v = keyV |
| 85 | } else { |
| 86 | keyV := model.NewMapValue() |
| 87 | if err := v.SetMapKey(key, keyV); err != nil { |
| 88 | return err |
| 89 | } |
| 90 | v = keyV |
| 91 | } |
| 92 | |
| 93 | key = label |
| 94 | } |
| 95 | |
| 96 | body, err := r.decodeHCLBody(block.Body) |
| 97 | if err != nil { |
| 98 | return err |
| 99 | } |
| 100 | |
| 101 | exists, err := v.MapKeyExists(key) |
| 102 | if err != nil { |
| 103 | return err |
| 104 | } |
| 105 | if exists { |
| 106 | keyV, err := v.GetMapKey(key) |
| 107 | if err != nil { |
| 108 | return err |
| 109 | } |
| 110 | |
| 111 | switch keyV.Type() { |
| 112 | case model.TypeSlice: |
| 113 | if err := keyV.Append(body); err != nil { |
| 114 | return err |
| 115 | } |
| 116 | case model.TypeMap: |
| 117 | // Previous value was a map. |
| 118 | // Create a new slice containing the previous map and the new map. |
| 119 | newKeyV := model.NewSliceValue() |
| 120 | previousKeyV, err := keyV.Copy() |
| 121 | if err != nil { |
| 122 | return err |
| 123 | } |
| 124 | if err := newKeyV.Append(previousKeyV); err != nil { |
| 125 | return err |
| 126 | } |
| 127 | if err := newKeyV.Append(body); err != nil { |
| 128 | return err |
| 129 | } |
| 130 | if err := keyV.Set(newKeyV); err != nil { |
| 131 | return err |
| 132 | } |
| 133 | default: |
| 134 | return fmt.Errorf("unexpected type: %s", keyV.Type()) |
| 135 | } |
| 136 | } else { |
| 137 | if r.alwaysReadLabelsToSlice { |
| 138 | slice := model.NewSliceValue() |
| 139 | if err := slice.Append(body); err != nil { |
| 140 | return err |
| 141 | } |
| 142 | if err := v.SetMapKey(key, slice); err != nil { |
| 143 | return err |
| 144 | } |
| 145 | } else { |
| 146 | if err := v.SetMapKey(key, body); err != nil { |
| 147 | return err |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return nil |
| 153 | } |
| 154 | |
| 155 | func (r *hclReader) decodeHCLExpr(expr hcl.Expression) (*model.Value, error) { |
| 156 | source := cty.Value{} |
| 157 | _ = gohcl.DecodeExpression(expr, nil, &source) |
| 158 | |
| 159 | return r.decodeCtyValue(source) |
| 160 | } |
| 161 | |
| 162 | func (r *hclReader) decodeCtyValue(source cty.Value) (res *model.Value, err error) { |
| 163 | defer func() { |
| 164 | r := recover() |
| 165 | if r != nil { |
| 166 | err = fmt.Errorf("failed to decode: %v", r) |
| 167 | return |
| 168 | } |
| 169 | }() |
| 170 | if source.IsNull() { |
| 171 | return model.NewNullValue(), nil |
| 172 | } |
| 173 | |
| 174 | sourceT := source.Type() |
| 175 | if sourceT.HasDynamicTypes() { |
| 176 | // TODO : Handle DynamicPseudoType. |
| 177 | // I haben't found a clear way to do this. |
| 178 | return model.NewNullValue(), nil |
| 179 | } |
| 180 | switch { |
| 181 | case sourceT.IsListType(), sourceT.IsTupleType(): |
| 182 | res = model.NewSliceValue() |
| 183 | it := source.ElementIterator() |
| 184 | for it.Next() { |
| 185 | k, v := it.Element() |
| 186 | // We don't need the index as they should be in order. |
| 187 | // Just validates the key is correct. |
| 188 | _, _ = k.AsBigFloat().Float64() |
| 189 | |
| 190 | val, err := r.decodeCtyValue(v) |
| 191 | if err != nil { |
| 192 | return nil, fmt.Errorf("failed to decode tuple value: %w", err) |
| 193 | } |
| 194 | |
| 195 | if err := res.Append(val); err != nil { |
| 196 | return nil, err |
| 197 | } |
| 198 | } |
| 199 | return res, nil |
| 200 | case sourceT.IsMapType(), sourceT.IsObjectType(), sourceT.IsSetType(): |
| 201 | v := model.NewMapValue() |
| 202 | it := source.ElementIterator() |
| 203 | for it.Next() { |
| 204 | k, el := it.Element() |
| 205 | if k.Type() != cty.String { |
| 206 | return nil, fmt.Errorf("object key must be a string") |
| 207 | } |
| 208 | kStr := k.AsString() |
| 209 | |
| 210 | elVal, err := r.decodeCtyValue(el) |
| 211 | if err != nil { |
| 212 | return nil, fmt.Errorf("failed to decode object value: %w", err) |
| 213 | } |
| 214 | |
| 215 | if err := v.SetMapKey(kStr, elVal); err != nil { |
| 216 | return nil, err |
| 217 | } |
| 218 | } |
| 219 | return v, nil |
| 220 | case sourceT.IsPrimitiveType(): |
| 221 | switch sourceT { |
| 222 | case cty.String: |
| 223 | v := source.AsString() |
| 224 | return model.NewStringValue(v), nil |
| 225 | case cty.Bool: |
| 226 | v := source.True() |
| 227 | return model.NewBoolValue(v), nil |
| 228 | case cty.Number: |
| 229 | v := source.AsBigFloat() |
| 230 | f64, _ := v.Float64() |
| 231 | if v.IsInt() { |
| 232 | return model.NewIntValue(int64(f64)), nil |
| 233 | } |
| 234 | return model.NewFloatValue(f64), nil |
| 235 | default: |
| 236 | return nil, fmt.Errorf("unhandled primitive type %q", source.Type()) |
| 237 | } |
| 238 | default: |
| 239 | return nil, fmt.Errorf("unsupported type: %s", sourceT.FriendlyName()) |
| 240 | } |
| 241 | } |