Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add optional support #537

Merged
merged 12 commits into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from 8 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
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ go_library(
visibility = ["//visibility:private"],
deps = [
"//module",
"@org_golang_google_protobuf//types/pluginpb",
"@com_github_lyft_protoc_gen_star//:protoc-gen-star",
"@com_github_lyft_protoc_gen_star//lang/go",
],
Expand Down
6 changes: 3 additions & 3 deletions bazel/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def pgv_dependencies(maven_repos = _DEFAULT_REPOSITORIES):
if not native.existing_rule("com_google_protobuf"):
http_archive(
name = "com_google_protobuf",
url = "https://github.com/protocolbuffers/protobuf/archive/v3.14.0.tar.gz",
sha256 = "d0f5f605d0d656007ce6c8b5a82df3037e1d8fe8b121ed42e536f569dec16113",
strip_prefix = "protobuf-3.14.0",
url = "https://github.com/protocolbuffers/protobuf/archive/v3.15.3.tar.gz",
sha256 = "b10bf4e2d1a7586f54e64a5d9e7837e5188fc75ae69e36f215eb01def4f9721b",
strip_prefix = "protobuf-3.15.3",
)

# TODO(akonradi): This shouldn't be necesary since the same http_archive block is imported by
Expand Down
6 changes: 3 additions & 3 deletions dependencies.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ def go_third_party():
go_repository(
name = "com_github_lyft_protoc_gen_star",
importpath = "github.com/lyft/protoc-gen-star",
sum = "h1:zSGLzsUew8RT+ZKPHc3jnf8XLaVyHzTcAFBzHtCNR20=",
version = "v0.5.3",
sum = "h1:xOpFu4vwmIoUeUrRuAtdCrZZymT/6AkW/bsUWA506Fo=",
version = "v0.6.0",
)
go_repository(
name = "com_github_pkg_errors",
Expand Down Expand Up @@ -105,7 +105,7 @@ def go_third_party():
name = "org_golang_google_protobuf",
importpath = "google.golang.org/protobuf",
sum = "h1:bxAC2xTBsZGibn2RTntX0oH50xLsqy1OxA9tTL3p/lk=",
version = "v1.26.0",
version = "v1.27.1",
)
go_repository(
name = "org_golang_x_crypto",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.12

require (
github.com/iancoleman/strcase v0.2.0
github.com/lyft/protoc-gen-star v0.5.3
github.com/lyft/protoc-gen-star v0.6.0
github.com/spf13/afero v1.6.0 // indirect
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
golang.org/x/mod v0.5.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/iancoleman/strcase v0.2.0 h1:05I4QRnGpI0m37iZQRuskXh+w77mr6Z41lwQzuHLwW0=
github.com/iancoleman/strcase v0.2.0/go.mod h1:iwCmte+B7n89clKwxIoIXy/HfoL7AsD47ZCWhYzw7ho=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/lyft/protoc-gen-star v0.5.3 h1:zSGLzsUew8RT+ZKPHc3jnf8XLaVyHzTcAFBzHtCNR20=
github.com/lyft/protoc-gen-star v0.5.3/go.mod h1:V0xaHgaf5oCCqmcxYcWiDfTiKsZsRc87/1qhoTACD8w=
github.com/lyft/protoc-gen-star v0.6.0 h1:xOpFu4vwmIoUeUrRuAtdCrZZymT/6AkW/bsUWA506Fo=
github.com/lyft/protoc-gen-star v0.6.0/go.mod h1:TGAoBVkt8w7MPG72TrKIu85MIdXwDuzJYeZuUPFPNwA=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package main

import (
"github.com/envoyproxy/protoc-gen-validate/module"
"github.com/lyft/protoc-gen-star"
"github.com/lyft/protoc-gen-star/lang/go"
"github.com/envoyproxy/protoc-gen-validate/module"
"google.golang.org/protobuf/types/pluginpb"
)

func main() {
optional := uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL)
pgs.
Init(pgs.DebugEnv("DEBUG_PGV")).
Init(pgs.DebugEnv("DEBUG_PGV"), pgs.SupportedFeatures(&optional)).
RegisterModule(module.Validator()).
RegisterPostProcessor(pgsgo.GoFmt()).
Render()
Expand Down
2 changes: 1 addition & 1 deletion templates/cc/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ bool Validate(const {{ class . }}& m, pgv::ValidationMsg* err) {
{{ range .NonOneOfFields }}
{{- render (context .) -}}
{{ end -}}
{{ range .OneOfs }}
{{ range .RealOneOfs }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this going to cause problems for messages with optional fields with validations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generating gofiles with a message like this works just fine now.

message AddEntityEntitlementRequest {
    optional string name= 1 [(validate.rules).string = {
        pattern:   "^[^[0-9]A-Za-z]+( [^[0-9]A-Za-z]+)*$",
        max_bytes: 256,
    }]; /** name */
}

the regex fails when expected and if no value is provided, no error is thrown, since it's marked as optional.

I don't know the ins and outs of generation, especially since that piece comes from the original PR, so I'm not sure what the halo effect is like with this change.

switch (m.{{ .Name }}_case()) {
{{ range .Fields -}}
case {{ oneof . }}:
Expand Down
9 changes: 8 additions & 1 deletion templates/goshared/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (m {{ (msgTyp .).Pointer }}) validate(all bool) error {
{{ render (context .) }}
{{ end }}

{{ range .OneOfs }}
{{ range .RealOneOfs }}
switch m.{{ name . }}.(type) {
{{ range .Fields }}
case {{ oneof . }}:
Expand All @@ -51,9 +51,16 @@ func (m {{ (msgTyp .).Pointer }}) validate(all bool) error {
}
{{ end }}

{{ range .SyntheticOneOfFields }}
if m.{{ name . }} != nil {
{{ render (context .) }}
}
{{ end }}

if len(errors) > 0 {
return {{ multierrname . }}(errors)
}

return nil
{{ end -}}
}
Expand Down
4 changes: 2 additions & 2 deletions templates/java/msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const msgInnerTpl = `
{{- range .NonOneOfFields }}
{{ renderConstants (context .) }}
{{ end }}
{{ range .OneOfs }}
{{ range .RealOneOfs }}
{{ template "oneOfConst" . }}
{{ end }}

Expand All @@ -27,7 +27,7 @@ const msgInnerTpl = `
{{ range .NonOneOfFields -}}
{{ render (context .) }}
{{ end -}}
{{ range .OneOfs }}
{{ range .RealOneOfs }}
{{ template "oneOf" . }}
{{- end -}}
{{- end }}
Expand Down
14 changes: 14 additions & 0 deletions tests/harness/cases/optional.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
syntax = "proto3";

package tests.harness.cases;
option go_package = "github.com/envoyproxy/protoc-gen-validate/tests/harness/cases/go;cases";

import "validate/validate.proto";

message TestOptionalMsg {
string const = 1 [(validate.rules).string.const = "foo"];
TestOptionalMsg nested = 2;
}

message MessageRequiredButOptional { optional TestOptionalMsg val = 1 [(validate.rules).message.required = true]; }
mgebundy marked this conversation as resolved.
Show resolved Hide resolved
message Int64LTEOptional { int64 val = 1 [(validate.rules).int64.lte = 64]; }