Skip to content

Commit

Permalink
Merge branch 'main' into bugfix/GH-585-walk-enum-parents
Browse files Browse the repository at this point in the history
  • Loading branch information
Elliot Jackson committed Sep 19, 2022
2 parents facd9fd + 05f8c12 commit 5137eee
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 9 deletions.
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,36 +84,36 @@ testcases: bin/protoc-gen-go ## generate the test harness case protos
-I . \
-I ../../../.. \
--go_out="module=${PACKAGE}/tests/harness/cases/other_package/go,${GO_IMPORT}:./go" \
--plugin=protoc-gen-go=${GOPATH}/bin/protoc-gen-go \
--plugin=protoc-gen-go=$(shell pwd)/bin/protoc-gen-go \
--validate_out="module=${PACKAGE}/tests/harness/cases/other_package/go,lang=go:./go" \
./*.proto
cd tests/harness/cases/yet_another_package && \
protoc \
-I . \
-I ../../../.. \
--go_out="module=${PACKAGE}/tests/harness/cases/yet_another_package/go,${GO_IMPORT}:./go" \
--plugin=protoc-gen-go=${GOPATH}/bin/protoc-gen-go \
--plugin=protoc-gen-go=$(shell pwd)/bin/protoc-gen-go \
--validate_out="module=${PACKAGE}/tests/harness/cases/yet_another_package/go,lang=go:./go" \
./*.proto
cd tests/harness/cases && \
protoc \
-I . \
-I ../../.. \
--go_out="module=${PACKAGE}/tests/harness/cases/go,Mtests/harness/cases/other_package/embed.proto=${PACKAGE}/tests/harness/cases/other_package/go;other_package,Mtests/harness/cases/yet_another_package/embed.proto=${PACKAGE}/tests/harness/cases/yet_another_package/go,${GO_IMPORT}:./go" \
--plugin=protoc-gen-go=${GOPATH}/bin/protoc-gen-go \
--plugin=protoc-gen-go=$(shell pwd)/bin/protoc-gen-go \
--validate_out="module=${PACKAGE}/tests/harness/cases/go,lang=go,Mtests/harness/cases/other_package/embed.proto=${PACKAGE}/tests/harness/cases/other_package/go,Mtests/harness/cases/yet_another_package/embed.proto=${PACKAGE}/tests/harness/cases/yet_another_package/go:./go" \
./*.proto

validate/validate.pb.go: bin/protoc-gen-go validate/validate.proto
protoc -I . \
--plugin=protoc-gen-go=${GOPATH}/bin/protoc-gen-go \
--plugin=protoc-gen-go=$(shell pwd)/bin/protoc-gen-go \
--go_opt=paths=source_relative \
--go_out="${GO_IMPORT}:." validate/validate.proto

tests/harness/go/harness.pb.go: bin/protoc-gen-go tests/harness/harness.proto
# generates the test harness protos
cd tests/harness && protoc -I . \
--plugin=protoc-gen-go=${GOPATH}/bin/protoc-gen-go \
--plugin=protoc-gen-go=$(shell pwd)/bin/protoc-gen-go \
--go_out="module=${PACKAGE}/tests/harness/go,${GO_IMPORT}:./go" harness.proto

tests/harness/go/main/go-harness:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ Person x = 1;
- **min_pairs/max_pairs**: these rules control how many KV pairs are contained in this field

```protobuf
// x must contain at most 3 KV pairs
// x must contain at least 3 KV pairs
map<string, uint64> x = 1 [(validate.rules).map.min_pairs = 3];
// x must contain between 5 and 10 KV pairs
Expand Down
19 changes: 19 additions & 0 deletions java/pgv-java-grpc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,25 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
19 changes: 19 additions & 0 deletions java/pgv-java-stub/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,25 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
19 changes: 19 additions & 0 deletions java/pgv-java-validation/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,25 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
59 changes: 59 additions & 0 deletions java/pgv-test-coverage-report/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>pgv-java</artifactId>
<groupId>io.envoyproxy.protoc-gen-validate</groupId>
<version>0.6.8-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>pgv-test-coverage-report</artifactId>
<name>PGV-Java Code Coverage Report</name>
<dependencies>
<dependency>
<groupId>io.envoyproxy.protoc-gen-validate</groupId>
<artifactId>protoc-gen-validate</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.envoyproxy.protoc-gen-validate</groupId>
<artifactId>pgv-java-stub</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.envoyproxy.protoc-gen-validate</groupId>
<artifactId>pgv-java-grpc</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.envoyproxy.protoc-gen-validate</groupId>
<artifactId>pgv-java-validation</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report-aggregate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
1 change: 1 addition & 0 deletions java/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<module>pgv-java-validation</module>
<module>pgv-java-grpc</module>
<module>pgv-artifacts</module>
<module>pgv-test-coverage-report</module>
</modules>

<packaging>pom</packaging>
Expand Down
3 changes: 2 additions & 1 deletion module/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/envoyproxy/protoc-gen-validate/templates/java"
pgs "github.com/lyft/protoc-gen-star"
pgsgo "github.com/lyft/protoc-gen-star/lang/go"
"path/filepath"
"strings"
)

Expand Down Expand Up @@ -51,7 +52,7 @@ func (m *Module) Execute(targets map[string]pgs.File, pkgs map[string]pgs.Packag
// implementation-specific FilePathFor implementations.
// Ex: Don't generate Java validators for files that don't reference PGV.
if out != nil {
outPath := strings.TrimLeft(strings.ReplaceAll(out.String(), module, ""), "/")
outPath := strings.TrimLeft(strings.ReplaceAll(filepath.ToSlash(out.String()), module, ""), "/")

if opts := f.Descriptor().GetOptions(); opts != nil && opts.GetJavaMultipleFiles() && lang == "java" {
// TODO: Only Java supports multiple file generation. If more languages add multiple file generation
Expand Down
14 changes: 14 additions & 0 deletions templates/cc/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ const msgTpl = `
{{- end }}
};
{{ end }}{{ end }}
{{ if has .Rules.Items.GetAny "In" }} {{ if .Rules.Items.GetAny.In }}
const std::set<string> {{ lookup .Field "InLookup" }} = {
{{- range .Rules.Items.GetAny.In }}
{{ inKey $f . }},
{{- end }}
};
{{ end }}{{ end }}
{{ end }}{{ end }}
{{ if has .Rules "Items"}}{{ if .Rules.Items }}
Expand All @@ -63,6 +70,13 @@ const msgTpl = `
{{- end }}
};
{{ end }}{{ end }}
{{ if has .Rules.Items.GetAny "NotIn" }} {{ if .Rules.Items.GetAny.NotIn }}
const std::set<string> {{ lookup .Field "NotInLookup" }} = {
{{- range .Rules.Items.GetAny.NotIn }}
{{ inKey $f . }},
{{- end }}
};
{{ end }}{{ end }}
{{ end }}{{ end }}
{{ if has .Rules "Pattern"}}{{ if .Rules.Pattern }}
Expand Down
14 changes: 14 additions & 0 deletions templates/goshared/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ var _ interface{
{{- end }}
}
{{ end }}{{ end }}
{{ if has .Rules.Items.GetAny "In" }} {{ if .Rules.Items.GetAny.In }}
var {{ lookup .Field "InLookup" }} = map[string]struct{}{
{{- range .Rules.Items.GetAny.In }}
{{ inKey $f . }}: {},
{{- end }}
}
{{ end }}{{ end }}
{{ end }}{{ end }}
{{ if has .Rules "Items"}}{{ if .Rules.Items }}
Expand All @@ -198,6 +205,13 @@ var _ interface{
{{- end }}
}
{{ end }}{{ end }}
{{ if has .Rules.Items.GetAny "NotIn" }} {{ if .Rules.Items.GetAny.NotIn }}
var {{ lookup .Field "NotInLookup" }} = map[string]struct{}{
{{- range .Rules.Items.GetAny.NotIn }}
{{ inKey $f . }}: {},
{{- end }}
}
{{ end }}{{ end }}
{{ end }}{{ end }}
{{ if has .Rules "Keys"}}{{ if .Rules.Keys }}
Expand Down
4 changes: 2 additions & 2 deletions templates/java/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,13 @@ func underscoreBetweenConsecutiveUppercase(name string) string {
func appendOuterClassName(outerClassName string, file pgs.File) string {
conflict := false

for _, enum := range file.Enums() {
for _, enum := range file.AllEnums() {
if enum.Name().String() == outerClassName {
conflict = true
}
}

for _, message := range file.Messages() {
for _, message := range file.AllMessages() {
if message.Name().String() == outerClassName {
conflict = true
}
Expand Down
3 changes: 3 additions & 0 deletions tests/harness/cases/repeated.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "tests/harness/cases/other_package/embed.proto";

import "validate/validate.proto";
import "google/protobuf/duration.proto";
import "google/protobuf/any.proto";

message Embed { int64 val = 1 [(validate.rules).int64.gt = 0]; }
enum AnEnum {
Expand All @@ -30,6 +31,8 @@ message RepeatedEnumIn { repeated AnEnum val = 1 [(validate.rules).repeated.item
message RepeatedEnumNotIn { repeated AnEnum val = 1 [(validate.rules).repeated.items.enum = {not_in: [0]}]; }
message RepeatedEmbeddedEnumIn { repeated AnotherInEnum val = 1 [(validate.rules).repeated.items.enum = {in: [0]}]; enum AnotherInEnum {A = 0; B = 1; }}
message RepeatedEmbeddedEnumNotIn { repeated AnotherNotInEnum val = 1 [(validate.rules).repeated.items.enum = {not_in: [0]}]; enum AnotherNotInEnum {A = 0; B = 1; }}
message RepeatedAnyIn { repeated google.protobuf.Any val = 1 [(validate.rules).repeated.items.any = {in: ["type.googleapis.com/google.protobuf.Duration"]}]; }
message RepeatedAnyNotIn { repeated google.protobuf.Any val = 1 [(validate.rules).repeated.items.any = {not_in: ["type.googleapis.com/google.protobuf.Timestamp"]}]; }
message RepeatedMinAndItemLen { repeated string val = 1 [(validate.rules).repeated = { items { string { len: 3 } }, min_items: 1 }]; }
message RepeatedMinAndMaxItemLen { repeated string val = 1 [(validate.rules).repeated.min_items = 1, (validate.rules).repeated.max_items = 3]; }
message RepeatedDuration { repeated google.protobuf.Duration val = 1 [(validate.rules).repeated = { items { duration { gte { nanos: 1000000}}}}]; }
Expand Down
5 changes: 5 additions & 0 deletions tests/harness/executor/cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,11 @@ var repeatedCases = []TestCase{
{"repeated - items - invalid (embedded enum not_in)", &cases.RepeatedEmbeddedEnumNotIn{Val: []cases.RepeatedEmbeddedEnumNotIn_AnotherNotInEnum{0}}, 1},
{"repeated - items - valid (embedded enum not_in)", &cases.RepeatedEmbeddedEnumNotIn{Val: []cases.RepeatedEmbeddedEnumNotIn_AnotherNotInEnum{1}}, 0},

{"repeated - items - invalid (any in)", &cases.RepeatedAnyIn{Val: []*anypb.Any{{TypeUrl: "type.googleapis.com/google.protobuf.Timestamp"}}}, 1},
{"repeated - items - valid (any in)", &cases.RepeatedAnyIn{Val: []*anypb.Any{{TypeUrl: "type.googleapis.com/google.protobuf.Duration"}}}, 0},
{"repeated - items - invalid (any not_in)", &cases.RepeatedAnyNotIn{Val: []*anypb.Any{{TypeUrl: "type.googleapis.com/google.protobuf.Timestamp"}}}, 1},
{"repeated - items - valid (any not_in)", &cases.RepeatedAnyNotIn{Val: []*anypb.Any{{TypeUrl: "type.googleapis.com/google.protobuf.Duration"}}}, 0},

{"repeated - embed skip - valid", &cases.RepeatedEmbedSkip{Val: []*cases.Embed{{Val: 1}}}, 0},
{"repeated - embed skip - valid (invalid element)", &cases.RepeatedEmbedSkip{Val: []*cases.Embed{{Val: -1}}}, 0},
{"repeated - min and items len - valid", &cases.RepeatedMinAndItemLen{Val: []string{"aaa", "bbb"}}, 0},
Expand Down
5 changes: 5 additions & 0 deletions validate/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ java_proto_library(
deps = [":validate_proto"],
)

filegroup(
name = "validate_src",
srcs = ["validate.proto"],
)

alias(
name = "go_default_library",
actual = ":validate",
Expand Down

0 comments on commit 5137eee

Please sign in to comment.