Sync changes to internal version:
- use append built-in
- remove NewT from commentary
- some idiom changes
R=r
CC=golang-dev
http://codereview.appspot.com/2926042
diff --git a/compiler/generator/generator.go b/compiler/generator/generator.go
index f64b5d4..2f9aebe 100644
--- a/compiler/generator/generator.go
+++ b/compiler/generator/generator.go
@@ -71,14 +71,7 @@
// RegisterPlugin installs a (second-order) plugin to be run when the Go output is generated.
// It is typically called during initialization.
func RegisterPlugin(p Plugin) {
- n := len(plugins)
- if cap(plugins) == n {
- nplugins := make([]Plugin, n, n+10) // very unlikely to need more than this
- copy(nplugins, plugins)
- plugins = nplugins
- }
- plugins = plugins[0 : n+1]
- plugins[n] = p
+ plugins = append(plugins, p)
}
// Each type we import as a protocol buffer (other than FileDescriptorProto) needs
@@ -438,14 +431,7 @@
d.ext[i] = &ExtensionDescriptor{common{File: file}, field, d}
}
- if len(sl) == cap(sl) {
- nsl := make([]*Descriptor, len(sl), 2*len(sl))
- copy(nsl, sl)
- sl = nsl
- }
- sl = sl[0 : len(sl)+1]
- sl[len(sl)-1] = d
- return sl
+ return append(sl, d)
}
// Return a slice of all the Descriptors defined within this file
@@ -469,14 +455,7 @@
// Construct the EnumDescriptor and add it to the slice
func addEnumDescriptor(sl []*EnumDescriptor, desc *descriptor.EnumDescriptorProto, parent *Descriptor, file *descriptor.FileDescriptorProto) []*EnumDescriptor {
- if len(sl) == cap(sl) {
- nsl := make([]*EnumDescriptor, len(sl), 2*len(sl))
- copy(nsl, sl)
- sl = nsl
- }
- sl = sl[0 : len(sl)+1]
- sl[len(sl)-1] = &EnumDescriptor{common{File: file}, desc, parent, nil}
- return sl
+ return append(sl, &EnumDescriptor{common{File: file}, desc, parent, nil})
}
// Return a slice of all the EnumDescriptors defined within this file
diff --git a/compiler/testdata/extension_test.go b/compiler/testdata/extension_test.go
index 85c7d56..8c805da 100644
--- a/compiler/testdata/extension_test.go
+++ b/compiler/testdata/extension_test.go
@@ -42,8 +42,9 @@
)
func TestSingleFieldExtension(t *testing.T) {
- bm := &base.BaseMessage{}
- bm.Height = proto.Int32(178)
+ bm := &base.BaseMessage{
+ Height: proto.Int32(178),
+ }
// Use extension within scope of another type.
vol := proto.Uint32(11)
@@ -55,7 +56,7 @@
if err != nil {
t.Fatal("Failed encoding message with extension:", err)
}
- bm_new := &base.BaseMessage{}
+ bm_new := new(base.BaseMessage)
if err := proto.Unmarshal(buf, bm_new); err != nil {
t.Fatal("Failed decoding message with extension:", err)
}
@@ -76,8 +77,9 @@
}
func TestMessageExtension(t *testing.T) {
- bm := &base.BaseMessage{}
- bm.Height = proto.Int32(179)
+ bm := &base.BaseMessage{
+ Height: proto.Int32(179),
+ }
// Use extension that is itself a message.
um := &user.UserMessage{
@@ -92,7 +94,7 @@
if err != nil {
t.Fatal("Failed encoding message with extension:", err)
}
- bm_new := &base.BaseMessage{}
+ bm_new := new(base.BaseMessage)
if err := proto.Unmarshal(buf, bm_new); err != nil {
t.Fatal("Failed decoding message with extension:", err)
}
@@ -116,8 +118,9 @@
}
func TestTopLevelExtension(t *testing.T) {
- bm := &base.BaseMessage{}
- bm.Height = proto.Int32(179)
+ bm := &base.BaseMessage{
+ Height: proto.Int32(179),
+ }
width := proto.Int32(17)
err := proto.SetExtension(bm, user.E_Width, width)
@@ -128,7 +131,7 @@
if err != nil {
t.Fatal("Failed encoding message with extension:", err)
}
- bm_new := &base.BaseMessage{}
+ bm_new := new(base.BaseMessage)
if err := proto.Unmarshal(buf, bm_new); err != nil {
t.Fatal("Failed decoding message with extension:", err)
}
diff --git a/proto/lib.go b/proto/lib.go
index f440758..083f104 100644
--- a/proto/lib.go
+++ b/proto/lib.go
@@ -43,8 +43,6 @@
- The zero value for a struct is its correct initialization state.
All desired fields must be set before marshaling.
- A Reset() method will restore a protobuf struct to its zero state.
- - Each type T has a method NewT() to create a new instance. It
- is equivalent to new(T).
- Non-repeated fields are pointers to the values; nil means unset.
That is, optional or required field int32 f becomes F *int32.
- Repeated fields are slices.
@@ -112,9 +110,6 @@
func (this *Test) Reset() {
*this = Test{}
}
- func NewTest() *Test {
- return new(Test)
- }
const Default_Test_Type int32 = 77
type Test_OptionalGroup struct {
@@ -124,9 +119,6 @@
func (this *Test_OptionalGroup) Reset() {
*this = Test_OptionalGroup{}
}
- func NewTest_OptionalGroup() *Test_OptionalGroup {
- return new(Test_OptionalGroup)
- }
func init() {
proto.RegisterEnum("example.FOO", FOO_name, FOO_value)
@@ -144,25 +136,25 @@
)
func main() {
- test := &example.Test {
+ test := &example.Test{
Label: proto.String("hello"),
Type: proto.Int32(17),
- Optionalgroup: &example.Test_OptionalGroup {
+ Optionalgroup: &example.Test_OptionalGroup{
RequiredField: proto.String("good bye"),
},
}
data, err := proto.Marshal(test)
if err != nil {
- log.Exitln("marshaling error:", err)
+ log.Exit("marshaling error:", err)
}
- newTest := example.NewTest()
+ newTest := new(example.Test)
err = proto.Unmarshal(data, newTest)
if err != nil {
- log.Exitln("unmarshaling error:", err)
+ log.Exit("unmarshaling error:", err)
}
// Now test and newTest contain the same data.
if proto.GetString(test.Label) != proto.GetString(newTest.Label) {
- log.Exitf("data mismatch %q %q", proto.GetString(test.Label), proto.GetString(newTest.Label))
+ log.Exit("data mismatch %q %q", proto.GetString(test.Label), proto.GetString(newTest.Label))
}
// etc.
}