68 lines
1.7 KiB
Go
68 lines
1.7 KiB
Go
package configkit
|
|
|
|
import (
|
|
"bytes"
|
|
|
|
"gitea.maximumdirect.net/eric/ConfigKit/internal/document"
|
|
yamladapter "gitea.maximumdirect.net/eric/ConfigKit/internal/yaml"
|
|
)
|
|
|
|
const (
|
|
maxDocumentDepth = 256
|
|
maxDocumentNodes = 1_000_000
|
|
)
|
|
|
|
type parseLimits struct {
|
|
source sourceLimits
|
|
document yamladapter.Limits
|
|
}
|
|
|
|
func defaultParseLimits() parseLimits {
|
|
return parseLimits{
|
|
source: defaultSourceLimits(),
|
|
document: yamladapter.Limits{
|
|
Depth: maxDocumentDepth,
|
|
Nodes: maxDocumentNodes,
|
|
},
|
|
}
|
|
}
|
|
|
|
type parsedSource struct {
|
|
document *document.Node
|
|
source *SourceInfo
|
|
bytes int64
|
|
}
|
|
|
|
func parseSource(source Source, sourceID string, limits parseLimits) (parsedSource, []Diagnostic) {
|
|
read, diagnostics := readSource(source, sourceID, limits.source)
|
|
if len(diagnostics) != 0 || read.info == nil {
|
|
return parsedSource{source: read.info}, diagnostics
|
|
}
|
|
|
|
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{document: root, source: read.info, bytes: int64(len(read.data))}, nil
|
|
}
|