Skip to content

Commit

Permalink
feat(dec.raw): introduce Raw type
Browse files Browse the repository at this point in the history
  • Loading branch information
ernado committed Nov 4, 2021
1 parent 2a5e59e commit 27b855f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dec.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/ogen-go/errors"
)

// Type the type for JSON element
// Type of json value.
type Type int

func (t Type) String() string {
Expand Down
15 changes: 12 additions & 3 deletions dec_raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import "github.com/ogen-go/errors"

// Raw is like Skip(), but saves and returns skipped value as raw json.
//
// Do not retain returned slice, it references underlying buffer.
func (d *Decoder) Raw() ([]byte, error) {
// Do not retain returned value, it references underlying buffer.
func (d *Decoder) Raw() (Raw, error) {
if d.reader != nil {
return nil, errors.New("not implemented for io.Reader")
}
Expand All @@ -19,10 +19,19 @@ func (d *Decoder) Raw() ([]byte, error) {
}

// RawAppend is Raw that appends saved raw json value to buf.
func (d *Decoder) RawAppend(buf []byte) ([]byte, error) {
func (d *Decoder) RawAppend(buf Raw) (Raw, error) {
raw, err := d.Raw()
if err != nil {
return nil, err
}
return append(buf, raw...), err
}

// Raw json value.
type Raw []byte

// Type of Raw json value.
func (r Raw) Type() Type {
d := Decoder{buf: r, tail: len(r)}
return d.Next()
}
11 changes: 11 additions & 0 deletions dec_raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,14 @@ func BenchmarkDecoder_Raw(b *testing.B) {
}
}
}

func BenchmarkRaw_Type(b *testing.B) {
v := Raw{'1'}

b.ReportAllocs()
for i := 0; i < b.N; i++ {
if v.Type() != Number {
b.Fatal("invalid")
}
}
}

0 comments on commit 27b855f

Please sign in to comment.