Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Builtins emit default values in JSON results (#507)
The `ds.checks` builtin returns an array of `CheckResponse` messages. Each element has a boolean `check` field indicating the outcome of one check. However, because `false` is the default value for `bool` the marshaled JSON object doesn't include the `check` field at all when it is false. Dealing with missing fields is difficult in Rego (I couldn't figure it out). The code below takes a resource context of the form: ```json { "object_type": "", "checks": [ {"object_id": "", "relation": ""} ] } ``` It computes an array of results of the form: ```json [ {"object_id": "", "relation": "", "allowed": true/false} ] ``` If default values are omitted from the returned results, the array will only include checks that evaluated to true. ```rego raw := ds.checks({ "default": { "object_type": input.resource.object_type, "subject_type": "user", "subject_id": input.user.id }, "checks": checks }) results := x { x := [ {"object_id": check.object_id, "relation": check.relation, "allowed": result.check } | check := checks[i] result = raw[i] ] } ``` This commit adds `EmitDefaultValues: true` to the `protojson.MarshalOptions` used by the builtins to marshal rpc responses to JSON.
- Loading branch information