Skip to content

Commit e5396c6

Browse files
authored
Modernize code through Go 1.21 (#19)
* Go 1.18 updates * Go 1.19 updates * Stop using reflect.DeepEqual for testing error values * go get github.com/google/go-cmp/cmp@latest
1 parent d0d028a commit e5396c6

File tree

7 files changed

+143
-48
lines changed

7 files changed

+143
-48
lines changed

decode_test.go

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ import (
77
"reflect"
88
"strings"
99
"testing"
10+
11+
"github.com/google/go-cmp/cmp"
12+
"github.com/google/go-cmp/cmp/cmpopts"
1013
)
1114

1215
type kv struct {
@@ -155,6 +158,35 @@ func TestDecoder_scan(t *testing.T) {
155158
}
156159

157160
func TestDecoder_errors(t *testing.T) {
161+
tests := []struct {
162+
data string
163+
dec func(string) *Decoder
164+
want error
165+
}{
166+
{
167+
data: "a=1\nb=2",
168+
dec: func(s string) *Decoder {
169+
dec := NewDecoderSize(strings.NewReader(s), 1)
170+
return dec
171+
},
172+
want: bufio.ErrTooLong,
173+
},
174+
}
175+
176+
for _, test := range tests {
177+
dec := test.dec(test.data)
178+
179+
for dec.ScanRecord() {
180+
for dec.ScanKeyval() {
181+
}
182+
}
183+
if diff := cmp.Diff(test.want, dec.Err(), cmpopts.EquateErrors()); diff != "" {
184+
t.Errorf("%#v: Decoder.Err() value mismatch (-want,+got):\n%s", test.data, diff)
185+
}
186+
}
187+
}
188+
189+
func TestDecoder_SyntaxError(t *testing.T) {
158190
defaultDecoder := func(s string) *Decoder { return NewDecoder(strings.NewReader(s)) }
159191
tests := []struct {
160192
data string
@@ -231,14 +263,6 @@ func TestDecoder_errors(t *testing.T) {
231263
dec: defaultDecoder,
232264
want: &SyntaxError{Msg: "invalid key", Line: 1, Pos: 2},
233265
},
234-
{
235-
data: "a=1\nb=2",
236-
dec: func(s string) *Decoder {
237-
dec := NewDecoderSize(strings.NewReader(s), 1)
238-
return dec
239-
},
240-
want: bufio.ErrTooLong,
241-
},
242266
}
243267

244268
for _, test := range tests {
@@ -248,8 +272,16 @@ func TestDecoder_errors(t *testing.T) {
248272
for dec.ScanKeyval() {
249273
}
250274
}
251-
if got, want := dec.Err(), test.want; !reflect.DeepEqual(got, want) {
252-
t.Errorf("got: %v, want: %v", got, want)
275+
276+
switch got := dec.Err().(type) {
277+
case nil:
278+
t.Errorf("%#v: dec.Err() == nil, want: *SyntaxError", test.data)
279+
case *SyntaxError:
280+
if diff := cmp.Diff(test.want, got); diff != "" {
281+
t.Errorf("%#v: dec.Err() mismatch (-want,+got):\n%s", test.data, diff)
282+
}
283+
default:
284+
t.Errorf("%#v: dec.Err().(type) == %T, want: *SyntaxError", test.data, got)
253285
}
254286
}
255287
}

encode.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313

1414
// MarshalKeyvals returns the logfmt encoding of keyvals, a variadic sequence
1515
// of alternating keys and values.
16-
func MarshalKeyvals(keyvals ...interface{}) ([]byte, error) {
16+
func MarshalKeyvals(keyvals ...any) ([]byte, error) {
1717
buf := &bytes.Buffer{}
1818
if err := NewEncoder(buf).EncodeKeyvals(keyvals...); err != nil {
1919
return nil, err
@@ -45,7 +45,7 @@ var (
4545
// EncodeKeyval writes the logfmt encoding of key and value to the stream. A
4646
// single space is written before the second and subsequent keys in a record.
4747
// Nothing is written if a non-nil error is returned.
48-
func (enc *Encoder) EncodeKeyval(key, value interface{}) error {
48+
func (enc *Encoder) EncodeKeyval(key, value any) error {
4949
enc.scratch.Reset()
5050
if enc.needSep {
5151
if _, err := enc.scratch.Write(space); err != nil {
@@ -72,7 +72,7 @@ func (enc *Encoder) EncodeKeyval(key, value interface{}) error {
7272
// unsupported type or that cause a MarshalerError are replaced by their error
7373
// but do not cause EncodeKeyvals to return an error. If a non-nil error is
7474
// returned some key/value pairs may not have be written.
75-
func (enc *Encoder) EncodeKeyvals(keyvals ...interface{}) error {
75+
func (enc *Encoder) EncodeKeyvals(keyvals ...any) error {
7676
if len(keyvals) == 0 {
7777
return nil
7878
}
@@ -122,7 +122,7 @@ var ErrUnsupportedKeyType = errors.New("unsupported key type")
122122
// unsupported type.
123123
var ErrUnsupportedValueType = errors.New("unsupported value type")
124124

125-
func writeKey(w io.Writer, key interface{}) error {
125+
func writeKey(w io.Writer, key any) error {
126126
if key == nil {
127127
return ErrNilKey
128128
}
@@ -155,7 +155,7 @@ func writeKey(w io.Writer, key interface{}) error {
155155
switch rkey.Kind() {
156156
case reflect.Array, reflect.Chan, reflect.Func, reflect.Map, reflect.Slice, reflect.Struct:
157157
return ErrUnsupportedKeyType
158-
case reflect.Ptr:
158+
case reflect.Pointer:
159159
if rkey.IsNil() {
160160
return ErrNilKey
161161
}
@@ -194,7 +194,7 @@ func writeBytesKey(w io.Writer, key []byte) error {
194194
return err
195195
}
196196

197-
func writeValue(w io.Writer, value interface{}) error {
197+
func writeValue(w io.Writer, value any) error {
198198
switch v := value.(type) {
199199
case nil:
200200
return writeBytesValue(w, null)
@@ -222,7 +222,7 @@ func writeValue(w io.Writer, value interface{}) error {
222222
switch rvalue.Kind() {
223223
case reflect.Array, reflect.Chan, reflect.Func, reflect.Map, reflect.Slice, reflect.Struct:
224224
return ErrUnsupportedValueType
225-
case reflect.Ptr:
225+
case reflect.Pointer:
226226
if rvalue.IsNil() {
227227
return writeBytesValue(w, null)
228228
}
@@ -276,7 +276,7 @@ func (enc *Encoder) Reset() {
276276
func safeError(err error) (s string, ok bool) {
277277
defer func() {
278278
if panicVal := recover(); panicVal != nil {
279-
if v := reflect.ValueOf(err); v.Kind() == reflect.Ptr && v.IsNil() {
279+
if v := reflect.ValueOf(err); v.Kind() == reflect.Pointer && v.IsNil() {
280280
s, ok = "null", false
281281
} else {
282282
s, ok = fmt.Sprintf("PANIC:%v", panicVal), false
@@ -290,7 +290,7 @@ func safeError(err error) (s string, ok bool) {
290290
func safeString(str fmt.Stringer) (s string, ok bool) {
291291
defer func() {
292292
if panicVal := recover(); panicVal != nil {
293-
if v := reflect.ValueOf(str); v.Kind() == reflect.Ptr && v.IsNil() {
293+
if v := reflect.ValueOf(str); v.Kind() == reflect.Pointer && v.IsNil() {
294294
s, ok = "null", false
295295
} else {
296296
s, ok = fmt.Sprintf("PANIC:%v", panicVal), true
@@ -304,7 +304,7 @@ func safeString(str fmt.Stringer) (s string, ok bool) {
304304
func safeMarshal(tm encoding.TextMarshaler) (b []byte, err error) {
305305
defer func() {
306306
if panicVal := recover(); panicVal != nil {
307-
if v := reflect.ValueOf(tm); v.Kind() == reflect.Ptr && v.IsNil() {
307+
if v := reflect.ValueOf(tm); v.Kind() == reflect.Pointer && v.IsNil() {
308308
b, err = nil, nil
309309
} else {
310310
b, err = nil, fmt.Errorf("panic when marshalling: %s", panicVal)

encode_internal_test.go

Lines changed: 80 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7-
"io/ioutil"
7+
"io"
88
"reflect"
99
"testing"
10+
11+
"github.com/google/go-cmp/cmp"
12+
"github.com/google/go-cmp/cmp/cmpopts"
1013
)
1114

1215
func TestSafeString(t *testing.T) {
@@ -29,23 +32,23 @@ func TestSafeMarshal(t *testing.T) {
2932
func TestWriteKeyStrings(t *testing.T) {
3033
keygen := []struct {
3134
name string
32-
fn func(string) interface{}
35+
fn func(string) any
3336
}{
3437
{
3538
name: "string",
36-
fn: func(s string) interface{} { return s },
39+
fn: func(s string) any { return s },
3740
},
3841
{
3942
name: "named-string",
40-
fn: func(s string) interface{} { return stringData(s) },
43+
fn: func(s string) any { return stringData(s) },
4144
},
4245
{
4346
name: "Stringer",
44-
fn: func(s string) interface{} { return stringStringer(s) },
47+
fn: func(s string) any { return stringStringer(s) },
4548
},
4649
{
4750
name: "TextMarshaler",
48-
fn: func(s string) interface{} { return stringMarshaler(s) },
51+
fn: func(s string) any { return stringMarshaler(s) },
4952
},
5053
}
5154

@@ -99,7 +102,7 @@ func TestWriteKey(t *testing.T) {
99102
)
100103

101104
data := []struct {
102-
key interface{}
105+
key any
103106
want string
104107
err error
105108
}{
@@ -110,7 +113,6 @@ func TestWriteKey(t *testing.T) {
110113
{key: (*stringerMarshaler)(nil), err: ErrNilKey},
111114
{key: ptr, want: "1"},
112115

113-
{key: errorMarshaler{}, err: &MarshalerError{Type: reflect.TypeOf(errorMarshaler{}), Err: errMarshaling}},
114116
{key: make(chan int), err: ErrUnsupportedKeyType},
115117
{key: []int{}, err: ErrUnsupportedKeyType},
116118
{key: map[int]int{}, err: ErrUnsupportedKeyType},
@@ -122,8 +124,8 @@ func TestWriteKey(t *testing.T) {
122124
for _, d := range data {
123125
w := &bytes.Buffer{}
124126
err := writeKey(w, d.key)
125-
if !reflect.DeepEqual(err, d.err) {
126-
t.Errorf("%#v: got error: %v, want error: %v", d.key, err, d.err)
127+
if diff := cmp.Diff(d.err, err, cmpopts.EquateErrors()); diff != "" {
128+
t.Errorf("%#v: error value mismatch (-want,+got):\n%s", d.key, diff)
127129
}
128130
if err != nil {
129131
continue
@@ -134,13 +136,42 @@ func TestWriteKey(t *testing.T) {
134136
}
135137
}
136138

139+
func TestWriteKeyMarshalError(t *testing.T) {
140+
data := []struct {
141+
key any
142+
want string
143+
err error
144+
}{
145+
{key: errorMarshaler{}, err: &MarshalerError{Type: reflect.TypeOf(errorMarshaler{}), Err: errMarshaling}},
146+
}
147+
148+
for _, d := range data {
149+
w := &bytes.Buffer{}
150+
err := writeKey(w, d.key)
151+
152+
switch err := err.(type) {
153+
case nil:
154+
t.Errorf("%#v: err == nil, want: not nil", d.key)
155+
case *MarshalerError:
156+
if got, want := err.Type, reflect.TypeOf(errorMarshaler{}); got != want {
157+
t.Errorf("%#v: MarshalerError.Type == %v, want: %v", d.key, got, want)
158+
}
159+
if diff := cmp.Diff(errMarshaling, err.Err, cmpopts.EquateErrors()); diff != "" {
160+
t.Errorf("%#v: MarshalerError.Err value mismatch (-want,+got):\n%s", d.key, diff)
161+
}
162+
default:
163+
t.Errorf("%#v: unexpected error, got: %q, want: a MarshalerError", d.key, err)
164+
}
165+
}
166+
}
167+
137168
func TestWriteValueStrings(t *testing.T) {
138-
keygen := []func(string) interface{}{
139-
func(s string) interface{} { return s },
140-
func(s string) interface{} { return errors.New(s) },
141-
func(s string) interface{} { return stringData(s) },
142-
func(s string) interface{} { return stringStringer(s) },
143-
func(s string) interface{} { return stringMarshaler(s) },
169+
keygen := []func(string) any{
170+
func(s string) any { return s },
171+
func(s string) any { return errors.New(s) },
172+
func(s string) any { return stringData(s) },
173+
func(s string) any { return stringStringer(s) },
174+
func(s string) any { return stringMarshaler(s) },
144175
}
145176

146177
data := []struct {
@@ -188,7 +219,7 @@ func TestWriteValue(t *testing.T) {
188219
)
189220

190221
data := []struct {
191-
value interface{}
222+
value any
192223
want string
193224
err error
194225
}{
@@ -199,7 +230,6 @@ func TestWriteValue(t *testing.T) {
199230
{value: (*stringerMarshaler)(nil), want: "null"},
200231
{value: ptr, want: "1"},
201232

202-
{value: errorMarshaler{}, err: &MarshalerError{Type: reflect.TypeOf(errorMarshaler{}), Err: errMarshaling}},
203233
{value: make(chan int), err: ErrUnsupportedValueType},
204234
{value: []int{}, err: ErrUnsupportedValueType},
205235
{value: map[int]int{}, err: ErrUnsupportedValueType},
@@ -211,8 +241,8 @@ func TestWriteValue(t *testing.T) {
211241
for _, d := range data {
212242
w := &bytes.Buffer{}
213243
err := writeValue(w, d.value)
214-
if !reflect.DeepEqual(err, d.err) {
215-
t.Errorf("%#v: got error: %v, want error: %v", d.value, err, d.err)
244+
if diff := cmp.Diff(d.err, err, cmpopts.EquateErrors()); diff != "" {
245+
t.Errorf("%#v: error value mismatch (-want,+got):\n%s", d.value, diff)
216246
}
217247
if err != nil {
218248
continue
@@ -223,6 +253,35 @@ func TestWriteValue(t *testing.T) {
223253
}
224254
}
225255

256+
func TestWriteValueMarshalError(t *testing.T) {
257+
data := []struct {
258+
value any
259+
want string
260+
err error
261+
}{
262+
{value: errorMarshaler{}, err: &MarshalerError{Type: reflect.TypeOf(errorMarshaler{}), Err: errMarshaling}},
263+
}
264+
265+
for _, d := range data {
266+
w := &bytes.Buffer{}
267+
err := writeValue(w, d.value)
268+
269+
switch err := err.(type) {
270+
case nil:
271+
t.Errorf("%#v: err == nil, want: not nil", d.value)
272+
case *MarshalerError:
273+
if got, want := err.Type, reflect.TypeOf(errorMarshaler{}); got != want {
274+
t.Errorf("%#v: MarshalerError.Type == %v, want: %v", d.value, got, want)
275+
}
276+
if diff := cmp.Diff(errMarshaling, err.Err, cmpopts.EquateErrors()); diff != "" {
277+
t.Errorf("%#v: MarshalerError.Err value mismatch (-want,+got):\n%s", d.value, diff)
278+
}
279+
default:
280+
t.Errorf("%#v: unexpected error, got: %q, want: a MarshalerError", d.value, err)
281+
}
282+
}
283+
}
284+
226285
type stringData string
227286

228287
type stringStringer string
@@ -266,7 +325,7 @@ func BenchmarkWriteStringKey(b *testing.B) {
266325
for _, k := range keys {
267326
b.Run(k, func(b *testing.B) {
268327
for i := 0; i < b.N; i++ {
269-
writeStringKey(ioutil.Discard, k)
328+
writeStringKey(io.Discard, k)
270329
}
271330
})
272331
}

0 commit comments

Comments
 (0)