-
Notifications
You must be signed in to change notification settings - Fork 123
/
source.go
68 lines (53 loc) · 1.42 KB
/
source.go
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
// Copyright 2021-2024 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
package parser
import (
"github.com/goccy/go-yaml"
"github.com/goccy/go-yaml/ast"
"github.com/goccy/go-yaml/printer"
sourcev1 "github.com/cerbos/cerbos/api/genpb/cerbos/source/v1"
)
type SourceCtx struct {
doc *ast.DocumentNode
*sourcev1.SourceContext
}
func newSourceCtx(srcCtx *sourcev1.SourceContext, doc *ast.DocumentNode) SourceCtx {
return SourceCtx{doc: doc, SourceContext: srcCtx}
}
func NewEmptySourceCtx() SourceCtx {
return SourceCtx{}
}
func (sc SourceCtx) StartPosition() *sourcev1.Position {
if sp := sc.GetStartPosition(); sp != nil {
return &sourcev1.Position{
Line: sp.GetLine(),
Column: sp.GetColumn(),
}
}
return nil
}
func (sc SourceCtx) PositionForProtoPath(path string) *sourcev1.Position {
return sc.GetFieldPositions()[path]
}
func (sc SourceCtx) ContextForYAMLPath(path string) string {
if sc.doc == nil || path == "" {
return ""
}
yamlPath, err := yaml.PathString(path)
if err != nil {
return ""
}
node, err := yamlPath.FilterNode(sc.doc.Body)
if err != nil {
return ""
}
var errPrinter printer.Printer
return errPrinter.PrintErrorToken(node.GetToken(), false)
}
func (sc SourceCtx) PositionAndContextForProtoPath(path string) (pos *sourcev1.Position, context string) {
pos = sc.PositionForProtoPath(path)
if pos != nil {
context = sc.ContextForYAMLPath(pos.GetPath())
}
return pos, context
}