| 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 common defines types and utilities common to expression parsing, |
| 16 | // checking, and interpretation |
| 17 | package commonscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/ |
| 18 | |
| 19 | import ( |
| 20 | "stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/" |
| 21 | "unicodescip-go gomod github.com/golang/go/src go1.23.0 unicode/" |
| 22 | ) |
| 23 | |
| 24 | // DocKind indicates the type of documentation element. |
| 25 | type DocKindscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/DocKind# int |
| 26 | |
| 27 | const ( |
| 28 | // DocEnv represents environment variable documentation. |
| 29 | DocEnvscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/DocEnv. DocKindscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/DocKind# = iota + 1 |
| 30 | // DocFunction represents function documentation. |
| 31 | DocFunctionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/DocFunction. |
| 32 | // DocOverload represents function overload documentation. |
| 33 | DocOverloadscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/DocOverload. |
| 34 | // DocVariable represents variable documentation. |
| 35 | DocVariablescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/DocVariable. |
| 36 | // DocMacro represents macro documentation. |
| 37 | DocMacroscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/DocMacro. |
| 38 | // DocExample represents example documentation. |
| 39 | DocExamplescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/DocExample. |
| 40 | ) |
| 41 | |
| 42 | // Doc holds the documentation details for a specific program element like |
| 43 | // a variable, function, macro, or example. |
| 44 | type Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc# struct { |
| 45 | // Kind specifies the type of documentation element (e.g., Function, Variable). |
| 46 | Kindscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Kind. DocKindscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/DocKind# |
| 47 | |
| 48 | // Name is the identifier of the documented element (e.g., function name, variable name). |
| 49 | Namescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Name. string |
| 50 | |
| 51 | // Type is the data type associated with the element, primarily used for variables. |
| 52 | Typescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Type. string |
| 53 | |
| 54 | // Signature represents the function or overload signature. |
| 55 | Signaturescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Signature. string |
| 56 | |
| 57 | // Description holds the textual description of the element, potentially spanning multiple lines. |
| 58 | Descriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Description. string |
| 59 | |
| 60 | // Children holds nested documentation elements, such as overloads for a function |
| 61 | // or examples for a function/macro. |
| 62 | Childrenscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Children. []*Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc# |
| 63 | } |
| 64 | |
| 65 | // MultilineDescription combines multiple lines into a newline separated string. |
| 66 | func MultilineDescriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/MultilineDescription().(lines ...string) string { |
| 67 | return stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.23.0 strings/Join().(lines, "\n") |
| 68 | } |
| 69 | |
| 70 | // ParseDescription takes a single string containing newline characters and splits |
| 71 | // it into a multiline description. All empty lines will be skipped. |
| 72 | // |
| 73 | // Returns an empty string if the input string is empty. |
| 74 | func ParseDescriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/ParseDescription().(doc string) string { |
| 75 | var lines []string |
| 76 | if len(doc) != 0 { |
| 77 | // Split the input string by newline characters. |
| 78 | for _, line := range stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.23.0 strings/Split().(doc, "\n") { |
| 79 | l := stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/.TrimRightFuncscip-go gomod github.com/golang/go/src go1.23.0 strings/TrimRightFunc().(line, unicodescip-go gomod github.com/golang/go/src go1.23.0 unicode/.IsSpacescip-go gomod github.com/golang/go/src go1.23.0 unicode/IsSpace().) |
| 80 | if len(l) == 0 { |
| 81 | continue |
| 82 | } |
| 83 | lines = append(lines, l) |
| 84 | } |
| 85 | } |
| 86 | // Return an empty slice if the input is empty. |
| 87 | return MultilineDescriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/MultilineDescription().(lines...) |
| 88 | } |
| 89 | |
| 90 | // ParseDescriptions splits a documentation string into multiple multi-line description |
| 91 | // sections, using blank lines as delimiters. |
| 92 | func ParseDescriptionsscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/ParseDescriptions().(doc string) []string { |
| 93 | var examples []string |
| 94 | if len(doc) != 0 { |
| 95 | lines := stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/.Splitscip-go gomod github.com/golang/go/src go1.23.0 strings/Split().(doc, "\n") |
| 96 | lineStart := 0 |
| 97 | for i, l := range lines { |
| 98 | // Trim trailing whitespace to identify effectively blank lines. |
| 99 | l = stringsscip-go gomod github.com/golang/go/src go1.23.0 strings/.TrimRightFuncscip-go gomod github.com/golang/go/src go1.23.0 strings/TrimRightFunc().(l, unicodescip-go gomod github.com/golang/go/src go1.23.0 unicode/.IsSpacescip-go gomod github.com/golang/go/src go1.23.0 unicode/IsSpace().) |
| 100 | // If a line is blank, it marks the end of the current section. |
| 101 | if len(l) == 0 { |
| 102 | // Start the next section after the blank line. |
| 103 | ex := lines[lineStart:i] |
| 104 | if len(ex) != 0 { |
| 105 | examples = append(examples, MultilineDescriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/MultilineDescription().(ex...)) |
| 106 | } |
| 107 | lineStart = i + 1 |
| 108 | } |
| 109 | } |
| 110 | // Append the last section if it wasn't terminated by a blank line. |
| 111 | if lineStart < len(lines) { |
| 112 | examples = append(examples, MultilineDescriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/MultilineDescription().(lines[lineStart:]...)) |
| 113 | } |
| 114 | } |
| 115 | return examples |
| 116 | } |
| 117 | |
| 118 | // NewVariableDoc creates a new Doc struct specifically for documenting a variable. |
| 119 | func NewVariableDocscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewVariableDoc().(name, celType, description string) *Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc# { |
| 120 | return &Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#{ |
| 121 | Kindscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Kind.: DocVariablescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/DocVariable., |
| 122 | Namescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Name.: name, |
| 123 | Typescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Type.: celType, |
| 124 | Descriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Description.: ParseDescriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/ParseDescription().(description), |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // NewFunctionDoc creates a new Doc struct for documenting a function. |
| 129 | func NewFunctionDocscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewFunctionDoc().(name, description string, overloads ...*Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#) *Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc# { |
| 130 | return &Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#{ |
| 131 | Kindscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Kind.: DocFunctionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/DocFunction., |
| 132 | Namescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Name.: name, |
| 133 | Descriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Description.: ParseDescriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/ParseDescription().(description), |
| 134 | Childrenscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Children.: overloads, |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // NewOverloadDoc creates a new Doc struct for a function example. |
| 139 | func NewOverloadDocscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewOverloadDoc().(id, signature string, examples ...*Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#) *Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc# { |
| 140 | return &Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#{ |
| 141 | Kindscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Kind.: DocOverloadscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/DocOverload., |
| 142 | Namescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Name.: id, |
| 143 | Signaturescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Signature.: signature, |
| 144 | Childrenscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Children.: examples, |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // NewMacroDoc creates a new Doc struct for documenting a macro. |
| 149 | func NewMacroDocscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewMacroDoc().(name, description string, examples ...*Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#) *Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc# { |
| 150 | return &Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#{ |
| 151 | Kindscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Kind.: DocMacroscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/DocMacro., |
| 152 | Namescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Name.: name, |
| 153 | Descriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Description.: ParseDescriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/ParseDescription().(description), |
| 154 | Childrenscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Children.: examples, |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // NewExampleDoc creates a new Doc struct specifically for holding an example. |
| 159 | func NewExampleDocscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/NewExampleDoc().(ex string) *Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc# { |
| 160 | return &Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#{ |
| 161 | Kindscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Kind.: DocExamplescip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/DocExample., |
| 162 | Descriptionscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc#Description.: ex, |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // Documentor is an interface for types that can provide their own documentation. |
| 167 | type Documentorscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Documentor# interface { |
| 168 | // Documentation returns the documentation coded by the DocKind to assist |
| 169 | // with text formatting. |
| 170 | Documentationscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Documentor#Documentation.() *Docscip-go gomod github.com/google/cel-go . `github.com/google/cel-go/common`/Doc# |
| 171 | } |