本文整理汇总了Golang中github.com/ugorji/go/codec.NewDecoderBytes函数的典型用法代码示例。如果您正苦于以下问题:Golang NewDecoderBytes函数的具体用法?Golang NewDecoderBytes怎么用?Golang NewDecoderBytes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewDecoderBytes函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestAPITopologyHosts
func TestAPITopologyHosts(t *testing.T) {
ts := topologyServer()
defer ts.Close()
is404(t, ts, "/api/topology/hosts/foobar")
{
body := getRawJSON(t, ts, "/api/topology/hosts")
var topo app.APITopology
decoder := codec.NewDecoderBytes(body, &codec.JsonHandle{})
if err := decoder.Decode(&topo); err != nil {
t.Fatal(err)
}
// Should have the rendered host nodes
for id := range expected.RenderedHosts {
if _, ok := topo.Nodes[id]; !ok {
t.Errorf("Expected output to include node: %s, but wasn't found", id)
}
}
}
{
body := getRawJSON(t, ts, "/api/topology/hosts/"+fixture.ServerHostNodeID)
var node app.APINode
decoder := codec.NewDecoderBytes(body, &codec.JsonHandle{})
if err := decoder.Decode(&node); err != nil {
t.Fatal(err)
}
equals(t, fixture.ServerHostNodeID, node.Node.ID)
equals(t, "server", node.Node.Label)
equals(t, false, node.Node.Pseudo)
// Let's not unit-test the specific content of the detail tables
}
}
示例2: Decode
// Decode decodes msgpack representation into Snapshot
func (s *Snapshot) Decode(input []byte) error {
decoder := codec.NewDecoderBytes(input, &codec.MsgpackHandle{})
err := decoder.Decode(s)
if err != nil {
if strings.HasPrefix(err.Error(), "codec.decoder: readContainerLen: Unrecognized descriptor byte: hex: 80") {
// probably it is broken DB from go < 1.2, try decoding w/o time.Time
var snapshot11 struct {
UUID string
Name string
CreatedAt []byte
SourceKind string
SourceIDs []string
Description string
}
decoder = codec.NewDecoderBytes(input, &codec.MsgpackHandle{})
err2 := decoder.Decode(&snapshot11)
if err2 != nil {
return err
}
s.UUID = snapshot11.UUID
s.Name = snapshot11.Name
s.SourceKind = snapshot11.SourceKind
s.SourceIDs = snapshot11.SourceIDs
s.Description = snapshot11.Description
} else {
return err
}
}
return nil
}
示例3: TestAPITopologyProcesses
func TestAPITopologyProcesses(t *testing.T) {
ts := topologyServer()
defer ts.Close()
is404(t, ts, "/api/topology/processes/foobar")
{
body := getRawJSON(t, ts, "/api/topology/processes/"+fixture.ServerProcessNodeID)
var node app.APINode
decoder := codec.NewDecoderBytes(body, &codec.JsonHandle{})
if err := decoder.Decode(&node); err != nil {
t.Fatal(err)
}
equals(t, fixture.ServerProcessNodeID, node.Node.ID)
equals(t, "apache", node.Node.Label)
equals(t, false, node.Node.Pseudo)
// Let's not unit-test the specific content of the detail tables
}
{
body := getRawJSON(t, ts, "/api/topology/processes-by-name/"+
url.QueryEscape(fixture.Client1Name))
var node app.APINode
decoder := codec.NewDecoderBytes(body, &codec.JsonHandle{})
if err := decoder.Decode(&node); err != nil {
t.Fatal(err)
}
equals(t, fixture.Client1Name, node.Node.ID)
equals(t, fixture.Client1Name, node.Node.Label)
equals(t, false, node.Node.Pseudo)
// Let's not unit-test the specific content of the detail tables
}
}
示例4: TestSaltpackEncryptHideRecipients
func TestSaltpackEncryptHideRecipients(t *testing.T) {
tc := SetupEngineTest(t, "SaltpackEncrypt")
defer tc.Cleanup()
u1 := CreateAndSignupFakeUser(tc, "nalcp")
u2 := CreateAndSignupFakeUser(tc, "nalcp")
u3 := CreateAndSignupFakeUser(tc, "nalcp")
trackUI := &FakeIdentifyUI{
Proofs: make(map[string]string),
}
ctx := &Context{IdentifyUI: trackUI, SecretUI: u3.NewSecretUI()}
run := func(Recips []string) {
sink := libkb.NewBufferCloser()
arg := &SaltpackEncryptArg{
Opts: keybase1.SaltpackEncryptOptions{
Recipients: Recips,
HideRecipients: true,
Binary: true,
},
Source: strings.NewReader("id2 and encrypt, id2 and encrypt"),
Sink: sink,
}
eng := NewSaltpackEncrypt(arg, tc.G)
if err := RunEngine(eng, ctx); err != nil {
t.Fatal(err)
}
out := sink.Bytes()
if len(out) == 0 {
t.Fatal("no output")
}
var header saltpack.EncryptionHeader
dec := codec.NewDecoderBytes(out, &codec.MsgpackHandle{WriteExt: true})
var b []byte
if err := dec.Decode(&b); err != nil {
t.Fatal(err)
}
dec = codec.NewDecoderBytes(b, &codec.MsgpackHandle{WriteExt: true})
if err := dec.Decode(&header); err != nil {
t.Fatal(err)
}
for _, receiver := range header.Receivers {
if receiver.ReceiverKID != nil {
t.Fatal("receiver KID included in anonymous saltpack header")
}
}
}
run([]string{u1.Username, u2.Username})
// If we add ourselves, we should be smart and not error out
// (We are u3 in this case)
run([]string{u1.Username, u2.Username, u3.Username})
}
示例5: TestAPITopologyAddsKubernetes
func TestAPITopologyAddsKubernetes(t *testing.T) {
router := mux.NewRouter()
c := app.NewCollector(1 * time.Minute)
app.RegisterReportPostHandler(c, router)
app.RegisterTopologyRoutes(router, c)
ts := httptest.NewServer(router)
defer ts.Close()
body := getRawJSON(t, ts, "/api/topology")
var topologies []app.APITopologyDesc
decoder := codec.NewDecoderBytes(body, &codec.JsonHandle{})
if err := decoder.Decode(&topologies); err != nil {
t.Fatalf("JSON parse error: %s", err)
}
equals(t, 4, len(topologies))
// Enable the kubernetes topologies
rpt := report.MakeReport()
rpt.Pod = report.MakeTopology()
rpt.Pod.Nodes[fixture.ClientPodNodeID] = kubernetes.NewPod(&api.Pod{
ObjectMeta: api.ObjectMeta{
Name: "pong-a",
Namespace: "ping",
Labels: map[string]string{"ponger": "true"},
},
Status: api.PodStatus{
HostIP: "1.2.3.4",
ContainerStatuses: []api.ContainerStatus{
{ContainerID: "container1"},
{ContainerID: "container2"},
},
},
}).GetNode("")
buf := &bytes.Buffer{}
encoder := codec.NewEncoder(buf, &codec.MsgpackHandle{})
if err := encoder.Encode(rpt); err != nil {
t.Fatalf("GOB encoding error: %s", err)
}
checkRequest(t, ts, "POST", "/api/report", buf.Bytes())
body = getRawJSON(t, ts, "/api/topology")
decoder = codec.NewDecoderBytes(body, &codec.JsonHandle{})
if err := decoder.Decode(&topologies); err != nil {
t.Fatalf("JSON parse error: %s", err)
}
equals(t, 4, len(topologies))
found := false
for _, topology := range topologies {
if topology.Name == "Pods" {
found = true
break
}
}
if !found {
t.Error("Could not find pods topology")
}
}
示例6: ByKey
// ByKey find package in DB by its key
func (collection *PackageCollection) ByKey(key []byte) (*Package, error) {
encoded, err := collection.db.Get(key)
if err != nil {
return nil, err
}
p := &Package{}
if len(encoded) > 2 && (encoded[0] != 0xc1 || encoded[1] != 0x1) {
oldp := &oldPackage{}
decoder := codec.NewDecoderBytes(encoded, collection.codecHandle)
err = decoder.Decode(oldp)
if err != nil {
return nil, err
}
p.Name = oldp.Name
p.Version = oldp.Version
p.Architecture = oldp.Architecture
p.IsSource = oldp.IsSource
p.SourceArchitecture = oldp.SourceArchitecture
p.Source = oldp.Source
p.Provides = oldp.Provides
p.deps = &PackageDependencies{
Depends: oldp.Depends,
BuildDepends: oldp.BuildDepends,
BuildDependsInDep: oldp.BuildDependsInDep,
PreDepends: oldp.PreDepends,
Suggests: oldp.Suggests,
Recommends: oldp.Recommends,
}
p.extra = &oldp.Extra
for i := range oldp.Files {
oldp.Files[i].Filename = filepath.Base(oldp.Files[i].Filename)
}
p.UpdateFiles(PackageFiles(oldp.Files))
// Save in new format
err = collection.Update(p)
if err != nil {
return nil, err
}
} else {
decoder := codec.NewDecoderBytes(encoded[2:], collection.codecHandle)
err = decoder.Decode(p)
if err != nil {
return nil, err
}
}
p.collection = collection
return p, nil
}
示例7: parseIncomingMessage
func parseIncomingMessage(msg *bw.SimpleMessage, h codec.Handle) (string, ChairMessage, error) {
var end int = strings.LastIndex(msg.URI, CHAIREND)
var start int = strings.Index(msg.URI, CHAIRSTART) + len(CHAIRSTART)
if end == -1 || start == -1 {
return "", nil, errors.New(fmt.Sprintf("invalid path %v", msg.URI))
}
var chairid string = msg.URI[start:end]
var contents []byte = msg.POs[0].GetContents()
var dec *codec.Decoder = codec.NewDecoderBytes(contents, h)
var cm ChairMessage
var err error
cm, err = NewChairMessageFrom(dec)
if err != nil {
return chairid, cm, errors.New(fmt.Sprintf("could not parse message: %v", err))
}
err = cm.SanityCheck()
if err != nil {
return chairid, cm, errors.New(fmt.Sprintf("decoded message fails sanity check: %v", err))
} else {
return chairid, cm, nil
}
}
示例8: readMessage
func (client *Client) readMessage() (typ MsgType, session [16]byte, seq uint64, meta MetaMap, args []interface{}, err error) {
h := &MsgHeader{}
if err = binary.Read(client.rbuf, binary.BigEndian, h); err != nil {
return
}
if h.Version != 0 {
err = ErrUnknownVersion
return
}
typ = h.Type
bodybuf := make([]byte, h.BodySize)
if _, err = io.ReadFull(client.rbuf, bodybuf); err != nil {
return
}
var body []interface{}
dec := codec.NewDecoderBytes(bodybuf, &mh)
if err = dec.Decode(&body); err != nil {
return
}
defer func() {
if e := recover(); e != nil {
switch e.(type) {
case error:
err = e.(error)
default:
err = errors.New("unexpected type during decoding")
}
}
}()
copy(session[:], []byte(body[0].(string)))
seq = binary.BigEndian.Uint64(([]byte(body[1].(string)))[8:])
meta = body[2].(map[string]interface{})
args = body[3:]
return
}
示例9: TestMessageUnpackErrorMsg
func TestMessageUnpackErrorMsg(t *testing.T) {
const (
session = 1
_type = 5
errorCode = 1
errorMessage = "someerror"
)
// msgpack.packb([5, 1, [1, "someerror"]])
payload := []byte{147, 5, 1, 146, 1, 169, 115, 111, 109, 101, 101, 114, 114, 111, 114}
var result []interface{}
assert.NoError(t, codec.NewDecoderBytes(payload, h).Decode(&result))
msg, err := unpackMessage(result)
assert.NoError(t, err)
assert.Equal(t, int64(session), msg.getSessionID(), "bad session")
if !assert.Equal(t, int64(_type), msg.getTypeID(), "bad message type") {
t.FailNow()
}
e := msg.(*errorMsg)
assert.Equal(t, errorCode, e.Code, "bad error code")
assert.Equal(t, errorMessage, e.Message, "bad error message")
}
示例10: TestDecodeResponse
func TestDecodeResponse(t *testing.T) {
inner := bytes.Join([][]byte{
[]byte{0xA2},
DeTestByteString("id").Data,
[]byte{0x02},
DeTestByteString("result").Data,
[]byte{0x81},
DeTestByteString("x").Data,
}, []byte{})
outer := bytes.Join([][]byte{
[]byte{0xD8, 0x18, 0x58, byte(len(inner))},
inner,
}, []byte{})
decoder := codec.NewDecoderBytes(outer, cbor)
var actual Response
expected := Response{
ID: 2,
Result: []interface{}{[]byte("x")},
Error: "",
}
err := decoder.Decode(&actual)
if assert.NoError(t, err) {
assert.Equal(t, expected, actual)
}
}
示例11: TestAPITopologyWebsocket
// Basic websocket test
func TestAPITopologyWebsocket(t *testing.T) {
ts := topologyServer()
defer ts.Close()
url := "/api/topology/processes/ws"
// Not a websocket request
res, _ := checkGet(t, ts, url)
if have := res.StatusCode; have != 400 {
t.Fatalf("Expected status %d, got %d.", 400, have)
}
// Proper websocket request
ts.URL = "ws" + ts.URL[len("http"):]
dialer := &websocket.Dialer{}
ws, res, err := dialer.Dial(ts.URL+url, nil)
ok(t, err)
defer ws.Close()
if want, have := 101, res.StatusCode; want != have {
t.Fatalf("want %d, have %d", want, have)
}
_, p, err := ws.ReadMessage()
ok(t, err)
var d detailed.Diff
decoder := codec.NewDecoderBytes(p, &codec.JsonHandle{})
if err := decoder.Decode(&d); err != nil {
t.Fatalf("JSON parse error: %s", err)
}
equals(t, 6, len(d.Add))
equals(t, 0, len(d.Update))
equals(t, 0, len(d.Remove))
}
示例12: DecodeToVersionedObject
func (s *Scheme) DecodeToVersionedObject(data []byte) (interface{}, string, string, error) {
version, kind, err := s.DataVersionAndKind(data)
if err != nil {
return nil, "", "", err
}
gv, err := unversioned.ParseGroupVersion(version)
if err != nil {
return nil, "", "", err
}
internalGV, exists := s.InternalVersions[gv.Group]
if !exists {
return nil, "", "", fmt.Errorf("no internalVersion specified for %v", gv)
}
if len(gv.Version) == 0 && len(internalGV.Version) != 0 {
return nil, "", "", fmt.Errorf("version not set in '%s'", string(data))
}
if kind == "" {
return nil, "", "", fmt.Errorf("kind not set in '%s'", string(data))
}
obj, err := s.NewObject(version, kind)
if err != nil {
return nil, "", "", err
}
if err := codec.NewDecoderBytes(data, new(codec.JsonHandle)).Decode(obj); err != nil {
return nil, "", "", err
}
return obj, version, kind, nil
}
示例13: ToServiceArrivedMessage
func ToServiceArrivedMessage(d eventual2go.Data) eventual2go.Data {
m := d.(connection.Message).Payload
var nsm service.ServiceArrived
dec := codec.NewDecoderBytes([]byte(m[3]), &mh)
dec.Decode(&nsm)
return nsm
}
示例14: DecryptAndUnpack
func DecryptAndUnpack(c web.C, out interface{}, secretKey string) err.ErrWriter {
ew := err.NewErrWriter()
c.Env[cKey.SecretKey] = secretKey
// 暗号化データ
iv := c.Env[cKey.Iv].([]byte)
cryptData := c.Env[cKey.CryptData].([]byte)
// decrypt
ci, err := aes.NewCipher([]byte(secretKey))
if err != nil {
return ew.Write(err)
}
cbcDecrypter := cipher.NewCBCDecrypter(ci, iv)
plain := make([]byte, len(cryptData))
cbcDecrypter.CryptBlocks(plain, cryptData)
// decode(codec)
mh := &codec.MsgpackHandle{RawToString: true}
dec := codec.NewDecoderBytes(plain, mh)
err = dec.Decode(out)
if err != nil {
return ew.Write(err)
}
return ew
}
示例15: DecodeFileDownloadResponse
func DecodeFileDownloadResponse(data []byte) (*FileDownload, error) {
type DownloadResp struct {
Off uint32 `json:"off"`
Size uint32 `json:"len"`
Data []byte `json:"data"`
ReturnCode int `json:"rc"`
}
resp := &DownloadResp{}
dec := codec.NewDecoderBytes(data, new(codec.CborHandle))
err := dec.Decode(&resp)
if err != nil {
return nil, util.NewNewtError(fmt.Sprintf("Invalid incoming cbor: %s",
err.Error()))
}
if resp.ReturnCode != 0 {
return nil, util.NewNewtError(fmt.Sprintf("Target error: %d",
resp.ReturnCode))
}
if err != nil {
return nil, util.NewNewtError(fmt.Sprintf("Invalid incoming json: %s",
err.Error()))
}
f := &FileDownload{
Offset: resp.Off,
Data: resp.Data,
Size: resp.Size,
}
return f, nil
}