Skip to content

Commit

Permalink
fixed order issue42
Browse files Browse the repository at this point in the history
  • Loading branch information
awalterschulze committed Apr 8, 2015
1 parent 632f1d9 commit 4d206a9
Show file tree
Hide file tree
Showing 20 changed files with 1,040 additions and 389 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ regenerate:
make -C test/oneof regenerate
make -C test/theproto3 regenerate
make -C test/mapsproto2 regenerate
make -C test/issue42order regenerate
make -C proto generate-test-pbs
make gofmt

Expand Down Expand Up @@ -98,6 +99,7 @@ tests:
go test -v ./test/theproto3/...
go test -v ./test/combos/...
go test -v ./test/mapsproto2/...
go test -v ./test/issue42order
make vet

vet:
Expand Down
17 changes: 17 additions & 0 deletions plugin/marshalto/marshalto.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ import (
"github.com/gogo/protobuf/proto"
descriptor "github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
"github.com/gogo/protobuf/protoc-gen-gogo/generator"
"sort"
"strconv"
"strings"
)
Expand Down Expand Up @@ -345,6 +346,20 @@ func (p *marshalto) mapField(numGen NumGen, mathPkg generator.Single, fieldTyp d
}
}

type orderFields []*descriptor.FieldDescriptorProto

func (this orderFields) Len() int {
return len(this)
}

func (this orderFields) Less(i, j int) bool {
return this[i].GetNumber() < this[j].GetNumber()
}

func (this orderFields) Swap(i, j int) {
this[i], this[j] = this[j], this[i]
}

func (p *marshalto) Generate(file *generator.FileDescriptor) {
proto3 := gogoproto.IsProto3(file.FileDescriptorProto)
numGen := NewNumGen()
Expand Down Expand Up @@ -400,6 +415,8 @@ func (p *marshalto) Generate(file *generator.FileDescriptor) {
p.P(`_ = i`)
p.P(`var l int`)
p.P(`_ = l`)
fields := orderFields(message.GetField())
sort.Sort(fields)
for _, field := range message.Field {
fieldname := p.GetFieldName(message, field)
nullable := gogoproto.IsNullable(field)
Expand Down
3 changes: 2 additions & 1 deletion protoc-gen-gogo/generator/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,11 @@ func (g *Generator) generatePlugin(file *FileDescriptor, p Plugin) {
g.Write(rem.Bytes())

// Reformat generated code.
contents := string(g.Buffer.Bytes())
fset := token.NewFileSet()
ast, err := parser.ParseFile(fset, "", g, parser.ParseComments)
if err != nil {
g.Fail("bad Go source code was generated:", err.Error())
g.Fail("bad Go source code was generated:", contents, err.Error())
return
}
g.Reset()
Expand Down
42 changes: 21 additions & 21 deletions test/combos/both/thetest.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4d206a9

Please sign in to comment.