예제 #1
0
파일: text_test.go 프로젝트: hail100/cli
func newTestMessage() *pb.MyMessage {
	msg := &pb.MyMessage{
		Count: proto.Int32(42),
		Name:  proto.String("Dave"),
		Quote: proto.String(`"I didn't want to go."`),
		Pet:   []string{"bunny", "kitty", "horsey"},
		Inner: &pb.InnerMessage{
			Host:      proto.String("footrest.syd"),
			Port:      proto.Int32(7001),
			Connected: proto.Bool(true),
		},
		Others: []*pb.OtherMessage{
			{
				Key:   proto.Int64(0xdeadbeef),
				Value: []byte{1, 65, 7, 12},
			},
			{
				Weight: proto.Float32(6.022),
				Inner: &pb.InnerMessage{
					Host: proto.String("lesha.mtv"),
					Port: proto.Int32(8002),
				},
			},
		},
		Bikeshed: pb.MyMessage_BLUE.Enum(),
		Somegroup: &pb.MyMessage_SomeGroup{
			GroupField: proto.Int32(8),
		},
		// One normally wouldn't do this.
		// This is an undeclared tag 13, as a varint (wire type 0) with value 4.
		XXX_unrecognized: []byte{13<<3 | 0, 4},
	}
	ext := &pb.Ext{
		Data: proto.String("Big gobs for big rats"),
	}
	if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil {
		panic(err)
	}
	greetings := []string{"adg", "easy", "cow"}
	if err := proto.SetExtension(msg, pb.E_Greeting, greetings); err != nil {
		panic(err)
	}

	// Add an unknown extension. We marshal a pb.Ext, and fake the ID.
	b, err := proto.Marshal(&pb.Ext{Data: proto.String("3G skiing")})
	if err != nil {
		panic(err)
	}
	b = append(proto.EncodeVarint(201<<3|proto.WireBytes), b...)
	proto.SetRawExtension(msg, 201, b)

	// Extensions can be plain fields, too, so let's test that.
	b = append(proto.EncodeVarint(202<<3|proto.WireVarint), 19)
	proto.SetRawExtension(msg, 202, b)

	return msg
}
예제 #2
0
func ExampleCompile() {
	a := &test.NinOptNative{
		Field4: proto.Int64(1234),
		Field7: proto.Int32(123),
	}
	fp1, err := fieldpath.NewInt64Path("test", "NinOptNative", test.ThetestDescription(), "Field4")
	if err != nil {
		panic(err)
	}
	fp2, err := fieldpath.NewSint32Path("test", "NinOptNative", test.ThetestDescription(), "Field7")
	if err != nil {
		panic(err)
	}
	buf, err := proto.Marshal(a)
	if err != nil {
		panic(err)
	}
	u1 := fieldpath.NewInt64Unmarshaler(fp1, &handler64{})
	u2 := fieldpath.NewSint32Unmarshaler(fp2, &handler32{})
	c := fieldpath.Compile(u1, u2)
	err = c.Unmarshal(buf)
	if err != nil {
		panic(err)
	}
	// Output:
	// 1234
	// 123
}
예제 #3
0
func generateGoGoProtobufMessages() []proto.Message {
	return []proto.Message{
		&example.GoGoProtobufTestMessage1{
			F0: proto.Int32(1),
			F1: proto.String("hello"),
			F2: proto.Float32(4.2)},
		&example.GoGoProtobufTestMessage2{
			F0: proto.Int32(2),
			F1: proto.String("world"),
			F2: proto.Float32(2.4)},
		&example.GoGoProtobufTestMessage3{
			F0: proto.Int32(3),
			F1: proto.String("test"),
			F2: proto.String("4.2")},
		&example.GoGoProtobufTestMessage4{
			F0: proto.Int32(3),
			F1: proto.String("codec")},
	}
}
예제 #4
0
func generateMessages(n int) []proto.Message {
	var m []proto.Message

	for i := 0; i < n; i++ {
		m1 := &example.GoGoProtobufTestMessage1{
			F0: proto.Int32(int32(rand.Int())),
			F1: proto.String(fmt.Sprintf("%10d", rand.Int())),
			F2: proto.Float32(rand.Float32()),
		}
		m = append(m, m1)

		m2 := &example.GoGoProtobufTestMessage2{
			F0: proto.Int32(int32(rand.Int())),
			F1: proto.String(fmt.Sprintf("%10d", rand.Int())),
			F2: proto.Float32(rand.Float32()),
		}
		m = append(m, m2)

		m3 := &example.GoGoProtobufTestMessage3{
			F0: proto.Int32(int32(rand.Int())),
			F1: proto.String(fmt.Sprintf("%10d", rand.Int())),
			F2: proto.String(fmt.Sprintf("%10d", rand.Int())),
		}
		m = append(m, m3)

		m4 := &example.GoGoProtobufTestMessage4{
			F0: proto.Int32(int32(rand.Int())),
			F1: proto.String(fmt.Sprintf("%10d", rand.Int())),
		}
		m = append(m, m4)
	}

	// Shuffle the messages.
	for i := range m {
		index := rand.Intn(i + 1)
		m[i], m[index] = m[index], m[i]
	}
	return m
}
			})
		})
	})

	Describe("NewHttpStop", func() {
		BeforeEach(func() {
			req.Header.Set("X-CF-ApplicationID", applicationId.String())
		})

		It("should set appropriate fields", func() {
			expectedStopEvent := &events.HttpStop{
				ApplicationId: factories.NewUUID(applicationId),
				RequestId:     factories.NewUUID(requestId),
				Uri:           proto.String("foo.example.com/"),
				PeerType:      events.PeerType_Server.Enum(),
				StatusCode:    proto.Int32(200),
				ContentLength: proto.Int64(3),
			}

			stopEvent := factories.NewHttpStop(req, 200, 3, events.PeerType_Server, requestId)

			Expect(stopEvent.GetTimestamp()).ToNot(BeZero())
			stopEvent.Timestamp = nil

			Expect(stopEvent).To(Equal(expectedStopEvent))
		})
	})

	Describe("StringFromUUID", func() {
		It("returns a string for a UUID", func() {
			id := factories.NewUUID(requestId)
예제 #6
0
파일: clone_test.go 프로젝트: hail100/cli
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package proto_test

import (
	"testing"

	"code.google.com/p/gogoprotobuf/proto"

	pb "./testdata"
)

var cloneTestMessage = &pb.MyMessage{
	Count: proto.Int32(42),
	Name:  proto.String("Dave"),
	Pet:   []string{"bunny", "kitty", "horsey"},
	Inner: &pb.InnerMessage{
		Host:      proto.String("niles"),
		Port:      proto.Int32(9099),
		Connected: proto.Bool(true),
	},
	Others: []*pb.OtherMessage{
		{
			Value: []byte("some bytes"),
		},
	},
	Somegroup: &pb.MyMessage_SomeGroup{
		GroupField: proto.Int32(6),
	},