Centralize parser diagnostic translation
This commit is contained in:
37
parse.go
37
parse.go
@@ -33,6 +33,21 @@ type parsedSource struct {
|
||||
bytes int64
|
||||
}
|
||||
|
||||
// parserDiagnostics translates adapter issues into detached, ordered public records.
|
||||
func parserDiagnostics(sourceID string, issues []yamladapter.Issue) []Diagnostic {
|
||||
diagnostics := make([]Diagnostic, len(issues))
|
||||
for index, issue := range issues {
|
||||
diagnostics[index] = Diagnostic{
|
||||
Code: Code(issue.Code), Severity: SeverityError, Message: issue.Message,
|
||||
Primary: &Location{SourceID: sourceID, Line: issue.Line, Column: issue.Column},
|
||||
}
|
||||
for _, related := range issue.Related {
|
||||
diagnostics[index].Related = append(diagnostics[index].Related, publicLocation(related))
|
||||
}
|
||||
}
|
||||
return canonicalDiagnostics(diagnostics)
|
||||
}
|
||||
|
||||
func parseSource(source Source, sourceID string, limits parseLimits) (parsedSource, []Diagnostic) {
|
||||
read, diagnostics := readSource(source, sourceID, limits.source)
|
||||
if len(diagnostics) != 0 || read.info == nil {
|
||||
@@ -41,27 +56,7 @@ func parseSource(source Source, sourceID string, limits parseLimits) (parsedSour
|
||||
|
||||
root, issues := yamladapter.Parse(bytes.NewReader(read.data), sourceID, limits.document)
|
||||
if len(issues) != 0 {
|
||||
diagnostics := make([]Diagnostic, len(issues))
|
||||
for i, issue := range issues {
|
||||
diagnostics[i] = Diagnostic{
|
||||
Code: Code(issue.Code),
|
||||
Severity: SeverityError,
|
||||
Message: issue.Message,
|
||||
Primary: &Location{
|
||||
SourceID: sourceID,
|
||||
Line: issue.Line,
|
||||
Column: issue.Column,
|
||||
},
|
||||
}
|
||||
for _, related := range issue.Related {
|
||||
diagnostics[i].Related = append(diagnostics[i].Related, Location{
|
||||
SourceID: related.SourceID,
|
||||
Line: related.Line,
|
||||
Column: related.Column,
|
||||
})
|
||||
}
|
||||
}
|
||||
return parsedSource{source: read.info, bytes: int64(len(read.data))}, canonicalDiagnostics(diagnostics)
|
||||
return parsedSource{source: read.info, bytes: int64(len(read.data))}, parserDiagnostics(sourceID, issues)
|
||||
}
|
||||
return parsedSource{document: root, source: read.info, bytes: int64(len(read.data))}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user