1515package dev .cel .verifier ;
1616
1717import com .google .auto .value .AutoValue ;
18+ import com .google .common .collect .ImmutableMap ;
19+ import com .google .errorprone .annotations .Immutable ;
20+ import java .util .Optional ;
1821
1922/** Result object containing the outcome of a CEL AST verification check. */
2023@ AutoValue
24+ @ Immutable
2125public abstract class CelVerificationResult {
2226
2327 /** Represents the outcome of the verification process. */
@@ -38,11 +42,24 @@ public enum VerificationStatus {
3842 */
3943 public abstract String reason ();
4044
41- /**
42- * Returns a detailed counterexample or satisfying model assignment, if one was found.
43- */
45+ /** Returns a detailed counterexample or satisfying model assignment string, if one was found. */
4446 public abstract String counterexample ();
4547
48+ /** Returns the structured counterexample or satisfying model assignment, if one was found. */
49+ public abstract Optional <CelCounterexample > counterexampleModel ();
50+
51+ /** Returns the variable bindings as a map of variable name to stringified CEL literal syntax. */
52+ public ImmutableMap <String , String > counterexampleBindings () {
53+ return counterexampleModel ()
54+ .map (
55+ model -> {
56+ ImmutableMap .Builder <String , String > builder = ImmutableMap .builder ();
57+ model .bindings ().forEach ((k , v ) -> builder .put (k , v .celString ()));
58+ return builder .buildOrThrow ();
59+ })
60+ .orElseGet (ImmutableMap ::of );
61+ }
62+
4663 /**
4764 * Returns a message detailing the outcome of the verification check, such as a counterexample
4865 * input, satisfying model assignments, or truncation reason. May be empty if status is VERIFIED
@@ -53,28 +70,56 @@ public String message() {
5370 }
5471
5572 static CelVerificationResult verified () {
56- return new AutoValue_CelVerificationResult (VerificationStatus .VERIFIED , "" , "" );
73+ return new AutoValue_CelVerificationResult (
74+ VerificationStatus .VERIFIED , "" , "" , Optional .empty ());
5775 }
5876
5977 static CelVerificationResult verified (String reason ) {
60- return new AutoValue_CelVerificationResult (VerificationStatus .VERIFIED , reason , "" );
78+ return new AutoValue_CelVerificationResult (
79+ VerificationStatus .VERIFIED , reason , "" , Optional .empty ());
80+ }
81+
82+ static CelVerificationResult verified (String reason , CelCounterexample counterexample ) {
83+ return new AutoValue_CelVerificationResult (
84+ VerificationStatus .VERIFIED ,
85+ reason ,
86+ counterexample .toDisplayString (),
87+ Optional .of (counterexample ));
6188 }
6289
6390 static CelVerificationResult failed (String reason ) {
64- return new AutoValue_CelVerificationResult (VerificationStatus .VIOLATED , reason , "" );
91+ return new AutoValue_CelVerificationResult (
92+ VerificationStatus .VIOLATED , reason , "" , Optional .empty ());
6593 }
6694
6795 static CelVerificationResult failed (String reason , String counterexample ) {
6896 return new AutoValue_CelVerificationResult (
69- VerificationStatus .VIOLATED , reason , counterexample );
97+ VerificationStatus .VIOLATED , reason , counterexample , Optional .empty ());
98+ }
99+
100+ static CelVerificationResult failed (String reason , CelCounterexample counterexample ) {
101+ return new AutoValue_CelVerificationResult (
102+ VerificationStatus .VIOLATED ,
103+ reason ,
104+ counterexample .toDisplayString (),
105+ Optional .of (counterexample ));
70106 }
71107
72108 static CelVerificationResult inconclusive (String reason ) {
73- return new AutoValue_CelVerificationResult (VerificationStatus .INCONCLUSIVE , reason , "" );
109+ return new AutoValue_CelVerificationResult (
110+ VerificationStatus .INCONCLUSIVE , reason , "" , Optional .empty ());
74111 }
75112
76113 static CelVerificationResult inconclusive (String reason , String counterexample ) {
77114 return new AutoValue_CelVerificationResult (
78- VerificationStatus .INCONCLUSIVE , reason , counterexample );
115+ VerificationStatus .INCONCLUSIVE , reason , counterexample , Optional .empty ());
116+ }
117+
118+ static CelVerificationResult inconclusive (String reason , CelCounterexample counterexample ) {
119+ return new AutoValue_CelVerificationResult (
120+ VerificationStatus .INCONCLUSIVE ,
121+ reason ,
122+ counterexample .toDisplayString (),
123+ Optional .of (counterexample ));
79124 }
80125}
0 commit comments