本文整理汇总了Golang中github.com/ugorji/go/codec.NewEncoder函数的典型用法代码示例。如果您正苦于以下问题:Golang NewEncoder函数的具体用法?Golang NewEncoder怎么用?Golang NewEncoder使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewEncoder函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: TestReportPostHandler
func TestReportPostHandler(t *testing.T) {
test := func(contentType string, encoder func(interface{}) ([]byte, error)) {
router := mux.NewRouter()
c := app.NewCollector(1 * time.Minute)
app.RegisterReportPostHandler(c, router)
ts := httptest.NewServer(router)
defer ts.Close()
b, err := encoder(fixture.Report)
if err != nil {
t.Fatalf("Content-Type %s: %s", contentType, err)
}
req, err := http.NewRequest("POST", ts.URL+"/api/report", bytes.NewReader(b))
if err != nil {
t.Fatalf("Error posting report: %v", err)
}
req.Header.Set("Content-Type", contentType)
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("Error posting report %v", err)
}
_, err = ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
t.Fatalf("Error posting report: %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Fatalf("Error posting report: %d", resp.StatusCode)
}
ctx := context.Background()
report, err := c.Report(ctx)
if err != nil {
t.Error(err)
}
if want, have := fixture.Report.Endpoint.Nodes, report.Endpoint.Nodes; len(have) == 0 || len(want) != len(have) {
t.Fatalf("Content-Type %s: %v", contentType, test.Diff(have, want))
}
}
test("", func(v interface{}) ([]byte, error) {
buf := &bytes.Buffer{}
err := gob.NewEncoder(buf).Encode(v)
return buf.Bytes(), err
})
test("application/json", func(v interface{}) ([]byte, error) {
buf := &bytes.Buffer{}
err := codec.NewEncoder(buf, &codec.JsonHandle{}).Encode(v)
return buf.Bytes(), err
})
test("application/msgpack", func(v interface{}) ([]byte, error) {
buf := &bytes.Buffer{}
err := codec.NewEncoder(buf, &codec.MsgpackHandle{}).Encode(v)
return buf.Bytes(), err
})
}
开发者ID:dilgerma,项目名称:scope,代码行数:60,代码来源:router_test.go
示例2: wrap
// wrap is used to wrap functions to make them more convenient
func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Request) (interface{}, error)) func(resp http.ResponseWriter, req *http.Request) {
f := func(resp http.ResponseWriter, req *http.Request) {
setHeaders(resp, s.agent.config.HTTPAPIResponseHeaders)
// Invoke the handler
reqURL := req.URL.String()
start := time.Now()
defer func() {
s.logger.Printf("[DEBUG] http: Request %v (%v)", reqURL, time.Now().Sub(start))
}()
obj, err := handler(resp, req)
// Check for an error
HAS_ERR:
if err != nil {
s.logger.Printf("[ERR] http: Request %v, error: %v", reqURL, err)
code := 500
if http, ok := err.(HTTPCodedError); ok {
code = http.Code()
}
resp.WriteHeader(code)
resp.Write([]byte(err.Error()))
return
}
prettyPrint := false
if v, ok := req.URL.Query()["pretty"]; ok {
if len(v) > 0 && (len(v[0]) == 0 || v[0] != "0") {
prettyPrint = true
}
}
// Write out the JSON object
if obj != nil {
var buf bytes.Buffer
if prettyPrint {
enc := codec.NewEncoder(&buf, jsonHandlePretty)
err = enc.Encode(obj)
if err == nil {
buf.Write([]byte("\n"))
}
} else {
enc := codec.NewEncoder(&buf, jsonHandle)
err = enc.Encode(obj)
}
if err != nil {
goto HAS_ERR
}
resp.Header().Set("Content-Type", "application/json")
resp.Write(buf.Bytes())
}
}
return f
}
开发者ID:cursesun,项目名称:nomad,代码行数:54,代码来源:http.go
示例3: newRPCCodec
func newRPCCodec(conn io.ReadWriteCloser, h codec.Handle) *rpcCodec {
bw := bufio.NewWriter(conn)
br := bufio.NewReader(conn)
bb := new(bytes.Buffer)
return &rpcCodec{
rwc: conn,
bw: bw,
br: br,
enc: codec.NewEncoder(bw, h),
dec: codec.NewDecoder(br, h),
benc: codec.NewEncoder(bb, h),
h: h,
byteBuf: bb,
}
}
开发者ID:aalness,项目名称:go-framed-msgpack-rpc,代码行数:15,代码来源:rpc.go
示例4: TestEdgeMetadatasEncodingNil
func TestEdgeMetadatasEncodingNil(t *testing.T) {
want := EdgeMetadatas{}
{
gobs, err := want.GobEncode()
if err != nil {
t.Fatal(err)
}
have := EmptyEdgeMetadatas
have.GobDecode(gobs)
if have.psMap == nil {
t.Error("needed to get back a non-nil psMap for EdgeMetadata")
}
}
{
for _, h := range []codec.Handle{
codec.Handle(&codec.MsgpackHandle{}),
codec.Handle(&codec.JsonHandle{}),
} {
buf := &bytes.Buffer{}
encoder := codec.NewEncoder(buf, h)
want.CodecEncodeSelf(encoder)
decoder := codec.NewDecoder(buf, h)
have := EmptyEdgeMetadatas
have.CodecDecodeSelf(decoder)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
}
}
}
开发者ID:dilgerma,项目名称:scope,代码行数:33,代码来源:edge_metadatas_internal_test.go
示例5: sendTCPUserMsg
// sendTCPUserMsg is used to send a TCP userMsg to another host
func (m *Memberlist) sendTCPUserMsg(to net.Addr, sendBuf []byte) error {
dialer := net.Dialer{Timeout: m.config.TCPTimeout}
conn, err := dialer.Dial("tcp", to.String())
if err != nil {
return err
}
defer conn.Close()
bufConn := bytes.NewBuffer(nil)
if err := bufConn.WriteByte(byte(userMsg)); err != nil {
return err
}
// Send our node state
header := userMsgHeader{UserMsgLen: len(sendBuf)}
hd := codec.MsgpackHandle{}
enc := codec.NewEncoder(bufConn, &hd)
if err := enc.Encode(&header); err != nil {
return err
}
if _, err := bufConn.Write(sendBuf); err != nil {
return err
}
return m.rawSendMsgTCP(conn, bufConn.Bytes())
}
开发者ID:rbg,项目名称:memberlist,代码行数:30,代码来源:net.go
示例6: TestCodec
// This test determines the behavior of codec with respect to advancing the
// Reader when decoding error scenarios. It seems that the codec advances
// the Reader if Decode fails, but sets its own state to expect a specific
// type for the next Decode, and thus is functionally the same as not
// advancing the Reader.
func TestCodec(t *testing.T) {
var buf bytes.Buffer
mh := &codec.MsgpackHandle{WriteExt: true}
enc := codec.NewEncoder(&buf, mh)
dec := codec.NewDecoder(&buf, mh)
var i int = math.MaxInt32
err := enc.Encode(i)
require.Nil(t, err, "expected encoding to succeed")
require.Equal(t, 5, len(buf.Bytes()), "expected buffer to contain bytes")
var targetInt int
err = dec.Decode(&targetInt)
require.Nil(t, err, "expected decoding to succeed")
require.Equal(t, math.MaxInt32, targetInt, "expected codec to successfully decode int")
require.Equal(t, 0, len(buf.Bytes()), "expected buffer to be empty")
var targetString string
enc.Encode(i)
require.Equal(t, 5, len(buf.Bytes()), "expected buffer to contain bytes")
err = dec.Decode(&targetString)
require.Error(t, err, "expected error while decoding")
require.Contains(t, err.Error(), "Unrecognized descriptor byte", "expected error while decoding")
require.Equal(t, 4, len(buf.Bytes()), "expected buffer to have bytes")
err = dec.Decode(&targetString)
require.Error(t, err, "expected error while decoding")
require.Contains(t, err.Error(), "Unrecognized descriptor byte", "expected error while decoding")
require.Equal(t, 4, len(buf.Bytes()), "expected buffer to have bytes")
targetInt = 0
err = dec.Decode(&targetInt)
require.Nil(t, err, "expected decoding to succeed")
require.Equal(t, math.MaxInt32, targetInt, "expected codec to successfully decode int")
require.Equal(t, 0, len(buf.Bytes()), "expected buffer to be empty")
}
开发者ID:jzila,项目名称:go-framed-msgpack-rpc,代码行数:40,代码来源:codec_test.go
示例7: loadContents
// loadContents loads or calculates and saves package contents
func (collection *PackageCollection) loadContents(p *Package, packagePool aptly.PackagePool) []string {
encoded, err := collection.db.Get(p.Key("xC"))
if err == nil {
contents := []string{}
decoder := codec.NewDecoderBytes(encoded, collection.codecHandle)
err = decoder.Decode(&contents)
if err != nil {
panic("unable to decode contents")
}
return contents
}
if err != database.ErrNotFound {
panic("unable to load contents")
}
contents := p.CalculateContents(packagePool)
var buf bytes.Buffer
err = codec.NewEncoder(&buf, collection.codecHandle).Encode(contents)
if err != nil {
panic("unable to encode contents")
}
err = collection.db.Put(p.Key("xC"), buf.Bytes())
if err != nil {
panic("unable to save contents")
}
return contents
}
开发者ID:liftup,项目名称:aptly,代码行数:34,代码来源:package_collection.go
示例8: encodeMsgPack
// Encode writes an encoded object to a new bytes buffer
func encodeMsgPack(in interface{}) (*bytes.Buffer, error) {
buf := bytes.NewBuffer(nil)
hd := codec.MsgpackHandle{}
enc := codec.NewEncoder(buf, &hd)
err := enc.Encode(in)
return buf, err
}
开发者ID:rbg,项目名称:raft,代码行数:8,代码来源:util.go
示例9: handleConnection
func handleConnection(conn net.Conn, jobd *jobserver.JobServer, cbor *codec.CborHandle) {
defer conn.Close()
jobdv := reflect.ValueOf(jobd)
reader := bufio.NewReader(conn)
decoder := codec.NewDecoder(reader, cbor)
writer := bufio.NewWriter(conn)
encoder := codec.NewEncoder(writer, cbor)
for {
var request cborrpc.Request
err := decoder.Decode(&request)
if err == io.EOF {
return
} else if err != nil {
fmt.Printf("Error reading message: %v\n", err)
return
}
fmt.Printf("Request: %v\n", request)
response := doRequest(jobdv, request)
fmt.Printf("Response: %v\n", response)
err = encoder.Encode(response)
if err != nil {
fmt.Printf("Error encoding response: %v\n", err)
return
}
err = writer.Flush()
if err != nil {
fmt.Printf("Error writing response: %v\n", err)
return
}
}
}
开发者ID:dmaze,项目名称:goordinate,代码行数:34,代码来源:main.go
示例10: idHandlerFunc
func idHandlerFunc(ctx *fasthttp.RequestCtx, idWorker *goflake.IdWorker, retry int) {
ua := string(ctx.UserAgent())
var (
id uint64
err error
)
for i := 0; i < retry; i++ {
id, err = idWorker.GetId(ua)
if err == nil {
break
}
}
r := map[string]string{
"id": strconv.FormatUint(id, 10),
}
if strings.HasSuffix(string(ctx.Path()), ".msgpack") {
ctx.SetContentType("application/x-msgpack; charset=UTF-8")
if err := codec.NewEncoder(ctx, mh).Encode(r); err != nil {
ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
}
} else {
ctx.SetContentType("application/json; charset=UTF-8")
if err := json.NewEncoder(ctx).Encode(r); err != nil {
ctx.Error(fmt.Sprintf(`{"error":"%v"}`, err.Error()), fasthttp.StatusInternalServerError)
}
}
}
开发者ID:hirokazumiyaji,项目名称:goflake-server,代码行数:31,代码来源:main.go
示例11: Add
func (c *dynamoDBCollector) Add(ctx context.Context, rep report.Report) error {
userid, err := c.userIDer(ctx)
if err != nil {
return err
}
var buf bytes.Buffer
writer := gzip.NewWriter(&buf)
if err := codec.NewEncoder(writer, &codec.MsgpackHandle{}).Encode(&rep); err != nil {
return err
}
writer.Close()
now := time.Now()
rowKey := fmt.Sprintf("%s-%s", userid, strconv.FormatInt(now.UnixNano()/time.Hour.Nanoseconds(), 10))
_, err = c.db.PutItem(&dynamodb.PutItemInput{
TableName: aws.String(tableName),
Item: map[string]*dynamodb.AttributeValue{
hourField: {
S: aws.String(rowKey),
},
tsField: {
N: aws.String(strconv.FormatInt(now.UnixNano(), 10)),
},
reportField: {
B: buf.Bytes(),
},
},
})
if err != nil {
return err
}
return nil
}
开发者ID:dilgerma,项目名称:scope,代码行数:34,代码来源:dynamo_collector.go
示例12: getConn
// getConn is used to get a connection from the pool
func (n *NetworkTransport) getConn(target net.Addr) (*netConn, error) {
// Check for a pooled conn
if conn := n.getPooledConn(target); conn != nil {
return conn, nil
}
// Dial a new connection
conn, err := n.stream.Dial(target.String(), n.timeout)
if err != nil {
return nil, err
}
// Wrap the conn
netConn := &netConn{
target: target,
conn: conn,
r: bufio.NewReader(conn),
w: bufio.NewWriter(conn),
}
// Setup encoder/decoders
netConn.dec = codec.NewDecoder(netConn.r, &codec.MsgpackHandle{})
netConn.enc = codec.NewEncoder(netConn.w, &codec.MsgpackHandle{})
// Done
return netConn, nil
}
开发者ID:rbg,项目名称:raft,代码行数:28,代码来源:net_transport.go
示例13: main
func main() {
var m = MsgPackSample{
Name: "ユイにゃん",
Num: 17,
Message: "AngelBeats!",
}
mh := &codec.MsgpackHandle{RawToString: true}
buf1 := &bytes.Buffer{}
buf2 := &bytes.Buffer{}
var msgpackExample = MsgPackExample{
mh: mh,
buf1: buf1,
buf2: buf2,
enc: codec.NewEncoder(buf1, mh),
dec: codec.NewDecoder(buf2, mh),
menc: msgpack.NewEncoder(buf1),
mdec: msgpack.NewDecoder(buf2),
}
msgpackExample.packUnPackMsgPack(m)
msgpackExample.packUnPackCodec(m)
}
开发者ID:kyokomi-sandbox,项目名称:sandbox,代码行数:26,代码来源:msgpack.go
示例14: spawnEmitter
func (output *TDOutput) spawnEmitter() {
output.logger.Notice("Spawning emitter")
output.wg.Add(1)
go func() {
defer func() {
output.spoolerDaemon.shutdownChan <- struct{}{}
output.wg.Done()
}()
output.logger.Notice("Emitter started")
buffer := bytes.Buffer{}
for recordSet := range output.emitterChan {
buffer.Reset()
encoder := codec.NewEncoder(&buffer, output.codec)
err := func() error {
spooler, err := output.spoolerDaemon.getSpooler(recordSet.Tag)
if err != nil {
return err
}
addMetadata(&recordSet, output.metadata)
err = encodeRecords(encoder, recordSet.Records)
if err != nil {
return err
}
output.logger.Debug("Emitter processed %d entries", len(recordSet.Records))
return spooler.journal.Write(buffer.Bytes())
}()
if err != nil {
output.logger.Error("%s", err.Error())
continue
}
}
output.logger.Notice("Emitter ended")
}()
}
开发者ID:idahoakl,项目名称:fluentd-forwarder,代码行数:34,代码来源:output_td.go
示例15: Persist
func (s *consulSnapshot) Persist(sink raft.SnapshotSink) error {
// Register the nodes
encoder := codec.NewEncoder(sink, msgpackHandle)
// Write the header
header := snapshotHeader{
LastIndex: s.state.LastIndex(),
}
if err := encoder.Encode(&header); err != nil {
sink.Cancel()
return err
}
if err := s.persistNodes(sink, encoder); err != nil {
sink.Cancel()
return err
}
if err := s.persistSessions(sink, encoder); err != nil {
sink.Cancel()
return err
}
if err := s.persistACLs(sink, encoder); err != nil {
sink.Cancel()
return err
}
if err := s.persistKV(sink, encoder); err != nil {
sink.Cancel()
return err
}
return nil
}
开发者ID:askagirl,项目名称:consul,代码行数:34,代码来源:fsm.go
示例16: WriteAssets
// WriteAssets creates the assets in a dir. It expects dir to already exist.
func WriteAssets(w io.Writer, shards uint64) {
encoder := codec.NewEncoder(w, &codec.JsonHandle{Indent: 2})
ServiceAccount().CodecEncodeSelf(encoder)
fmt.Fprintf(w, "\n")
EtcdRc().CodecEncodeSelf(encoder)
fmt.Fprintf(w, "\n")
EtcdService().CodecEncodeSelf(encoder)
fmt.Fprintf(w, "\n")
RethinkService().CodecEncodeSelf(encoder)
fmt.Fprintf(w, "\n")
RethinkRc().CodecEncodeSelf(encoder)
fmt.Fprintf(w, "\n")
ObjdRc().CodecEncodeSelf(encoder)
fmt.Fprintf(w, "\n")
ObjdService().CodecEncodeSelf(encoder)
fmt.Fprintf(w, "\n")
PfsdRc(uint64(shards)).CodecEncodeSelf(encoder)
fmt.Fprintf(w, "\n")
PfsdService().CodecEncodeSelf(encoder)
fmt.Fprintf(w, "\n")
PpsdRc().CodecEncodeSelf(encoder)
fmt.Fprintf(w, "\n")
PpsdService().CodecEncodeSelf(encoder)
fmt.Fprintf(w, "\n")
RolerRc(uint64(shards)).CodecEncodeSelf(encoder)
fmt.Fprintf(w, "\n")
}
开发者ID:angelabier1,项目名称:pachyderm,代码行数:35,代码来源:assets.go
示例17: TestLatestMapEncodingNil
func TestLatestMapEncodingNil(t *testing.T) {
want := LatestMap{}
{
gobs, err := want.GobEncode()
if err != nil {
t.Fatal(err)
}
have := EmptyLatestMap
have.GobDecode(gobs)
if have.Map == nil {
t.Error("Decoded LatestMap.psMap should not be nil")
}
}
{
for _, h := range []codec.Handle{
codec.Handle(&codec.MsgpackHandle{}),
codec.Handle(&codec.JsonHandle{}),
} {
buf := &bytes.Buffer{}
encoder := codec.NewEncoder(buf, h)
want.CodecEncodeSelf(encoder)
decoder := codec.NewDecoder(buf, h)
have := EmptyLatestMap
have.CodecDecodeSelf(decoder)
if !reflect.DeepEqual(want, have) {
t.Error(test.Diff(want, have))
}
}
}
}
开发者ID:CNDonny,项目名称:scope,代码行数:33,代码来源:latest_map_internal_test.go
示例18: TestSpanMsgPack
func TestSpanMsgPack(t *testing.T) {
span := Span{Id: TestId("33f25a1a750a471db5bafa59309d7d6f"),
SpanData: SpanData{
Begin: 1234,
End: 5678,
Description: "getFileDescriptors",
Parents: []SpanId{},
TracerId: "testTracerId",
}}
mh := new(codec.MsgpackHandle)
mh.WriteExt = true
w := bytes.NewBuffer(make([]byte, 0, 2048))
enc := codec.NewEncoder(w, mh)
err := enc.Encode(span)
if err != nil {
t.Fatal("Error encoding span as msgpack: " + err.Error())
}
buf := w.Bytes()
fmt.Printf("span: %s\n", hex.EncodeToString(buf))
mh = new(codec.MsgpackHandle)
mh.WriteExt = true
dec := codec.NewDecoder(bytes.NewReader(buf), mh)
var span2 Span
err = dec.Decode(&span2)
if err != nil {
t.Fatal("Failed to reverse msgpack encoding for " + span.String())
}
ExpectSpansEqual(t, &span, &span2)
}
开发者ID:gauravrmazra,项目名称:incubator-htrace,代码行数:29,代码来源:span_test.go
示例19: toJSON
func toJSON(out interface{}) ([]byte, error) {
buf := &bytes.Buffer{}
json := &codec.JsonHandle{}
encoder := codec.NewEncoder(buf, json)
err := encoder.Encode(out)
return buf.Bytes(), err
}
开发者ID:diffeo,项目名称:go-coordinate,代码行数:7,代码来源:rest.go
示例20: NewRPCClient
// NewRPCClient is used to create a new RPC client given the address.
// This will properly dial, handshake, and start listening
func NewRPCClient(addr string) (*RPCClient, error) {
// Try to dial to serf
conn, err := net.Dial("tcp", addr)
if err != nil {
return nil, err
}
// Create the client
client := &RPCClient{
seq: 0,
conn: conn.(*net.TCPConn),
reader: bufio.NewReader(conn),
writer: bufio.NewWriter(conn),
dispatch: make(map[uint64]seqHandler),
shutdownCh: make(chan struct{}),
}
client.dec = codec.NewDecoder(client.reader,
&codec.MsgpackHandle{RawToString: true, WriteExt: true})
client.enc = codec.NewEncoder(client.writer,
&codec.MsgpackHandle{RawToString: true, WriteExt: true})
go client.listen()
// Do the initial handshake
if err := client.handshake(); err != nil {
client.Close()
return nil, err
}
return client, err
}
开发者ID:justinclayton,项目名称:serf,代码行数:31,代码来源:rpc_client.go
注:本文中的github.com/ugorji/go/codec.NewEncoder函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论