From e389a172f1e9168f892bc2a600e65bff35c2de85 Mon Sep 17 00:00:00 2001 From: yoheimuta Date: Tue, 3 May 2022 14:15:29 +0900 Subject: [PATCH 1/2] feat: Signal proto3 optional support --- internal/cmd/protocgenprotolint/cmd.go | 27 ++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/internal/cmd/protocgenprotolint/cmd.go b/internal/cmd/protocgenprotolint/cmd.go index 9225b8c6..d105dc9a 100644 --- a/internal/cmd/protocgenprotolint/cmd.go +++ b/internal/cmd/protocgenprotolint/cmd.go @@ -1,9 +1,12 @@ package protocgenprotolint import ( + "bytes" "fmt" + "google.golang.org/protobuf/types/pluginpb" "io" "io/ioutil" + "os" "strings" "github.com/yoheimuta/protolint/internal/cmd/subcmds" @@ -36,6 +39,12 @@ func Do( return doVersion(stdout) } + err := signalSupportProto3Optional() + if err != nil { + _, _ = fmt.Fprintln(stderr, err) + return osutil.ExitInternalFailure + } + subCmd, err := newSubCmd(stdin, stdout, stderr) if err != nil { _, _ = fmt.Fprintln(stderr, err) @@ -44,6 +53,24 @@ func Do( return subCmd.Run() } +func signalSupportProto3Optional() error { + // supports proto3 field presence + // See https://github.com/protocolbuffers/protobuf/blob/cdc11c2d2d314ce0382fe0eaa715e5e0e1270438/docs/implementing_proto3_presence.md#signaling-that-your-code-generator-supports-proto3-optional + var supportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL) + data, err := proto.Marshal(&protogen.CodeGeneratorResponse{ + SupportedFeatures: &supportedFeatures, + }) + if err != nil { + return err + } + + _, err = io.Copy(os.Stdout, bytes.NewReader(data)) + if err != nil { + return err + } + return nil +} + func newSubCmd( stdin io.Reader, stdout io.Writer, From 5defbf65e2de0e4f94d4d8b309ee0ee838f0b35b Mon Sep 17 00:00:00 2001 From: yoheimuta Date: Tue, 3 May 2022 14:20:43 +0900 Subject: [PATCH 2/2] fix: a minor lint error --- internal/cmd/protocgenprotolint/cmd.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/cmd/protocgenprotolint/cmd.go b/internal/cmd/protocgenprotolint/cmd.go index d105dc9a..7dbe976d 100644 --- a/internal/cmd/protocgenprotolint/cmd.go +++ b/internal/cmd/protocgenprotolint/cmd.go @@ -3,12 +3,13 @@ package protocgenprotolint import ( "bytes" "fmt" - "google.golang.org/protobuf/types/pluginpb" "io" "io/ioutil" "os" "strings" + "google.golang.org/protobuf/types/pluginpb" + "github.com/yoheimuta/protolint/internal/cmd/subcmds" "github.com/yoheimuta/protolint/internal/cmd/subcmds/lint"