Skip to content

Commit

Permalink
Support for validation of isNaN & isInf (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
elliotmjackson authored Aug 23, 2023
1 parent 07586b7 commit 0ee9a63
Show file tree
Hide file tree
Showing 4 changed files with 1,107 additions and 1,060 deletions.
12 changes: 8 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ GO ?= go
ARGS ?= --strict_message
JAVA_COMPILE_OPTIONS = --enable-preview --release $(JAVA_VERSION)
JAVA_OPTIONS = --enable-preview
PROTOVALIDATE_VERSION ?= v0.3.1
PROTOVALIDATE_VERSION ?= v0.4.0
JAVA_MAIN_CLASS = build.buf.protovalidate
JAVA_SOURCES = $(wildcard src/main/java/**/**/**/*.java, src/main/java/**/**/*.java)
JAVA_CLASSES = $(patsubst src/main/java/%.java, target/classes/%.class, $(JAVA_SOURCES))
Expand Down Expand Up @@ -59,9 +59,9 @@ help: ## Describe useful make targets
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "%-15s %s\n", $$1, $$2}'

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

.PHONY: lint
lint: ## Lint code
Expand All @@ -87,6 +87,10 @@ test: ## Run all tests.
$(BIN):
@mkdir -p $(BIN)

$(BIN)/buf: $(BIN) Makefile
GOBIN=$(abspath $(@D)) $(GO) install \
github.com/bufbuild/buf/cmd/buf@latest

$(BIN)/license-header: $(BIN) Makefile
GOBIN=$(abspath $(@D)) $(GO) install \
github.com/bufbuild/buf/private/pkg/licenseheader/cmd/license-header@latest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ static List<Decl> create() {
Decls.newInstanceOverload(
"contains_bytes", Arrays.asList(Decls.Bytes, Decls.Bytes), Decls.Bool)));

// Add 'isNan' function declaration
decls.add(
Decls.newFunction(
"isNan",
Decls.newInstanceOverload(
"is_nan", Collections.singletonList(Decls.Double), Decls.Bool)));

// Add 'isInf' function declaration
decls.add(
Decls.newFunction(
"isInf",
Decls.newInstanceOverload(
"is_inf_unary", Collections.singletonList(Decls.Double), Decls.Bool),
Decls.newInstanceOverload(
"is_inf_binary", Arrays.asList(Decls.Double, Decls.Int), Decls.Bool)));

// Add 'unique' function declaration
List<Decl.FunctionDecl.Overload> uniqueOverloads = new ArrayList<>();
for (com.google.api.expr.v1alpha1.Type type :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ static Overload[] create() {
unaryIsEmail(),
isIp(),
isUri(),
isUriRef()
isUriRef(),
isNan(),
isInf(),
};
}

Expand Down Expand Up @@ -305,6 +307,33 @@ private static Overload isUriRef() {
});
}

/**
* Creates a custom unary function overload for the "isNan" operation.
*
* @return The {@link Overload} instance for the "isNan" operation.
*/
private static Overload isNan() {
return Overload.unary(
"isNan", value -> value.convertToNative(Double.TYPE).isNaN() ? BoolT.True : BoolT.False);
}

/**
* Creates a custom unary function overload for the "isInf" operation.
*
* @return The {@link Overload} instance for the "isInf" operation.
*/
private static Overload isInf() {
return Overload.overload(
"isInf",
null,
value -> value.convertToNative(Double.TYPE).isInfinite() ? BoolT.True : BoolT.False,
(lhs, rhs) -> {
Double value = lhs.convertToNative(Double.TYPE);
return value.isInfinite(rhs.intValue()) ? BoolT.True : BoolT.False;
},
null);
}

/**
* Retrieves the appropriate unary operation for a primitive value based on its type. This method
* returns the unary operation that should be applied to the given primitive value.
Expand Down
Loading

0 comments on commit 0ee9a63

Please sign in to comment.