Skip to content

Commit

Permalink
Merge pull request #19 from grpc-ecosystem/ylai/optional-styling-check
Browse files Browse the repository at this point in the history
Add option enable_styling_check to toggle styling check, the option is false by default.
  • Loading branch information
lyonlai committed May 24, 2021
2 parents 69c2d74 + e791584 commit bd4c72b
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
2 changes: 2 additions & 0 deletions data/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type File struct {
TSFileName string
// PackageNonScalarType stores the type inside the same packages within the file, which will be used to figure out external dependencies inside the same package (different files)
PackageNonScalarType []Type
// EnableStylingCheck enables the styling check for the given file
EnableStylingCheck bool
}

// StableDependencies are dependencies in a stable order.
Expand Down
22 changes: 20 additions & 2 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,34 @@ import (
// TypeScriptGRPCGatewayGenerator is the protobuf generator for typescript
type TypeScriptGRPCGatewayGenerator struct {
Registry *registry.Registry
// EnableStylingCheck enables both eslint and tsc check for the generated code
// This option will only turn on in integration test to ensure the readability in
// the generated code.
EnableStylingCheck bool
}

const (
// EnableStylingCheckOption is the option name for EnableStylingCheck
EnableStylingCheckOption = "enable_styling_check"
)

// New returns an initialised generator
func New(paramsMap map[string]string) (*TypeScriptGRPCGatewayGenerator, error) {
registry, err := registry.NewRegistry(paramsMap)
if err != nil {
return nil, errors.Wrap(err, "error instantiating a new registry")
}

enableStylingCheck := false
enableStylingCheckVal, ok := paramsMap[EnableStylingCheckOption]
if ok {
// default to true if not disabled specifi
enableStylingCheck = enableStylingCheckVal == "true"
}

return &TypeScriptGRPCGatewayGenerator{
Registry: registry,
Registry: registry,
EnableStylingCheck: enableStylingCheck,
}, nil
}

Expand All @@ -46,6 +63,7 @@ func (t *TypeScriptGRPCGatewayGenerator) Generate(req *plugin.CodeGeneratorReque
needToGenerateFetchModule := false
// feed fileData into rendering process
for _, fileData := range filesData {
fileData.EnableStylingCheck = t.EnableStylingCheck
if !t.Registry.IsFileToGenerate(fileData.Name) {
log.Debugf("file %s is not the file to generate, skipping", fileData.Name)
continue
Expand Down Expand Up @@ -100,7 +118,7 @@ func (t *TypeScriptGRPCGatewayGenerator) generateFile(fileData *data.File, tmpl
func (t *TypeScriptGRPCGatewayGenerator) generateFetchModule(tmpl *template.Template) (*plugin.CodeGeneratorResponse_File, error) {
w := bytes.NewBufferString("")
fileName := filepath.Join(t.Registry.FetchModuleDirectory, t.Registry.FetchModuleFilename)
err := tmpl.Execute(w, nil)
err := tmpl.Execute(w, &data.File{EnableStylingCheck: t.EnableStylingCheck})
if err != nil {
return nil, errors.Wrapf(err, "error generating fetch module at %s", fileName)
}
Expand Down
8 changes: 8 additions & 0 deletions generator/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export type {{.Name}} = {
}
{{end}}{{end}}
{{- if not .EnableStylingCheck}}
/* eslint-disable */
// @ts-nocheck
{{- end}}
/*
* This file is a generated Typescript file for GRPC Gateway, DO NOT MODIFY
*/
Expand All @@ -86,6 +90,10 @@ type OneOf<T> =
`

const fetchTmpl = `
{{- if not .EnableStylingCheck}}
/* eslint-disable */
// @ts-nocheck
{{- end}}
/*
* This file is a generated Typescript file for GRPC Gateway, DO NOT MODIFY
*/
Expand Down
3 changes: 2 additions & 1 deletion integration_tests/scripts/gen-protos.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/bin/bash
USE_PROTO_NAMES=${1:-"false"}
ENABLE_STYLING_CHECK=${2:-"false"}
cd .. && go install && cd integration_tests && \
protoc -I . -I ../.. \
--grpc-gateway-ts_out=logtostderr=true,use_proto_names=$USE_PROTO_NAMES,loglevel=debug:./ \
--grpc-gateway-ts_out=logtostderr=true,use_proto_names=$USE_PROTO_NAMES,enable_styling_check=$ENABLE_STYLING_CHECK,loglevel=debug:./ \
service.proto msg.proto empty.proto
2 changes: 1 addition & 1 deletion integration_tests/scripts/source.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
function runTest {
ORIG_NAME=${1:="false"}
CONFIG_NAME=${2:="karma.conf.js"}
./scripts/gen-protos.sh $ORIG_NAME
./scripts/gen-protos.sh $ORIG_NAME true
go run ./ -orig=$ORIG_NAME &
pid=$!

Expand Down

0 comments on commit bd4c72b

Please sign in to comment.