Skip to content

Commit

Permalink
Bump Go version (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg authored Aug 30, 2023
1 parent c96ca4b commit 6cf8c4d
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 89 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ See [docs][pkg-url].

## Install

Go version 1.15+
Go version 1.18+

```
go get github.com/cristalhq/bencode
Expand All @@ -29,7 +29,7 @@ Marshaling into Bencode

```go
// data to process, most of the types are supported
var data interface{} = map[string]interface{}{
var data any = map[string]any{
"1": 42,
"hello": "world",
"foo": []string{"bar", "baz"},
Expand All @@ -51,7 +51,7 @@ checkErr(err)
Unmarshaling from Bencode

```go
var data interface{}
var data any

buf := []byte("li1ei42ee")

Expand Down
12 changes: 6 additions & 6 deletions bencode.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type Marshaler interface {
}

// Marshal returns bencode encoding of v.
func Marshal(v interface{}) ([]byte, error) {
func Marshal(v any) ([]byte, error) {
buf := &bytes.Buffer{}
if err := NewEncoder(buf).Encode(v); err != nil {
return nil, err
Expand All @@ -20,7 +20,7 @@ func Marshal(v interface{}) ([]byte, error) {
}

// MarshalTo returns bencode encoding of v written to dst.
func MarshalTo(dst []byte, v interface{}) ([]byte, error) {
func MarshalTo(dst []byte, v any) ([]byte, error) {
enc := &Encoder{buf: dst}
if err := enc.marshal(v); err != nil {
return nil, err
Expand All @@ -36,7 +36,7 @@ type Unmarshaler interface {

// Unmarshal parses the bencoded data and stores the result
// in the value pointed to by v.
func Unmarshal(data []byte, v interface{}) error {
func Unmarshal(data []byte, v any) error {
d := NewDecodeBytes(data)
if err := d.Decode(v); err != nil {
return err
Expand All @@ -49,7 +49,7 @@ func Unmarshal(data []byte, v interface{}) error {
// Example:
//
// bencode.A{"hello", "world", 3.14159, bencode.D{{"foo", 12345}}}
type A []interface{}
type A []any

// D is an ordered representation of a Bencode document.
//
Expand All @@ -61,12 +61,12 @@ type D []e
// e represents a Bencode element for a D. It is usually used inside a D.
type e struct {
K string
V interface{}
V any
}

// M is an unordered representation of a Bencode document.
//
// Example usage:
//
// bencode.M{"hello": "world", "foo": "bar", "pi": 3.14159}
type M map[string]interface{}
type M map[string]any
24 changes: 12 additions & 12 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewDecodeBytes(src []byte) *Decoder {
}

// Decode writes the Bencode encoding of v to the stream.
func (d *Decoder) Decode(v interface{}) error {
func (d *Decoder) Decode(v any) error {
switch v.(type) {
case nil:
return errors.New("bencode: cannot marshal into nil")
Expand Down Expand Up @@ -61,20 +61,20 @@ func (d *Decoder) Decode(v interface{}) error {
return d.writeResult(v, got)
}

func (d *Decoder) writeResult(v, got interface{}) error {
func (d *Decoder) writeResult(v, got any) error {
switch v := v.(type) {
case *interface{}: // catch any type
case *any: // catch any type
*v = got

case *map[string]interface{}:
val, ok := got.(map[string]interface{})
case *map[string]any:
val, ok := got.(map[string]any)
if !ok {
return fmt.Errorf("bencode: cannot decode %T into map", got)
}
*v = val

case *[]interface{}:
val, ok := got.([]interface{})
case *[]any:
val, ok := got.([]any)
if !ok {
return fmt.Errorf("bencode: cannot decode %T into list", got)
}
Expand All @@ -86,7 +86,7 @@ func (d *Decoder) writeResult(v, got interface{}) error {
return nil
}

func (d *Decoder) unmarshal() (interface{}, error) {
func (d *Decoder) unmarshal() (any, error) {
switch d.data[d.cursor] {
case 'i':
return d.unmarshalInt()
Expand Down Expand Up @@ -115,8 +115,8 @@ func (d *Decoder) unmarshalInt() (int64, error) {
return integer, nil
}

func (d *Decoder) unmarshalMap() (interface{}, error) {
dictionary := make(map[string]interface{})
func (d *Decoder) unmarshalMap() (any, error) {
dictionary := make(map[string]any)
d.cursor++
for {
if d.cursor == d.length {
Expand All @@ -141,8 +141,8 @@ func (d *Decoder) unmarshalMap() (interface{}, error) {
}
}

func (d *Decoder) unmarshalList() (interface{}, error) {
list := make([]interface{}, 0)
func (d *Decoder) unmarshalList() (any, error) {
list := make([]any, 0)
d.cursor++
for {
if d.cursor == d.length {
Expand Down
68 changes: 34 additions & 34 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type unmarshalTestCase struct {
input string
want interface{}
want any
wantErr bool
}

Expand Down Expand Up @@ -89,58 +89,58 @@ func TestUnmarshalSlice(t *testing.T) {
tcs := []unmarshalTestCase{
{
`le`,
[]interface{}{},
[]any{},
false,
},
{
`li1ei2ei3ee`,
[]interface{}{int64(1), int64(2), int64(3)},
[]any{int64(1), int64(2), int64(3)},
false,
},
{
`l3:foo3:bar3:baze`,
[]interface{}{[]byte("foo"), []byte("bar"), []byte("baz")},
[]any{[]byte("foo"), []byte("bar"), []byte("baz")},
false,
},
{
`li1e3:fooi2e3:bare`,
[]interface{}{int64(1), []byte("foo"), int64(2), []byte("bar")},
[]any{int64(1), []byte("foo"), int64(2), []byte("bar")},
false,
},
{
`li1el3:baree`,
[]interface{}{int64(1), []interface{}{[]byte("bar")}},
[]any{int64(1), []any{[]byte("bar")}},
false,
},
{
`l3:fooe`,
[]interface{}{[]byte("foo")},
[]any{[]byte("foo")},
false,
},
{
`l3:foo6:barbaze`,
[]interface{}{[]byte("foo"), []byte("barbaz")},
[]any{[]byte("foo"), []byte("barbaz")},
false,
},
{
`l3:foo6:barbaz2:goe`,
[]interface{}{[]byte("foo"), []byte("barbaz"), []byte("go")},
[]any{[]byte("foo"), []byte("barbaz"), []byte("go")},
false,
},
{
`l3:fooi20ee`,
[]interface{}{[]byte("foo"), int64(20)},
[]any{[]byte("foo"), int64(20)},
false,
},
{
`li90ei20ee`,
[]interface{}{int64(90), int64(20)},
[]any{int64(90), int64(20)},
false,
},
{
`ll3:foo3:barei20ee`,
[]interface{}{
[]interface{}{
[]any{
[]any{
[]byte("foo"),
[]byte("bar"),
},
Expand All @@ -150,20 +150,20 @@ func TestUnmarshalSlice(t *testing.T) {
},
{
`l5:024683:acee`,
[]interface{}{
[]any{
[]byte{'0', '2', '4', '6', '8'},
[]byte{'a', 'c', 'e'},
},
false,
},
{
`ld1:ai0e1:bi1eed1:ci2e1:di3eee`,
[]interface{}{
map[string]interface{}{
[]any{
map[string]any{
"a": int64(0),
"b": int64(1),
},
map[string]interface{}{
map[string]any{
"c": int64(2),
"d": int64(3),
},
Expand All @@ -178,26 +178,26 @@ func TestUnmarshalSlice(t *testing.T) {
// tcs := []unmarshalTestCase{
// {
// `le`,
// [...]interface{}{},
// [...]any{},
// false,
// },
// {
// `li1ei2ei3ee`,
// [...]interface{}{1, 2, 3},
// [...]any{1, 2, 3},
// false,
// },
// {
// [...]interface{}{"foo", "bar", "baz"},
// [...]any{"foo", "bar", "baz"},
// `l3:foo3:bar3:baze`,
// false,
// },
// {
// [...]interface{}{1, "foo", 2, "bar"},
// [...]any{1, "foo", 2, "bar"},
// `li1e3:fooi2e3:bare`,
// false,
// },
// {
// [...]interface{}{1, [...]interface{}{"bar"}},
// [...]any{1, [...]any{"bar"}},
// `li1el3:baree`,
// false,
// },
Expand All @@ -219,17 +219,17 @@ func TestUnmarshalMap(t *testing.T) {
tcs := []unmarshalTestCase{
{
`de`,
map[string]interface{}{},
map[string]any{},
false,
},
{
`d1:1i2e1:4i5ee`,
map[string]interface{}{"1": int64(2), "4": int64(5)},
map[string]any{"1": int64(2), "4": int64(5)},
false,
},
{
"d1:1i1e3:123i123e1:3i3ee",
map[string]interface{}{
map[string]any{
"1": int64(1),
"3": int64(3),
"123": int64(123),
Expand All @@ -238,7 +238,7 @@ func TestUnmarshalMap(t *testing.T) {
},
{
"d9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee",
map[string]interface{}{
map[string]any{
"publisher": []byte("bob"),
"publisher-webpage": []byte("www.example.com"),
"publisher.location": []byte("home"),
Expand All @@ -247,22 +247,22 @@ func TestUnmarshalMap(t *testing.T) {
},
{
"d1:13:onee",
map[string]interface{}{"1": []byte("one")},
map[string]any{"1": []byte("one")},
false,
},
{
"d1:13:one3:two1:2e",
map[string]interface{}{
map[string]any{
"1": []byte("one"),
"two": []byte("2"),
},
false,
},
{
`d1:ali0ei1ee1:bli2ei3eee`,
map[string]interface{}{
"a": []interface{}{int64(0), int64(1)},
"b": []interface{}{int64(2), int64(3)},
map[string]any{
"a": []any{int64(0), int64(1)},
"b": []any{int64(2), int64(3)},
},
false,
},
Expand All @@ -274,7 +274,7 @@ func testLoopUnmarshal(t *testing.T, tcs []unmarshalTestCase) {
t.Helper()

for i, test := range tcs {
var got interface{}
var got any
err := Unmarshal([]byte(test.input), &got)
if err != nil {
if test.wantErr {
Expand All @@ -292,7 +292,7 @@ func testLoopUnmarshal(t *testing.T, tcs []unmarshalTestCase) {
var unmarshalBenchData = []byte("d4:infod6:lengthi170917888e12:piece lengthi262144e4:name30:debian-8.8.0-arm64-netinst.isoe8:announce38:udp://tracker.publicbt.com:80/announce13:announce-listll38:udp://tracker.publicbt.com:80/announceel44:udp://tracker.openbittorrent.com:80/announceee7:comment33:Debian CD from cdimage.debian.orge")

func Benchmark_Unmarshal(b *testing.B) {
var res interface{}
var res any
b.ReportAllocs()
for n := 0; n < b.N; n++ {
err := NewDecodeBytes(unmarshalBenchData).Decode(&res)
Expand All @@ -306,7 +306,7 @@ func Benchmark_Unmarshal(b *testing.B) {
}

func Benchmark_UnmarshalReader(b *testing.B) {
var res interface{}
var res any
b.ReportAllocs()
for n := 0; n < b.N; n++ {
r := bytes.NewReader(unmarshalBenchData)
Expand Down
Loading

0 comments on commit 6cf8c4d

Please sign in to comment.