Skip to content

Commit

Permalink
thrift-gen: Use namespace to generate the output filename (#574)
Browse files Browse the repository at this point in the history
The namespace was not being used everywhere in #559 -- extend the test
to create services so that all the tchan-*.go files are generated and
we can ensure it's created in the right directory.
  • Loading branch information
prashantv committed Jan 25, 2017
1 parent 6bb33bd commit 940fa93
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 22 deletions.
4 changes: 2 additions & 2 deletions testutils/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ var (

func checkCacheSize(n int) {
// Start with a reasonably large cache.
if n < 8 {
n = 8
if n < 1024 {
n = 1024
}

randMut.RLock()
Expand Down
8 changes: 4 additions & 4 deletions thrift/thrift-gen/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ var (
func TestMain(m *testing.M) {
exitCode := m.Run()

// If we created a fake GOPATH, we should clean it up.
if _testGoPath != "" {
// If we created a fake GOPATH, we should clean it up on success.
if _testGoPath != "" && exitCode == 0 {
os.RemoveAll(_testGoPath)
}

Expand Down Expand Up @@ -193,7 +193,7 @@ func TestExternalTemplate(t *testing.T) {
}

// Verify the contents of the extra file.
outFile := filepath.Join(dir, packageName(templateFile)+"-service_extend.go")
outFile := filepath.Join(dir, defaultPackageName(templateFile)+"-service_extend.go")
return verifyFileContents(outFile, expected)
}
if err := runTest(t, opts, checks); err != nil {
Expand Down Expand Up @@ -301,7 +301,7 @@ func checkDirectoryFiles(dir string, n int) error {

func runBuildTest(t *testing.T, thriftFile string) error {
extraChecks := func(dir string) error {
return checkDirectoryFiles(filepath.Join(dir, packageName(thriftFile)), 4)
return checkDirectoryFiles(filepath.Join(dir, defaultPackageName(thriftFile)), 4)
}

opts := processOptions{InputFile: thriftFile}
Expand Down
1 change: 1 addition & 0 deletions thrift/thrift-gen/extends.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func setExtends(state map[string]parseState) error {
searchFor = s.Extends
} else {
include := v.global.includes[parts[0]]
s.ExtendsPrefix = include.pkg + "."
searchServices = state[include.file].services
searchFor = parts[1]
}
Expand Down
2 changes: 1 addition & 1 deletion thrift/thrift-gen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func runThrift(inFile string, outDir string) error {
}

// Delete any existing generated code for this Thrift file.
genDir := filepath.Join(outDir, packageName(inFile))
genDir := filepath.Join(outDir, defaultPackageName(inFile))
if err := execCmd("rm", "-rf", genDir); err != nil {
return fmt.Errorf("failed to delete directory %s: %v", genDir, err)
}
Expand Down
20 changes: 9 additions & 11 deletions thrift/thrift-gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func processFile(opts processOptions) error {
}

for filename, v := range allParsed {
pkg := packageName(filename)
pkg := getNamespace(filename, v.ast)

for _, template := range allTemplates {
outputFile := filepath.Join(opts.OutputDir, pkg, template.outputFile(pkg))
Expand Down Expand Up @@ -180,14 +180,19 @@ func parseFile(inputFile string) (map[string]parseState, error) {
return allParsed, setExtends(allParsed)
}

func defaultPackageName(fullPath string) string {
filename := filepath.Base(fullPath)
file := strings.TrimSuffix(filename, filepath.Ext(filename))
return strings.ToLower(file)
}

func getNamespace(filename string, v *parser.Thrift) string {
if ns, ok := v.Namespaces["go"]; ok {
return ns
}

base := filepath.Base(filename)
base = strings.TrimSuffix(base, filepath.Ext(base))
return strings.ToLower(base)
// TODO(prashant): Remove any characters that are not valid in Go package names.
return defaultPackageName(filename)
}

func generateCode(outputFile string, template *Template, pkg string, state parseState) error {
Expand All @@ -212,13 +217,6 @@ func generateCode(outputFile string, template *Template, pkg string, state parse
return template.execute(outputFile, td)
}

func packageName(fullPath string) string {
// TODO(prashant): Remove any characters that are not valid in Go package names.
_, filename := filepath.Split(fullPath)
file := strings.TrimSuffix(filename, filepath.Ext(filename))
return strings.ToLower(file)
}

type stringSliceFlag []string

func (s *stringSliceFlag) String() string {
Expand Down
2 changes: 1 addition & 1 deletion thrift/thrift-gen/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func parseTemplateFile(file string) (*Template, error) {
return nil, fmt.Errorf("failed to parse template in file %q: %v", file, err)
}

return &Template{packageName(file), t}, nil
return &Template{defaultPackageName(file), t}, nil
}

func contextType() string {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ namespace go a_shared
include "../b/shared.thrift"

typedef shared.b_string a_string

service AShared extends shared.BShared {
bool healthA()
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
namespace go b_shared

typedef string b_string

service BShared {
bool healthB()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
include "a/shared.thrift"

service Foo {
service Foo extends shared.AShared {
void Foo(1: shared.a_string str)
}
5 changes: 3 additions & 2 deletions thrift/thrift-gen/wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ type Service struct {
*parser.Service
state *State

// ExtendsService is not set in wrap, but in setExtends.
// ExtendsService and ExtendsPrefix are set in `setExtends`.
ExtendsService *Service
ExtendsPrefix string

// methods is a cache of all methods.
methods []*Method
Expand Down Expand Up @@ -91,7 +92,7 @@ func (s *Service) HasExtends() bool {
// ExtendsServicePrefix returns a package selector (if any) for the extended service.
func (s *Service) ExtendsServicePrefix() string {
if dotIndex := strings.Index(s.Extends, "."); dotIndex > 0 {
return s.Extends[:dotIndex+1]
return s.ExtendsPrefix
}
return ""
}
Expand Down

0 comments on commit 940fa93

Please sign in to comment.