Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick fix for TS module names #20

Merged
merged 2 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
2. Supports both one way and server side streaming gRPC calls.
3. POJO request construction guarded by message type definitions, which is way easier compare to `grpc-web`.
4. No need to use swagger/open api to generate client code for the web.
5. Fixes inconsistent field naming when fields contain numbers, e.g. `k8s_field` --> `k8sField`.

### Changes made since the fork

Expand All @@ -20,6 +19,8 @@
3. Support for proto3 optional fields
4. Updated to satisfy strict TS and eslint checks
5. Generator options managed through standard flags
6. Fixes inconsistent field naming when fields contain numbers, e.g. `k8s_field` --> `k8sField`
7. Fixes module names when they contain dots or dashes

## Getting Started:

Expand Down
16 changes: 7 additions & 9 deletions data/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"path/filepath"
"sort"
"strings"

"github.com/iancoleman/strcase"
)

// File store the information about rendering a file
Expand Down Expand Up @@ -95,20 +97,16 @@ type Dependency struct {
SourceFile string
}

// GetModuleName returns module name = package name + file name to be the unique identifier for source file in a ts file
// GetModuleName returns module name = package name + base file name to be the
// unique identifier for source file in a ts file. Package name and base file
// name are converted to camel case, special characters like dot, dash and
// underscore are removed.
func GetModuleName(packageName, fileName string) string {
baseName := filepath.Base(fileName)
ext := filepath.Ext(fileName)
name := baseName[0 : len(baseName)-len(ext)]
packageParts := strings.Split(packageName, ".")

if packageName != "" {
for i, p := range packageParts {
packageParts[i] = strings.ToUpper(p[:1]) + p[1:]
}
}

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

// GetTSFileName gets the typescript filename out of the proto file name
Expand Down
27 changes: 27 additions & 0 deletions data/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package data

import "testing"

func TestGetModuleName(t *testing.T) {
tests := []struct {
name string
packageName string
fileName string
want string
}{
{"empty", "", "", ""},
{"simple", "mypackage", "service.proto", "MypackageService"},
{"with file path", "mypackage", "path/to/proto/file/service.proto", "MypackageService"},
{"with underscore", "my_package", "cool_service.proto", "MyPackageCoolService"},
{"with dash", "my-package", "cool-service.proto", "MyPackageCoolService"},
{"with dash and underscore", "my-package", "cool_service.proto", "MyPackageCoolService"},
{"with dots", "my.package", "cool.service.proto", "MyPackageCoolService"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := GetModuleName(tt.packageName, tt.fileName); got != tt.want {
t.Errorf("GetModuleName() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func (r *Registry) collectExternalDependenciesFromData(filesData map[string]*dat

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