Skip to content

Commit

Permalink
empty typescript file renders only empty export (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
lyonlai authored Mar 4, 2021
1 parent b1199a9 commit 312da67
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 4 deletions.
4 changes: 4 additions & 0 deletions data/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ func (f *File) TrackPackageNonScalarType(t Type) {
}
}

func (f *File) IsEmpty() bool {
return len(f.Enums) == 0 && len(f.Messages) == 0 && len(f.Services) == 0
}

// NewFile returns an initialised new file
func NewFile() *File {
return &File{
Expand Down
11 changes: 8 additions & 3 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package generator

import (
"bytes"
"fmt"
"path/filepath"
"strings"
"text/template"
Expand Down Expand Up @@ -77,9 +78,13 @@ func (t *TypeScriptGRPCGatewayGenerator) Generate(req *plugin.CodeGeneratorReque
func (t *TypeScriptGRPCGatewayGenerator) generateFile(fileData *data.File, tmpl *template.Template) (*plugin.CodeGeneratorResponse_File, error) {
w := bytes.NewBufferString("")

err := tmpl.Execute(w, fileData)
if err != nil {
return nil, errors.Wrapf(err, "error generating ts file for %s", fileData.Name)
if fileData.IsEmpty() {
w.Write([]byte(fmt.Sprintln("export default {}")))
} else {
err := tmpl.Execute(w, fileData)
if err != nil {
return nil, errors.Wrapf(err, "error generating ts file for %s", fileData.Name)
}
}

fileName := fileData.TSFileName
Expand Down
3 changes: 3 additions & 0 deletions integration_tests/empty.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
syntax = "proto3";
option go_package = ".;main";

1 change: 1 addition & 0 deletions integration_tests/integration_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {CounterService} from "./service.pb";
import _ from './empty.pb';
import camelCase from 'lodash.camelcase';
import { pathOr } from 'ramda'
import {expect} from 'chai'
Expand Down
2 changes: 1 addition & 1 deletion integration_tests/scripts/gen-protos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ USE_PROTO_NAMES=${1:-"false"}
cd .. && go install && cd integration_tests && \
protoc -I . \
--grpc-gateway-ts_out=logtostderr=true,use_proto_names=$USE_PROTO_NAMES,loglevel=debug:./ \
service.proto msg.proto
service.proto msg.proto empty.proto

0 comments on commit 312da67

Please sign in to comment.