protogen: automatic handling of imports
The GoIdent type is now a tuple of import path and name. Generated files
have an associated import path. Writing a GoIdent to a generated file
qualifies the name if the identifier is from a different package.
All necessary imports are automatically added to generated Go files.
Change-Id: I839e0b7aa8ec967ce178aea4ffb960b62779cf74
Reviewed-on: https://go-review.googlesource.com/133635
Reviewed-by: Joe Tsai <thebrokentoaster@gmail.com>
diff --git a/protogen/names.go b/protogen/names.go
index b97c47d..ea3d057 100644
--- a/protogen/names.go
+++ b/protogen/names.go
@@ -1,6 +1,7 @@
package protogen
import (
+ "fmt"
"go/token"
"strconv"
"strings"
@@ -8,8 +9,13 @@
"unicode/utf8"
)
-// A GoIdent is a Go identifier.
-type GoIdent string
+// A GoIdent is a Go identifier, consisting of a name and import path.
+type GoIdent struct {
+ GoName string
+ GoImportPath GoImportPath
+}
+
+func (id GoIdent) String() string { return fmt.Sprintf("%q.%v", id.GoImportPath, id.GoName) }
// A GoImportPath is the import path of a Go package. e.g., "google.golang.org/genproto/protobuf".
type GoImportPath string
@@ -64,7 +70,7 @@
// but it's so remote we're prepared to pretend it's nonexistent - since the
// C++ generator lowercases names, it's extremely unlikely to have two fields
// with different capitalizations.
-func camelCase(s string) GoIdent {
+func camelCase(s string) string {
if s == "" {
return ""
}
@@ -102,7 +108,7 @@
}
}
}
- return GoIdent(t)
+ return string(t)
}
// Is c an ASCII lower-case letter?