]> git.immae.eu Git - github/fretlink/terraform-provider-statuscake.git/blob - vendor/github.com/hashicorp/terraform-config-inspect/tfconfig/source_pos.go
Upgrade to 0.12
[github/fretlink/terraform-provider-statuscake.git] / vendor / github.com / hashicorp / terraform-config-inspect / tfconfig / source_pos.go
1 package tfconfig
2
3 import (
4 legacyhcltoken "github.com/hashicorp/hcl/hcl/token"
5 "github.com/hashicorp/hcl2/hcl"
6 )
7
8 // SourcePos is a pointer to a particular location in a source file.
9 //
10 // This type is embedded into other structs to allow callers to locate the
11 // definition of each described module element. The SourcePos of an element
12 // is usually the first line of its definition, although the definition can
13 // be a little "fuzzy" with JSON-based config files.
14 type SourcePos struct {
15 Filename string `json:"filename"`
16 Line int `json:"line"`
17 }
18
19 func sourcePos(filename string, line int) SourcePos {
20 return SourcePos{
21 Filename: filename,
22 Line: line,
23 }
24 }
25
26 func sourcePosHCL(rng hcl.Range) SourcePos {
27 // We intentionally throw away the column information here because
28 // current and legacy HCL both disagree on the definition of a column
29 // and so a line-only reference is the best granularity we can do
30 // such that the result is consistent between both parsers.
31 return SourcePos{
32 Filename: rng.Filename,
33 Line: rng.Start.Line,
34 }
35 }
36
37 func sourcePosLegacyHCL(pos legacyhcltoken.Pos, filename string) SourcePos {
38 useFilename := pos.Filename
39 // We'll try to use the filename given in legacy HCL position, but
40 // in practice there's no way to actually get this populated via
41 // the HCL API so it's usually empty except in some specialized
42 // situations, such as positions in error objects.
43 if useFilename == "" {
44 useFilename = filename
45 }
46 return SourcePos{
47 Filename: useFilename,
48 Line: pos.Line,
49 }
50 }