-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support 'optional' fields in proto3 (#34)
- Loading branch information
1 parent
c6ad161
commit ea7f077
Showing
10 changed files
with
2,753 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/compiler/protogen" | ||
"google.golang.org/protobuf/types/pluginpb" | ||
|
||
"github.com/ckaznocha/protoc-gen-lint/linter" | ||
) | ||
|
||
// SortImports represents the parameter, which can be specified to the tool invocation | ||
// to enable checking, whether the proto file imports are sorted alphabetically. | ||
const SortImports = "sort_imports" | ||
type BoolFlag interface { | ||
IsBoolFlag() bool | ||
} | ||
|
||
var errLint = errors.New("encountered lint errors") | ||
|
||
func main() { | ||
var ( | ||
totalErrors int | ||
generatorRequest pluginpb.CodeGeneratorRequest | ||
parameters struct { | ||
SortImports bool | ||
} | ||
) | ||
|
||
data, err := io.ReadAll(os.Stdin) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
if err := proto.Unmarshal(data, &generatorRequest); err != nil { | ||
panic(err) | ||
} | ||
|
||
for _, p := range strings.Split(generatorRequest.GetParameter(), ",") { | ||
switch strings.TrimSpace(p) { | ||
case "": | ||
continue | ||
case SortImports: | ||
parameters.SortImports = true | ||
default: | ||
fmt.Fprintf(os.Stderr, "Unmatched parameter: %s", p) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
for _, file := range generatorRequest.GetProtoFile() { | ||
numErrors, err := linter.LintProtoFile(linter.Config{ | ||
ProtoFile: file, | ||
OutFile: os.Stderr, | ||
SortImports: parameters.SortImports, | ||
}) | ||
if err != nil { | ||
panic(err) | ||
var flags flag.FlagSet | ||
sortImports := flags.Bool("sort_imports", false, "check whether or not the proto "+ | ||
"file imports are sorted alphabetically") | ||
|
||
protogen.Options{ | ||
ParamFunc: func(param, value string) error { | ||
// For backwards compatibility, treat a present flag with an empty | ||
// string value as boolean true. The Go flag module parsing | ||
// handles this, but the protogen flag parse behavior doesn't, | ||
// because it doesn't parse flags the same way. | ||
f := flags.Lookup(param) | ||
if f != nil { | ||
if _, ok := f.Value.(BoolFlag); ok { | ||
value = "true" | ||
} | ||
} | ||
|
||
return flags.Set(param, value) | ||
}, | ||
}.Run(func(gen *protogen.Plugin) error { | ||
gen.SupportedFeatures = uint64(pluginpb.CodeGeneratorResponse_FEATURE_PROTO3_OPTIONAL) | ||
|
||
totalErrors := 0 | ||
for _, f := range gen.Files { | ||
numErrors, err := linter.LintProtoFile(linter.Config{ | ||
ProtoFile: f.Proto, | ||
OutFile: os.Stderr, | ||
SortImports: *sortImports, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
totalErrors += numErrors | ||
} | ||
|
||
totalErrors += numErrors | ||
} | ||
if totalErrors > 0 { | ||
return fmt.Errorf("%w: %d total", errLint, totalErrors) | ||
} | ||
|
||
os.Exit(totalErrors) | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
syntax = "proto3"; | ||
|
||
package test.optional; | ||
|
||
message WithOptional { | ||
optional string test = 1; | ||
} |
Oops, something went wrong.