This commit is contained in:
Shreyas Goenka 2024-10-14 10:21:13 +02:00
parent f0e2981596
commit 5abb009c4f
No known key found for this signature in database
GPG Key ID: 92A07DF49CCB0622
4 changed files with 49 additions and 26 deletions

View File

@ -30,10 +30,14 @@ func createGlobError(v dyn.Value, p dyn.Path, message string) diag.Diagnostic {
} }
return diag.Diagnostic{ return diag.Diagnostic{
Severity: diag.Error, Severity: diag.Error,
Summary: fmt.Sprintf("%s: %s", source, message), Summary: fmt.Sprintf("%s: %s", source, message),
Locations: []dyn.Location{v.Location()}, LocationPathPairs: []diag.LocationPathPair{
Paths: []dyn.Path{p}, {
L: v.Location(),
P: p,
},
},
} }
} }

View File

@ -107,27 +107,24 @@ func validateSingleResourceDefined(configRoot dyn.Value, ext, typ string) diag.D
detail.WriteString(l) detail.WriteString(l)
} }
locations := []dyn.Location{} // TODO: test this.
paths := []dyn.Path{} locationPathPairs := []diag.LocationPathPair{}
for _, rr := range resources { for _, rr := range resources {
locations = append(locations, rr.value.Locations()...) for _, l := range rr.value.Locations() {
paths = append(paths, rr.path) locationPathPairs = append(locationPathPairs, diag.LocationPathPair{L: l, P: rr.path})
}
} }
// Sort the locations and paths to make the output deterministic. // Sort the location-path pairs to make the output deterministic.
sort.Slice(locations, func(i, j int) bool { sort.Slice(locationPathPairs, func(i, j int) bool {
return locations[i].String() < locations[j].String() return locationPathPairs[i].L.String() < locationPathPairs[j].L.String()
})
sort.Slice(paths, func(i, j int) bool {
return paths[i].String() < paths[j].String()
}) })
return diag.Diagnostics{ return diag.Diagnostics{
{ {
Severity: diag.Recommendation, Severity: diag.Recommendation,
Summary: fmt.Sprintf("define a single %s in a file with the %s extension.", strings.ReplaceAll(typ, "_", " "), ext), Summary: fmt.Sprintf("define a single %s in a file with the %s extension.", strings.ReplaceAll(typ, "_", " "), ext),
Detail: detail.String(), Detail: detail.String(),
Locations: locations, LocationPathPairs: locationPathPairs,
Paths: paths,
}, },
} }
} }

View File

@ -420,12 +420,23 @@ const warningTemplate = `{{ "Warning" | yellow }}: {{ .Summary }}
` `
// const recommendationTemplate = `{{ "Recommendation" | blue }}: {{ .Summary }}
// {{- range $index, $element := .Paths }}
// {{ if eq $index 0 }}at {{else}} {{ end}}{{ $element.String | green }}
// {{- end }}
// {{- range $index, $element := .Locations }}
// {{ if eq $index 0 }}in {{else}} {{ end}}{{ $element.String | cyan }}
// {{- end }}
// {{- if .Detail }}
// {{ .Detail }}
// {{- end }}
// `
const recommendationTemplate = `{{ "Recommendation" | blue }}: {{ .Summary }} const recommendationTemplate = `{{ "Recommendation" | blue }}: {{ .Summary }}
{{- range $index, $element := .Paths }} {{- range $index, $element := .LocationPathPairs}}
{{ if eq $index 0 }}at {{else}} {{ end}}{{ $element.String | green }} {{ if eq $index 0 }}at {{else}} {{ end}}{{ $element.L.String | green }} in {{ $element.P.String | cyan }}
{{- end }}
{{- range $index, $element := .Locations }}
{{ if eq $index 0 }}in {{else}} {{ end}}{{ $element.String | cyan }}
{{- end }} {{- end }}
{{- if .Detail }} {{- if .Detail }}

View File

@ -7,6 +7,15 @@ import (
"github.com/databricks/cli/libs/dyn" "github.com/databricks/cli/libs/dyn"
) )
// Convenience struct for retaining a 1:1 mapping between locations and paths.
type LocationPathPair struct {
// Locations are the source code locations associated with the diagnostic message.
L dyn.Location
// Paths are paths to the values in the configuration tree that the diagnostic is associated with.
P dyn.Path
}
type Diagnostic struct { type Diagnostic struct {
Severity Severity Severity Severity
@ -18,11 +27,13 @@ type Diagnostic struct {
// This may be multiple lines and may be nil. // This may be multiple lines and may be nil.
Detail string Detail string
// Locations are the source code locations associated with the diagnostic message. // LocationPathPairs are the source code locations and paths associated with the diagnostic message.
// It may be empty if there are no associated locations and paths.
LocationPathPairs []LocationPathPair
// It may be empty if there are no associated locations. // It may be empty if there are no associated locations.
Locations []dyn.Location Locations []dyn.Location
// Paths are paths to the values in the configuration tree that the diagnostic is associated with.
// It may be nil if there are no associated paths. // It may be nil if there are no associated paths.
Paths []dyn.Path Paths []dyn.Path