Skip to content

Commit 6c0b3ff

Browse files
committed
fix: invalid TS module name for proto files with dash or dot chars
Fixes #42 The fix patches `data.GetModuleName` func to return a camel cased string to be used as a unique module name in TS artifacts. Here we replace manual conversion of a package name and base file name with `ToCamel` func of the `github.com/iancoleman/strcase` package. This helper package has already been included in go.mod. `ToCamel` removes `.`, `-`, `_` or whitespace chars from the input string and uses those as a mark to capitalize the next letter.
1 parent 36143bb commit 6c0b3ff

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

data/file.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"path/filepath"
66
"sort"
77
"strings"
8+
9+
"github.com/iancoleman/strcase"
810
)
911

1012
// File store the information about rendering a file
@@ -85,20 +87,16 @@ type Dependency struct {
8587
SourceFile string
8688
}
8789

88-
// GetModuleName returns module name = package name + file name to be the unique identifier for source file in a ts file
90+
// GetModuleName returns module name = package name + base file name to be the
91+
// unique identifier for source file in a ts file. Package name and base file
92+
// name are converted to camel case, special characters like dot, dash and
93+
// underscore are removed.
8994
func GetModuleName(packageName, fileName string) string {
9095
baseName := filepath.Base(fileName)
9196
ext := filepath.Ext(fileName)
9297
name := baseName[0 : len(baseName)-len(ext)]
93-
packageParts := strings.Split(packageName, ".")
94-
95-
if packageName != "" {
96-
for i, p := range packageParts {
97-
packageParts[i] = strings.ToUpper(p[:1]) + p[1:]
98-
}
99-
}
10098

101-
return strings.Join(packageParts, "") + strings.ToUpper(name[:1]) + name[1:]
99+
return strcase.ToCamel(packageName) + strcase.ToCamel(name)
102100
}
103101

104102
// GetTSFileName gets the typescript filename out of the proto file name

data/file_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package data
2+
3+
import "testing"
4+
5+
func TestGetModuleName(t *testing.T) {
6+
tests := []struct {
7+
name string
8+
packageName string
9+
fileName string
10+
want string
11+
}{
12+
{"empty", "", "", ""},
13+
{"simple", "mypackage", "service.proto", "MypackageService"},
14+
{"with file path", "mypackage", "path/to/proto/file/service.proto", "MypackageService"},
15+
{"with underscore", "my_package", "cool_service.proto", "MyPackageCoolService"},
16+
{"with dash", "my-package", "cool-service.proto", "MyPackageCoolService"},
17+
{"with dash and underscore", "my-package", "cool_service.proto", "MyPackageCoolService"},
18+
{"with dots", "my.package", "cool.service.proto", "MyPackageCoolService"},
19+
}
20+
for _, tt := range tests {
21+
t.Run(tt.name, func(t *testing.T) {
22+
if got := GetModuleName(tt.packageName, tt.fileName); got != tt.want {
23+
t.Errorf("GetModuleName() = %v, want %v", got, tt.want)
24+
}
25+
})
26+
}
27+
}

registry/registry.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ func (r *Registry) collectExternalDependenciesFromData(filesData map[string]*dat
336336

337337
if _, ok := dependencies[identifier]; !ok {
338338
// only fill in if this file has not been mentioned before.
339-
// the way import in the genrated file works is like
339+
// the way import in the generated file works is like
340340
// import * as [ModuleIdentifier] from '[Source File]'
341341
// so there only needs to be added once.
342342
// Referencing types will be [ModuleIdentifier].[PackageIdentifier]

0 commit comments

Comments
 (0)