• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ory / keto / 4564745533

30 Mar 2023 01:13PM UTC coverage: 80.557%. First build
4564745533

push

github

GitHub
chore(deps): bump alpine from 3.17.2 to 3.17.3 in /.docker (#1296)

5552 of 6892 relevant lines covered (80.56%)

501.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

38.75
/internal/schema/parse_errors.go
1
// Copyright © 2023 Ory Corp
2
// SPDX-License-Identifier: Apache-2.0
3

4
package schema
5

6
import (
7
        "fmt"
8
        "strings"
9
        "unicode"
10

11
        "github.com/ory/keto/ketoapi"
12
        opl "github.com/ory/keto/proto/ory/keto/opl/v1alpha1"
13
)
14

15
type (
16
        ParseError struct {
17
                msg  string
18
                item item
19
                p    *parser
20
        }
21
)
22

23
func max(a, b int) int {
×
24
        if a > b {
×
25
                return a
×
26
        }
×
27
        return b
×
28
}
29

30
func (e *ParseError) Error() string {
×
31
        var s strings.Builder
×
32
        start := e.toSrcPos(e.item.Start)
×
33
        end := e.toSrcPos(e.item.End)
×
34
        rows := e.rows()
×
35
        startLineIdx := max(start.Line-2, 0)
×
36
        errorLineIdx := max(start.Line-1, 0)
×
37

×
38
        s.WriteString(fmt.Sprintf("error from %d:%d to %d:%d: %s\n\n",
×
39
                start.Line, start.Col,
×
40
                end.Line, end.Col,
×
41
                e.msg))
×
42

×
43
        if len(rows) < start.Line {
×
44
                s.WriteString("meta error: could not find source position in input\n")
×
45
                return s.String()
×
46
        }
×
47

48
        for line := startLineIdx; line <= errorLineIdx; line++ {
×
49
                s.WriteString(fmt.Sprintf("%4d | %s\n", line, rows[line]))
×
50
        }
×
51
        s.WriteString("     | ")
×
52
        for i, r := range rows[errorLineIdx] {
×
53
                switch {
×
54
                case start.Col == i:
×
55
                        s.WriteRune('^')
×
56
                case start.Col <= i && i <= end.Col-1:
×
57
                        s.WriteRune('~')
×
58
                case unicode.IsSpace(r):
×
59
                        s.WriteRune(r)
×
60
                default:
×
61
                        s.WriteRune(' ')
×
62
                }
63
        }
64
        s.WriteRune('\n')
×
65

×
66
        if errorLineIdx+1 < len(rows) {
×
67
                s.WriteString(fmt.Sprintf("%4d | %s\n", errorLineIdx+1, rows[errorLineIdx+1]))
×
68
                s.WriteRune('\n')
×
69
        }
×
70

71
        return s.String()
×
72
}
73

74
func (e *ParseError) ToAPI() *ketoapi.ParseError {
11✔
75
        return &ketoapi.ParseError{
11✔
76
                Message: e.msg,
11✔
77
                Start:   e.toSrcPos(e.item.Start),
11✔
78
                End:     e.toSrcPos(e.item.End),
11✔
79
        }
11✔
80
}
11✔
81
func (e *ParseError) ToProto() *opl.ParseError {
6✔
82
        start := e.toSrcPos(e.item.Start)
6✔
83
        end := e.toSrcPos(e.item.End)
6✔
84
        return &opl.ParseError{
6✔
85
                Message: e.msg,
6✔
86
                Start: &opl.SourcePosition{
6✔
87
                        Line:   uint32(start.Line),
6✔
88
                        Column: uint32(start.Col),
6✔
89
                },
6✔
90
                End: &opl.SourcePosition{
6✔
91
                        Line:   uint32(end.Line),
6✔
92
                        Column: uint32(end.Col),
6✔
93
                },
6✔
94
        }
6✔
95
}
6✔
96

97
// toSrcPos converts the given position in the input to a Line and column
98
// number.
99
func (e *ParseError) toSrcPos(pos int) (srcPos ketoapi.SourcePosition) {
34✔
100
        srcPos.Line = 1
34✔
101
        for _, c := range e.p.lexer.input {
362✔
102
                srcPos.Col++
328✔
103
                pos--
328✔
104
                if pos <= 0 {
362✔
105
                        break
34✔
106
                }
107
                if c == '\n' {
294✔
108
                        srcPos.Line++
×
109
                        srcPos.Col = 0
×
110
                }
×
111
        }
112
        return
34✔
113
}
114

115
func (e *ParseError) rows() []string {
×
116
        return strings.Split(e.p.lexer.input, "\n")
×
117
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc