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