当前位置: 首页>>代码示例>>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;未经允许,请勿转载。