| 1 | // Copyright 2018 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/ |
| 16 | |
| 17 | import ( |
| 18 | "fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/" |
| 19 | "stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/" |
| 20 | "unicode/utf8scip-go gomod github.com/golang/go/src go1.23.0 `unicode/utf8`/" |
| 21 | ) |
| 22 | |
| 23 | // NewError creates an error associated with an expression id with the given message at the given location. |
| 24 | func NewErrorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewError().(id int64, message string, location Locationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Location#) *Errorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Error# { |
| 25 | return &Errorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Error#{Messagescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Error#Message.: message, Locationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Error#Location.: location, ExprIDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Error#ExprID.: id} |
| 26 | } |
| 27 | |
| 28 | // Error type which references an expression id, a location within source, and a message. |
| 29 | type Errorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Error# struct { |
| 30 | Locationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Error#Location. Locationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Location# |
| 31 | Messagescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Error#Message. string |
| 32 | ExprIDscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Error#ExprID. int64 |
| 33 | } |
| 34 | |
| 35 | const ( |
| 36 | dotscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/dot. = "." |
| 37 | indscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/ind. = "^" |
| 38 | wideDotscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/wideDot. = "\uff0e" |
| 39 | wideIndscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/wideInd. = "\uff3e" |
| 40 | |
| 41 | // maxSnippetLength is the largest number of characters which can be rendered in an error message snippet. |
| 42 | maxSnippetLengthscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/maxSnippetLength. = 16384 |
| 43 | ) |
| 44 | |
| 45 | // ToDisplayString decorates the error message with the source location. |
| 46 | func (e *Errorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Error#) ToDisplayStringscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Error#ToDisplayString().(source Sourcescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#) string { |
| 47 | var result = fmtscip-go gomod github.com/golang/go/src go1.23.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.23.0 fmt/Sprintf().("ERROR: %s:%d:%d: %s", |
| 48 | source.Descriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#Description.(), |
| 49 | e.Locationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Error#Location..Linescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Location#Line.(), |
| 50 | e.Locationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Error#Location..Columnscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Location#Column.()+1, // add one to the 0-based column for display |
| 51 | e.Messagescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Error#Message.) |
| 52 | if snippet, found := source.Snippetscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Source#Snippet.(e.Locationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Error#Location..Linescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Location#Line.()); found && len(snippet) <= maxSnippetLengthscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/maxSnippetLength. { |
| 53 | snippet := stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/.Replacescip-go gomod github.com/golang/go/src go1.23.0 strings/Replace().(snippet, "\t", " ", -1) |
| 54 | srcLine := "\n | " + snippet |
| 55 | var bytes = []byte(snippet) |
| 56 | var indLine = "\n | " |
| 57 | for i := 0; i < e.Locationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Error#Location..Columnscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Location#Column.() && len(bytes) > 0; i++ { |
| 58 | _, sz := utf8scip-go gomod github.com/golang/go/src go1.23.0 `unicode/utf8`/.DecodeRunescip-go gomod github.com/golang/go/src go1.23.0 `unicode/utf8`/DecodeRune().(bytes) |
| 59 | bytes = bytes[sz:] |
| 60 | if sz > 1 { |
| 61 | indLine += wideDotscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/wideDot. |
| 62 | } else { |
| 63 | indLine += dotscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/dot. |
| 64 | } |
| 65 | } |
| 66 | if _, sz := utf8scip-go gomod github.com/golang/go/src go1.23.0 `unicode/utf8`/.DecodeRunescip-go gomod github.com/golang/go/src go1.23.0 `unicode/utf8`/DecodeRune().(bytes); sz > 1 { |
| 67 | indLine += wideIndscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/wideInd. |
| 68 | } else { |
| 69 | indLine += indscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/ind. |
| 70 | } |
| 71 | result += srcLine + indLine |
| 72 | } |
| 73 | return result |
| 74 | } |