Merge pull request #106 from peterbourgon/fix-ext-pbutil

s/ext/pbutil/g
This commit is contained in:
Björn Rabenstein 2015-04-07 15:16:34 +02:00
commit 419b21ce61
9 changed files with 25 additions and 60 deletions

4
Godeps/Godeps.json generated
View File

@ -19,8 +19,8 @@
"Rev": "c22ae3cf020a21ebb7ae566dccbe90fc8ea4f9ea" "Rev": "c22ae3cf020a21ebb7ae566dccbe90fc8ea4f9ea"
}, },
{ {
"ImportPath": "github.com/matttproud/golang_protobuf_extensions/ext", "ImportPath": "github.com/matttproud/golang_protobuf_extensions/pbutil",
"Rev": "ba7d65ac66e9da93a714ca18f6d1bc7a0c09100c" "Rev": "fc2b8d3a73c4867e51861bbdd5ae3c1f0869dd6a"
}, },
{ {
"ImportPath": "github.com/prometheus/client_model/go", "ImportPath": "github.com/prometheus/client_model/go",

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package ext package pbutil
import ( import (
"bytes" "bytes"
@ -21,6 +21,8 @@ import (
"testing" "testing"
"testing/quick" "testing/quick"
"github.com/matttproud/golang_protobuf_extensions/pbtest"
. "github.com/golang/protobuf/proto" . "github.com/golang/protobuf/proto"
. "github.com/golang/protobuf/proto/testdata" . "github.com/golang/protobuf/proto/testdata"
) )
@ -138,10 +140,10 @@ I expect it may. Let's hope you enjoy testing as much as we do.`),
func TestEndToEndValid(t *testing.T) { func TestEndToEndValid(t *testing.T) {
for _, test := range [][]Message{ for _, test := range [][]Message{
[]Message{&Empty{}}, {&Empty{}},
[]Message{&GoEnum{Foo: FOO_FOO1.Enum()}, &Empty{}, &GoEnum{Foo: FOO_FOO1.Enum()}}, {&GoEnum{Foo: FOO_FOO1.Enum()}, &Empty{}, &GoEnum{Foo: FOO_FOO1.Enum()}},
[]Message{&GoEnum{Foo: FOO_FOO1.Enum()}}, {&GoEnum{Foo: FOO_FOO1.Enum()}},
[]Message{&Strings{ {&Strings{
StringField: String(`This is my gigantic, unhappy string. It exceeds StringField: String(`This is my gigantic, unhappy string. It exceeds
the encoding size of a single byte varint. We are using it to fuzz test the the encoding size of a single byte varint. We are using it to fuzz test the
correctness of the header decoding mechanisms, which may prove problematic. correctness of the header decoding mechanisms, which may prove problematic.
@ -176,45 +178,6 @@ I expect it may. Let's hope you enjoy testing as much as we do.`),
} }
} }
// visitMessage empties the private state fields of the quick.Value()-generated
// Protocol Buffer messages, for they cause an inordinate amount of problems.
// This is because we are using an automated fuzz generator on a type with
// private fields.
func visitMessage(m Message) {
t := reflect.TypeOf(m)
if t.Kind() != reflect.Ptr {
return
}
derefed := t.Elem()
if derefed.Kind() != reflect.Struct {
return
}
v := reflect.ValueOf(m)
elem := v.Elem()
for i := 0; i < elem.NumField(); i++ {
field := elem.FieldByIndex([]int{i})
fieldType := field.Type()
if fieldType.Implements(reflect.TypeOf((*Message)(nil)).Elem()) {
visitMessage(field.Interface().(Message))
}
if field.Kind() == reflect.Slice {
for i := 0; i < field.Len(); i++ {
elem := field.Index(i)
elemType := elem.Type()
if elemType.Implements(reflect.TypeOf((*Message)(nil)).Elem()) {
visitMessage(elem.Interface().(Message))
}
}
}
}
if field := elem.FieldByName("XXX_unrecognized"); field.IsValid() {
field.Set(reflect.ValueOf([]byte{}))
}
if field := elem.FieldByName("XXX_extensions"); field.IsValid() {
field.Set(reflect.ValueOf(nil))
}
}
// rndMessage generates a random valid Protocol Buffer message. // rndMessage generates a random valid Protocol Buffer message.
func rndMessage(r *rand.Rand) Message { func rndMessage(r *rand.Rand) Message {
var t reflect.Type var t reflect.Type
@ -307,7 +270,9 @@ func rndMessage(r *rand.Rand) Message {
if !ok { if !ok {
panic("attempt to generate illegal item; consult item 11") panic("attempt to generate illegal item; consult item 11")
} }
visitMessage(v.Interface().(Message)) if err := pbtest.SanitizeGenerated(v.Interface().(Message)); err != nil {
panic(err)
}
return v.Interface().(Message) return v.Interface().(Message)
} }

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package ext package pbutil
import ( import (
"encoding/binary" "encoding/binary"

View File

@ -12,5 +12,5 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// Package ext enables record length-delimited Protocol Buffer streaming. // Package pbutil provides record length-delimited Protocol Buffer streaming.
package ext package pbutil

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package ext package pbutil
import ( import (
"encoding/binary" "encoding/binary"

View File

@ -27,7 +27,7 @@
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package ext package pbutil
import ( import (
. "github.com/golang/protobuf/proto" . "github.com/golang/protobuf/proto"

View File

@ -20,7 +20,7 @@ import (
dto "github.com/prometheus/client_model/go" dto "github.com/prometheus/client_model/go"
"github.com/matttproud/golang_protobuf_extensions/ext" "github.com/matttproud/golang_protobuf_extensions/pbutil"
"github.com/prometheus/client_golang/model" "github.com/prometheus/client_golang/model"
) )
@ -40,7 +40,7 @@ func (m *metricFamilyProcessor) ProcessSingle(i io.Reader, out Ingester, o *Proc
for { for {
family.Reset() family.Reset()
if _, err := ext.ReadDelimited(i, family); err != nil { if _, err := pbutil.ReadDelimited(i, family); err != nil {
if err == io.EOF { if err == io.EOF {
return nil return nil
} }

View File

@ -21,7 +21,7 @@ import (
"testing" "testing"
dto "github.com/prometheus/client_model/go" dto "github.com/prometheus/client_model/go"
"github.com/matttproud/golang_protobuf_extensions/ext" "github.com/matttproud/golang_protobuf_extensions/pbutil"
) )
// Benchmarks to show how much penalty text format parsing actually inflicts. // Benchmarks to show how much penalty text format parsing actually inflicts.
@ -101,7 +101,7 @@ func BenchmarkParseProto(b *testing.B) {
in := bytes.NewReader(data) in := bytes.NewReader(data)
for { for {
family.Reset() family.Reset()
if _, err := ext.ReadDelimited(in, family); err != nil { if _, err := pbutil.ReadDelimited(in, family); err != nil {
if err == io.EOF { if err == io.EOF {
break break
} }
@ -129,7 +129,7 @@ func BenchmarkParseProtoGzip(b *testing.B) {
} }
for { for {
family.Reset() family.Reset()
if _, err := ext.ReadDelimited(in, family); err != nil { if _, err := pbutil.ReadDelimited(in, family); err != nil {
if err == io.EOF { if err == io.EOF {
break break
} }
@ -156,7 +156,7 @@ func BenchmarkParseProtoMap(b *testing.B) {
in := bytes.NewReader(data) in := bytes.NewReader(data)
for { for {
family := &dto.MetricFamily{} family := &dto.MetricFamily{}
if _, err := ext.ReadDelimited(in, family); err != nil { if _, err := pbutil.ReadDelimited(in, family); err != nil {
if err == io.EOF { if err == io.EOF {
break break
} }

View File

@ -18,7 +18,7 @@ import (
"io" "io"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"github.com/matttproud/golang_protobuf_extensions/ext" "github.com/matttproud/golang_protobuf_extensions/pbutil"
dto "github.com/prometheus/client_model/go" dto "github.com/prometheus/client_model/go"
) )
@ -27,7 +27,7 @@ import (
// protobuf format and returns the number of bytes written and any error // protobuf format and returns the number of bytes written and any error
// encountered. // encountered.
func WriteProtoDelimited(w io.Writer, p *dto.MetricFamily) (int, error) { func WriteProtoDelimited(w io.Writer, p *dto.MetricFamily) (int, error) {
return ext.WriteDelimited(w, p) return pbutil.WriteDelimited(w, p)
} }
// WriteProtoText writes the MetricFamily to the writer in text format and // WriteProtoText writes the MetricFamily to the writer in text format and