RegoVarPath will consume the following terms from the given rego Ref and return the remaining terms. If the path does not fully match, an error is returned. The first term must always be a Var.
(path []string, terms []*ast.Term)
| 36 | // return the remaining terms. If the path does not fully match, an error is |
| 37 | // returned. The first term must always be a Var. |
| 38 | func RegoVarPath(path []string, terms []*ast.Term) ([]*ast.Term, error) { |
| 39 | if len(terms) < len(path) { |
| 40 | return nil, xerrors.Errorf("path %s longer than rego path %s", path, terms) |
| 41 | } |
| 42 | |
| 43 | if len(terms) == 0 || len(path) == 0 { |
| 44 | return nil, xerrors.Errorf("path %s and rego path %s must not be empty", path, terms) |
| 45 | } |
| 46 | |
| 47 | varTerm, ok := terms[0].Value.(ast.Var) |
| 48 | if !ok { |
| 49 | return nil, xerrors.Errorf("expected var, got %T", terms[0]) |
| 50 | } |
| 51 | |
| 52 | if string(varTerm) != path[0] { |
| 53 | return nil, xerrors.Errorf("expected var %s, got %s", path[0], varTerm) |
| 54 | } |
| 55 | |
| 56 | for i := 1; i < len(path); i++ { |
| 57 | nextTerm, ok := terms[i].Value.(ast.String) |
| 58 | if !ok { |
| 59 | return nil, xerrors.Errorf("expected ast.string, got %T", terms[i]) |
| 60 | } |
| 61 | |
| 62 | if string(nextTerm) != path[i] { |
| 63 | return nil, xerrors.Errorf("expected string %s, got %s", path[i], nextTerm) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return terms[len(path):], nil |
| 68 | } |
| 69 | |
| 70 | var ( |
| 71 | _ VariableMatcher = astStringVar{} |
no test coverage detected