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
4 changes: 2 additions & 2 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
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
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