Skip to content

Commit

Permalink
[gogenproto] Allow specifying a Go package mapping for import paths (#36
Browse files Browse the repository at this point in the history
)

### Background

→ It may be the case that you're importing protos that are generated in
some other way than with `gogenproto` but which similarly have a
conventional Go path mapping (e.g. they don't specify `go_package` but
instead get automatic package names. Unfortunately right now that means
you can't depend on these protos from your `gogenproto`-using protos,
since you can't tell `gogenproto` what their package name will be.

### Changes

- Add an optional Go package prefix mapping to import paths. By
appending `=` and the Go package prefix, you can use conventional
(directory based) path mapping.

### Testing

- Tested in stately monorepo
  • Loading branch information
bhollis authored Jan 24, 2024
1 parent 01a0403 commit 2f40c97
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 16 deletions.
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

0 comments on commit 2f40c97

Please sign in to comment.