Commit b17a70e0 authored by Kirill Smelkov's avatar Kirill Smelkov Committed by GitHub

*: interface{} -> any (#81)

Since 64d5926e (Drop support for Go < 1.18) and 3bf6c92d (Add PyDict
mode) ogórek started to require go ≥ 1.18 that added generics. That
release also added `any` type alias for interface{} to increase
signal/noise ratio in the programs where interface{} is frequently used.

Any is aliased to interface{} similarly to how rune is aliased to
int32, and so any and interface{} are fully interchangeable, just any
reads more shorter and concise:

    https://pkg.go.dev/builtin#any

-> Replace interface{} with any throughout the whole codebase for the
   sources to be read more well.

The replacement was done via running the following sed script:

    $ sed -i -e 's/interface{}/any/g' *.go
parent b8a99f47
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
// Use [Decoder] to decode a pickle from input stream, for example: // Use [Decoder] to decode a pickle from input stream, for example:
// //
// d := ogórek.NewDecoder(r) // d := ogórek.NewDecoder(r)
// obj, err := d.Decode() // obj is interface{} representing decoded Python object // obj, err := d.Decode() // obj is any representing decoded Python object
// //
// Use [Encoder] to encode an object as pickle into output stream, for example: // Use [Encoder] to encode an object as pickle into output stream, for example:
// //
......
...@@ -44,7 +44,7 @@ type EncoderConfig struct { ...@@ -44,7 +44,7 @@ type EncoderConfig struct {
// will be encoded as an object reference. // will be encoded as an object reference.
// //
// See Ref documentation for more details. // See Ref documentation for more details.
PersistentRef func(obj interface{}) *Ref PersistentRef func(obj any) *Ref
// StrictUnicode, when true, requests to always encode Go string // StrictUnicode, when true, requests to always encode Go string
// objects as Python unicode independently of used pickle protocol. // objects as Python unicode independently of used pickle protocol.
...@@ -71,7 +71,7 @@ func NewEncoderWithConfig(w io.Writer, config *EncoderConfig) *Encoder { ...@@ -71,7 +71,7 @@ func NewEncoderWithConfig(w io.Writer, config *EncoderConfig) *Encoder {
} }
// Encode writes the pickle encoding of v to w, the encoder's writer // Encode writes the pickle encoding of v to w, the encoder's writer
func (e *Encoder) Encode(v interface{}) error { func (e *Encoder) Encode(v any) error {
proto := e.config.Protocol proto := e.config.Protocol
if !(0 <= proto && proto <= highestProtocol) { if !(0 <= proto && proto <= highestProtocol) {
return fmt.Errorf("pickle: encode: invalid protocol %d", proto) return fmt.Errorf("pickle: encode: invalid protocol %d", proto)
...@@ -109,7 +109,7 @@ func (e *Encoder) emit(bv ...byte) error { ...@@ -109,7 +109,7 @@ func (e *Encoder) emit(bv ...byte) error {
} }
// emitf writes formatted string into encoder output. // emitf writes formatted string into encoder output.
func (e *Encoder) emitf(format string, argv ...interface{}) error { func (e *Encoder) emitf(format string, argv ...any) error {
_, err := fmt.Fprintf(e.w, format, argv...) _, err := fmt.Fprintf(e.w, format, argv...)
return err return err
} }
...@@ -659,7 +659,7 @@ func (e *Encoder) encodeStruct(st reflect.Value) error { ...@@ -659,7 +659,7 @@ func (e *Encoder) encodeStruct(st reflect.Value) error {
return e.emit(opDict) return e.emit(opDict)
} }
func reflectValueOf(v interface{}) reflect.Value { func reflectValueOf(v any) reflect.Value {
rv, ok := v.(reflect.Value) rv, ok := v.(reflect.Value)
if !ok { if !ok {
......
...@@ -127,7 +127,7 @@ type mark struct{} ...@@ -127,7 +127,7 @@ type mark struct{}
type None struct{} type None struct{}
// Tuple is a representation of Python's tuple. // Tuple is a representation of Python's tuple.
type Tuple []interface{} type Tuple []any
// Bytes represents Python's bytes. // Bytes represents Python's bytes.
type Bytes string type Bytes string
...@@ -154,8 +154,8 @@ func (v unicode) GoString() string { ...@@ -154,8 +154,8 @@ func (v unicode) GoString() string {
type Decoder struct { type Decoder struct {
r *bufio.Reader r *bufio.Reader
config *DecoderConfig config *DecoderConfig
stack []interface{} stack []any
memo map[string]interface{} memo map[string]any
// a reusable buffer that can be used by the various decoding functions // a reusable buffer that can be used by the various decoding functions
// functions using this should call buf.Reset to clear the old contents // functions using this should call buf.Reset to clear the old contents
...@@ -182,7 +182,7 @@ type DecoderConfig struct { ...@@ -182,7 +182,7 @@ type DecoderConfig struct {
// equivalent-to-type Go ghost object, e.g. equivalent to zodb.BTree. // equivalent-to-type Go ghost object, e.g. equivalent to zodb.BTree.
// //
// See Ref documentation for more details. // See Ref documentation for more details.
PersistentLoad func(ref Ref) (interface{}, error) PersistentLoad func(ref Ref) (any, error)
// StrictUnicode, when true, requests to decode to Go string only // StrictUnicode, when true, requests to decode to Go string only
// Python unicode objects. Python2 bytestrings (py2 str type) are // Python unicode objects. Python2 bytestrings (py2 str type) are
...@@ -211,14 +211,14 @@ func NewDecoderWithConfig(r io.Reader, config *DecoderConfig) *Decoder { ...@@ -211,14 +211,14 @@ func NewDecoderWithConfig(r io.Reader, config *DecoderConfig) *Decoder {
return &Decoder{ return &Decoder{
r: reader, r: reader,
config: config, config: config,
stack: make([]interface{}, 0), stack: make([]any, 0),
memo: make(map[string]interface{}), memo: make(map[string]any),
protocol: 0, protocol: 0,
} }
} }
// Decode decodes the pickle stream and returns the result or an error. // Decode decodes the pickle stream and returns the result or an error.
func (d *Decoder) Decode() (interface{}, error) { func (d *Decoder) Decode() (any, error) {
insn := 0 insn := 0
loop: loop:
...@@ -303,7 +303,7 @@ loop: ...@@ -303,7 +303,7 @@ loop:
case opList: case opList:
err = d.loadList() err = d.loadList()
case opEmptyList: case opEmptyList:
d.push([]interface{}{}) d.push([]any{})
case opObj: case opObj:
err = d.obj() err = d.obj()
case opPut: case opPut:
...@@ -411,7 +411,7 @@ func (d *Decoder) readLine() ([]byte, error) { ...@@ -411,7 +411,7 @@ func (d *Decoder) readLine() ([]byte, error) {
// userOK tells whether it is ok to return all objects to user. // userOK tells whether it is ok to return all objects to user.
// //
// for example it is not ok to return the mark object. // for example it is not ok to return the mark object.
func userOK(objv ...interface{}) error { func userOK(objv ...any) error {
for _, obj := range objv { for _, obj := range objv {
switch obj.(type) { switch obj.(type) {
case mark: case mark:
...@@ -439,13 +439,13 @@ func (d *Decoder) marker() (int, error) { ...@@ -439,13 +439,13 @@ func (d *Decoder) marker() (int, error) {
} }
// Append a new value // Append a new value
func (d *Decoder) push(v interface{}) { func (d *Decoder) push(v any) {
d.stack = append(d.stack, v) d.stack = append(d.stack, v)
} }
// Pop a value // Pop a value
// The returned error is errStackUnderflow if decoder stack is empty // The returned error is errStackUnderflow if decoder stack is empty
func (d *Decoder) pop() (interface{}, error) { func (d *Decoder) pop() (any, error) {
ln := len(d.stack) - 1 ln := len(d.stack) - 1
if ln < 0 { if ln < 0 {
return nil, errStackUnderflow return nil, errStackUnderflow
...@@ -456,7 +456,7 @@ func (d *Decoder) pop() (interface{}, error) { ...@@ -456,7 +456,7 @@ func (d *Decoder) pop() (interface{}, error) {
} }
// Pop a value (when you know for sure decoder stack is not empty) // Pop a value (when you know for sure decoder stack is not empty)
func (d *Decoder) xpop() interface{} { func (d *Decoder) xpop() any {
v, err := d.pop() v, err := d.pop()
if err != nil { if err != nil {
panic(err) panic(err)
...@@ -465,7 +465,7 @@ func (d *Decoder) xpop() interface{} { ...@@ -465,7 +465,7 @@ func (d *Decoder) xpop() interface{} {
} }
// popUser pops stack value and checks whether it is ok to return to user. // popUser pops stack value and checks whether it is ok to return to user.
func (d *Decoder) popUser() (interface{}, error) { func (d *Decoder) popUser() (any, error) {
v, err := d.pop() v, err := d.pop()
if err != nil { if err != nil {
return nil, err return nil, err
...@@ -511,7 +511,7 @@ func (d *Decoder) loadInt() error { ...@@ -511,7 +511,7 @@ func (d *Decoder) loadInt() error {
return err return err
} }
var val interface{} var val any
switch string(line) { switch string(line) {
case opFalse[1:3]: case opFalse[1:3]:
...@@ -641,7 +641,7 @@ type Ref struct { ...@@ -641,7 +641,7 @@ type Ref struct {
// //
// used to be string for protocol 0, but "upgraded" to be arbitrary // used to be string for protocol 0, but "upgraded" to be arbitrary
// object for later protocols. // object for later protocols.
Pid interface{} Pid any
} }
// Push a persistent object id // Push a persistent object id
...@@ -950,8 +950,8 @@ func (d *Decoder) loadAppend() error { ...@@ -950,8 +950,8 @@ func (d *Decoder) loadAppend() error {
return err return err
} }
switch l.(type) { switch l.(type) {
case []interface{}: case []any:
l := l.([]interface{}) l := l.([]any)
d.stack[len(d.stack)-1] = append(l, v) d.stack[len(d.stack)-1] = append(l, v)
default: default:
return fmt.Errorf("pickle: loadAppend: expected a list, got %T", l) return fmt.Errorf("pickle: loadAppend: expected a list, got %T", l)
...@@ -987,7 +987,7 @@ func (d *Decoder) global() error { ...@@ -987,7 +987,7 @@ func (d *Decoder) global() error {
// //
// It checks whether key is of appropriate type, and if yes - succeeds. // It checks whether key is of appropriate type, and if yes - succeeds.
// If key is not appropriate - the map stays unchanged and false is returned. // If key is not appropriate - the map stays unchanged and false is returned.
func mapTryAssign(m map[interface{}]interface{}, key, value interface{}) (ok bool) { func mapTryAssign(m map[any]any, key, value any) (ok bool) {
// use panic/recover to detect inappropriate keys. // use panic/recover to detect inappropriate keys.
// //
// We could try to use reflect.TypeOf(key).Comparable() instead, but that // We could try to use reflect.TypeOf(key).Comparable() instead, but that
...@@ -1015,7 +1015,7 @@ func mapTryAssign(m map[interface{}]interface{}, key, value interface{}) (ok boo ...@@ -1015,7 +1015,7 @@ func mapTryAssign(m map[interface{}]interface{}, key, value interface{}) (ok boo
} }
// dictTryAssign is like mapTryAssign but for Dict. // dictTryAssign is like mapTryAssign but for Dict.
func dictTryAssign(d Dict, key, value interface{}) (ok bool) { func dictTryAssign(d Dict, key, value any) (ok bool) {
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
ok = false ok = false
...@@ -1038,7 +1038,7 @@ func (d *Decoder) loadDict() error { ...@@ -1038,7 +1038,7 @@ func (d *Decoder) loadDict() error {
return fmt.Errorf("pickle: loadDict: odd # of elements") return fmt.Errorf("pickle: loadDict: odd # of elements")
} }
var m interface{} var m any
if d.config.PyDict { if d.config.PyDict {
m, err = d.loadDictDict(items) m, err = d.loadDictDict(items)
} else { } else {
...@@ -1053,8 +1053,8 @@ func (d *Decoder) loadDict() error { ...@@ -1053,8 +1053,8 @@ func (d *Decoder) loadDict() error {
return nil return nil
} }
func (d *Decoder) loadDictMap(items []interface{}) (map[interface{}]interface{}, error) { func (d *Decoder) loadDictMap(items []any) (map[any]any, error) {
m := make(map[interface{}]interface{}, len(items)/2) m := make(map[any]any, len(items)/2)
for i := 0; i < len(items); i += 2 { for i := 0; i < len(items); i += 2 {
key := items[i] key := items[i]
if !mapTryAssign(m, key, items[i+1]) { if !mapTryAssign(m, key, items[i+1]) {
...@@ -1064,7 +1064,7 @@ func (d *Decoder) loadDictMap(items []interface{}) (map[interface{}]interface{}, ...@@ -1064,7 +1064,7 @@ func (d *Decoder) loadDictMap(items []interface{}) (map[interface{}]interface{},
return m, nil return m, nil
} }
func (d *Decoder) loadDictDict(items []interface{}) (Dict, error) { func (d *Decoder) loadDictDict(items []any) (Dict, error) {
m := NewDictWithSizeHint(len(items)/2) m := NewDictWithSizeHint(len(items)/2)
for i := 0; i < len(items); i += 2 { for i := 0; i < len(items); i += 2 {
key := items[i] key := items[i]
...@@ -1077,11 +1077,11 @@ func (d *Decoder) loadDictDict(items []interface{}) (Dict, error) { ...@@ -1077,11 +1077,11 @@ func (d *Decoder) loadDictDict(items []interface{}) (Dict, error) {
func (d *Decoder) loadEmptyDict() error { func (d *Decoder) loadEmptyDict() error {
var m interface{} var m any
if d.config.PyDict { if d.config.PyDict {
m = NewDict() m = NewDict()
} else { } else {
m = make(map[interface{}]interface{}, 0) m = make(map[any]any, 0)
} }
d.push(m) d.push(m)
return nil return nil
...@@ -1098,8 +1098,8 @@ func (d *Decoder) loadAppends() error { ...@@ -1098,8 +1098,8 @@ func (d *Decoder) loadAppends() error {
l := d.stack[k-1] l := d.stack[k-1]
switch l.(type) { switch l.(type) {
case []interface{}: case []any:
l := l.([]interface{}) l := l.([]any)
for _, v := range d.stack[k+1 : len(d.stack)] { for _, v := range d.stack[k+1 : len(d.stack)] {
l = append(l, v) l = append(l, v)
} }
...@@ -1167,7 +1167,7 @@ func (d *Decoder) loadList() error { ...@@ -1167,7 +1167,7 @@ func (d *Decoder) loadList() error {
return err return err
} }
v := append([]interface{}{}, d.stack[k+1:]...) v := append([]any{}, d.stack[k+1:]...)
d.stack = append(d.stack[:k], v) d.stack = append(d.stack[:k], v)
return nil return nil
} }
...@@ -1267,7 +1267,7 @@ func (d *Decoder) loadSetItem() error { ...@@ -1267,7 +1267,7 @@ func (d *Decoder) loadSetItem() error {
} }
m := d.stack[len(d.stack)-1] m := d.stack[len(d.stack)-1]
switch m := m.(type) { switch m := m.(type) {
case map[interface{}]interface{}: case map[any]any:
if !mapTryAssign(m, k, v) { if !mapTryAssign(m, k, v) {
return fmt.Errorf("pickle: loadSetItem: map: invalid key type %T", k) return fmt.Errorf("pickle: loadSetItem: map: invalid key type %T", k)
} }
...@@ -1295,7 +1295,7 @@ func (d *Decoder) loadSetItems() error { ...@@ -1295,7 +1295,7 @@ func (d *Decoder) loadSetItems() error {
l := d.stack[k-1] l := d.stack[k-1]
switch m := l.(type) { switch m := l.(type) {
case map[interface{}]interface{}: case map[any]any:
for i := k + 1; i < len(d.stack); i += 2 { for i := k + 1; i < len(d.stack); i += 2 {
key := d.stack[i] key := d.stack[i]
if !mapTryAssign(m, key, d.stack[i+1]) { if !mapTryAssign(m, key, d.stack[i+1]) {
...@@ -1478,7 +1478,7 @@ func decodeLong(data string) (*big.Int, error) { ...@@ -1478,7 +1478,7 @@ func decodeLong(data string) (*big.Int, error) {
// //
// Python uses such representation of bytes for protocols <= 2 - where there is // Python uses such representation of bytes for protocols <= 2 - where there is
// no BYTES* opcodes. // no BYTES* opcodes.
func decodeLatin1Bytes(arg interface{}) ([]byte, error) { func decodeLatin1Bytes(arg any) ([]byte, error) {
// bytes as latin1-decoded unicode // bytes as latin1-decoded unicode
ulatin1, ok := arg.(string) ulatin1, ok := arg.(string)
if !ok { if !ok {
......
This diff is collapsed.
...@@ -9,14 +9,14 @@ import ( ...@@ -9,14 +9,14 @@ import (
// Under the given transformation function in must be transformed to outOK. // Under the given transformation function in must be transformed to outOK.
type CodecTestCase struct { type CodecTestCase struct {
in string in string
outOK interface{} // string | error outOK any // string | error
} }
// testCodec tests transform func applied to all test cases from testv. // testCodec tests transform func applied to all test cases from testv.
func testCodec(t *testing.T, transform func(in string)(string, error), testv []CodecTestCase) { func testCodec(t *testing.T, transform func(in string)(string, error), testv []CodecTestCase) {
for _, tt := range testv { for _, tt := range testv {
s, err := transform(tt.in) s, err := transform(tt.in)
var out interface{} = s var out any = s
if err != nil { if err != nil {
out = err out = err
} }
......
...@@ -12,7 +12,7 @@ import ( ...@@ -12,7 +12,7 @@ import (
// Python int is decoded as int64, while Python long is decoded as big.Int. // Python int is decoded as int64, while Python long is decoded as big.Int.
// Go code should use AsInt64 to accept normal-range integers independently of // Go code should use AsInt64 to accept normal-range integers independently of
// their Python representation. // their Python representation.
func AsInt64(x interface{}) (int64, error) { func AsInt64(x any) (int64, error) {
switch x := x.(type) { switch x := x.(type) {
case int64: case int64:
return x, nil return x, nil
...@@ -33,7 +33,7 @@ func AsInt64(x interface{}) (int64, error) { ...@@ -33,7 +33,7 @@ func AsInt64(x interface{}) (int64, error) {
// //
// [ByteString] is treated related to [Bytes] because [ByteString] represents str // [ByteString] is treated related to [Bytes] because [ByteString] represents str
// type from py2 which can contain both string and binary data. // type from py2 which can contain both string and binary data.
func AsBytes(x interface{}) (Bytes, error) { func AsBytes(x any) (Bytes, error) {
switch x := x.(type) { switch x := x.(type) {
case Bytes: case Bytes:
return x, nil return x, nil
...@@ -50,7 +50,7 @@ func AsBytes(x interface{}) (Bytes, error) { ...@@ -50,7 +50,7 @@ func AsBytes(x interface{}) (Bytes, error) {
// //
// [ByteString] is treated related to string because [ByteString] represents str // [ByteString] is treated related to string because [ByteString] represents str
// type from py2 which can contain both string and binary data. // type from py2 which can contain both string and binary data.
func AsString(x interface{}) (string, error) { func AsString(x any) (string, error) {
switch x := x.(type) { switch x := x.(type) {
case string: case string:
return x, nil return x, nil
...@@ -64,7 +64,7 @@ func AsString(x interface{}) (string, error) { ...@@ -64,7 +64,7 @@ func AsString(x interface{}) (string, error) {
// stringEQ compares arbitrary x to string y. // stringEQ compares arbitrary x to string y.
// //
// It succeeds only if AsString(x) succeeds and string data of x equals to y. // It succeeds only if AsString(x) succeeds and string data of x equals to y.
func stringEQ(x interface{}, y string) bool { func stringEQ(x any, y string) bool {
s, err := AsString(x) s, err := AsString(x)
if err != nil { if err != nil {
return false return false
......
...@@ -13,8 +13,8 @@ func TestAsInt64(t *testing.T) { ...@@ -13,8 +13,8 @@ func TestAsInt64(t *testing.T) {
Erange := fmt.Errorf("long outside of int64 range") Erange := fmt.Errorf("long outside of int64 range")
testv := []struct { testv := []struct {
in interface{} in any
outOK interface{} outOK any
}{ }{
{int64(0), int64(0)}, {int64(0), int64(0)},
{int64(1), int64(1)}, {int64(1), int64(1)},
...@@ -36,7 +36,7 @@ func TestAsInt64(t *testing.T) { ...@@ -36,7 +36,7 @@ func TestAsInt64(t *testing.T) {
for _, tt := range testv { for _, tt := range testv {
iout, err := AsInt64(tt.in) iout, err := AsInt64(tt.in)
var out interface{} = iout var out any = iout
if err != nil { if err != nil {
out = err out = err
if iout != 0 { if iout != 0 {
...@@ -54,10 +54,10 @@ func TestAsInt64(t *testing.T) { ...@@ -54,10 +54,10 @@ func TestAsInt64(t *testing.T) {
} }
func TestAsBytesString(t *testing.T) { func TestAsBytesString(t *testing.T) {
Ebytes := func(x interface{}) error { Ebytes := func(x any) error {
return fmt.Errorf("expect bytes|bytestr; got %T", x) return fmt.Errorf("expect bytes|bytestr; got %T", x)
} }
Estring := func(x interface{}) error { Estring := func(x any) error {
return fmt.Errorf("expect unicode|bytestr; got %T", x) return fmt.Errorf("expect unicode|bytestr; got %T", x)
} }
...@@ -65,7 +65,7 @@ func TestAsBytesString(t *testing.T) { ...@@ -65,7 +65,7 @@ func TestAsBytesString(t *testing.T) {
const n = false const n = false
testv := []struct { testv := []struct {
in interface{} in any
bok bool // AsBytes succeeds bok bool // AsBytes succeeds
sok bool // AsString succeeds sok bool // AsString succeeds
}{ }{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment