protogen: add GoImportPath.Ident helper
The GoImportPath.Ident helper creates a GoIdent using the receiver
as the GoImportPath in the GoIdent. This helper helps with the construction
of qualified identifiers.
Example usage:
const protoPackage = protogen.GoImportPath("github.com/golang/protobuf/proto")
protoPackage.Ident("ExtensionRange") // produces "proto.ExtensionRange"
The advantage of this helper is that usage of it looks similar to how
the identifier will eventually be rendered.
This is significantly more readable than the current approach:
protogen.GoIdent{
GoImportPath: protoPackage,
GoName: "ExtensionRange",
}
Change-Id: If7ecd7e60fad12bc491eee0dcb05f8fdebc9c94e
Reviewed-on: https://go-review.googlesource.com/c/150058
Reviewed-by: Damien Neil <dneil@google.com>
diff --git a/protogen/names.go b/protogen/names.go
index 9da11d9..29b97ac 100644
--- a/protogen/names.go
+++ b/protogen/names.go
@@ -33,6 +33,11 @@
func (p GoImportPath) String() string { return strconv.Quote(string(p)) }
+// Ident returns a GoIdent with s as the GoName and p as the GoImportPath.
+func (p GoImportPath) Ident(s string) GoIdent {
+ return GoIdent{GoName: s, GoImportPath: p}
+}
+
// A GoPackageName is the name of a Go package. e.g., "protobuf".
type GoPackageName string