Fix package

This commit is contained in:
Wojciech Tyczynski 2016-03-09 12:30:20 +01:00
parent 454468f5c2
commit 62dc80c9a2
3 changed files with 23 additions and 51 deletions

View File

@ -347,8 +347,6 @@ type protoField struct {
Extras map[string]string
CommentLines string
OptionalSet bool
}
var (
@ -473,6 +471,7 @@ func protobufTagToField(tag string, field *protoField, m types.Member, t *types.
}
field.Tag = protoTag
// TODO: we are converting a Protobuf type back into an internal type, which is questionable
// TODO; Allow a way to unambiguously represent a type into two systems at the same time, like Go and Protobuf.
if last := strings.LastIndex(parts[0], "."); last != -1 {
prefix := parts[0][:last]
field.Type = &types.Type{
@ -498,7 +497,6 @@ func protobufTagToField(tag string, field *protoField, m types.Member, t *types.
}
}
}
field.OptionalSet = true
protoExtra := make(map[string]string)
for i, extra := range parts[3:] {
@ -511,6 +509,7 @@ func protobufTagToField(tag string, field *protoField, m types.Member, t *types.
parts[0] = fmt.Sprintf("(gogoproto.%s)", parts[0])
protoExtra[parts[0]] = parts[1]
}
// TODO: Should we parse castkey and castvalue too?
}
field.Extras = protoExtra
@ -710,18 +709,6 @@ func (ft protoIDLFileType) assemble(w io.Writer, f *generator.File) {
w.Write(f.Body.Bytes())
}
func isPackable(t *types.Type) bool {
if t.Kind != typesKindProtobuf {
return false
}
switch t.Name.Name {
case "int32", "int64", "varint":
return true
default:
return false
}
}
func isPrivateGoName(name string) bool {
if len(name) == 0 {
return true

View File

@ -27,6 +27,8 @@ import (
//
// TODO: pay attention to the package name (instead of renaming every package).
// TODO: Figure out the best way to make names for packages that collide.
//
// TODO: Try to merge this into generator.ImportTracker (or refactor common parts).
type ImportTracker struct {
pathToName map[string]string
// forbidden names are in here. (e.g. "go" is a directory in which

View File

@ -32,44 +32,37 @@ import (
func newProtobufPackage(packagePath, packageName string, generateAll bool, omitFieldTypes map[types.Name]struct{}) *protobufPackage {
pkg := &protobufPackage{
// The protobuf package name (foo.bar.baz)
PackageName: packageName,
// A path segment relative to the GOPATH root (foo/bar/baz)
PackagePath: packagePath,
GenerateAll: generateAll,
OmitFieldTypes: omitFieldTypes,
HeaderText: []byte(
`
// This file was autogenerated by the command:
// $ ` + os.Args[0] + `
// Do not edit it manually!
DefaultPackage: generator.DefaultPackage{
// The protobuf package name (foo.bar.baz)
PackageName: packageName,
// A path segment relative to the GOPATH root (foo/bar/baz)
PackagePath: packagePath,
HeaderText: []byte(
`
// This file was autogenerated by go-to-protobuf. Do not edit it manually!
`),
PackageDocumentation: []byte(fmt.Sprintf(
`// Package %s is an autogenerated protobuf IDL.
PackageDocumentation: []byte(fmt.Sprintf(
`// Package %s is an autogenerated protobuf IDL.
`, packageName)),
},
GenerateAll: generateAll,
OmitFieldTypes: omitFieldTypes,
}
pkg.FilterFunc = pkg.filterFunc
pkg.GeneratorFunc = pkg.generatorFunc
return pkg
}
// protobufPackage contains the protobuf implementation of Package.
type protobufPackage struct {
// Short name of package, used in the "package xxxx" line.
PackageName string
// Import path of the package, and the location on disk of the package.
PackagePath string
generator.DefaultPackage
// If true, generate protobuf serializations for all public types.
// If false, only generate protobuf serializations for structs that
// request serialization.
GenerateAll bool
// Emitted at the top of every file.
HeaderText []byte
// Emitted only for a "doc.go" file; appended to the HeaderText for
// that file.
PackageDocumentation []byte
// A list of types to filter to; if not specified all types will be included.
FilterTypes map[types.Name]struct{}
@ -106,10 +99,7 @@ func (p *protobufPackage) ProtoTypeName() types.Name {
}
}
func (p *protobufPackage) Name() string { return p.PackageName }
func (p *protobufPackage) Path() string { return p.PackagePath }
func (p *protobufPackage) Filter(c *generator.Context, t *types.Type) bool {
func (p *protobufPackage) filterFunc(c *generator.Context, t *types.Type) bool {
switch t.Kind {
case types.Func, types.Chan:
return false
@ -175,7 +165,7 @@ func (p *protobufPackage) ExtractGeneratedType(t *ast.TypeSpec) bool {
return true
}
func (p *protobufPackage) Generators(c *generator.Context) []generator.Generator {
func (p *protobufPackage) generatorFunc(c *generator.Context) []generator.Generator {
generators := []generator.Generator{}
p.Imports.AddNullable()
@ -194,13 +184,6 @@ func (p *protobufPackage) Generators(c *generator.Context) []generator.Generator
return generators
}
func (p *protobufPackage) Header(filename string) []byte {
if filename == "doc.go" {
return append(p.HeaderText, p.PackageDocumentation...)
}
return p.HeaderText
}
func (p *protobufPackage) GoPackageName() string {
return filepath.Base(p.PackagePath)
}