MCPcopy
hub / github.com/hashicorp/hcl / showVarRefsJSON

Function showVarRefsJSON

cmd/hcldec/main.go:262–365  ·  view source on GitHub ↗
(vars []hcl.Traversal, ctx *hcl.EvalContext)

Source from the content-addressed store, hash-verified

260}
261
262func showVarRefsJSON(vars []hcl.Traversal, ctx *hcl.EvalContext) error {
263 type PosJSON struct {
264 Line int `json:"line"`
265 Column int `json:"column"`
266 Byte int `json:"byte"`
267 }
268 type RangeJSON struct {
269 Filename string `json:"filename"`
270 Start PosJSON `json:"start"`
271 End PosJSON `json:"end"`
272 }
273 type StepJSON struct {
274 Kind string `json:"kind"`
275 Name string `json:"name,omitempty"`
276 Key json.RawMessage `json:"key,omitempty"`
277 Range RangeJSON `json:"range"`
278 }
279 type TraversalJSON struct {
280 RootName string `json:"root_name"`
281 Value json.RawMessage `json:"value,omitempty"`
282 Steps []StepJSON `json:"steps"`
283 Range RangeJSON `json:"range"`
284 }
285
286 ret := make([]TraversalJSON, 0, len(vars))
287 for _, traversal := range vars {
288 tJSON := TraversalJSON{
289 Steps: make([]StepJSON, 0, len(traversal)),
290 }
291
292 for _, step := range traversal {
293 var sJSON StepJSON
294 rng := step.SourceRange()
295 sJSON.Range.Filename = rng.Filename
296 sJSON.Range.Start.Line = rng.Start.Line
297 sJSON.Range.Start.Column = rng.Start.Column
298 sJSON.Range.Start.Byte = rng.Start.Byte
299 sJSON.Range.End.Line = rng.End.Line
300 sJSON.Range.End.Column = rng.End.Column
301 sJSON.Range.End.Byte = rng.End.Byte
302 switch ts := step.(type) {
303 case hcl.TraverseRoot:
304 sJSON.Kind = "root"
305 sJSON.Name = ts.Name
306 tJSON.RootName = ts.Name
307 case hcl.TraverseAttr:
308 sJSON.Kind = "attr"
309 sJSON.Name = ts.Name
310 case hcl.TraverseIndex:
311 sJSON.Kind = "index"
312 src, err := ctyjson.Marshal(ts.Key, ts.Key.Type())
313 if err == nil {
314 sJSON.Key = json.RawMessage(src)
315 }
316 default:
317 // Should never get here, since the above should be exhaustive
318 // for all possible traversal step types.
319 sJSON.Kind = "(unknown)"

Callers 1

realmainFunction · 0.85

Calls 4

HasErrorsMethod · 0.80
SourceRangeMethod · 0.65
TypeMethod · 0.45
TraverseAbsMethod · 0.45

Tested by

no test coverage detected