本文整理汇总了Golang中github.com/ugorji/go/codec.NewEncoder函数的典型用法代码示例。如果您正苦于以下问题:Golang NewEncoder函数的具体用法?Golang NewEncoder怎么用?Golang NewEncoder使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewEncoder函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
})
}
示例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
}
示例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,
}
}
示例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))
}
}
}
}
示例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())
}
示例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")
}
示例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
}
示例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
}
示例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
}
}
}
示例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)
}
}
}
示例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
}
示例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
}
示例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)
}
示例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")
}()
}
示例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
}