Skip to content

Commit 798d6d6

Browse files
README: Update examples to account for stricter nullability behavior. (#63)
Signed-off-by: Philip Conrad <[email protected]>
1 parent 3175d4f commit 798d6d6

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

README.md

+17-5
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,21 @@ For the `evaluate` method, the output type is configurable using generics, as sh
7373
```csharp
7474
string path = "authz/accounts/max_limit";
7575

76-
double? maxLimit = opa.evaluate<double>(path, "example");
76+
double maxLimit
77+
try {
78+
maxLimit = opa.evaluate<double?>(path, "example");
79+
}
80+
catch (OpaException) {
81+
maxLimit = 0.0f;
82+
}
7783
```
7884

79-
For cases where a default value is appropriate, you can avoid the nullable `double` type in the example like so:
85+
Nullable types are also allowed for output types, and if an error occurs during evaluation, a null result will be returned to the caller.
86+
8087
```csharp
8188
string path = "authz/accounts/max_limit";
8289

83-
double maxLimit = opa.evaluate<double>(path, "example") ?? 0.0f;
90+
double? maxLimit = opa.evaluate<double?>(path, "example");
8491
```
8592

8693
If the selected return type `<T>` is possible to deserialize from the returned JSON, `evaluate<T>` will attempt to populate the variable with the value(s) present.
@@ -103,8 +110,13 @@ var input = new Dictionary<string, object>() {
103110
{ "action", "read" },
104111
};
105112

106-
// (local variable) AuthzStatus status
107-
var status = opa.evaluate<AuthzStatus>(path, input) ?? AuthzStatus(false);
113+
AuthzStatus status;
114+
try {
115+
status = opa.evaluate<AuthzStatus>(path, input);
116+
}
117+
catch (OpaException) {
118+
status = new AuthzStatus(false);
119+
}
108120
```
109121

110122
> [!NOTE]

0 commit comments

Comments
 (0)