Skip to content

Commit

Permalink
Added a utility function to decode a CBOR blob into a printable string (
Browse files Browse the repository at this point in the history
#58)

* Added a utility function to decode a CBOR blob into a printable string

* Better naming for decode.go and DecodeCBOR

* Moved readable.go into the main cbornode package
  • Loading branch information
aschmahmann authored and Kubuxu committed Feb 22, 2019
1 parent fed7b4b commit 5e2ba3a
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions readable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cbornode

import (
"bufio"
"bytes"

cbor "github.com/polydawn/refmt/cbor"
"github.com/polydawn/refmt/pretty"
"github.com/polydawn/refmt/shared"
)

//HumanReadable returns a string representation of a CBOR blob
func HumanReadable(blob []byte) (string, error) {
reader := bytes.NewReader(blob)

var buf bytes.Buffer
writer := bufio.NewWriter(&buf)

err := shared.TokenPump{
TokenSource: cbor.NewDecoder(cbor.DecodeOptions{}, reader),
TokenSink: pretty.NewEncoder(writer),
}.Run()

if err != nil {
return "", err
}

if err = writer.Flush(); err != nil {
return "", err
}

return buf.String(), nil
}

0 comments on commit 5e2ba3a

Please sign in to comment.