@@ -16,7 +16,7 @@ let package = Package(
1616 // name, platforms, products, etc.
1717 dependencies : [
1818 // other dependencies
19- .package (url : " https://github.com/open-policy-agent/swift-opa" ),
19+ .package (url : " https://github.com/open-policy-agent/swift-opa" , branch : " main " ),
2020 ],
2121 targets : [
2222 .[executable| library]Target (name : " <target-name>" , dependencies : [
@@ -35,46 +35,49 @@ An `OPA.Engine` can be initialized with an on-disk bundle using its constructor:
3535
3636``` swift
3737import Rego
38+ import Foundation
3839
39- ...
40+ // ...
4041
4142let path = " some/local/path"
42- let bundlePath = OPA.Engine .BundlePath (name : " policyBundle" , url : URL (fileURLWithPath : path)
43+ let bundlePath = OPA.Engine .BundlePath (name : " policyBundle" , url : URL (fileURLWithPath : path))
4344var regoEngine = try OPA.Engine (bundlePaths : [bundlePath])
4445
4546// Prepare does as much pre-processing as possible to get ready to evaluate queries.
4647// This only needs to be done once when loading the engine and after updating it.
4748// PreparedQuery's can be re-used.
48- let preparedQuery = try await regoEngine.prepareForEvaluation (query : self .evalOptions .query )
49+ let rawQuery = " data.policy.main.is_valid"
50+ let preparedQuery = try await regoEngine.prepareForEvaluation (query : rawQuery)
4951```
5052
51- Policies often expect an `input` document to be passed in . This can be parsed from JSON data:
53+ Policies often expect an ` input ` document to be passed in. This can be parsed from JSON data, for example :
5254
53- ```
55+ ``` swift
5456import AST
5557
56- let inputDocument = try AST.RegoValue (jsonData : data)
58+ // ...
59+
60+ let rawInput = #" {"favorite_fruit": "apple"}"# .data (using : .utf8 )!
61+ let inputDocument = try AST.RegoValue (jsonData : rawInput)
5762```
5863
59- Evaluation is performed by providing a query. For example, given the following policy:
64+ Evaluation is performed with the prepared query. We used ` data.policy.main.is_valid ` above, which makes sense given our policy source :
6065
6166``` rego
6267package policy.main
6368
6469default is_valid := false
6570is_valid := true if {
66- ...
71+ input.favorite_fruit == "apple"
6772}
6873```
6974
70- ... we could query `data.policy .main .is_valid `.
71-
75+ Putting it all together, we can evaluate our query and interpret the results, in this case just printing them:
7276
73- Putting it all together, we can evaluate our query:
74-
75- ```
77+ ``` swift
7678let resultSet = try await preparedQuery.evaluate (
77- query : queryString,
7879 input : inputDocument
7980)
81+
82+ print (try resultSet.jsonString )
8083```
0 commit comments