github.com/Burmuley/terraform@v0.11.12-beta1/tfdiags/hcl.go (about) 1 package tfdiags 2 3 import ( 4 "github.com/hashicorp/hcl2/hcl" 5 ) 6 7 // hclDiagnostic is a Diagnostic implementation that wraps a HCL Diagnostic 8 type hclDiagnostic struct { 9 diag *hcl.Diagnostic 10 } 11 12 var _ Diagnostic = hclDiagnostic{} 13 14 func (d hclDiagnostic) Severity() Severity { 15 switch d.diag.Severity { 16 case hcl.DiagWarning: 17 return Warning 18 default: 19 return Error 20 } 21 } 22 23 func (d hclDiagnostic) Description() Description { 24 return Description{ 25 Summary: d.diag.Summary, 26 Detail: d.diag.Detail, 27 } 28 } 29 30 func (d hclDiagnostic) Source() Source { 31 var ret Source 32 if d.diag.Subject != nil { 33 rng := SourceRangeFromHCL(*d.diag.Subject) 34 ret.Subject = &rng 35 } 36 if d.diag.Context != nil { 37 rng := SourceRangeFromHCL(*d.diag.Context) 38 ret.Context = &rng 39 } 40 return ret 41 } 42 43 // SourceRangeFromHCL constructs a SourceRange from the corresponding range 44 // type within the HCL package. 45 func SourceRangeFromHCL(hclRange hcl.Range) SourceRange { 46 return SourceRange{ 47 Filename: hclRange.Filename, 48 Start: SourcePos{ 49 Line: hclRange.Start.Line, 50 Column: hclRange.Start.Column, 51 Byte: hclRange.Start.Byte, 52 }, 53 End: SourcePos{ 54 Line: hclRange.End.Line, 55 Column: hclRange.End.Column, 56 Byte: hclRange.End.Byte, 57 }, 58 } 59 } 60 61 // ToHCL constructs a HCL Range from the receiving SourceRange. This is the 62 // opposite of SourceRangeFromHCL. 63 func (r SourceRange) ToHCL() hcl.Range { 64 return hcl.Range{ 65 Filename: r.Filename, 66 Start: hcl.Pos{ 67 Line: r.Start.Line, 68 Column: r.Start.Column, 69 Byte: r.Start.Byte, 70 }, 71 End: hcl.Pos{ 72 Line: r.End.Line, 73 Column: r.End.Column, 74 Byte: r.End.Byte, 75 }, 76 } 77 }