Skip to content

Commit e1d3718

Browse files
committed
update usage instructions in README
I tried going through and copy/pasting from the README to integrated this package into my project, but found some of the snippets didn't work as is. These are the changes I had to make to get my project compiling again. Signed-off-by: Chris Rice <[email protected]>
1 parent 2bf8b3f commit e1d3718

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

README.md

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3737
import Rego
38+
import Foundation
3839

39-
...
40+
// ...
4041

4142
let 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))
4344
var 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
5456
import 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
6267
package policy.main
6368
6469
default is_valid := false
6570
is_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
7678
let resultSet = try await preparedQuery.evaluate(
77-
query: queryString,
7879
input: inputDocument
7980
)
81+
82+
print(try resultSet.jsonString)
8083
```

0 commit comments

Comments
 (0)