Debian Codegraph @

report/report.go

raw symbols SCIP
1 // Copyright 2019 Red Hat, Inc
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.)
14
15 package report
16
17 import (
18 "fmt"
19
20 "github.com/coreos/vcontext/path"
21 "github.com/coreos/vcontext/tree"
22 )
23
24 // EntryKind represents an Entry's severity.
25 type EntryKind interface {
26 String() string
27 IsFatal() bool
28 }
29
30 // Report is a collection of information from validating a struct.
31 type Report struct {
32 Entries []Entry
33 }
34
35 // Merge adds the entries from child to r.
36 func (r *Report) Merge(child Report) {
37 r.Entries = append(r.Entries, child.Entries...)
38 }
39
40 // getDeepestNode returns the deepest node matching the context.
41 func getDeepestNode(n tree.Node, c path.ContextPath) tree.Node {
42 if child, err := n.Get(c); err != nil {
43 return getDeepestNode(n, c.Pop())
44 } else {
45 return child
46 }
47 }
48
49 // Correlate takes a node tree and populates the markers in the report's entries
50 // based on the entries' context.
51 func (r *Report) Correlate(n tree.Node) {
52 for i, e := range r.Entries {
53 r.Entries[i].Marker = getDeepestNode(n, e.Context).GetMarker()
54 }
55 }
56
57 // IsFatal returns true if any entries are fatal.
58 func (r Report) IsFatal() bool {
59 for _, e := range r.Entries {
60 if e.Kind.IsFatal() {
61 return true
62 }
63 }
64 return false
65 }
66
67 func (r Report) String() string {
68 str := ""
69 for _, e := range r.Entries {
70 str += e.String() + "\n"
71 }
72 return str
73 }
74
75 // Entry represents one error or message from validation.
76 type Entry struct {
77 // Kind is the severity of the message.
78 Kind EntryKind
79 Message string
80
81 // Context is the logical location of the error.
82 Context path.ContextPath
83
84 // Marker is the literal location in a json or yaml blob of the error.
85 Marker tree.Marker
86 }
87
88 func (e Entry) String() string {
89 at := ""
90 switch {
91 case e.Marker.StartP != nil && e.Context.Len() != 0:
92 at = fmt.Sprintf(" at %s, %s", e.Context.String(), e.Marker.String())
93 case e.Marker.StartP != nil:
94 at = fmt.Sprintf(" at %s", e.Marker.String())
95 case e.Context.Len() != 0:
96 at = fmt.Sprintf(" at %s", e.Context.String())
97 }
98
99 return fmt.Sprintf("%s%s: %s", e.Kind.String(), at, e.Message)
100 }
101
102 // Kind is a default set of EntryKind.
103 type Kind int
104
105 const (
106 Error Kind = iota
107 Warn Kind = iota
108 Info Kind = iota
109 )
110
111 func (k Kind) String() string {
112 switch k {
113 case Error:
114 return "error"
115 case Warn:
116 return "warning"
117 case Info:
118 return "info"
119 default:
120 return "unknown severity"
121 }
122 }
123
124 func (k Kind) IsFatal() bool {
125 return k == Error
126 }
127
128 func (r *Report) AddOn(c path.ContextPath, err error, k EntryKind) {
129 if err == nil {
130 return
131 }
132 r.Entries = append(r.Entries, Entry{
133 Message: err.Error(),
134 Context: c.Copy(),
135 Kind: k,
136 })
137 }
138
139 // AddOnError adds err to report with kind "Error" if err is not nil.
140 func (r *Report) AddOnError(c path.ContextPath, err error) {
141 r.AddOn(c, err, Error)
142
143 }
144
145 // AddOnWarn adds err to report with kind "Warning" if err is not nil.
146 func (r *Report) AddOnWarn(c path.ContextPath, err error) {
147 r.AddOn(c, err, Warn)
148 }
149
150 // AddOnInfo adds err to report with kind "Info" if err is not nil.
151 func (r *Report) AddOnInfo(c path.ContextPath, err error) {
152 r.AddOn(c, err, Info)
153 }