-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for pretty priting CBOR values (#6)
Also hex representation is always displayed now so that users can easily copy it.
- Loading branch information
Showing
19 changed files
with
384 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package display | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"github.com/acarl005/stripansi" | ||
"github.com/boreq/errors" | ||
"github.com/fxamacker/cbor/v2" | ||
refmtcbor "github.com/polydawn/refmt/cbor" | ||
refmtpretty "github.com/polydawn/refmt/pretty" | ||
refmtshared "github.com/polydawn/refmt/shared" | ||
) | ||
|
||
type PrettifierCBOR struct { | ||
} | ||
|
||
func NewPrettifierCBOR() *PrettifierCBOR { | ||
return &PrettifierCBOR{} | ||
} | ||
|
||
func (p PrettifierCBOR) Prettify(b []byte) (string, error) { | ||
if err := cbor.Wellformed(b); err != nil { | ||
return "", errors.Wrap(err, "invalid cbor") | ||
} | ||
return cborToText(b) | ||
} | ||
|
||
// from https://github.com/boreq/bolt-ui/pull/2 | ||
func cborToText(dataCBOR []byte) (string, error) { | ||
var buf bytes.Buffer | ||
bufWriter := bufio.NewWriter(&buf) | ||
err := refmtshared.TokenPump{ | ||
TokenSource: refmtcbor.NewDecoder(refmtcbor.DecodeOptions{}, bytes.NewReader(dataCBOR)), | ||
TokenSink: refmtpretty.NewEncoder(bufWriter), | ||
}.Run() | ||
if err != nil { | ||
return "", errors.Wrap(err, "tokenpump run failed") | ||
} | ||
err = bufWriter.Flush() | ||
if err != nil { | ||
return "", errors.Wrap(err, "error flushing the buffer") | ||
} | ||
return stripansi.Strip(buf.String()), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package display | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"github.com/boreq/errors" | ||
) | ||
|
||
type PrettifierJSON struct { | ||
} | ||
|
||
func NewPrettifierJSON() *PrettifierJSON { | ||
return &PrettifierJSON{} | ||
} | ||
|
||
func (p PrettifierJSON) Prettify(b []byte) (string, error) { | ||
if json.Valid(b) { | ||
buf := &bytes.Buffer{} | ||
if err := json.Indent(buf, b, "", " "); err != nil { | ||
return "", errors.Wrap(err, "error indenting") | ||
} | ||
return buf.String(), nil | ||
} | ||
return "", errors.New("invalid json") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package display | ||
|
||
import ( | ||
"github.com/boreq/errors" | ||
) | ||
|
||
type ContentType struct { | ||
s string | ||
} | ||
|
||
var ( | ||
ContentTypeJSON ContentType = ContentType{"json"} | ||
ContentTypeCBOR ContentType = ContentType{"cbor"} | ||
ContentTypeString ContentType = ContentType{"string"} | ||
) | ||
|
||
type Prettifier interface { | ||
Prettify(b []byte) (string, error) | ||
} | ||
|
||
type Prettified struct { | ||
Type ContentType | ||
Value string | ||
} | ||
|
||
type prettifier struct { | ||
Prettifier Prettifier | ||
ContentType ContentType | ||
} | ||
|
||
type Pretty struct { | ||
prettifiers []prettifier | ||
} | ||
|
||
func NewPretty() *Pretty { | ||
return &Pretty{prettifiers: []prettifier{ | ||
{ | ||
Prettifier: NewPrettifierCBOR(), | ||
ContentType: ContentTypeCBOR, | ||
}, | ||
{ | ||
Prettifier: NewPrettifierJSON(), | ||
ContentType: ContentTypeJSON, | ||
}, | ||
{ | ||
Prettifier: NewPrettifierString(), | ||
ContentType: ContentTypeString, | ||
}, | ||
}} | ||
} | ||
|
||
func (p *Pretty) Print(b []byte) (Prettified, error) { | ||
for _, prettifier := range p.prettifiers { | ||
v, err := prettifier.Prettifier.Prettify(b) | ||
if err == nil { | ||
return Prettified{ | ||
Type: prettifier.ContentType, | ||
Value: v, | ||
}, nil | ||
} | ||
} | ||
return Prettified{}, errors.New("no prettifiers completed successfully") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package display_test | ||
|
||
import ( | ||
"github.com/boreq/bolt-ui/display" | ||
"github.com/fxamacker/cbor/v2" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
) | ||
|
||
func TestPretty(t *testing.T) { | ||
cbor, err := cbor.Marshal(struct { | ||
Field1 string `cbor:"1,keyasint"` | ||
Field2 int `cbor:"2,keyasint"` | ||
}{ | ||
Field1: "string", | ||
Field2: 123, | ||
}) | ||
require.NoError(t, err) | ||
|
||
testCases := []struct { | ||
Name string | ||
Bytes []byte | ||
Result display.Prettified | ||
}{ | ||
{ | ||
Name: "json", | ||
Bytes: []byte(`{"some":"json"}`), | ||
Result: display.Prettified{ | ||
Type: display.ContentTypeJSON, | ||
Value: `{ | ||
"some": "json" | ||
}`, | ||
}, | ||
}, | ||
{ | ||
Name: "cbor", | ||
Bytes: cbor, | ||
Result: display.Prettified{ | ||
Type: display.ContentTypeCBOR, | ||
Value: "Map<len:2> {\n\r\t1: \"string\"\n\r\t2: 123\n\r}\n\r", | ||
}, | ||
}, | ||
{ | ||
Name: "string", | ||
Bytes: []byte("some_string"), | ||
Result: display.Prettified{ | ||
Type: display.ContentTypeString, | ||
Value: "some_string", | ||
}, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.Name, func(t *testing.T) { | ||
p := display.NewPretty() | ||
result, err := p.Print(testCase.Bytes) | ||
require.NoError(t, err) | ||
require.Equal(t, testCase.Result, result) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package display | ||
|
||
import ( | ||
"github.com/boreq/errors" | ||
"unicode" | ||
) | ||
|
||
type PrettifierString struct { | ||
} | ||
|
||
func NewPrettifierString() *PrettifierString { | ||
return &PrettifierString{} | ||
} | ||
|
||
func (p *PrettifierString) Prettify(b []byte) (string, error) { | ||
if p.canDisplayAsString(b) { | ||
return string(b), nil | ||
} | ||
return "", errors.New("can't display as string") | ||
} | ||
|
||
func (p *PrettifierString) canDisplayAsString(b []byte) bool { | ||
for _, rne := range string(b) { | ||
if !unicode.IsGraphic(rne) && !unicode.IsSpace(rne) { | ||
return false | ||
} | ||
} | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.