Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -88,7 +88,7 @@ private static String formatExpr(
} else if (decl.equals(typeSystem.timestampCons().ConstructorDecl())) {
return "timestamp(" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + ")";
} else if (decl.equals(typeSystem.durationCons().ConstructorDecl())) {
return "duration(" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + ")";
return "duration('" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + "s')";
} else if (decl.equals(typeSystem.uintCons().ConstructorDecl())) {
return formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + "u";
} else if (decl.equals(typeSystem.boolCons().ConstructorDecl())) {
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -314,15 +314,21 @@ private static void printHelp(String topic, PrintStream out) {
out.println("Declares a variable in the REPL session with a specific type.");
out.println();
out.println("Supported Types:");
out.println(" - Primitive types: int, uint, string, bool, double, bytes");
out.println(" - List types: list<T> (e.g., list<int>, list<string>)");
out.println(" - Map types: map<K,V> (e.g., map<string,int>, map<string,string>)");
out.println(" - Primitive types: int, uint, string, bool, double, bytes, dyn");
out.println(" - Well-known types: timestamp, duration");
out.println(" - List types: list<T> (e.g., list<int>, list<string>)");
out.println(" - Map types: map<K,V> (e.g., map<string,int>, map<string,string>)");
out.println(" - Optional types: optional<T> (e.g., optional<string>, optional<int>)");
out.println(" - Protobuf types: coming soon");
out.println();
out.println("Examples:");
out.println(" cel-verifier> :var role string");
out.println(" cel-verifier> :var port int");
out.println(" cel-verifier> :var scores map<string,int>");
out.println(" cel-verifier> :var tags list<string>");
out.println(" cel-verifier> :var created_at timestamp");
out.println(" cel-verifier> :var timeout duration");
out.println(" cel-verifier> :var opt_flag optional<bool>");
break;
case "unknown":
out.println("Command: :unknown <identifier>");
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@
import dev.cel.common.types.CelType;
import dev.cel.common.types.ListType;
import dev.cel.common.types.MapType;
import dev.cel.common.types.OptionalType;
import dev.cel.common.types.SimpleType;
import java.time.Duration;
import java.util.ArrayList;
Expand DownExpand Up@@ -147,17 +148,20 @@ static ImmutableMap<String, CelType> parseVariables(List<String> varSpecs) {
}

static CelType parseCelType(String typeStr) {
// TODO: Replace with shorthand type parser once it is available.
Preconditions.checkNotNull(typeStr, "Type string cannot be null.");
String str = typeStr.trim().toLowerCase(Locale.US);

if (str.startsWith("list<") && str.endsWith(">")) {
String inner = str.substring(5, str.length() - 1).trim();
// Strip "list<" prefix and trailing ">" to extract the element type "T".
String inner = str.substring("list<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return ListType.create(elemType);
}

if (str.startsWith("map<") && str.endsWith(">")) {
String inner = str.substring(4, str.length() - 1).trim();
// Strip "map<" prefix and trailing ">" to extract the key and value types "K, V".
String inner = str.substring("map<".length(), str.length() - 1).trim();
List<String> parts = splitGenericArgs(inner);
if (parts.size() != 2) {
throw new IllegalArgumentException(
Expand All@@ -170,6 +174,20 @@ static CelType parseCelType(String typeStr) {
return MapType.create(keyType, valueType);
}

if (str.startsWith("optional<") && str.endsWith(">")) {
// Strip "optional<" prefix and trailing ">" to extract the wrapped type "T".
String inner = str.substring("optional<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return OptionalType.create(elemType);
}

if (str.startsWith("optional_type<") && str.endsWith(">")) {
// Strip "optional_type<" prefix and trailing ">" to extract the wrapped type "T".
String inner = str.substring("optional_type<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return OptionalType.create(elemType);
}

switch (str) {
case "int":
return SimpleType.INT;
Expand All@@ -187,12 +205,19 @@ static CelType parseCelType(String typeStr) {
return SimpleType.BYTES;
case "dyn":
return SimpleType.DYN;
case "timestamp":
case "google.protobuf.timestamp":
return SimpleType.TIMESTAMP;
case "duration":
case "google.protobuf.duration":
return SimpleType.DURATION;
default:
// TODO: Support protobuf message types (coming soon).
throw new IllegalArgumentException(
"Unsupported type for CLI variable declaration: '"
+ typeStr
+ "'. Supported types: int, uint, string, bool, double, bytes, dyn, list<T>, map<K,"
+ " V>.");
+ "'. Supported types: int, uint, string, bool, double, bytes, dyn, timestamp,"
+ " duration, list<T>, map<K, V>, optional<T>.");
}
}

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -1312,7 +1312,7 @@ private enum IsAlwaysTrueViolationTestCase {
"dur != dur",
"Condition is not always true\\.",
"Counterexample input:",
"dur = duration\\(-?\\d+\\)"),
"dur = duration\\('-?\\d+s'\\)"),
TIMESTAMP_VARIABLE_COUNTEREXAMPLE(
"ts != ts",
"Condition is not always true\\.",
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -53,9 +53,11 @@ private String[] runReplWithCommands(String... commands) throws Exception {
@Test
public void repl_quitAndExit() throws Exception {
String[] output1 = runReplWithCommands(":quit");

assertThat(output1[0]).contains("Goodbye!");

String[] output2 = runReplWithCommands(":exit");

assertThat(output2[0]).contains("Goodbye!");
}

Expand All@@ -73,6 +75,7 @@ public void repl_helpCommands() throws Exception {
":help equiv",
":help non_existent_topic",
":quit");

assertThat(output[0]).contains("REPL Commands:");
assertThat(output[0]).contains("Command: :var <name> <type>");
assertThat(output[0]).contains("Command: :unknown <identifier>");
Expand All@@ -81,6 +84,9 @@ public void repl_helpCommands() throws Exception {
assertThat(output[0]).contains("Query: sat <expression>");
assertThat(output[0]).contains("Query: valid <expression>");
assertThat(output[0]).contains("Query: equiv <expression1> <=> <expression2>");
assertThat(output[0]).contains("Well-known types: timestamp, duration");
assertThat(output[0]).contains("Optional types: optional<T>");
assertThat(output[0]).contains("Protobuf types: coming soon");
}

@Test
Expand All@@ -91,19 +97,27 @@ public void repl_varDeclarations() throws Exception {
":var port int",
":var scores map<string,int>",
":var tags list<string>",
":var created_at timestamp",
":var timeout duration",
":var opt_user optional<string>",
":vars",
":quit");

assertThat(output[0]).contains("Variable declared: role : string");
assertThat(output[0]).contains("Variable declared: port : int");
assertThat(output[0]).contains("Variable declared: scores : map(string, int)");
assertThat(output[0]).contains("Variable declared: tags : list(string)");
assertThat(output[0]).contains("Variables (4):");
assertThat(output[0]).contains("Variable declared: created_at : google.protobuf.Timestamp");
assertThat(output[0]).contains("Variable declared: timeout : google.protobuf.Duration");
assertThat(output[0]).contains("Variable declared: opt_user : optional_type(string)");
assertThat(output[0]).contains("Variables (7):");
}

@Test
public void repl_unknownIdentifiers() throws Exception {
String[] output =
runReplWithCommands(":unknown request.headers", ":unknown request.auth", ":vars", ":quit");

assertThat(output[0]).contains("Added unknown identifier: 'request.headers'");
assertThat(output[0]).contains("Added unknown identifier: 'request.auth'");
assertThat(output[0]).contains("Unknowns: [request.headers, request.auth]");
Expand All@@ -114,6 +128,7 @@ public void repl_timeoutConfiguration() throws Exception {
String[] output =
runReplWithCommands(
":timeout 15", ":vars", ":timeout -5", ":timeout abc", ":timeout", ":quit");

assertThat(output[0]).contains("Timeout set to 15s.");
assertThat(output[0]).contains("Timeout: 15s");
assertThat(output[1]).contains("Timeout must be a positive integer.");
Expand All@@ -125,6 +140,7 @@ public void repl_timeoutConfiguration() throws Exception {
public void repl_unrollConfiguration() throws Exception {
String[] output =
runReplWithCommands(":unroll 10", ":vars", ":unroll -1", ":unroll xyz", ":unroll", ":quit");

assertThat(output[0]).contains("Comprehension unroll limit set to 10.");
assertThat(output[0]).contains("Unroll limit: 10");
assertThat(output[1]).contains("Unroll limit must be non-negative.");
Expand All@@ -137,6 +153,7 @@ public void repl_sessionStateAndClear() throws Exception {
String[] output =
runReplWithCommands(
":var role string", ":unknown req.headers", ":vars", ":clear", ":vars", ":quit");

assertThat(output[0]).contains("Variables (1):");
assertThat(output[0]).contains("Session state reset.");
assertThat(output[0]).contains("Variables (0):");
Expand All@@ -147,6 +164,7 @@ public void repl_sessionStateAndClear() throws Exception {
public void repl_satQueries() throws Exception {
String[] output =
runReplWithCommands(":var port int", "sat port > 1024", "port > 1024", "sat", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).contains("Usage: sat <expression>");
}
Expand All@@ -155,6 +173,7 @@ public void repl_satQueries() throws Exception {
public void repl_validQueries() throws Exception {
String[] output =
runReplWithCommands(":var x int", "valid x > 0 || x <= 0", "valid x > 0", "valid", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[0]).contains("[VIOLATED]");
assertThat(output[1]).contains("Usage: valid <expression>");
Expand All@@ -164,13 +183,15 @@ public void repl_validQueries() throws Exception {
public void repl_equivQueries() throws Exception {
String[] output =
runReplWithCommands(":var x int", "equiv x > 10 <=> 10 < x", "equiv x > 10", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).contains("Equivalence query format: equiv <expr1> <=> <expr2>");
}

@Test
public void repl_equivDoubleNegation() throws Exception {
String[] output = runReplWithCommands(":var x int", "equiv !!(x == 10) <=> (x == 10)", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
}

Expand All@@ -184,6 +205,45 @@ public void repl_equivCanonicalization() throws Exception {
+ " v, v == 1 && k == 'foo')",
"equiv int_list.all(e, e > 0) <=> int_list.all(elem, elem > 0)",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_timestampAndDurationQueries() throws Exception {
String[] output =
runReplWithCommands(
":var t timestamp",
":var d duration",
"sat t > timestamp(1000)",
"sat d > duration('60s')",
"sat t + d > timestamp(2000)",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_durationSatisfyingInputFormat() throws Exception {
String[] output =
runReplWithCommands(":var dur duration", "timestamp(100) - timestamp(50) == dur", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[0]).contains("dur = duration('50s')");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_optionalQueries() throws Exception {
String[] output =
runReplWithCommands(
":var opt_val optional<int>",
"sat opt_val.hasValue() && opt_val.value() > 100",
"sat !opt_val.hasValue()",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}
Expand All@@ -199,6 +259,7 @@ public void repl_unknownCommandsAndErrors() throws Exception {
":unknown",
"invalid + + syntax",
":quit");

assertThat(output[1]).contains("Unknown command: :unknowncommand");
assertThat(output[1]).contains("Usage: :var <name> <type>");
assertThat(output[1]).contains("Unsupported type");
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Add copy buttons to all
 blocks\n(function() {\n function addCopyButtons() {\n document.querySelectorAll('pre code').forEach(function(codeBlock) {\n if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;\n codeBlock.parentElement.setAttribute('data-copy-added', 'true');\n \n var btn = document.createElement('button');\n btn.textContent = 'Copy';\n btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';\n btn.onmouseover = function() { this.style.opacity = '1'; };\n btn.onmouseout = function() { this.style.opacity = '0.7'; };\n btn.onclick = function() {\n navigator.clipboard.writeText(codeBlock.textContent).then(function() {\n btn.textContent = 'Copied!';\n setTimeout(function() { btn.textContent = 'Copy'; }, 1500);\n });\n };\n codeBlock.parentElement.style.position = 'relative';\n codeBlock.parentElement.appendChild(btn);\n });\n }\n \n addCopyButtons();\n \n // Re-run on dynamic content\n var observer = new MutationObserver(addCopyButtons);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Add Copy Buttons to Code Blocks");
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -88,7 +88,7 @@ private static String formatExpr(
} else if (decl.equals(typeSystem.timestampCons().ConstructorDecl())) {
return "timestamp(" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + ")";
} else if (decl.equals(typeSystem.durationCons().ConstructorDecl())) {
return "duration(" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + ")";
return "duration('" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + "s')";
} else if (decl.equals(typeSystem.uintCons().ConstructorDecl())) {
return formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + "u";
} else if (decl.equals(typeSystem.boolCons().ConstructorDecl())) {
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -314,15 +314,21 @@ private static void printHelp(String topic, PrintStream out) {
out.println("Declares a variable in the REPL session with a specific type.");
out.println();
out.println("Supported Types:");
out.println(" - Primitive types: int, uint, string, bool, double, bytes");
out.println(" - List types: list<T> (e.g., list<int>, list<string>)");
out.println(" - Map types: map<K,V> (e.g., map<string,int>, map<string,string>)");
out.println(" - Primitive types: int, uint, string, bool, double, bytes, dyn");
out.println(" - Well-known types: timestamp, duration");
out.println(" - List types: list<T> (e.g., list<int>, list<string>)");
out.println(" - Map types: map<K,V> (e.g., map<string,int>, map<string,string>)");
out.println(" - Optional types: optional<T> (e.g., optional<string>, optional<int>)");
out.println(" - Protobuf types: coming soon");
out.println();
out.println("Examples:");
out.println(" cel-verifier> :var role string");
out.println(" cel-verifier> :var port int");
out.println(" cel-verifier> :var scores map<string,int>");
out.println(" cel-verifier> :var tags list<string>");
out.println(" cel-verifier> :var created_at timestamp");
out.println(" cel-verifier> :var timeout duration");
out.println(" cel-verifier> :var opt_flag optional<bool>");
break;
case "unknown":
out.println("Command: :unknown <identifier>");
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@
import dev.cel.common.types.CelType;
import dev.cel.common.types.ListType;
import dev.cel.common.types.MapType;
import dev.cel.common.types.OptionalType;
import dev.cel.common.types.SimpleType;
import java.time.Duration;
import java.util.ArrayList;
Expand DownExpand Up@@ -147,17 +148,20 @@ static ImmutableMap<String, CelType> parseVariables(List<String> varSpecs) {
}

static CelType parseCelType(String typeStr) {
// TODO: Replace with shorthand type parser once it is available.
Preconditions.checkNotNull(typeStr, "Type string cannot be null.");
String str = typeStr.trim().toLowerCase(Locale.US);

if (str.startsWith("list<") && str.endsWith(">")) {
String inner = str.substring(5, str.length() - 1).trim();
// Strip "list<" prefix and trailing ">" to extract the element type "T".
String inner = str.substring("list<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return ListType.create(elemType);
}

if (str.startsWith("map<") && str.endsWith(">")) {
String inner = str.substring(4, str.length() - 1).trim();
// Strip "map<" prefix and trailing ">" to extract the key and value types "K, V".
String inner = str.substring("map<".length(), str.length() - 1).trim();
List<String> parts = splitGenericArgs(inner);
if (parts.size() != 2) {
throw new IllegalArgumentException(
Expand All@@ -170,6 +174,20 @@ static CelType parseCelType(String typeStr) {
return MapType.create(keyType, valueType);
}

if (str.startsWith("optional<") && str.endsWith(">")) {
// Strip "optional<" prefix and trailing ">" to extract the wrapped type "T".
String inner = str.substring("optional<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return OptionalType.create(elemType);
}

if (str.startsWith("optional_type<") && str.endsWith(">")) {
// Strip "optional_type<" prefix and trailing ">" to extract the wrapped type "T".
String inner = str.substring("optional_type<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return OptionalType.create(elemType);
}

switch (str) {
case "int":
return SimpleType.INT;
Expand All@@ -187,12 +205,19 @@ static CelType parseCelType(String typeStr) {
return SimpleType.BYTES;
case "dyn":
return SimpleType.DYN;
case "timestamp":
case "google.protobuf.timestamp":
return SimpleType.TIMESTAMP;
case "duration":
case "google.protobuf.duration":
return SimpleType.DURATION;
default:
// TODO: Support protobuf message types (coming soon).
throw new IllegalArgumentException(
"Unsupported type for CLI variable declaration: '"
+ typeStr
+ "'. Supported types: int, uint, string, bool, double, bytes, dyn, list<T>, map<K,"
+ " V>.");
+ "'. Supported types: int, uint, string, bool, double, bytes, dyn, timestamp,"
+ " duration, list<T>, map<K, V>, optional<T>.");
}
}

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -1312,7 +1312,7 @@ private enum IsAlwaysTrueViolationTestCase {
"dur != dur",
"Condition is not always true\\.",
"Counterexample input:",
"dur = duration\\(-?\\d+\\)"),
"dur = duration\\('-?\\d+s'\\)"),
TIMESTAMP_VARIABLE_COUNTEREXAMPLE(
"ts != ts",
"Condition is not always true\\.",
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -53,9 +53,11 @@ private String[] runReplWithCommands(String... commands) throws Exception {
@Test
public void repl_quitAndExit() throws Exception {
String[] output1 = runReplWithCommands(":quit");

assertThat(output1[0]).contains("Goodbye!");

String[] output2 = runReplWithCommands(":exit");

assertThat(output2[0]).contains("Goodbye!");
}

Expand All@@ -73,6 +75,7 @@ public void repl_helpCommands() throws Exception {
":help equiv",
":help non_existent_topic",
":quit");

assertThat(output[0]).contains("REPL Commands:");
assertThat(output[0]).contains("Command: :var <name> <type>");
assertThat(output[0]).contains("Command: :unknown <identifier>");
Expand All@@ -81,6 +84,9 @@ public void repl_helpCommands() throws Exception {
assertThat(output[0]).contains("Query: sat <expression>");
assertThat(output[0]).contains("Query: valid <expression>");
assertThat(output[0]).contains("Query: equiv <expression1> <=> <expression2>");
assertThat(output[0]).contains("Well-known types: timestamp, duration");
assertThat(output[0]).contains("Optional types: optional<T>");
assertThat(output[0]).contains("Protobuf types: coming soon");
}

@Test
Expand All@@ -91,19 +97,27 @@ public void repl_varDeclarations() throws Exception {
":var port int",
":var scores map<string,int>",
":var tags list<string>",
":var created_at timestamp",
":var timeout duration",
":var opt_user optional<string>",
":vars",
":quit");

assertThat(output[0]).contains("Variable declared: role : string");
assertThat(output[0]).contains("Variable declared: port : int");
assertThat(output[0]).contains("Variable declared: scores : map(string, int)");
assertThat(output[0]).contains("Variable declared: tags : list(string)");
assertThat(output[0]).contains("Variables (4):");
assertThat(output[0]).contains("Variable declared: created_at : google.protobuf.Timestamp");
assertThat(output[0]).contains("Variable declared: timeout : google.protobuf.Duration");
assertThat(output[0]).contains("Variable declared: opt_user : optional_type(string)");
assertThat(output[0]).contains("Variables (7):");
}

@Test
public void repl_unknownIdentifiers() throws Exception {
String[] output =
runReplWithCommands(":unknown request.headers", ":unknown request.auth", ":vars", ":quit");

assertThat(output[0]).contains("Added unknown identifier: 'request.headers'");
assertThat(output[0]).contains("Added unknown identifier: 'request.auth'");
assertThat(output[0]).contains("Unknowns: [request.headers, request.auth]");
Expand All@@ -114,6 +128,7 @@ public void repl_timeoutConfiguration() throws Exception {
String[] output =
runReplWithCommands(
":timeout 15", ":vars", ":timeout -5", ":timeout abc", ":timeout", ":quit");

assertThat(output[0]).contains("Timeout set to 15s.");
assertThat(output[0]).contains("Timeout: 15s");
assertThat(output[1]).contains("Timeout must be a positive integer.");
Expand All@@ -125,6 +140,7 @@ public void repl_timeoutConfiguration() throws Exception {
public void repl_unrollConfiguration() throws Exception {
String[] output =
runReplWithCommands(":unroll 10", ":vars", ":unroll -1", ":unroll xyz", ":unroll", ":quit");

assertThat(output[0]).contains("Comprehension unroll limit set to 10.");
assertThat(output[0]).contains("Unroll limit: 10");
assertThat(output[1]).contains("Unroll limit must be non-negative.");
Expand All@@ -137,6 +153,7 @@ public void repl_sessionStateAndClear() throws Exception {
String[] output =
runReplWithCommands(
":var role string", ":unknown req.headers", ":vars", ":clear", ":vars", ":quit");

assertThat(output[0]).contains("Variables (1):");
assertThat(output[0]).contains("Session state reset.");
assertThat(output[0]).contains("Variables (0):");
Expand All@@ -147,6 +164,7 @@ public void repl_sessionStateAndClear() throws Exception {
public void repl_satQueries() throws Exception {
String[] output =
runReplWithCommands(":var port int", "sat port > 1024", "port > 1024", "sat", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).contains("Usage: sat <expression>");
}
Expand All@@ -155,6 +173,7 @@ public void repl_satQueries() throws Exception {
public void repl_validQueries() throws Exception {
String[] output =
runReplWithCommands(":var x int", "valid x > 0 || x <= 0", "valid x > 0", "valid", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[0]).contains("[VIOLATED]");
assertThat(output[1]).contains("Usage: valid <expression>");
Expand All@@ -164,13 +183,15 @@ public void repl_validQueries() throws Exception {
public void repl_equivQueries() throws Exception {
String[] output =
runReplWithCommands(":var x int", "equiv x > 10 <=> 10 < x", "equiv x > 10", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).contains("Equivalence query format: equiv <expr1> <=> <expr2>");
}

@Test
public void repl_equivDoubleNegation() throws Exception {
String[] output = runReplWithCommands(":var x int", "equiv !!(x == 10) <=> (x == 10)", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
}

Expand All@@ -184,6 +205,45 @@ public void repl_equivCanonicalization() throws Exception {
+ " v, v == 1 && k == 'foo')",
"equiv int_list.all(e, e > 0) <=> int_list.all(elem, elem > 0)",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_timestampAndDurationQueries() throws Exception {
String[] output =
runReplWithCommands(
":var t timestamp",
":var d duration",
"sat t > timestamp(1000)",
"sat d > duration('60s')",
"sat t + d > timestamp(2000)",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_durationSatisfyingInputFormat() throws Exception {
String[] output =
runReplWithCommands(":var dur duration", "timestamp(100) - timestamp(50) == dur", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[0]).contains("dur = duration('50s')");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_optionalQueries() throws Exception {
String[] output =
runReplWithCommands(
":var opt_val optional<int>",
"sat opt_val.hasValue() && opt_val.value() > 100",
"sat !opt_val.hasValue()",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}
Expand All@@ -199,6 +259,7 @@ public void repl_unknownCommandsAndErrors() throws Exception {
":unknown",
"invalid + + syntax",
":quit");

assertThat(output[1]).contains("Unknown command: :unknowncommand");
assertThat(output[1]).contains("Usage: :var <name> <type>");
assertThat(output[1]).contains("Unsupported type");
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Force GitHub README to respect dark mode\n(function() {\n var style = document.createElement('style');\n style.textContent = '\n .markdown-body {\n color-scheme: dark light;\n }\n .markdown-body pre { background: #161b22 !important; }\n .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; }\n .markdown-body table th, .markdown-body table td { border-color: #30363d !important; }\n .markdown-body img { background: #0d1117; }\n .markdown-body blockquote { border-left-color: #8b949e; }\n .markdown-body hr { border-color: #30363d; }\n ';\n document.head.appendChild(style);\n})();", "GitHub Dark Mode README Fix"); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -88,7 +88,7 @@ private static String formatExpr(
} else if (decl.equals(typeSystem.timestampCons().ConstructorDecl())) {
return "timestamp(" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + ")";
} else if (decl.equals(typeSystem.durationCons().ConstructorDecl())) {
return "duration(" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + ")";
return "duration('" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + "s')";
} else if (decl.equals(typeSystem.uintCons().ConstructorDecl())) {
return formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + "u";
} else if (decl.equals(typeSystem.boolCons().ConstructorDecl())) {
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -314,15 +314,21 @@ private static void printHelp(String topic, PrintStream out) {
out.println("Declares a variable in the REPL session with a specific type.");
out.println();
out.println("Supported Types:");
out.println(" - Primitive types: int, uint, string, bool, double, bytes");
out.println(" - List types: list<T> (e.g., list<int>, list<string>)");
out.println(" - Map types: map<K,V> (e.g., map<string,int>, map<string,string>)");
out.println(" - Primitive types: int, uint, string, bool, double, bytes, dyn");
out.println(" - Well-known types: timestamp, duration");
out.println(" - List types: list<T> (e.g., list<int>, list<string>)");
out.println(" - Map types: map<K,V> (e.g., map<string,int>, map<string,string>)");
out.println(" - Optional types: optional<T> (e.g., optional<string>, optional<int>)");
out.println(" - Protobuf types: coming soon");
out.println();
out.println("Examples:");
out.println(" cel-verifier> :var role string");
out.println(" cel-verifier> :var port int");
out.println(" cel-verifier> :var scores map<string,int>");
out.println(" cel-verifier> :var tags list<string>");
out.println(" cel-verifier> :var created_at timestamp");
out.println(" cel-verifier> :var timeout duration");
out.println(" cel-verifier> :var opt_flag optional<bool>");
break;
case "unknown":
out.println("Command: :unknown <identifier>");
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@
import dev.cel.common.types.CelType;
import dev.cel.common.types.ListType;
import dev.cel.common.types.MapType;
import dev.cel.common.types.OptionalType;
import dev.cel.common.types.SimpleType;
import java.time.Duration;
import java.util.ArrayList;
Expand DownExpand Up@@ -147,17 +148,20 @@ static ImmutableMap<String, CelType> parseVariables(List<String> varSpecs) {
}

static CelType parseCelType(String typeStr) {
// TODO: Replace with shorthand type parser once it is available.
Preconditions.checkNotNull(typeStr, "Type string cannot be null.");
String str = typeStr.trim().toLowerCase(Locale.US);

if (str.startsWith("list<") && str.endsWith(">")) {
String inner = str.substring(5, str.length() - 1).trim();
// Strip "list<" prefix and trailing ">" to extract the element type "T".
String inner = str.substring("list<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return ListType.create(elemType);
}

if (str.startsWith("map<") && str.endsWith(">")) {
String inner = str.substring(4, str.length() - 1).trim();
// Strip "map<" prefix and trailing ">" to extract the key and value types "K, V".
String inner = str.substring("map<".length(), str.length() - 1).trim();
List<String> parts = splitGenericArgs(inner);
if (parts.size() != 2) {
throw new IllegalArgumentException(
Expand All@@ -170,6 +174,20 @@ static CelType parseCelType(String typeStr) {
return MapType.create(keyType, valueType);
}

if (str.startsWith("optional<") && str.endsWith(">")) {
// Strip "optional<" prefix and trailing ">" to extract the wrapped type "T".
String inner = str.substring("optional<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return OptionalType.create(elemType);
}

if (str.startsWith("optional_type<") && str.endsWith(">")) {
// Strip "optional_type<" prefix and trailing ">" to extract the wrapped type "T".
String inner = str.substring("optional_type<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return OptionalType.create(elemType);
}

switch (str) {
case "int":
return SimpleType.INT;
Expand All@@ -187,12 +205,19 @@ static CelType parseCelType(String typeStr) {
return SimpleType.BYTES;
case "dyn":
return SimpleType.DYN;
case "timestamp":
case "google.protobuf.timestamp":
return SimpleType.TIMESTAMP;
case "duration":
case "google.protobuf.duration":
return SimpleType.DURATION;
default:
// TODO: Support protobuf message types (coming soon).
throw new IllegalArgumentException(
"Unsupported type for CLI variable declaration: '"
+ typeStr
+ "'. Supported types: int, uint, string, bool, double, bytes, dyn, list<T>, map<K,"
+ " V>.");
+ "'. Supported types: int, uint, string, bool, double, bytes, dyn, timestamp,"
+ " duration, list<T>, map<K, V>, optional<T>.");
}
}

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -1312,7 +1312,7 @@ private enum IsAlwaysTrueViolationTestCase {
"dur != dur",
"Condition is not always true\\.",
"Counterexample input:",
"dur = duration\\(-?\\d+\\)"),
"dur = duration\\('-?\\d+s'\\)"),
TIMESTAMP_VARIABLE_COUNTEREXAMPLE(
"ts != ts",
"Condition is not always true\\.",
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -53,9 +53,11 @@ private String[] runReplWithCommands(String... commands) throws Exception {
@Test
public void repl_quitAndExit() throws Exception {
String[] output1 = runReplWithCommands(":quit");

assertThat(output1[0]).contains("Goodbye!");

String[] output2 = runReplWithCommands(":exit");

assertThat(output2[0]).contains("Goodbye!");
}

Expand All@@ -73,6 +75,7 @@ public void repl_helpCommands() throws Exception {
":help equiv",
":help non_existent_topic",
":quit");

assertThat(output[0]).contains("REPL Commands:");
assertThat(output[0]).contains("Command: :var <name> <type>");
assertThat(output[0]).contains("Command: :unknown <identifier>");
Expand All@@ -81,6 +84,9 @@ public void repl_helpCommands() throws Exception {
assertThat(output[0]).contains("Query: sat <expression>");
assertThat(output[0]).contains("Query: valid <expression>");
assertThat(output[0]).contains("Query: equiv <expression1> <=> <expression2>");
assertThat(output[0]).contains("Well-known types: timestamp, duration");
assertThat(output[0]).contains("Optional types: optional<T>");
assertThat(output[0]).contains("Protobuf types: coming soon");
}

@Test
Expand All@@ -91,19 +97,27 @@ public void repl_varDeclarations() throws Exception {
":var port int",
":var scores map<string,int>",
":var tags list<string>",
":var created_at timestamp",
":var timeout duration",
":var opt_user optional<string>",
":vars",
":quit");

assertThat(output[0]).contains("Variable declared: role : string");
assertThat(output[0]).contains("Variable declared: port : int");
assertThat(output[0]).contains("Variable declared: scores : map(string, int)");
assertThat(output[0]).contains("Variable declared: tags : list(string)");
assertThat(output[0]).contains("Variables (4):");
assertThat(output[0]).contains("Variable declared: created_at : google.protobuf.Timestamp");
assertThat(output[0]).contains("Variable declared: timeout : google.protobuf.Duration");
assertThat(output[0]).contains("Variable declared: opt_user : optional_type(string)");
assertThat(output[0]).contains("Variables (7):");
}

@Test
public void repl_unknownIdentifiers() throws Exception {
String[] output =
runReplWithCommands(":unknown request.headers", ":unknown request.auth", ":vars", ":quit");

assertThat(output[0]).contains("Added unknown identifier: 'request.headers'");
assertThat(output[0]).contains("Added unknown identifier: 'request.auth'");
assertThat(output[0]).contains("Unknowns: [request.headers, request.auth]");
Expand All@@ -114,6 +128,7 @@ public void repl_timeoutConfiguration() throws Exception {
String[] output =
runReplWithCommands(
":timeout 15", ":vars", ":timeout -5", ":timeout abc", ":timeout", ":quit");

assertThat(output[0]).contains("Timeout set to 15s.");
assertThat(output[0]).contains("Timeout: 15s");
assertThat(output[1]).contains("Timeout must be a positive integer.");
Expand All@@ -125,6 +140,7 @@ public void repl_timeoutConfiguration() throws Exception {
public void repl_unrollConfiguration() throws Exception {
String[] output =
runReplWithCommands(":unroll 10", ":vars", ":unroll -1", ":unroll xyz", ":unroll", ":quit");

assertThat(output[0]).contains("Comprehension unroll limit set to 10.");
assertThat(output[0]).contains("Unroll limit: 10");
assertThat(output[1]).contains("Unroll limit must be non-negative.");
Expand All@@ -137,6 +153,7 @@ public void repl_sessionStateAndClear() throws Exception {
String[] output =
runReplWithCommands(
":var role string", ":unknown req.headers", ":vars", ":clear", ":vars", ":quit");

assertThat(output[0]).contains("Variables (1):");
assertThat(output[0]).contains("Session state reset.");
assertThat(output[0]).contains("Variables (0):");
Expand All@@ -147,6 +164,7 @@ public void repl_sessionStateAndClear() throws Exception {
public void repl_satQueries() throws Exception {
String[] output =
runReplWithCommands(":var port int", "sat port > 1024", "port > 1024", "sat", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).contains("Usage: sat <expression>");
}
Expand All@@ -155,6 +173,7 @@ public void repl_satQueries() throws Exception {
public void repl_validQueries() throws Exception {
String[] output =
runReplWithCommands(":var x int", "valid x > 0 || x <= 0", "valid x > 0", "valid", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[0]).contains("[VIOLATED]");
assertThat(output[1]).contains("Usage: valid <expression>");
Expand All@@ -164,13 +183,15 @@ public void repl_validQueries() throws Exception {
public void repl_equivQueries() throws Exception {
String[] output =
runReplWithCommands(":var x int", "equiv x > 10 <=> 10 < x", "equiv x > 10", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).contains("Equivalence query format: equiv <expr1> <=> <expr2>");
}

@Test
public void repl_equivDoubleNegation() throws Exception {
String[] output = runReplWithCommands(":var x int", "equiv !!(x == 10) <=> (x == 10)", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
}

Expand All@@ -184,6 +205,45 @@ public void repl_equivCanonicalization() throws Exception {
+ " v, v == 1 && k == 'foo')",
"equiv int_list.all(e, e > 0) <=> int_list.all(elem, elem > 0)",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_timestampAndDurationQueries() throws Exception {
String[] output =
runReplWithCommands(
":var t timestamp",
":var d duration",
"sat t > timestamp(1000)",
"sat d > duration('60s')",
"sat t + d > timestamp(2000)",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_durationSatisfyingInputFormat() throws Exception {
String[] output =
runReplWithCommands(":var dur duration", "timestamp(100) - timestamp(50) == dur", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[0]).contains("dur = duration('50s')");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_optionalQueries() throws Exception {
String[] output =
runReplWithCommands(
":var opt_val optional<int>",
"sat opt_val.hasValue() && opt_val.value() > 100",
"sat !opt_val.hasValue()",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}
Expand All@@ -199,6 +259,7 @@ public void repl_unknownCommandsAndErrors() throws Exception {
":unknown",
"invalid + + syntax",
":quit");

assertThat(output[1]).contains("Unknown command: :unknowncommand");
assertThat(output[1]).contains("Usage: :var <name> <type>");
assertThat(output[1]).contains("Unsupported type");
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Highlight search terms from Google/DuckDuckGo/Bing referrer\n(function() {\n var ref = document.referrer;\n var terms = [];\n \n if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) {\n var url = new URL(ref);\n var q = url.searchParams.get('q') || url.searchParams.get('p');\n if (q) {\n terms = q.split(/\\s+/).filter(function(t) { return t.length > 2; });\n }\n }\n \n if (terms.length === 0) return;\n \n var style = document.createElement('style');\n style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }';\n document.head.appendChild(style);\n \n function highlight(node) {\n if (node.nodeType === 3) { // text node\n var text = node.textContent;\n var found = false;\n terms.forEach(function(term) {\n var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\\]\\\\]/g, '\\\\') + ')', 'gi');\n if (regex.test(text)) {\n found = true;\n var frag = document.createDocumentFragment();\n var parts = text.split(regex);\n parts.forEach(function(part, i) {\n if (i % 2 === 0) {\n frag.appendChild(document.createTextNode(part));\n } else {\n var span = document.createElement('span');\n span.className = 'userscript-highlight';\n span.textContent = part;\n frag.appendChild(span);\n }\n });\n node.parentNode.replaceChild(frag, node);\n }\n });\n } else if (node.nodeType === 1 && node.childNodes) { // element\n var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT'];\n if (!skipTags.includes(node.tagName)) {\n Array.from(node.childNodes).forEach(highlight);\n }\n }\n }\n \n highlight(document.body);\n \n // Re-highlight on dynamic content\n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1 || node.nodeType === 3) highlight(node);\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Highlight Search Terms"); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -88,7 +88,7 @@ private static String formatExpr(
} else if (decl.equals(typeSystem.timestampCons().ConstructorDecl())) {
return "timestamp(" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + ")";
} else if (decl.equals(typeSystem.durationCons().ConstructorDecl())) {
return "duration(" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + ")";
return "duration('" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + "s')";
} else if (decl.equals(typeSystem.uintCons().ConstructorDecl())) {
return formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + "u";
} else if (decl.equals(typeSystem.boolCons().ConstructorDecl())) {
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -314,15 +314,21 @@ private static void printHelp(String topic, PrintStream out) {
out.println("Declares a variable in the REPL session with a specific type.");
out.println();
out.println("Supported Types:");
out.println(" - Primitive types: int, uint, string, bool, double, bytes");
out.println(" - List types: list<T> (e.g., list<int>, list<string>)");
out.println(" - Map types: map<K,V> (e.g., map<string,int>, map<string,string>)");
out.println(" - Primitive types: int, uint, string, bool, double, bytes, dyn");
out.println(" - Well-known types: timestamp, duration");
out.println(" - List types: list<T> (e.g., list<int>, list<string>)");
out.println(" - Map types: map<K,V> (e.g., map<string,int>, map<string,string>)");
out.println(" - Optional types: optional<T> (e.g., optional<string>, optional<int>)");
out.println(" - Protobuf types: coming soon");
out.println();
out.println("Examples:");
out.println(" cel-verifier> :var role string");
out.println(" cel-verifier> :var port int");
out.println(" cel-verifier> :var scores map<string,int>");
out.println(" cel-verifier> :var tags list<string>");
out.println(" cel-verifier> :var created_at timestamp");
out.println(" cel-verifier> :var timeout duration");
out.println(" cel-verifier> :var opt_flag optional<bool>");
break;
case "unknown":
out.println("Command: :unknown <identifier>");
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@
import dev.cel.common.types.CelType;
import dev.cel.common.types.ListType;
import dev.cel.common.types.MapType;
import dev.cel.common.types.OptionalType;
import dev.cel.common.types.SimpleType;
import java.time.Duration;
import java.util.ArrayList;
Expand DownExpand Up@@ -147,17 +148,20 @@ static ImmutableMap<String, CelType> parseVariables(List<String> varSpecs) {
}

static CelType parseCelType(String typeStr) {
// TODO: Replace with shorthand type parser once it is available.
Preconditions.checkNotNull(typeStr, "Type string cannot be null.");
String str = typeStr.trim().toLowerCase(Locale.US);

if (str.startsWith("list<") && str.endsWith(">")) {
String inner = str.substring(5, str.length() - 1).trim();
// Strip "list<" prefix and trailing ">" to extract the element type "T".
String inner = str.substring("list<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return ListType.create(elemType);
}

if (str.startsWith("map<") && str.endsWith(">")) {
String inner = str.substring(4, str.length() - 1).trim();
// Strip "map<" prefix and trailing ">" to extract the key and value types "K, V".
String inner = str.substring("map<".length(), str.length() - 1).trim();
List<String> parts = splitGenericArgs(inner);
if (parts.size() != 2) {
throw new IllegalArgumentException(
Expand All@@ -170,6 +174,20 @@ static CelType parseCelType(String typeStr) {
return MapType.create(keyType, valueType);
}

if (str.startsWith("optional<") && str.endsWith(">")) {
// Strip "optional<" prefix and trailing ">" to extract the wrapped type "T".
String inner = str.substring("optional<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return OptionalType.create(elemType);
}

if (str.startsWith("optional_type<") && str.endsWith(">")) {
// Strip "optional_type<" prefix and trailing ">" to extract the wrapped type "T".
String inner = str.substring("optional_type<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return OptionalType.create(elemType);
}

switch (str) {
case "int":
return SimpleType.INT;
Expand All@@ -187,12 +205,19 @@ static CelType parseCelType(String typeStr) {
return SimpleType.BYTES;
case "dyn":
return SimpleType.DYN;
case "timestamp":
case "google.protobuf.timestamp":
return SimpleType.TIMESTAMP;
case "duration":
case "google.protobuf.duration":
return SimpleType.DURATION;
default:
// TODO: Support protobuf message types (coming soon).
throw new IllegalArgumentException(
"Unsupported type for CLI variable declaration: '"
+ typeStr
+ "'. Supported types: int, uint, string, bool, double, bytes, dyn, list<T>, map<K,"
+ " V>.");
+ "'. Supported types: int, uint, string, bool, double, bytes, dyn, timestamp,"
+ " duration, list<T>, map<K, V>, optional<T>.");
}
}

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -1312,7 +1312,7 @@ private enum IsAlwaysTrueViolationTestCase {
"dur != dur",
"Condition is not always true\\.",
"Counterexample input:",
"dur = duration\\(-?\\d+\\)"),
"dur = duration\\('-?\\d+s'\\)"),
TIMESTAMP_VARIABLE_COUNTEREXAMPLE(
"ts != ts",
"Condition is not always true\\.",
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -53,9 +53,11 @@ private String[] runReplWithCommands(String... commands) throws Exception {
@Test
public void repl_quitAndExit() throws Exception {
String[] output1 = runReplWithCommands(":quit");

assertThat(output1[0]).contains("Goodbye!");

String[] output2 = runReplWithCommands(":exit");

assertThat(output2[0]).contains("Goodbye!");
}

Expand All@@ -73,6 +75,7 @@ public void repl_helpCommands() throws Exception {
":help equiv",
":help non_existent_topic",
":quit");

assertThat(output[0]).contains("REPL Commands:");
assertThat(output[0]).contains("Command: :var <name> <type>");
assertThat(output[0]).contains("Command: :unknown <identifier>");
Expand All@@ -81,6 +84,9 @@ public void repl_helpCommands() throws Exception {
assertThat(output[0]).contains("Query: sat <expression>");
assertThat(output[0]).contains("Query: valid <expression>");
assertThat(output[0]).contains("Query: equiv <expression1> <=> <expression2>");
assertThat(output[0]).contains("Well-known types: timestamp, duration");
assertThat(output[0]).contains("Optional types: optional<T>");
assertThat(output[0]).contains("Protobuf types: coming soon");
}

@Test
Expand All@@ -91,19 +97,27 @@ public void repl_varDeclarations() throws Exception {
":var port int",
":var scores map<string,int>",
":var tags list<string>",
":var created_at timestamp",
":var timeout duration",
":var opt_user optional<string>",
":vars",
":quit");

assertThat(output[0]).contains("Variable declared: role : string");
assertThat(output[0]).contains("Variable declared: port : int");
assertThat(output[0]).contains("Variable declared: scores : map(string, int)");
assertThat(output[0]).contains("Variable declared: tags : list(string)");
assertThat(output[0]).contains("Variables (4):");
assertThat(output[0]).contains("Variable declared: created_at : google.protobuf.Timestamp");
assertThat(output[0]).contains("Variable declared: timeout : google.protobuf.Duration");
assertThat(output[0]).contains("Variable declared: opt_user : optional_type(string)");
assertThat(output[0]).contains("Variables (7):");
}

@Test
public void repl_unknownIdentifiers() throws Exception {
String[] output =
runReplWithCommands(":unknown request.headers", ":unknown request.auth", ":vars", ":quit");

assertThat(output[0]).contains("Added unknown identifier: 'request.headers'");
assertThat(output[0]).contains("Added unknown identifier: 'request.auth'");
assertThat(output[0]).contains("Unknowns: [request.headers, request.auth]");
Expand All@@ -114,6 +128,7 @@ public void repl_timeoutConfiguration() throws Exception {
String[] output =
runReplWithCommands(
":timeout 15", ":vars", ":timeout -5", ":timeout abc", ":timeout", ":quit");

assertThat(output[0]).contains("Timeout set to 15s.");
assertThat(output[0]).contains("Timeout: 15s");
assertThat(output[1]).contains("Timeout must be a positive integer.");
Expand All@@ -125,6 +140,7 @@ public void repl_timeoutConfiguration() throws Exception {
public void repl_unrollConfiguration() throws Exception {
String[] output =
runReplWithCommands(":unroll 10", ":vars", ":unroll -1", ":unroll xyz", ":unroll", ":quit");

assertThat(output[0]).contains("Comprehension unroll limit set to 10.");
assertThat(output[0]).contains("Unroll limit: 10");
assertThat(output[1]).contains("Unroll limit must be non-negative.");
Expand All@@ -137,6 +153,7 @@ public void repl_sessionStateAndClear() throws Exception {
String[] output =
runReplWithCommands(
":var role string", ":unknown req.headers", ":vars", ":clear", ":vars", ":quit");

assertThat(output[0]).contains("Variables (1):");
assertThat(output[0]).contains("Session state reset.");
assertThat(output[0]).contains("Variables (0):");
Expand All@@ -147,6 +164,7 @@ public void repl_sessionStateAndClear() throws Exception {
public void repl_satQueries() throws Exception {
String[] output =
runReplWithCommands(":var port int", "sat port > 1024", "port > 1024", "sat", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).contains("Usage: sat <expression>");
}
Expand All@@ -155,6 +173,7 @@ public void repl_satQueries() throws Exception {
public void repl_validQueries() throws Exception {
String[] output =
runReplWithCommands(":var x int", "valid x > 0 || x <= 0", "valid x > 0", "valid", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[0]).contains("[VIOLATED]");
assertThat(output[1]).contains("Usage: valid <expression>");
Expand All@@ -164,13 +183,15 @@ public void repl_validQueries() throws Exception {
public void repl_equivQueries() throws Exception {
String[] output =
runReplWithCommands(":var x int", "equiv x > 10 <=> 10 < x", "equiv x > 10", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).contains("Equivalence query format: equiv <expr1> <=> <expr2>");
}

@Test
public void repl_equivDoubleNegation() throws Exception {
String[] output = runReplWithCommands(":var x int", "equiv !!(x == 10) <=> (x == 10)", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
}

Expand All@@ -184,6 +205,45 @@ public void repl_equivCanonicalization() throws Exception {
+ " v, v == 1 && k == 'foo')",
"equiv int_list.all(e, e > 0) <=> int_list.all(elem, elem > 0)",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_timestampAndDurationQueries() throws Exception {
String[] output =
runReplWithCommands(
":var t timestamp",
":var d duration",
"sat t > timestamp(1000)",
"sat d > duration('60s')",
"sat t + d > timestamp(2000)",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_durationSatisfyingInputFormat() throws Exception {
String[] output =
runReplWithCommands(":var dur duration", "timestamp(100) - timestamp(50) == dur", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[0]).contains("dur = duration('50s')");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_optionalQueries() throws Exception {
String[] output =
runReplWithCommands(
":var opt_val optional<int>",
"sat opt_val.hasValue() && opt_val.value() > 100",
"sat !opt_val.hasValue()",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}
Expand All@@ -199,6 +259,7 @@ public void repl_unknownCommandsAndErrors() throws Exception {
":unknown",
"invalid + + syntax",
":quit");

assertThat(output[1]).contains("Unknown command: :unknowncommand");
assertThat(output[1]).contains("Usage: :var <name> <type>");
assertThat(output[1]).contains("Unsupported type");
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Strip utm_, fbclid, gclid, etc. from all links on page\n(function() {\n var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content',\n 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid',\n 'ref', 'ref_src', 'source', 'medium', 'campaign'];\n \n function cleanUrl(url) {\n try {\n var u = new URL(url, window.location.origin);\n var changed = false;\n trackingParams.forEach(function(p) {\n if (u.searchParams.has(p)) {\n u.searchParams.delete(p);\n changed = true;\n }\n });\n return changed ? u.toString() : url;\n } catch (e) {\n return url;\n }\n }\n \n function cleanLinks() {\n document.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n \n cleanLinks();\n \n var observer = new MutationObserver(function(mutations) {\n mutations.forEach(function(m) {\n m.addedNodes.forEach(function(node) {\n if (node.nodeType === 1) {\n if (node.tagName === 'A') cleanLinks();\n node.querySelectorAll('a[href]').forEach(function(a) {\n var clean = cleanUrl(a.href);\n if (clean !== a.href) a.href = clean;\n });\n }\n });\n });\n });\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "Remove Tracking Parameters from Links"); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -88,7 +88,7 @@ private static String formatExpr(
} else if (decl.equals(typeSystem.timestampCons().ConstructorDecl())) {
return "timestamp(" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + ")";
} else if (decl.equals(typeSystem.durationCons().ConstructorDecl())) {
return "duration(" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + ")";
return "duration('" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + "s')";
} else if (decl.equals(typeSystem.uintCons().ConstructorDecl())) {
return formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + "u";
} else if (decl.equals(typeSystem.boolCons().ConstructorDecl())) {
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -314,15 +314,21 @@ private static void printHelp(String topic, PrintStream out) {
out.println("Declares a variable in the REPL session with a specific type.");
out.println();
out.println("Supported Types:");
out.println(" - Primitive types: int, uint, string, bool, double, bytes");
out.println(" - List types: list<T> (e.g., list<int>, list<string>)");
out.println(" - Map types: map<K,V> (e.g., map<string,int>, map<string,string>)");
out.println(" - Primitive types: int, uint, string, bool, double, bytes, dyn");
out.println(" - Well-known types: timestamp, duration");
out.println(" - List types: list<T> (e.g., list<int>, list<string>)");
out.println(" - Map types: map<K,V> (e.g., map<string,int>, map<string,string>)");
out.println(" - Optional types: optional<T> (e.g., optional<string>, optional<int>)");
out.println(" - Protobuf types: coming soon");
out.println();
out.println("Examples:");
out.println(" cel-verifier> :var role string");
out.println(" cel-verifier> :var port int");
out.println(" cel-verifier> :var scores map<string,int>");
out.println(" cel-verifier> :var tags list<string>");
out.println(" cel-verifier> :var created_at timestamp");
out.println(" cel-verifier> :var timeout duration");
out.println(" cel-verifier> :var opt_flag optional<bool>");
break;
case "unknown":
out.println("Command: :unknown <identifier>");
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@
import dev.cel.common.types.CelType;
import dev.cel.common.types.ListType;
import dev.cel.common.types.MapType;
import dev.cel.common.types.OptionalType;
import dev.cel.common.types.SimpleType;
import java.time.Duration;
import java.util.ArrayList;
Expand DownExpand Up@@ -147,17 +148,20 @@ static ImmutableMap<String, CelType> parseVariables(List<String> varSpecs) {
}

static CelType parseCelType(String typeStr) {
// TODO: Replace with shorthand type parser once it is available.
Preconditions.checkNotNull(typeStr, "Type string cannot be null.");
String str = typeStr.trim().toLowerCase(Locale.US);

if (str.startsWith("list<") && str.endsWith(">")) {
String inner = str.substring(5, str.length() - 1).trim();
// Strip "list<" prefix and trailing ">" to extract the element type "T".
String inner = str.substring("list<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return ListType.create(elemType);
}

if (str.startsWith("map<") && str.endsWith(">")) {
String inner = str.substring(4, str.length() - 1).trim();
// Strip "map<" prefix and trailing ">" to extract the key and value types "K, V".
String inner = str.substring("map<".length(), str.length() - 1).trim();
List<String> parts = splitGenericArgs(inner);
if (parts.size() != 2) {
throw new IllegalArgumentException(
Expand All@@ -170,6 +174,20 @@ static CelType parseCelType(String typeStr) {
return MapType.create(keyType, valueType);
}

if (str.startsWith("optional<") && str.endsWith(">")) {
// Strip "optional<" prefix and trailing ">" to extract the wrapped type "T".
String inner = str.substring("optional<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return OptionalType.create(elemType);
}

if (str.startsWith("optional_type<") && str.endsWith(">")) {
// Strip "optional_type<" prefix and trailing ">" to extract the wrapped type "T".
String inner = str.substring("optional_type<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return OptionalType.create(elemType);
}

switch (str) {
case "int":
return SimpleType.INT;
Expand All@@ -187,12 +205,19 @@ static CelType parseCelType(String typeStr) {
return SimpleType.BYTES;
case "dyn":
return SimpleType.DYN;
case "timestamp":
case "google.protobuf.timestamp":
return SimpleType.TIMESTAMP;
case "duration":
case "google.protobuf.duration":
return SimpleType.DURATION;
default:
// TODO: Support protobuf message types (coming soon).
throw new IllegalArgumentException(
"Unsupported type for CLI variable declaration: '"
+ typeStr
+ "'. Supported types: int, uint, string, bool, double, bytes, dyn, list<T>, map<K,"
+ " V>.");
+ "'. Supported types: int, uint, string, bool, double, bytes, dyn, timestamp,"
+ " duration, list<T>, map<K, V>, optional<T>.");
}
}

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -1312,7 +1312,7 @@ private enum IsAlwaysTrueViolationTestCase {
"dur != dur",
"Condition is not always true\\.",
"Counterexample input:",
"dur = duration\\(-?\\d+\\)"),
"dur = duration\\('-?\\d+s'\\)"),
TIMESTAMP_VARIABLE_COUNTEREXAMPLE(
"ts != ts",
"Condition is not always true\\.",
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -53,9 +53,11 @@ private String[] runReplWithCommands(String... commands) throws Exception {
@Test
public void repl_quitAndExit() throws Exception {
String[] output1 = runReplWithCommands(":quit");

assertThat(output1[0]).contains("Goodbye!");

String[] output2 = runReplWithCommands(":exit");

assertThat(output2[0]).contains("Goodbye!");
}

Expand All@@ -73,6 +75,7 @@ public void repl_helpCommands() throws Exception {
":help equiv",
":help non_existent_topic",
":quit");

assertThat(output[0]).contains("REPL Commands:");
assertThat(output[0]).contains("Command: :var <name> <type>");
assertThat(output[0]).contains("Command: :unknown <identifier>");
Expand All@@ -81,6 +84,9 @@ public void repl_helpCommands() throws Exception {
assertThat(output[0]).contains("Query: sat <expression>");
assertThat(output[0]).contains("Query: valid <expression>");
assertThat(output[0]).contains("Query: equiv <expression1> <=> <expression2>");
assertThat(output[0]).contains("Well-known types: timestamp, duration");
assertThat(output[0]).contains("Optional types: optional<T>");
assertThat(output[0]).contains("Protobuf types: coming soon");
}

@Test
Expand All@@ -91,19 +97,27 @@ public void repl_varDeclarations() throws Exception {
":var port int",
":var scores map<string,int>",
":var tags list<string>",
":var created_at timestamp",
":var timeout duration",
":var opt_user optional<string>",
":vars",
":quit");

assertThat(output[0]).contains("Variable declared: role : string");
assertThat(output[0]).contains("Variable declared: port : int");
assertThat(output[0]).contains("Variable declared: scores : map(string, int)");
assertThat(output[0]).contains("Variable declared: tags : list(string)");
assertThat(output[0]).contains("Variables (4):");
assertThat(output[0]).contains("Variable declared: created_at : google.protobuf.Timestamp");
assertThat(output[0]).contains("Variable declared: timeout : google.protobuf.Duration");
assertThat(output[0]).contains("Variable declared: opt_user : optional_type(string)");
assertThat(output[0]).contains("Variables (7):");
}

@Test
public void repl_unknownIdentifiers() throws Exception {
String[] output =
runReplWithCommands(":unknown request.headers", ":unknown request.auth", ":vars", ":quit");

assertThat(output[0]).contains("Added unknown identifier: 'request.headers'");
assertThat(output[0]).contains("Added unknown identifier: 'request.auth'");
assertThat(output[0]).contains("Unknowns: [request.headers, request.auth]");
Expand All@@ -114,6 +128,7 @@ public void repl_timeoutConfiguration() throws Exception {
String[] output =
runReplWithCommands(
":timeout 15", ":vars", ":timeout -5", ":timeout abc", ":timeout", ":quit");

assertThat(output[0]).contains("Timeout set to 15s.");
assertThat(output[0]).contains("Timeout: 15s");
assertThat(output[1]).contains("Timeout must be a positive integer.");
Expand All@@ -125,6 +140,7 @@ public void repl_timeoutConfiguration() throws Exception {
public void repl_unrollConfiguration() throws Exception {
String[] output =
runReplWithCommands(":unroll 10", ":vars", ":unroll -1", ":unroll xyz", ":unroll", ":quit");

assertThat(output[0]).contains("Comprehension unroll limit set to 10.");
assertThat(output[0]).contains("Unroll limit: 10");
assertThat(output[1]).contains("Unroll limit must be non-negative.");
Expand All@@ -137,6 +153,7 @@ public void repl_sessionStateAndClear() throws Exception {
String[] output =
runReplWithCommands(
":var role string", ":unknown req.headers", ":vars", ":clear", ":vars", ":quit");

assertThat(output[0]).contains("Variables (1):");
assertThat(output[0]).contains("Session state reset.");
assertThat(output[0]).contains("Variables (0):");
Expand All@@ -147,6 +164,7 @@ public void repl_sessionStateAndClear() throws Exception {
public void repl_satQueries() throws Exception {
String[] output =
runReplWithCommands(":var port int", "sat port > 1024", "port > 1024", "sat", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).contains("Usage: sat <expression>");
}
Expand All@@ -155,6 +173,7 @@ public void repl_satQueries() throws Exception {
public void repl_validQueries() throws Exception {
String[] output =
runReplWithCommands(":var x int", "valid x > 0 || x <= 0", "valid x > 0", "valid", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[0]).contains("[VIOLATED]");
assertThat(output[1]).contains("Usage: valid <expression>");
Expand All@@ -164,13 +183,15 @@ public void repl_validQueries() throws Exception {
public void repl_equivQueries() throws Exception {
String[] output =
runReplWithCommands(":var x int", "equiv x > 10 <=> 10 < x", "equiv x > 10", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).contains("Equivalence query format: equiv <expr1> <=> <expr2>");
}

@Test
public void repl_equivDoubleNegation() throws Exception {
String[] output = runReplWithCommands(":var x int", "equiv !!(x == 10) <=> (x == 10)", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
}

Expand All@@ -184,6 +205,45 @@ public void repl_equivCanonicalization() throws Exception {
+ " v, v == 1 && k == 'foo')",
"equiv int_list.all(e, e > 0) <=> int_list.all(elem, elem > 0)",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_timestampAndDurationQueries() throws Exception {
String[] output =
runReplWithCommands(
":var t timestamp",
":var d duration",
"sat t > timestamp(1000)",
"sat d > duration('60s')",
"sat t + d > timestamp(2000)",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_durationSatisfyingInputFormat() throws Exception {
String[] output =
runReplWithCommands(":var dur duration", "timestamp(100) - timestamp(50) == dur", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[0]).contains("dur = duration('50s')");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_optionalQueries() throws Exception {
String[] output =
runReplWithCommands(
":var opt_val optional<int>",
"sat opt_val.hasValue() && opt_val.value() > 100",
"sat !opt_val.hasValue()",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}
Expand All@@ -199,6 +259,7 @@ public void repl_unknownCommandsAndErrors() throws Exception {
":unknown",
"invalid + + syntax",
":quit");

assertThat(output[1]).contains("Unknown command: :unknowncommand");
assertThat(output[1]).contains("Usage: :var <name> <type>");
assertThat(output[1]).contains("Unsupported type");
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Auto-enable theater mode on YouTube\n(function() {\n function tryTheater() {\n var btn = document.querySelector('button[aria-label=\"Theater mode\"], ytd-player #player button[title=\"Theater mode\"]');\n if (btn && !btn.classList.contains('activated')) {\n btn.click();\n }\n }\n \n // Try immediately\n tryTheater();\n \n // Try after navigation (SPA)\n var lastUrl = location.href;\n setInterval(function() {\n if (location.href !== lastUrl) {\n lastUrl = location.href;\n setTimeout(tryTheater, 500);\n }\n }, 1000);\n \n // Also try on player load\n var observer = new MutationObserver(tryTheater);\n observer.observe(document.body, { childList: true, subtree: true });\n})();", "YouTube Theater Mode Default"); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -88,7 +88,7 @@ private static String formatExpr(
} else if (decl.equals(typeSystem.timestampCons().ConstructorDecl())) {
return "timestamp(" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + ")";
} else if (decl.equals(typeSystem.durationCons().ConstructorDecl())) {
return "duration(" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + ")";
return "duration('" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + "s')";
} else if (decl.equals(typeSystem.uintCons().ConstructorDecl())) {
return formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + "u";
} else if (decl.equals(typeSystem.boolCons().ConstructorDecl())) {
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -314,15 +314,21 @@ private static void printHelp(String topic, PrintStream out) {
out.println("Declares a variable in the REPL session with a specific type.");
out.println();
out.println("Supported Types:");
out.println(" - Primitive types: int, uint, string, bool, double, bytes");
out.println(" - List types: list<T> (e.g., list<int>, list<string>)");
out.println(" - Map types: map<K,V> (e.g., map<string,int>, map<string,string>)");
out.println(" - Primitive types: int, uint, string, bool, double, bytes, dyn");
out.println(" - Well-known types: timestamp, duration");
out.println(" - List types: list<T> (e.g., list<int>, list<string>)");
out.println(" - Map types: map<K,V> (e.g., map<string,int>, map<string,string>)");
out.println(" - Optional types: optional<T> (e.g., optional<string>, optional<int>)");
out.println(" - Protobuf types: coming soon");
out.println();
out.println("Examples:");
out.println(" cel-verifier> :var role string");
out.println(" cel-verifier> :var port int");
out.println(" cel-verifier> :var scores map<string,int>");
out.println(" cel-verifier> :var tags list<string>");
out.println(" cel-verifier> :var created_at timestamp");
out.println(" cel-verifier> :var timeout duration");
out.println(" cel-verifier> :var opt_flag optional<bool>");
break;
case "unknown":
out.println("Command: :unknown <identifier>");
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@
import dev.cel.common.types.CelType;
import dev.cel.common.types.ListType;
import dev.cel.common.types.MapType;
import dev.cel.common.types.OptionalType;
import dev.cel.common.types.SimpleType;
import java.time.Duration;
import java.util.ArrayList;
Expand DownExpand Up@@ -147,17 +148,20 @@ static ImmutableMap<String, CelType> parseVariables(List<String> varSpecs) {
}

static CelType parseCelType(String typeStr) {
// TODO: Replace with shorthand type parser once it is available.
Preconditions.checkNotNull(typeStr, "Type string cannot be null.");
String str = typeStr.trim().toLowerCase(Locale.US);

if (str.startsWith("list<") && str.endsWith(">")) {
String inner = str.substring(5, str.length() - 1).trim();
// Strip "list<" prefix and trailing ">" to extract the element type "T".
String inner = str.substring("list<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return ListType.create(elemType);
}

if (str.startsWith("map<") && str.endsWith(">")) {
String inner = str.substring(4, str.length() - 1).trim();
// Strip "map<" prefix and trailing ">" to extract the key and value types "K, V".
String inner = str.substring("map<".length(), str.length() - 1).trim();
List<String> parts = splitGenericArgs(inner);
if (parts.size() != 2) {
throw new IllegalArgumentException(
Expand All@@ -170,6 +174,20 @@ static CelType parseCelType(String typeStr) {
return MapType.create(keyType, valueType);
}

if (str.startsWith("optional<") && str.endsWith(">")) {
// Strip "optional<" prefix and trailing ">" to extract the wrapped type "T".
String inner = str.substring("optional<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return OptionalType.create(elemType);
}

if (str.startsWith("optional_type<") && str.endsWith(">")) {
// Strip "optional_type<" prefix and trailing ">" to extract the wrapped type "T".
String inner = str.substring("optional_type<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return OptionalType.create(elemType);
}

switch (str) {
case "int":
return SimpleType.INT;
Expand All@@ -187,12 +205,19 @@ static CelType parseCelType(String typeStr) {
return SimpleType.BYTES;
case "dyn":
return SimpleType.DYN;
case "timestamp":
case "google.protobuf.timestamp":
return SimpleType.TIMESTAMP;
case "duration":
case "google.protobuf.duration":
return SimpleType.DURATION;
default:
// TODO: Support protobuf message types (coming soon).
throw new IllegalArgumentException(
"Unsupported type for CLI variable declaration: '"
+ typeStr
+ "'. Supported types: int, uint, string, bool, double, bytes, dyn, list<T>, map<K,"
+ " V>.");
+ "'. Supported types: int, uint, string, bool, double, bytes, dyn, timestamp,"
+ " duration, list<T>, map<K, V>, optional<T>.");
}
}

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -1312,7 +1312,7 @@ private enum IsAlwaysTrueViolationTestCase {
"dur != dur",
"Condition is not always true\\.",
"Counterexample input:",
"dur = duration\\(-?\\d+\\)"),
"dur = duration\\('-?\\d+s'\\)"),
TIMESTAMP_VARIABLE_COUNTEREXAMPLE(
"ts != ts",
"Condition is not always true\\.",
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -53,9 +53,11 @@ private String[] runReplWithCommands(String... commands) throws Exception {
@Test
public void repl_quitAndExit() throws Exception {
String[] output1 = runReplWithCommands(":quit");

assertThat(output1[0]).contains("Goodbye!");

String[] output2 = runReplWithCommands(":exit");

assertThat(output2[0]).contains("Goodbye!");
}

Expand All@@ -73,6 +75,7 @@ public void repl_helpCommands() throws Exception {
":help equiv",
":help non_existent_topic",
":quit");

assertThat(output[0]).contains("REPL Commands:");
assertThat(output[0]).contains("Command: :var <name> <type>");
assertThat(output[0]).contains("Command: :unknown <identifier>");
Expand All@@ -81,6 +84,9 @@ public void repl_helpCommands() throws Exception {
assertThat(output[0]).contains("Query: sat <expression>");
assertThat(output[0]).contains("Query: valid <expression>");
assertThat(output[0]).contains("Query: equiv <expression1> <=> <expression2>");
assertThat(output[0]).contains("Well-known types: timestamp, duration");
assertThat(output[0]).contains("Optional types: optional<T>");
assertThat(output[0]).contains("Protobuf types: coming soon");
}

@Test
Expand All@@ -91,19 +97,27 @@ public void repl_varDeclarations() throws Exception {
":var port int",
":var scores map<string,int>",
":var tags list<string>",
":var created_at timestamp",
":var timeout duration",
":var opt_user optional<string>",
":vars",
":quit");

assertThat(output[0]).contains("Variable declared: role : string");
assertThat(output[0]).contains("Variable declared: port : int");
assertThat(output[0]).contains("Variable declared: scores : map(string, int)");
assertThat(output[0]).contains("Variable declared: tags : list(string)");
assertThat(output[0]).contains("Variables (4):");
assertThat(output[0]).contains("Variable declared: created_at : google.protobuf.Timestamp");
assertThat(output[0]).contains("Variable declared: timeout : google.protobuf.Duration");
assertThat(output[0]).contains("Variable declared: opt_user : optional_type(string)");
assertThat(output[0]).contains("Variables (7):");
}

@Test
public void repl_unknownIdentifiers() throws Exception {
String[] output =
runReplWithCommands(":unknown request.headers", ":unknown request.auth", ":vars", ":quit");

assertThat(output[0]).contains("Added unknown identifier: 'request.headers'");
assertThat(output[0]).contains("Added unknown identifier: 'request.auth'");
assertThat(output[0]).contains("Unknowns: [request.headers, request.auth]");
Expand All@@ -114,6 +128,7 @@ public void repl_timeoutConfiguration() throws Exception {
String[] output =
runReplWithCommands(
":timeout 15", ":vars", ":timeout -5", ":timeout abc", ":timeout", ":quit");

assertThat(output[0]).contains("Timeout set to 15s.");
assertThat(output[0]).contains("Timeout: 15s");
assertThat(output[1]).contains("Timeout must be a positive integer.");
Expand All@@ -125,6 +140,7 @@ public void repl_timeoutConfiguration() throws Exception {
public void repl_unrollConfiguration() throws Exception {
String[] output =
runReplWithCommands(":unroll 10", ":vars", ":unroll -1", ":unroll xyz", ":unroll", ":quit");

assertThat(output[0]).contains("Comprehension unroll limit set to 10.");
assertThat(output[0]).contains("Unroll limit: 10");
assertThat(output[1]).contains("Unroll limit must be non-negative.");
Expand All@@ -137,6 +153,7 @@ public void repl_sessionStateAndClear() throws Exception {
String[] output =
runReplWithCommands(
":var role string", ":unknown req.headers", ":vars", ":clear", ":vars", ":quit");

assertThat(output[0]).contains("Variables (1):");
assertThat(output[0]).contains("Session state reset.");
assertThat(output[0]).contains("Variables (0):");
Expand All@@ -147,6 +164,7 @@ public void repl_sessionStateAndClear() throws Exception {
public void repl_satQueries() throws Exception {
String[] output =
runReplWithCommands(":var port int", "sat port > 1024", "port > 1024", "sat", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).contains("Usage: sat <expression>");
}
Expand All@@ -155,6 +173,7 @@ public void repl_satQueries() throws Exception {
public void repl_validQueries() throws Exception {
String[] output =
runReplWithCommands(":var x int", "valid x > 0 || x <= 0", "valid x > 0", "valid", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[0]).contains("[VIOLATED]");
assertThat(output[1]).contains("Usage: valid <expression>");
Expand All@@ -164,13 +183,15 @@ public void repl_validQueries() throws Exception {
public void repl_equivQueries() throws Exception {
String[] output =
runReplWithCommands(":var x int", "equiv x > 10 <=> 10 < x", "equiv x > 10", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).contains("Equivalence query format: equiv <expr1> <=> <expr2>");
}

@Test
public void repl_equivDoubleNegation() throws Exception {
String[] output = runReplWithCommands(":var x int", "equiv !!(x == 10) <=> (x == 10)", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
}

Expand All@@ -184,6 +205,45 @@ public void repl_equivCanonicalization() throws Exception {
+ " v, v == 1 && k == 'foo')",
"equiv int_list.all(e, e > 0) <=> int_list.all(elem, elem > 0)",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_timestampAndDurationQueries() throws Exception {
String[] output =
runReplWithCommands(
":var t timestamp",
":var d duration",
"sat t > timestamp(1000)",
"sat d > duration('60s')",
"sat t + d > timestamp(2000)",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_durationSatisfyingInputFormat() throws Exception {
String[] output =
runReplWithCommands(":var dur duration", "timestamp(100) - timestamp(50) == dur", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[0]).contains("dur = duration('50s')");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_optionalQueries() throws Exception {
String[] output =
runReplWithCommands(
":var opt_val optional<int>",
"sat opt_val.hasValue() && opt_val.value() > 100",
"sat !opt_val.hasValue()",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}
Expand All@@ -199,6 +259,7 @@ public void repl_unknownCommandsAndErrors() throws Exception {
":unknown",
"invalid + + syntax",
":quit");

assertThat(output[1]).contains("Unknown command: :unknowncommand");
assertThat(output[1]).contains("Usage: :var <name> <type>");
assertThat(output[1]).contains("Unsupported type");
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Remove or un-stick sticky/fixed headers that block content\n(function() {\n function unstick() {\n document.querySelectorAll('header, nav, [role=\"banner\"], .header, .navbar, .sticky, .fixed-top, [style*=\"position: fixed\"], [style*=\"position:sticky\"]').forEach(function(el) {\n if (el.style.position === 'fixed' || el.style.position === 'sticky' || \n getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') {\n el.style.position = 'static';\n el.style.top = 'auto';\n el.style.zIndex = 'auto';\n }\n });\n }\n \n unstick();\n \n var observer = new MutationObserver(unstick);\n observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] });\n})();", "Kill Sticky Headers"); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + '
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -88,7 +88,7 @@ private static String formatExpr(
} else if (decl.equals(typeSystem.timestampCons().ConstructorDecl())) {
return "timestamp(" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + ")";
} else if (decl.equals(typeSystem.durationCons().ConstructorDecl())) {
return "duration(" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + ")";
return "duration('" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + "s')";
} else if (decl.equals(typeSystem.uintCons().ConstructorDecl())) {
return formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + "u";
} else if (decl.equals(typeSystem.boolCons().ConstructorDecl())) {
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -314,15 +314,21 @@ private static void printHelp(String topic, PrintStream out) {
out.println("Declares a variable in the REPL session with a specific type.");
out.println();
out.println("Supported Types:");
out.println(" - Primitive types: int, uint, string, bool, double, bytes");
out.println(" - List types: list<T> (e.g., list<int>, list<string>)");
out.println(" - Map types: map<K,V> (e.g., map<string,int>, map<string,string>)");
out.println(" - Primitive types: int, uint, string, bool, double, bytes, dyn");
out.println(" - Well-known types: timestamp, duration");
out.println(" - List types: list<T> (e.g., list<int>, list<string>)");
out.println(" - Map types: map<K,V> (e.g., map<string,int>, map<string,string>)");
out.println(" - Optional types: optional<T> (e.g., optional<string>, optional<int>)");
out.println(" - Protobuf types: coming soon");
out.println();
out.println("Examples:");
out.println(" cel-verifier> :var role string");
out.println(" cel-verifier> :var port int");
out.println(" cel-verifier> :var scores map<string,int>");
out.println(" cel-verifier> :var tags list<string>");
out.println(" cel-verifier> :var created_at timestamp");
out.println(" cel-verifier> :var timeout duration");
out.println(" cel-verifier> :var opt_flag optional<bool>");
break;
case "unknown":
out.println("Command: :unknown <identifier>");
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@
import dev.cel.common.types.CelType;
import dev.cel.common.types.ListType;
import dev.cel.common.types.MapType;
import dev.cel.common.types.OptionalType;
import dev.cel.common.types.SimpleType;
import java.time.Duration;
import java.util.ArrayList;
Expand DownExpand Up@@ -147,17 +148,20 @@ static ImmutableMap<String, CelType> parseVariables(List<String> varSpecs) {
}

static CelType parseCelType(String typeStr) {
// TODO: Replace with shorthand type parser once it is available.
Preconditions.checkNotNull(typeStr, "Type string cannot be null.");
String str = typeStr.trim().toLowerCase(Locale.US);

if (str.startsWith("list<") && str.endsWith(">")) {
String inner = str.substring(5, str.length() - 1).trim();
// Strip "list<" prefix and trailing ">" to extract the element type "T".
String inner = str.substring("list<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return ListType.create(elemType);
}

if (str.startsWith("map<") && str.endsWith(">")) {
String inner = str.substring(4, str.length() - 1).trim();
// Strip "map<" prefix and trailing ">" to extract the key and value types "K, V".
String inner = str.substring("map<".length(), str.length() - 1).trim();
List<String> parts = splitGenericArgs(inner);
if (parts.size() != 2) {
throw new IllegalArgumentException(
Expand All@@ -170,6 +174,20 @@ static CelType parseCelType(String typeStr) {
return MapType.create(keyType, valueType);
}

if (str.startsWith("optional<") && str.endsWith(">")) {
// Strip "optional<" prefix and trailing ">" to extract the wrapped type "T".
String inner = str.substring("optional<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return OptionalType.create(elemType);
}

if (str.startsWith("optional_type<") && str.endsWith(">")) {
// Strip "optional_type<" prefix and trailing ">" to extract the wrapped type "T".
String inner = str.substring("optional_type<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return OptionalType.create(elemType);
}

switch (str) {
case "int":
return SimpleType.INT;
Expand All@@ -187,12 +205,19 @@ static CelType parseCelType(String typeStr) {
return SimpleType.BYTES;
case "dyn":
return SimpleType.DYN;
case "timestamp":
case "google.protobuf.timestamp":
return SimpleType.TIMESTAMP;
case "duration":
case "google.protobuf.duration":
return SimpleType.DURATION;
default:
// TODO: Support protobuf message types (coming soon).
throw new IllegalArgumentException(
"Unsupported type for CLI variable declaration: '"
+ typeStr
+ "'. Supported types: int, uint, string, bool, double, bytes, dyn, list<T>, map<K,"
+ " V>.");
+ "'. Supported types: int, uint, string, bool, double, bytes, dyn, timestamp,"
+ " duration, list<T>, map<K, V>, optional<T>.");
}
}

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -1312,7 +1312,7 @@ private enum IsAlwaysTrueViolationTestCase {
"dur != dur",
"Condition is not always true\\.",
"Counterexample input:",
"dur = duration\\(-?\\d+\\)"),
"dur = duration\\('-?\\d+s'\\)"),
TIMESTAMP_VARIABLE_COUNTEREXAMPLE(
"ts != ts",
"Condition is not always true\\.",
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -53,9 +53,11 @@ private String[] runReplWithCommands(String... commands) throws Exception {
@Test
public void repl_quitAndExit() throws Exception {
String[] output1 = runReplWithCommands(":quit");

assertThat(output1[0]).contains("Goodbye!");

String[] output2 = runReplWithCommands(":exit");

assertThat(output2[0]).contains("Goodbye!");
}

Expand All@@ -73,6 +75,7 @@ public void repl_helpCommands() throws Exception {
":help equiv",
":help non_existent_topic",
":quit");

assertThat(output[0]).contains("REPL Commands:");
assertThat(output[0]).contains("Command: :var <name> <type>");
assertThat(output[0]).contains("Command: :unknown <identifier>");
Expand All@@ -81,6 +84,9 @@ public void repl_helpCommands() throws Exception {
assertThat(output[0]).contains("Query: sat <expression>");
assertThat(output[0]).contains("Query: valid <expression>");
assertThat(output[0]).contains("Query: equiv <expression1> <=> <expression2>");
assertThat(output[0]).contains("Well-known types: timestamp, duration");
assertThat(output[0]).contains("Optional types: optional<T>");
assertThat(output[0]).contains("Protobuf types: coming soon");
}

@Test
Expand All@@ -91,19 +97,27 @@ public void repl_varDeclarations() throws Exception {
":var port int",
":var scores map<string,int>",
":var tags list<string>",
":var created_at timestamp",
":var timeout duration",
":var opt_user optional<string>",
":vars",
":quit");

assertThat(output[0]).contains("Variable declared: role : string");
assertThat(output[0]).contains("Variable declared: port : int");
assertThat(output[0]).contains("Variable declared: scores : map(string, int)");
assertThat(output[0]).contains("Variable declared: tags : list(string)");
assertThat(output[0]).contains("Variables (4):");
assertThat(output[0]).contains("Variable declared: created_at : google.protobuf.Timestamp");
assertThat(output[0]).contains("Variable declared: timeout : google.protobuf.Duration");
assertThat(output[0]).contains("Variable declared: opt_user : optional_type(string)");
assertThat(output[0]).contains("Variables (7):");
}

@Test
public void repl_unknownIdentifiers() throws Exception {
String[] output =
runReplWithCommands(":unknown request.headers", ":unknown request.auth", ":vars", ":quit");

assertThat(output[0]).contains("Added unknown identifier: 'request.headers'");
assertThat(output[0]).contains("Added unknown identifier: 'request.auth'");
assertThat(output[0]).contains("Unknowns: [request.headers, request.auth]");
Expand All@@ -114,6 +128,7 @@ public void repl_timeoutConfiguration() throws Exception {
String[] output =
runReplWithCommands(
":timeout 15", ":vars", ":timeout -5", ":timeout abc", ":timeout", ":quit");

assertThat(output[0]).contains("Timeout set to 15s.");
assertThat(output[0]).contains("Timeout: 15s");
assertThat(output[1]).contains("Timeout must be a positive integer.");
Expand All@@ -125,6 +140,7 @@ public void repl_timeoutConfiguration() throws Exception {
public void repl_unrollConfiguration() throws Exception {
String[] output =
runReplWithCommands(":unroll 10", ":vars", ":unroll -1", ":unroll xyz", ":unroll", ":quit");

assertThat(output[0]).contains("Comprehension unroll limit set to 10.");
assertThat(output[0]).contains("Unroll limit: 10");
assertThat(output[1]).contains("Unroll limit must be non-negative.");
Expand All@@ -137,6 +153,7 @@ public void repl_sessionStateAndClear() throws Exception {
String[] output =
runReplWithCommands(
":var role string", ":unknown req.headers", ":vars", ":clear", ":vars", ":quit");

assertThat(output[0]).contains("Variables (1):");
assertThat(output[0]).contains("Session state reset.");
assertThat(output[0]).contains("Variables (0):");
Expand All@@ -147,6 +164,7 @@ public void repl_sessionStateAndClear() throws Exception {
public void repl_satQueries() throws Exception {
String[] output =
runReplWithCommands(":var port int", "sat port > 1024", "port > 1024", "sat", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).contains("Usage: sat <expression>");
}
Expand All@@ -155,6 +173,7 @@ public void repl_satQueries() throws Exception {
public void repl_validQueries() throws Exception {
String[] output =
runReplWithCommands(":var x int", "valid x > 0 || x <= 0", "valid x > 0", "valid", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[0]).contains("[VIOLATED]");
assertThat(output[1]).contains("Usage: valid <expression>");
Expand All@@ -164,13 +183,15 @@ public void repl_validQueries() throws Exception {
public void repl_equivQueries() throws Exception {
String[] output =
runReplWithCommands(":var x int", "equiv x > 10 <=> 10 < x", "equiv x > 10", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).contains("Equivalence query format: equiv <expr1> <=> <expr2>");
}

@Test
public void repl_equivDoubleNegation() throws Exception {
String[] output = runReplWithCommands(":var x int", "equiv !!(x == 10) <=> (x == 10)", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
}

Expand All@@ -184,6 +205,45 @@ public void repl_equivCanonicalization() throws Exception {
+ " v, v == 1 && k == 'foo')",
"equiv int_list.all(e, e > 0) <=> int_list.all(elem, elem > 0)",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_timestampAndDurationQueries() throws Exception {
String[] output =
runReplWithCommands(
":var t timestamp",
":var d duration",
"sat t > timestamp(1000)",
"sat d > duration('60s')",
"sat t + d > timestamp(2000)",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_durationSatisfyingInputFormat() throws Exception {
String[] output =
runReplWithCommands(":var dur duration", "timestamp(100) - timestamp(50) == dur", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[0]).contains("dur = duration('50s')");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_optionalQueries() throws Exception {
String[] output =
runReplWithCommands(
":var opt_val optional<int>",
"sat opt_val.hasValue() && opt_val.value() > 100",
"sat !opt_val.hasValue()",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}
Expand All@@ -199,6 +259,7 @@ public void repl_unknownCommandsAndErrors() throws Exception {
":unknown",
"invalid + + syntax",
":quit");

assertThat(output[1]).contains("Unknown command: :unknowncommand");
assertThat(output[1]).contains("Usage: :var <name> <type>");
assertThat(output[1]).contains("Unsupported type");
Expand Down
Loading
Loading
, 'i'); if (__m === '*' || __re.test(location.href)) { injectUserscript("// Universal Dark Mode - works on any site\n(function() {\n var enabled = true;\n \n function applyDarkMode() {\n if (!enabled) return;\n \n // Create style element if it doesn't exist\n var style = document.getElementById('universal-dark-mode-style');\n if (!style) {\n style = document.createElement('style');\n style.id = 'universal-dark-mode-style';\n document.head.appendChild(style);\n }\n \n // Dark mode CSS - inverts colors but preserves images/video\n style.textContent = '\n /* Invert everything except media */\n html {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #1a1a2e !important;\n }\n \n /* Restore images, videos, iframes, canvas */\n img, video, iframe, canvas, svg, picture, [style*=\"background-image\"] {\n filter: invert(1) hue-rotate(180deg) !important;\n }\n \n /* Preserve specific elements that should not be inverted */\n .no-dark-mode, .no-dark-mode *,\n [data-theme=\"light\"], [data-theme=\"light\"],\n .ace_editor, .ace_editor *,\n .CodeMirror, .CodeMirror *,\n .monaco-editor, .monaco-editor *,\n .markdown-body pre, .markdown-body pre *,\n .highlight, .highlight *,\n pre code, pre code * {\n filter: none !important;\n }\n \n /* Fix common UI elements */\n .modal, .popup, .dropdown-menu, .tooltip, .popover {\n filter: invert(1) hue-rotate(180deg) !important;\n background: #2d2d44 !important;\n border-color: #444 !important;\n }\n \n /* Scrollbars */\n ::-webkit-scrollbar { background: #1a1a2e !important; }\n ::-webkit-scrollbar-thumb { background: #444 !important; }\n ::-webkit-scrollbar-thumb:hover { background: #555 !important; }\n \n /* Selection */\n ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; }\n ';\n }\n \n function removeDarkMode() {\n var style = document.getElementById('universal-dark-mode-style');\n if (style) style.remove();\n }\n \n // Toggle with Alt+Shift+D\n document.addEventListener('keydown', function(e) {\n if (e.altKey && e.shiftKey && e.key === 'D') {\n e.preventDefault();\n enabled = !enabled;\n if (enabled) {\n applyDarkMode();\n console.log('[Universal Dark Mode] Enabled');\n } else {\n removeDarkMode();\n console.log('[Universal Dark Mode] Disabled');\n }\n }\n });\n \n // Apply on load\n applyDarkMode();\n \n // Re-apply on dynamic content\n var observer = new MutationObserver(function(mutations) {\n if (enabled && !document.getElementById('universal-dark-mode-style')) {\n applyDarkMode();\n }\n });\n observer.observe(document.head, { childList: true });\n \n console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle');\n})();", "Universal Dark Mode"); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })();
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line numberDiff line numberDiff line change
Expand Up@@ -88,7 +88,7 @@ private static String formatExpr(
} else if (decl.equals(typeSystem.timestampCons().ConstructorDecl())) {
return "timestamp(" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + ")";
} else if (decl.equals(typeSystem.durationCons().ConstructorDecl())) {
return "duration(" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + ")";
return "duration('" + formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + "s')";
} else if (decl.equals(typeSystem.uintCons().ConstructorDecl())) {
return formatExpr(ctx, typeSystem, model, expr.getArgs()[0]) + "u";
} else if (decl.equals(typeSystem.boolCons().ConstructorDecl())) {
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -314,15 +314,21 @@ private static void printHelp(String topic, PrintStream out) {
out.println("Declares a variable in the REPL session with a specific type.");
out.println();
out.println("Supported Types:");
out.println(" - Primitive types: int, uint, string, bool, double, bytes");
out.println(" - List types: list<T> (e.g., list<int>, list<string>)");
out.println(" - Map types: map<K,V> (e.g., map<string,int>, map<string,string>)");
out.println(" - Primitive types: int, uint, string, bool, double, bytes, dyn");
out.println(" - Well-known types: timestamp, duration");
out.println(" - List types: list<T> (e.g., list<int>, list<string>)");
out.println(" - Map types: map<K,V> (e.g., map<string,int>, map<string,string>)");
out.println(" - Optional types: optional<T> (e.g., optional<string>, optional<int>)");
out.println(" - Protobuf types: coming soon");
out.println();
out.println("Examples:");
out.println(" cel-verifier> :var role string");
out.println(" cel-verifier> :var port int");
out.println(" cel-verifier> :var scores map<string,int>");
out.println(" cel-verifier> :var tags list<string>");
out.println(" cel-verifier> :var created_at timestamp");
out.println(" cel-verifier> :var timeout duration");
out.println(" cel-verifier> :var opt_flag optional<bool>");
break;
case "unknown":
out.println("Command: :unknown <identifier>");
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -21,6 +21,7 @@
import dev.cel.common.types.CelType;
import dev.cel.common.types.ListType;
import dev.cel.common.types.MapType;
import dev.cel.common.types.OptionalType;
import dev.cel.common.types.SimpleType;
import java.time.Duration;
import java.util.ArrayList;
Expand DownExpand Up@@ -147,17 +148,20 @@ static ImmutableMap<String, CelType> parseVariables(List<String> varSpecs) {
}

static CelType parseCelType(String typeStr) {
// TODO: Replace with shorthand type parser once it is available.
Preconditions.checkNotNull(typeStr, "Type string cannot be null.");
String str = typeStr.trim().toLowerCase(Locale.US);

if (str.startsWith("list<") && str.endsWith(">")) {
String inner = str.substring(5, str.length() - 1).trim();
// Strip "list<" prefix and trailing ">" to extract the element type "T".
String inner = str.substring("list<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return ListType.create(elemType);
}

if (str.startsWith("map<") && str.endsWith(">")) {
String inner = str.substring(4, str.length() - 1).trim();
// Strip "map<" prefix and trailing ">" to extract the key and value types "K, V".
String inner = str.substring("map<".length(), str.length() - 1).trim();
List<String> parts = splitGenericArgs(inner);
if (parts.size() != 2) {
throw new IllegalArgumentException(
Expand All@@ -170,6 +174,20 @@ static CelType parseCelType(String typeStr) {
return MapType.create(keyType, valueType);
}

if (str.startsWith("optional<") && str.endsWith(">")) {
// Strip "optional<" prefix and trailing ">" to extract the wrapped type "T".
String inner = str.substring("optional<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return OptionalType.create(elemType);
}

if (str.startsWith("optional_type<") && str.endsWith(">")) {
// Strip "optional_type<" prefix and trailing ">" to extract the wrapped type "T".
String inner = str.substring("optional_type<".length(), str.length() - 1).trim();
CelType elemType = parseCelType(inner);
return OptionalType.create(elemType);
}

switch (str) {
case "int":
return SimpleType.INT;
Expand All@@ -187,12 +205,19 @@ static CelType parseCelType(String typeStr) {
return SimpleType.BYTES;
case "dyn":
return SimpleType.DYN;
case "timestamp":
case "google.protobuf.timestamp":
return SimpleType.TIMESTAMP;
case "duration":
case "google.protobuf.duration":
return SimpleType.DURATION;
default:
// TODO: Support protobuf message types (coming soon).
throw new IllegalArgumentException(
"Unsupported type for CLI variable declaration: '"
+ typeStr
+ "'. Supported types: int, uint, string, bool, double, bytes, dyn, list<T>, map<K,"
+ " V>.");
+ "'. Supported types: int, uint, string, bool, double, bytes, dyn, timestamp,"
+ " duration, list<T>, map<K, V>, optional<T>.");
}
}

Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -1312,7 +1312,7 @@ private enum IsAlwaysTrueViolationTestCase {
"dur != dur",
"Condition is not always true\\.",
"Counterexample input:",
"dur = duration\\(-?\\d+\\)"),
"dur = duration\\('-?\\d+s'\\)"),
TIMESTAMP_VARIABLE_COUNTEREXAMPLE(
"ts != ts",
"Condition is not always true\\.",
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -53,9 +53,11 @@ private String[] runReplWithCommands(String... commands) throws Exception {
@Test
public void repl_quitAndExit() throws Exception {
String[] output1 = runReplWithCommands(":quit");

assertThat(output1[0]).contains("Goodbye!");

String[] output2 = runReplWithCommands(":exit");

assertThat(output2[0]).contains("Goodbye!");
}

Expand All@@ -73,6 +75,7 @@ public void repl_helpCommands() throws Exception {
":help equiv",
":help non_existent_topic",
":quit");

assertThat(output[0]).contains("REPL Commands:");
assertThat(output[0]).contains("Command: :var <name> <type>");
assertThat(output[0]).contains("Command: :unknown <identifier>");
Expand All@@ -81,6 +84,9 @@ public void repl_helpCommands() throws Exception {
assertThat(output[0]).contains("Query: sat <expression>");
assertThat(output[0]).contains("Query: valid <expression>");
assertThat(output[0]).contains("Query: equiv <expression1> <=> <expression2>");
assertThat(output[0]).contains("Well-known types: timestamp, duration");
assertThat(output[0]).contains("Optional types: optional<T>");
assertThat(output[0]).contains("Protobuf types: coming soon");
}

@Test
Expand All@@ -91,19 +97,27 @@ public void repl_varDeclarations() throws Exception {
":var port int",
":var scores map<string,int>",
":var tags list<string>",
":var created_at timestamp",
":var timeout duration",
":var opt_user optional<string>",
":vars",
":quit");

assertThat(output[0]).contains("Variable declared: role : string");
assertThat(output[0]).contains("Variable declared: port : int");
assertThat(output[0]).contains("Variable declared: scores : map(string, int)");
assertThat(output[0]).contains("Variable declared: tags : list(string)");
assertThat(output[0]).contains("Variables (4):");
assertThat(output[0]).contains("Variable declared: created_at : google.protobuf.Timestamp");
assertThat(output[0]).contains("Variable declared: timeout : google.protobuf.Duration");
assertThat(output[0]).contains("Variable declared: opt_user : optional_type(string)");
assertThat(output[0]).contains("Variables (7):");
}

@Test
public void repl_unknownIdentifiers() throws Exception {
String[] output =
runReplWithCommands(":unknown request.headers", ":unknown request.auth", ":vars", ":quit");

assertThat(output[0]).contains("Added unknown identifier: 'request.headers'");
assertThat(output[0]).contains("Added unknown identifier: 'request.auth'");
assertThat(output[0]).contains("Unknowns: [request.headers, request.auth]");
Expand All@@ -114,6 +128,7 @@ public void repl_timeoutConfiguration() throws Exception {
String[] output =
runReplWithCommands(
":timeout 15", ":vars", ":timeout -5", ":timeout abc", ":timeout", ":quit");

assertThat(output[0]).contains("Timeout set to 15s.");
assertThat(output[0]).contains("Timeout: 15s");
assertThat(output[1]).contains("Timeout must be a positive integer.");
Expand All@@ -125,6 +140,7 @@ public void repl_timeoutConfiguration() throws Exception {
public void repl_unrollConfiguration() throws Exception {
String[] output =
runReplWithCommands(":unroll 10", ":vars", ":unroll -1", ":unroll xyz", ":unroll", ":quit");

assertThat(output[0]).contains("Comprehension unroll limit set to 10.");
assertThat(output[0]).contains("Unroll limit: 10");
assertThat(output[1]).contains("Unroll limit must be non-negative.");
Expand All@@ -137,6 +153,7 @@ public void repl_sessionStateAndClear() throws Exception {
String[] output =
runReplWithCommands(
":var role string", ":unknown req.headers", ":vars", ":clear", ":vars", ":quit");

assertThat(output[0]).contains("Variables (1):");
assertThat(output[0]).contains("Session state reset.");
assertThat(output[0]).contains("Variables (0):");
Expand All@@ -147,6 +164,7 @@ public void repl_sessionStateAndClear() throws Exception {
public void repl_satQueries() throws Exception {
String[] output =
runReplWithCommands(":var port int", "sat port > 1024", "port > 1024", "sat", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).contains("Usage: sat <expression>");
}
Expand All@@ -155,6 +173,7 @@ public void repl_satQueries() throws Exception {
public void repl_validQueries() throws Exception {
String[] output =
runReplWithCommands(":var x int", "valid x > 0 || x <= 0", "valid x > 0", "valid", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[0]).contains("[VIOLATED]");
assertThat(output[1]).contains("Usage: valid <expression>");
Expand All@@ -164,13 +183,15 @@ public void repl_validQueries() throws Exception {
public void repl_equivQueries() throws Exception {
String[] output =
runReplWithCommands(":var x int", "equiv x > 10 <=> 10 < x", "equiv x > 10", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).contains("Equivalence query format: equiv <expr1> <=> <expr2>");
}

@Test
public void repl_equivDoubleNegation() throws Exception {
String[] output = runReplWithCommands(":var x int", "equiv !!(x == 10) <=> (x == 10)", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
}

Expand All@@ -184,6 +205,45 @@ public void repl_equivCanonicalization() throws Exception {
+ " v, v == 1 && k == 'foo')",
"equiv int_list.all(e, e > 0) <=> int_list.all(elem, elem > 0)",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_timestampAndDurationQueries() throws Exception {
String[] output =
runReplWithCommands(
":var t timestamp",
":var d duration",
"sat t > timestamp(1000)",
"sat d > duration('60s')",
"sat t + d > timestamp(2000)",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_durationSatisfyingInputFormat() throws Exception {
String[] output =
runReplWithCommands(":var dur duration", "timestamp(100) - timestamp(50) == dur", ":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[0]).contains("dur = duration('50s')");
assertThat(output[1]).isEmpty();
}

@Test
public void repl_optionalQueries() throws Exception {
String[] output =
runReplWithCommands(
":var opt_val optional<int>",
"sat opt_val.hasValue() && opt_val.value() > 100",
"sat !opt_val.hasValue()",
":quit");

assertThat(output[0]).contains("[VERIFIED]");
assertThat(output[1]).isEmpty();
}
Expand All@@ -199,6 +259,7 @@ public void repl_unknownCommandsAndErrors() throws Exception {
":unknown",
"invalid + + syntax",
":quit");

assertThat(output[1]).contains("Unknown command: :unknowncommand");
assertThat(output[1]).contains("Usage: :var <name> <type>");
assertThat(output[1]).contains("Unsupported type");
Expand Down
Loading
Loading