Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: code optimization #767

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 11 additions & 30 deletions gogoproto/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@

package gogoproto

import google_protobuf "github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
import proto "github.com/gogo/protobuf/proto"
import (
proto "github.com/gogo/protobuf/proto"
google_protobuf "github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
)

func IsEmbed(field *google_protobuf.FieldDescriptorProto) bool {
return proto.GetBoolExtension(field.Options, E_Embed, false)
Expand Down Expand Up @@ -109,34 +111,22 @@ func NeedsNilCheck(proto3 bool, field *google_protobuf.FieldDescriptorProto) boo

func IsCustomType(field *google_protobuf.FieldDescriptorProto) bool {
typ := GetCustomType(field)
if len(typ) > 0 {
return true
}
return false
return len(typ) > 0
}

func IsCastType(field *google_protobuf.FieldDescriptorProto) bool {
typ := GetCastType(field)
if len(typ) > 0 {
return true
}
return false
return len(typ) > 0
}

func IsCastKey(field *google_protobuf.FieldDescriptorProto) bool {
typ := GetCastKey(field)
if len(typ) > 0 {
return true
}
return false
return len(typ) > 0
}

func IsCastValue(field *google_protobuf.FieldDescriptorProto) bool {
typ := GetCastValue(field)
if len(typ) > 0 {
return true
}
return false
return len(typ) > 0
}

func HasEnumDecl(file *google_protobuf.FileDescriptorProto, enum *google_protobuf.EnumDescriptorProto) bool {
Expand Down Expand Up @@ -201,26 +191,17 @@ func GetCastValue(field *google_protobuf.FieldDescriptorProto) string {

func IsCustomName(field *google_protobuf.FieldDescriptorProto) bool {
name := GetCustomName(field)
if len(name) > 0 {
return true
}
return false
return len(name) > 0
}

func IsEnumCustomName(field *google_protobuf.EnumDescriptorProto) bool {
name := GetEnumCustomName(field)
if len(name) > 0 {
return true
}
return false
return len(name) > 0
}

func IsEnumValueCustomName(field *google_protobuf.EnumValueDescriptorProto) bool {
name := GetEnumValueCustomName(field)
if len(name) > 0 {
return true
}
return false
return len(name) > 0
}

func GetCustomName(field *google_protobuf.FieldDescriptorProto) string {
Expand Down
2 changes: 1 addition & 1 deletion protoc-gen-gogo/generator/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ func (g *Generator) generatePlugin(file *FileDescriptor, p Plugin) {
g.Write(rem.Bytes())

// Reformat generated code.
contents := string(g.Buffer.Bytes())
contents := g.Buffer.String()
fset := token.NewFileSet()
ast, err := parser.ParseFile(fset, "", g, parser.ParseComments)
if err != nil {
Expand Down
11 changes: 6 additions & 5 deletions test/deterministic/deterministic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ package deterministic

import (
"bytes"
"github.com/gogo/protobuf/proto"
"testing"

"github.com/gogo/protobuf/proto"
)

func getTestMap() map[string]string {
Expand Down Expand Up @@ -75,7 +76,7 @@ func TestOrderedMap(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if bytes.Compare(data1, data2) != 0 {
if !bytes.Equal(data1, data2) {
t.Fatal("byte arrays are not the same\n", data1, "\n", data2)
}
}
Expand Down Expand Up @@ -121,7 +122,7 @@ func TestMapNoMarshaler(t *testing.T) {
}
data2 := b2.Bytes()

if bytes.Compare(data1, data2) != 0 {
if !bytes.Equal(data1, data2) {
t.Fatal("byte arrays are not the same:\n", data1, "\n", data2)
}
}
Expand Down Expand Up @@ -149,7 +150,7 @@ func TestOrderedNestedMap(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if bytes.Compare(data1, data2) != 0 {
if !bytes.Equal(data1, data2) {
t.Fatal("byte arrays are not the same\n", data1, "\n", data2)
}
}
Expand Down Expand Up @@ -189,7 +190,7 @@ func TestOrderedNestedStructMap(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if bytes.Compare(data1, data2) != 0 {
if !bytes.Equal(data1, data2) {
t.Fatal("byte arrays are not the same\n", data1, "\n", data2)
}
}
Expand Down