1- // Package eval provides a simple interface for evaluating a policy node in a given environment.
1+ // Package eval provides a simple interface for evaluating or partially evaluating a policy node in a given environment.
22package eval
33
44import (
@@ -15,3 +15,68 @@ func Eval(n ast.IsNode, env Env) (types.Value, error) {
1515 evaler := eval .ToEval (n )
1616 return evaler .Eval (env )
1717}
18+
19+ // PartialPolicy returns a partially evaluated version of the policy and a boolean indicating if the policy should be kept.
20+ // (Policies that are determined to evaluate to false are not kept.)
21+ //
22+ // it is supposed to use `PartialPolicy` to partially evaluate a policy, and then use `PolicyToNode` to compile the policy to a node.
23+ // but you can also use `PartialPolicy` directly.
24+ //
25+ // All the env parts (PARC) must be specified, but you can
26+ // specify `Variable` as `Variable("principal")` or `Variable("action")` or `Variable("resource")` or `Variable("context")`.
27+ // also you can specify part of Context to be a `Variable`, such as `key` in `Context` could be
28+ // `
29+ //
30+ // context := types.NewRecord(types.RecordMap{
31+ // "key": Variable("key"),
32+ // })
33+ //
34+ // `
35+ //
36+ // when the node is kept, it can be one of three kinds:
37+ // 1. it is a `ValueNode`, and Must be `ast.True()` (e.g. `ast.True()`)
38+ // 2. it is a `Node` contains `Variable` (e.g. `ast.Permit().When(ast.Context().Access("key").Equal(ast.Long(42)))`)
39+ // 3. it is a `Node` contains `PartialError` (e.g. `ast.ExtensionCall(partialErrorName, ast.String("type error: expected comparable value, got string"))`)
40+ //
41+ // you can use the partial evaluation result `ast.Node` to do any additional work you want
42+ // for example, you can convert it to an sql query.
43+ // in which case the variable should be a column name and binary node should be an sql expression.
44+ func PartialPolicy (env Env , p * ast.Policy ) (policy * ast.Policy , keep bool ) {
45+ return eval .PartialPolicy (env , p )
46+ }
47+
48+ // PolicyToNode returns a node compiled from a policy.
49+ func PolicyToNode (p * ast.Policy ) ast.Node {
50+ return eval .PolicyToNode (p )
51+ }
52+
53+ // PartialError returns a node that represents a partial error.
54+ func PartialError (err error ) ast.IsNode {
55+ return eval .PartialError (err )
56+ }
57+
58+ // ToPartialError returns the error if the node is a partial error.
59+ func ToPartialError (n ast.IsNode ) (err error , ok bool ) {
60+ return eval .ToPartialError (n )
61+ }
62+
63+ // Variable is a variable in the policy.
64+ func Variable (v types.String ) types.Value {
65+ return eval .Variable (v )
66+ }
67+
68+ // ToVariable converts a value to a variable.
69+ func ToVariable (v types.Value ) (types.String , bool ) {
70+ if ent , ok := v .(types.EntityUID ); ok {
71+ return eval .ToVariable (ent )
72+ }
73+ return "" , false
74+ }
75+
76+ // TypeName returns the type name of a value.
77+ func TypeName (v types.Value ) string {
78+ return eval .TypeName (v )
79+ }
80+
81+ // ErrType is the error type for type errors.
82+ var ErrType = eval .ErrType
0 commit comments