Debian Codegraph @

debian/missing-sources/github.com/google/cel-go/policy/source.go

raw symbols SCIP
1 // Copyright 2024 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 // https://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 policy
16
17 import (
18 "github.com/google/cel-go/common"
19 )
20
21 // ByteSource converts a byte sequence and location description to a model.Source.
22 func ByteSource(contents []byte, location string) *Source {
23 return StringSource(string(contents), location)
24 }
25
26 // StringSource converts a string and location description to a model.Source.
27 func StringSource(contents, location string) *Source {
28 return &Source{
29 Source: common.NewStringSource(contents, location),
30 }
31 }
32
33 // Source represents the contents of a single source file.
34 type Source struct {
35 common.Source
36 }
37
38 // Relative produces a RelativeSource object for the content provided at the absolute location
39 // within the parent Source as indicated by the line and column.
40 func (src *Source) Relative(content string, line, col int) *RelativeSource {
41 return &RelativeSource{
42 Source: src.Source,
43 localSrc: common.NewStringSource(content, src.Description()),
44 absLoc: common.NewLocation(line, col),
45 }
46 }
47
48 // RelativeSource represents an embedded source element within a larger source.
49 type RelativeSource struct {
50 common.Source
51 localSrc common.Source
52 absLoc common.Location
53 }
54
55 // Content returns the embedded source snippet.
56 func (rel *RelativeSource) Content() string {
57 return rel.localSrc.Content()
58 }
59
60 // OffsetLocation returns the absolute location given the relative offset, if found.
61 func (rel *RelativeSource) OffsetLocation(offset int32) (common.Location, bool) {
62 absOffset, found := rel.Source.LocationOffset(rel.absLoc)
63 if !found {
64 return common.NoLocation, false
65 }
66 return rel.Source.OffsetLocation(absOffset + offset)
67 }