MCPcopy Index your code
hub / github.com/coder/coder / convertTerm

Function convertTerm

coderd/rbac/regosql/compile.go:171–231  ·  view source on GitHub ↗
(cfg ConvertConfig, term *ast.Term)

Source from the content-addressed store, hash-verified

169}
170
171func convertTerm(cfg ConvertConfig, term *ast.Term) (sqltypes.Node, error) {
172 source := sqltypes.RegoSource(term.String())
173 switch t := term.Value.(type) {
174 case ast.Var:
175 // All vars should be contained in ast.Ref's.
176 return nil, xerrors.New("var not yet supported")
177 case ast.Ref:
178 if len(t) == 0 {
179 // A reference with no text is a variable with no name?
180 // This makes no sense.
181 return nil, xerrors.New("empty ref not supported")
182 }
183
184 if cfg.VariableConverter == nil {
185 return nil, xerrors.New("no variable converter provided to handle variables")
186 }
187
188 // The structure of references is as follows:
189 // 1. All variables start with a regoAst.Var as the first term.
190 // 2. The next term is either a regoAst.String or a regoAst.Var.
191 // - regoAst.String if a static field name or index.
192 // - regoAst.Var if the field reference is a variable itself. Such as
193 // the wildcard "[_]"
194 // 3. Repeat 1-2 until the end of the reference.
195 node, ok := cfg.VariableConverter.ConvertVariable(t)
196 if !ok {
197 return nil, xerrors.Errorf("variable %q cannot be converted", t.String())
198 }
199 return node, nil
200 case ast.String:
201 return sqltypes.String(string(t)), nil
202 case ast.Number:
203 return sqltypes.Number(source, json.Number(t)), nil
204 case ast.Boolean:
205 return sqltypes.Bool(bool(t)), nil
206 case *ast.Array:
207 elems := make([]sqltypes.Node, 0, t.Len())
208 for i := 0; i < t.Len(); i++ {
209 value, err := convertTerm(cfg, t.Elem(i))
210 if err != nil {
211 return nil, xerrors.Errorf("array element %d in %q: %w", i, t.String(), err)
212 }
213 elems = append(elems, value)
214 }
215 return sqltypes.Array(source, elems...)
216 case ast.Object:
217 return nil, xerrors.New("object not yet supported")
218 case ast.Set:
219 // Just treat a set like an array for now.
220 arr := t.Sorted()
221 return convertTerm(cfg, &ast.Term{
222 Value: arr,
223 Location: term.Location,
224 })
225 case ast.Call:
226 // This is a function call
227 return convertCall(cfg, t)
228 default:

Callers 2

convertExpressionFunction · 0.85
convertTermsFunction · 0.85

Calls 12

RegoSourceTypeAlias · 0.92
StringFunction · 0.92
NumberFunction · 0.92
BoolFunction · 0.92
ArrayFunction · 0.92
convertCallFunction · 0.85
NewMethod · 0.65
ConvertVariableMethod · 0.65
StringMethod · 0.45
ErrorfMethod · 0.45
NumberMethod · 0.45
LenMethod · 0.45

Tested by

no test coverage detected