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{
Severity: diag.Error,
Summary: fmt.Sprintf("%s: %s", source, message),
Locations: []dyn.Location{v.Location()},
Paths: []dyn.Path{p},
Severity: diag.Error,
Summary: fmt.Sprintf("%s: %s", source, message),
LocationPathPairs: []diag.LocationPathPair{
{
L: v.Location(),
P: p,
},
},
}
}

View File

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

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 }}
{{- 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 }}
{{- range $index, $element := .LocationPathPairs}}
{{ if eq $index 0 }}at {{else}} {{ end}}{{ $element.L.String | green }} in {{ $element.P.String | cyan }}
{{- end }}
{{- if .Detail }}

View File

@ -7,6 +7,15 @@ import (
"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 {
Severity Severity
@ -18,11 +27,13 @@ type Diagnostic struct {
// This may be multiple lines and may be nil.
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.
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.
Paths []dyn.Path