Skip to content

Commit 0ee9a63

Browse files
Support for validation of isNaN & isInf (#24)
1 parent 07586b7 commit 0ee9a63

File tree

4 files changed

+1107
-1060
lines changed

4 files changed

+1107
-1060
lines changed

Makefile

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ GO ?= go
1616
ARGS ?= --strict_message
1717
JAVA_COMPILE_OPTIONS = --enable-preview --release $(JAVA_VERSION)
1818
JAVA_OPTIONS = --enable-preview
19-
PROTOVALIDATE_VERSION ?= v0.3.1
19+
PROTOVALIDATE_VERSION ?= v0.4.0
2020
JAVA_MAIN_CLASS = build.buf.protovalidate
2121
JAVA_SOURCES = $(wildcard src/main/java/**/**/**/*.java, src/main/java/**/**/*.java)
2222
JAVA_CLASSES = $(patsubst src/main/java/%.java, target/classes/%.class, $(JAVA_SOURCES))
@@ -59,9 +59,9 @@ help: ## Describe useful make targets
5959
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "%-15s %s\n", $$1, $$2}'
6060

6161
.PHONY: generate
62-
generate: generate-license ## Regenerate code and license headers
63-
buf generate --template buf.gen.yaml buf.build/bufbuild/protovalidate
64-
buf generate --template conformance/buf.gen.yaml -o conformance/ buf.build/bufbuild/protovalidate-testing
62+
generate: $(BIN)/buf generate-license ## Regenerate code and license headers
63+
$(BIN)/buf generate --template buf.gen.yaml buf.build/bufbuild/protovalidate:$(PROTOVALIDATE_VERSION)
64+
$(BIN)/buf generate --template conformance/buf.gen.yaml -o conformance/ buf.build/bufbuild/protovalidate-testing:$(PROTOVALIDATE_VERSION)
6565

6666
.PHONY: lint
6767
lint: ## Lint code
@@ -87,6 +87,10 @@ test: ## Run all tests.
8787
$(BIN):
8888
@mkdir -p $(BIN)
8989

90+
$(BIN)/buf: $(BIN) Makefile
91+
GOBIN=$(abspath $(@D)) $(GO) install \
92+
github.com/bufbuild/buf/cmd/buf@latest
93+
9094
$(BIN)/license-header: $(BIN) Makefile
9195
GOBIN=$(abspath $(@D)) $(GO) install \
9296
github.com/bufbuild/buf/private/pkg/licenseheader/cmd/license-header@latest

src/main/java/build/buf/protovalidate/internal/celext/CustomDeclarations.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,22 @@ static List<Decl> create() {
9595
Decls.newInstanceOverload(
9696
"contains_bytes", Arrays.asList(Decls.Bytes, Decls.Bytes), Decls.Bool)));
9797

98+
// Add 'isNan' function declaration
99+
decls.add(
100+
Decls.newFunction(
101+
"isNan",
102+
Decls.newInstanceOverload(
103+
"is_nan", Collections.singletonList(Decls.Double), Decls.Bool)));
104+
105+
// Add 'isInf' function declaration
106+
decls.add(
107+
Decls.newFunction(
108+
"isInf",
109+
Decls.newInstanceOverload(
110+
"is_inf_unary", Collections.singletonList(Decls.Double), Decls.Bool),
111+
Decls.newInstanceOverload(
112+
"is_inf_binary", Arrays.asList(Decls.Double, Decls.Int), Decls.Bool)));
113+
98114
// Add 'unique' function declaration
99115
List<Decl.FunctionDecl.Overload> uniqueOverloads = new ArrayList<>();
100116
for (com.google.api.expr.v1alpha1.Type type :

src/main/java/build/buf/protovalidate/internal/celext/CustomOverload.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ static Overload[] create() {
6161
unaryIsEmail(),
6262
isIp(),
6363
isUri(),
64-
isUriRef()
64+
isUriRef(),
65+
isNan(),
66+
isInf(),
6567
};
6668
}
6769

@@ -305,6 +307,33 @@ private static Overload isUriRef() {
305307
});
306308
}
307309

310+
/**
311+
* Creates a custom unary function overload for the "isNan" operation.
312+
*
313+
* @return The {@link Overload} instance for the "isNan" operation.
314+
*/
315+
private static Overload isNan() {
316+
return Overload.unary(
317+
"isNan", value -> value.convertToNative(Double.TYPE).isNaN() ? BoolT.True : BoolT.False);
318+
}
319+
320+
/**
321+
* Creates a custom unary function overload for the "isInf" operation.
322+
*
323+
* @return The {@link Overload} instance for the "isInf" operation.
324+
*/
325+
private static Overload isInf() {
326+
return Overload.overload(
327+
"isInf",
328+
null,
329+
value -> value.convertToNative(Double.TYPE).isInfinite() ? BoolT.True : BoolT.False,
330+
(lhs, rhs) -> {
331+
Double value = lhs.convertToNative(Double.TYPE);
332+
return value.isInfinite(rhs.intValue()) ? BoolT.True : BoolT.False;
333+
},
334+
null);
335+
}
336+
308337
/**
309338
* Retrieves the appropriate unary operation for a primitive value based on its type. This method
310339
* returns the unary operation that should be applied to the given primitive value.

0 commit comments

Comments
 (0)