當前位置: 首頁>>代碼示例>>Golang>>正文


Golang pktline.NewEncoder函數代碼示例

本文整理匯總了Golang中gopkg/in/src-d/go-git/v4/plumbing/format/pktline.NewEncoder函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewEncoder函數的具體用法?Golang NewEncoder怎麽用?Golang NewEncoder使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了NewEncoder函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: test

func (s *AdvRefsDecodeEncodeSuite) test(c *C, in []string, exp []string) {
	var err error
	var input io.Reader
	{
		var buf bytes.Buffer
		p := pktline.NewEncoder(&buf)
		err = p.EncodeString(in...)
		c.Assert(err, IsNil)
		input = &buf
	}

	var expected []byte
	{
		var buf bytes.Buffer
		p := pktline.NewEncoder(&buf)
		err = p.EncodeString(exp...)
		c.Assert(err, IsNil)

		expected = buf.Bytes()
	}

	var obtained []byte
	{
		ar := NewAdvRefs()
		c.Assert(ar.Decode(input), IsNil)

		var buf bytes.Buffer
		c.Assert(ar.Encode(&buf), IsNil)

		obtained = buf.Bytes()
	}

	c.Assert(string(obtained), DeepEquals, string(expected))
}
開發者ID:src-d,項目名稱:go-git,代碼行數:34,代碼來源:advrefs_test.go

示例2: Encode

// Encode writes the ReferenceUpdateRequest encoding to the stream.
func (r *ReferenceUpdateRequest) Encode(w io.Writer) error {
	if err := r.validate(); err != nil {
		return err
	}

	e := pktline.NewEncoder(w)

	if err := r.encodeShallow(e, r.Shallow); err != nil {
		return err
	}

	if err := r.encodeCommands(e, r.Commands, r.Capabilities); err != nil {
		return err
	}

	if r.Packfile != nil {
		if _, err := io.Copy(w, r.Packfile); err != nil {
			return err
		}

		return r.Packfile.Close()
	}

	return nil
}
開發者ID:src-d,項目名稱:go-git,代碼行數:26,代碼來源:updreq_encode.go

示例3: TestEmptyFlush

func (s *AdvRefsDecodeSuite) TestEmptyFlush(c *C) {
	var buf bytes.Buffer
	e := pktline.NewEncoder(&buf)
	e.Flush()
	ar := NewAdvRefs()
	c.Assert(ar.Decode(&buf), Equals, ErrEmptyAdvRefs)
}
開發者ID:src-d,項目名稱:go-git,代碼行數:7,代碼來源:advrefs_decode_test.go

示例4: TestScanAndPayload

func (s *SuiteScanner) TestScanAndPayload(c *C) {
	for _, test := range [...]string{
		"a",
		"a\n",
		strings.Repeat("a", 100),
		strings.Repeat("a", 100) + "\n",
		strings.Repeat("\x00", 100),
		strings.Repeat("\x00", 100) + "\n",
		strings.Repeat("a", pktline.MaxPayloadSize),
		strings.Repeat("a", pktline.MaxPayloadSize-1) + "\n",
	} {
		var buf bytes.Buffer
		e := pktline.NewEncoder(&buf)
		err := e.EncodeString(test)
		c.Assert(err, IsNil,
			Commentf("input len=%x, contents=%.10q\n", len(test), test))

		sc := pktline.NewScanner(&buf)
		c.Assert(sc.Scan(), Equals, true,
			Commentf("test = %.20q...", test))

		obtained := sc.Bytes()
		c.Assert(obtained, DeepEquals, []byte(test),
			Commentf("in = %.20q out = %.20q", test, string(obtained)))
	}
}
開發者ID:src-d,項目名稱:go-git,代碼行數:26,代碼來源:scanner_test.go

示例5: Encode

// Encode encodes the UploadHaves into the Writer.
func (u *UploadHaves) Encode(w io.Writer) error {
	e := pktline.NewEncoder(w)

	plumbing.HashesSort(u.Haves)

	var last plumbing.Hash
	for _, have := range u.Haves {
		if bytes.Compare(last[:], have[:]) == 0 {
			continue
		}

		if err := e.Encodef("have %s\n", have); err != nil {
			return fmt.Errorf("sending haves for %q: %s", have, err)
		}

		last = have
	}

	if len(u.Haves) != 0 {
		if err := e.Flush(); err != nil {
			return fmt.Errorf("sending flush-pkt after haves: %s", err)
		}
	}

	return nil
}
開發者ID:src-d,項目名稱:go-git,代碼行數:27,代碼來源:uppackreq.go

示例6: ExampleEncoder

func ExampleEncoder() {
	// Create an encoder that writes pktlines to stdout.
	e := pktline.NewEncoder(os.Stdout)

	// Encode some data as a new pkt-line.
	_ = e.Encode([]byte("data\n")) // error checks removed for brevity

	// Encode a flush-pkt.
	_ = e.Flush()

	// Encode a couple of byte slices and a flush in one go. Each of
	// them will end up as payloads of their own pktlines.
	_ = e.Encode(
		[]byte("hello\n"),
		[]byte("world!\n"),
		pktline.Flush,
	)

	// You can also encode strings:
	_ = e.EncodeString(
		"foo\n",
		"bar\n",
		pktline.FlushString,
	)

	// You can also format and encode a payload:
	_ = e.Encodef(" %s %d\n", "foo", 42)
	// Output:
	// 0009data
	// 0000000ahello
	// 000bworld!
	// 00000008foo
	// 0008bar
	// 0000000c foo 42
}
開發者ID:src-d,項目名稱:go-git,代碼行數:35,代碼來源:encoder_test.go

示例7: TestEncode

func (s *SuiteEncoder) TestEncode(c *C) {
	for i, test := range [...]struct {
		input    [][]byte
		expected []byte
	}{
		{
			input: [][]byte{
				[]byte("hello\n"),
			},
			expected: []byte("000ahello\n"),
		}, {
			input: [][]byte{
				[]byte("hello\n"),
				pktline.Flush,
			},
			expected: []byte("000ahello\n0000"),
		}, {
			input: [][]byte{
				[]byte("hello\n"),
				[]byte("world!\n"),
				[]byte("foo"),
			},
			expected: []byte("000ahello\n000bworld!\n0007foo"),
		}, {
			input: [][]byte{
				[]byte("hello\n"),
				pktline.Flush,
				[]byte("world!\n"),
				[]byte("foo"),
				pktline.Flush,
			},
			expected: []byte("000ahello\n0000000bworld!\n0007foo0000"),
		}, {
			input: [][]byte{
				[]byte(strings.Repeat("a", pktline.MaxPayloadSize)),
			},
			expected: []byte(
				"fff0" + strings.Repeat("a", pktline.MaxPayloadSize)),
		}, {
			input: [][]byte{
				[]byte(strings.Repeat("a", pktline.MaxPayloadSize)),
				[]byte(strings.Repeat("b", pktline.MaxPayloadSize)),
			},
			expected: []byte(
				"fff0" + strings.Repeat("a", pktline.MaxPayloadSize) +
					"fff0" + strings.Repeat("b", pktline.MaxPayloadSize)),
		},
	} {
		comment := Commentf("input %d = %v\n", i, test.input)

		var buf bytes.Buffer
		e := pktline.NewEncoder(&buf)

		err := e.Encode(test.input...)
		c.Assert(err, IsNil, comment)

		c.Assert(buf.Bytes(), DeepEquals, test.expected, comment)
	}
}
開發者ID:src-d,項目名稱:go-git,代碼行數:59,代碼來源:encoder_test.go

示例8: encode

func (s *CommandStatus) encode(w io.Writer) error {
	e := pktline.NewEncoder(w)
	if s.Error() == nil {
		return e.Encodef("ok %s\n", s.ReferenceName.String())
	}

	return e.Encodef("ng %s %s\n", s.ReferenceName.String(), s.Status)
}
開發者ID:src-d,項目名稱:go-git,代碼行數:8,代碼來源:report_status.go

示例9: toPktLines

func toPktLines(c *C, payloads []string) io.Reader {
	var buf bytes.Buffer
	e := pktline.NewEncoder(&buf)
	err := e.EncodeString(payloads...)
	c.Assert(err, IsNil)

	return &buf
}
開發者ID:src-d,項目名稱:go-git,代碼行數:8,代碼來源:common_test.go

示例10: TestEmptyPrefixFlush

func (s *AdvRefsDecodeSuite) TestEmptyPrefixFlush(c *C) {
	var buf bytes.Buffer
	e := pktline.NewEncoder(&buf)
	e.EncodeString("# service=git-upload-pack")
	e.Flush()
	e.Flush()
	ar := NewAdvRefs()
	c.Assert(ar.Decode(&buf), Equals, ErrEmptyAdvRefs)
}
開發者ID:src-d,項目名稱:go-git,代碼行數:9,代碼來源:advrefs_decode_test.go

示例11: pktlines

// returns a byte slice with the pkt-lines for the given payloads.
func pktlines(c *C, payloads ...string) []byte {
	var buf bytes.Buffer
	e := pktline.NewEncoder(&buf)

	err := e.EncodeString(payloads...)
	c.Assert(err, IsNil, Commentf("building pktlines for %v\n", payloads))

	return buf.Bytes()
}
開發者ID:src-d,項目名稱:go-git,代碼行數:10,代碼來源:common_test.go

示例12: TestFlush

func (s *SuiteEncoder) TestFlush(c *C) {
	var buf bytes.Buffer
	e := pktline.NewEncoder(&buf)

	err := e.Flush()
	c.Assert(err, IsNil)

	obtained := buf.Bytes()
	c.Assert(obtained, DeepEquals, pktline.FlushPkt)
}
開發者ID:src-d,項目名稱:go-git,代碼行數:10,代碼來源:encoder_test.go

示例13: testDecodeOK

func (s *AdvRefsDecodeSuite) testDecodeOK(c *C, payloads []string) *AdvRefs {
	var buf bytes.Buffer
	e := pktline.NewEncoder(&buf)
	err := e.EncodeString(payloads...)
	c.Assert(err, IsNil)

	ar := NewAdvRefs()
	c.Assert(ar.Decode(&buf), IsNil)

	return ar
}
開發者ID:src-d,項目名稱:go-git,代碼行數:11,代碼來源:advrefs_decode_test.go

示例14: NewMuxer

// NewMuxer returns a new Muxer for the given t that writes on w.
//
// If t is equal to `Sideband` the max pack size is set to MaxPackedSize, in any
// other value is given, max pack is set to MaxPackedSize64k, that is the
// maximum length of a line in pktline format.
func NewMuxer(t Type, w io.Writer) *Muxer {
	max := MaxPackedSize64k
	if t == Sideband {
		max = MaxPackedSize
	}

	return &Muxer{
		max: max - chLen,
		e:   pktline.NewEncoder(w),
	}
}
開發者ID:src-d,項目名稱:go-git,代碼行數:16,代碼來源:muxer.go

示例15: TestEOF

func (s *SuiteScanner) TestEOF(c *C) {
	var buf bytes.Buffer
	e := pktline.NewEncoder(&buf)
	err := e.EncodeString("first", "second")
	c.Assert(err, IsNil)

	sc := pktline.NewScanner(&buf)
	for sc.Scan() {
	}
	c.Assert(sc.Err(), IsNil)
}
開發者ID:src-d,項目名稱:go-git,代碼行數:11,代碼來源:scanner_test.go


注:本文中的gopkg/in/src-d/go-git/v4/plumbing/format/pktline.NewEncoder函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。