aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/hashicorp/hcl2/hcl/diagnostic.go
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/github.com/hashicorp/hcl2/hcl/diagnostic.go')
-rw-r--r--vendor/github.com/hashicorp/hcl2/hcl/diagnostic.go103
1 files changed, 103 insertions, 0 deletions
diff --git a/vendor/github.com/hashicorp/hcl2/hcl/diagnostic.go b/vendor/github.com/hashicorp/hcl2/hcl/diagnostic.go
new file mode 100644
index 0000000..6ecf744
--- /dev/null
+++ b/vendor/github.com/hashicorp/hcl2/hcl/diagnostic.go
@@ -0,0 +1,103 @@
1package hcl
2
3import (
4 "fmt"
5)
6
7// DiagnosticSeverity represents the severity of a diagnostic.
8type DiagnosticSeverity int
9
10const (
11 // DiagInvalid is the invalid zero value of DiagnosticSeverity
12 DiagInvalid DiagnosticSeverity = iota
13
14 // DiagError indicates that the problem reported by a diagnostic prevents
15 // further progress in parsing and/or evaluating the subject.
16 DiagError
17
18 // DiagWarning indicates that the problem reported by a diagnostic warrants
19 // user attention but does not prevent further progress. It is most
20 // commonly used for showing deprecation notices.
21 DiagWarning
22)
23
24// Diagnostic represents information to be presented to a user about an
25// error or anomoly in parsing or evaluating configuration.
26type Diagnostic struct {
27 Severity DiagnosticSeverity
28
29 // Summary and detail contain the English-language description of the
30 // problem. Summary is a terse description of the general problem and
31 // detail is a more elaborate, often-multi-sentence description of
32 // the probem and what might be done to solve it.
33 Summary string
34 Detail string
35 Subject *Range
36 Context *Range
37}
38
39// Diagnostics is a list of Diagnostic instances.
40type Diagnostics []*Diagnostic
41
42// error implementation, so that diagnostics can be returned via APIs
43// that normally deal in vanilla Go errors.
44//
45// This presents only minimal context about the error, for compatibility
46// with usual expectations about how errors will present as strings.
47func (d *Diagnostic) Error() string {
48 return fmt.Sprintf("%s: %s; %s", d.Subject, d.Summary, d.Detail)
49}
50
51// error implementation, so that sets of diagnostics can be returned via
52// APIs that normally deal in vanilla Go errors.
53func (d Diagnostics) Error() string {
54 count := len(d)
55 switch {
56 case count == 0:
57 return "no diagnostics"
58 case count == 1:
59 return d[0].Error()
60 default:
61 return fmt.Sprintf("%s, and %d other diagnostic(s)", d[0].Error(), count-1)
62 }
63}
64
65// Append appends a new error to a Diagnostics and return the whole Diagnostics.
66//
67// This is provided as a convenience for returning from a function that
68// collects and then returns a set of diagnostics:
69//
70// return nil, diags.Append(&hcl.Diagnostic{ ... })
71//
72// Note that this modifies the array underlying the diagnostics slice, so
73// must be used carefully within a single codepath. It is incorrect (and rude)
74// to extend a diagnostics created by a different subsystem.
75func (d Diagnostics) Append(diag *Diagnostic) Diagnostics {
76 return append(d, diag)
77}
78
79// Extend concatenates the given Diagnostics with the receiver and returns
80// the whole new Diagnostics.
81//
82// This is similar to Append but accepts multiple diagnostics to add. It has
83// all the same caveats and constraints.
84func (d Diagnostics) Extend(diags Diagnostics) Diagnostics {
85 return append(d, diags...)
86}
87
88// HasErrors returns true if the receiver contains any diagnostics of
89// severity DiagError.
90func (d Diagnostics) HasErrors() bool {
91 for _, diag := range d {
92 if diag.Severity == DiagError {
93 return true
94 }
95 }
96 return false
97}
98
99// A DiagnosticWriter emits diagnostics somehow.
100type DiagnosticWriter interface {
101 WriteDiagnostic(*Diagnostic) error
102 WriteDiagnostics(Diagnostics) error
103}