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

[gogenproto] Allow specifying a Go package mapping for import paths #36

Merged
merged 1 commit into from
Jan 24, 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
16 changes: 6 additions & 10 deletions gogenproto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,17 @@ package models
→ ./path/to/bin/gogenproto --help
Usage of ./bin/gogenproto:
-grpc
also generate grpc service definitions (experimental)
also generate grpc service definitions (experimental)
-include value
comma-separated paths to additional packages to include
comma-separated paths to additional directories to add to the proto include path. You can set an optional Go package mapping by appending a = and the package path, e.g. foo=github.com/foo/bar
-input-dir string
path to root directory for proto generation (env PWD)
path to root directory for proto generation (env PWD)
-inputDir string
path to root directory for proto generation (env PWD)
-output-dir string
relative output path for generated files (default "../")
-outputDir string
relative output path for generated files (default "../")
path to root directory for proto generation (env PWD)
-recurse
generate protos recursively
generate protos recursively
-vt-proto
also generate vtproto
also generate vtproto
```

### TODO:
Expand Down
22 changes: 16 additions & 6 deletions gogenproto/gen/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Generate struct {
Recurse bool `default:"false" usage:"generate protos recursively"`
VTProto bool `default:"false" usage:"also generate vtproto"`
GRPC bool `default:"false" usage:"also generate grpc service definitions (experimental)"`
Include []string `usage:"comma-separated paths to additional packages to include"`
Include []string `usage:"comma-separated paths to additional directories to add to the proto include path. You can set an optional Go package mapping by appending a = and the package path, e.g. foo=github.com/foo/bar"`

// TODO: add flags for other languages, TS, etc.
// TODO: add NATIVE validation support.
Expand Down Expand Up @@ -52,7 +52,8 @@ func (g Generate) Run() error {
)
}
includePaths := append([]string{g.InputDir}, g.Include...)
for _, path := range includePaths {
for _, pathAndMaybePkg := range includePaths {
path, pkgPrefix, hasPkgPrefix := strings.Cut(pathAndMaybePkg, "=")
includePath, err := filepath.Abs(path)
if err != nil {
return err
Expand All @@ -63,14 +64,23 @@ func (g Generate) Run() error {
return err
}
for _, path := range protoImportPaths {
pkg, err := gencommon.PackageNameFromPath(filepath.Dir(path))
if err != nil {
return err
}
relPath, err := filepath.Rel(includePath, path)
if err != nil {
return err
}

var pkg string
if hasPkgPrefix {
// Use the explicit package mapping
pkg = filepath.Join(pkgPrefix, filepath.Dir(relPath))
} else {
// Auto detect package name from directory
pkg, err = gencommon.PackageNameFromPath(filepath.Dir(path))
if err != nil {
return err
}
}

mapping := relPath + "=" + pkg

args = append(args,
Expand Down
Loading