Skip to content

Commit ce9e3d5

Browse files
authored
fix: use any instead of interface{} (#1012)
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent d6d4007 commit ce9e3d5

33 files changed

+113
-113
lines changed

cbor/decode.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
"github.com/jinzhu/copier"
2727
)
2828

29-
func Decode(dataBytes []byte, dest interface{}) (int, error) {
29+
func Decode(dataBytes []byte, dest any) (int, error) {
3030
data := bytes.NewReader(dataBytes)
3131
// Create a custom decoder that returns an error on unknown fields
3232
decOptions := _cbor.DecOptions{
@@ -67,7 +67,7 @@ func DecodeIdFromList(cborData []byte) (int, error) {
6767
return 0, err
6868
}
6969
// Make sure that the value is actually numeric
70-
switch v := tmp.Value().([]interface{})[0].(type) {
70+
switch v := tmp.Value().([]any)[0].(type) {
7171
// The upstream CBOR library uses uint64 by default for numeric values
7272
case uint64:
7373
if v > uint64(math.MaxInt) {
@@ -99,8 +99,8 @@ func ListLength(cborData []byte) (int, error) {
9999
// a map of numbers to object pointers to decode into
100100
func DecodeById(
101101
cborData []byte,
102-
idMap map[int]interface{},
103-
) (interface{}, error) {
102+
idMap map[int]any,
103+
) (any, error) {
104104
id, err := DecodeIdFromList(cborData)
105105
if err != nil {
106106
return nil, err
@@ -122,7 +122,7 @@ var (
122122

123123
// DecodeGeneric decodes the specified CBOR into the destination object without using the
124124
// destination object's UnmarshalCBOR() function
125-
func DecodeGeneric(cborData []byte, dest interface{}) error {
125+
func DecodeGeneric(cborData []byte, dest any) error {
126126
// Get destination type
127127
valueDest := reflect.ValueOf(dest)
128128
typeDest := valueDest.Elem().Type()

cbor/decode_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ import (
2525

2626
type decodeTestDefinition struct {
2727
CborHex string
28-
Object interface{}
28+
Object any
2929
BytesRead int
3030
}
3131

3232
var decodeTests = []decodeTestDefinition{
3333
// Simple list of numbers
3434
{
3535
CborHex: "83010203",
36-
Object: []interface{}{uint64(1), uint64(2), uint64(3)},
36+
Object: []any{uint64(1), uint64(2), uint64(3)},
3737
},
3838
// Multiple CBOR objects
3939
{
4040
CborHex: "81018102",
41-
Object: []interface{}{uint64(1)},
41+
Object: []any{uint64(1)},
4242
BytesRead: 2,
4343
},
4444
}
@@ -49,7 +49,7 @@ func TestDecode(t *testing.T) {
4949
if err != nil {
5050
t.Fatalf("failed to decode CBOR hex: %s", err)
5151
}
52-
var dest interface{}
52+
var dest any
5353
bytesRead, err := cbor.Decode(cborData, &dest)
5454
if err != nil {
5555
t.Fatalf("failed to decode CBOR: %s", err)
@@ -205,7 +205,7 @@ type decodeByIdObjectC struct {
205205

206206
type decodeByIdTestDefinition struct {
207207
CborHex string
208-
Object interface{}
208+
Object any
209209
Error error
210210
}
211211

@@ -245,7 +245,7 @@ var decodeByIdTests = []decodeByIdTestDefinition{
245245
func TestDecodeById(t *testing.T) {
246246
for _, test := range decodeByIdTests {
247247
// We define this inside the loop to make sure to get fresh objects each time
248-
var decodeByIdObjectMap = map[int]interface{}{
248+
var decodeByIdObjectMap = map[int]any{
249249
1: &decodeByIdObjectA{},
250250
2: &decodeByIdObjectB{},
251251
3: &decodeByIdObjectC{},

cbor/encode.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424
"github.com/jinzhu/copier"
2525
)
2626

27-
func Encode(data interface{}) ([]byte, error) {
27+
func Encode(data any) ([]byte, error) {
2828
buf := bytes.NewBuffer(nil)
2929
opts := _cbor.EncOptions{
3030
// Make sure that maps have ordered keys
@@ -46,7 +46,7 @@ var (
4646

4747
// EncodeGeneric encodes the specified object to CBOR without using the source object's
4848
// MarshalCBOR() function
49-
func EncodeGeneric(src interface{}) ([]byte, error) {
49+
func EncodeGeneric(src any) ([]byte, error) {
5050
// Get source type
5151
valueSrc := reflect.ValueOf(src)
5252
typeSrc := valueSrc.Elem().Type()

cbor/encode_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ import (
2323

2424
type encodeTestDefinition struct {
2525
CborHex string
26-
Object interface{}
26+
Object any
2727
}
2828

2929
var encodeTests = []encodeTestDefinition{
3030
// Simple list of numbers
3131
{
3232
CborHex: "83010203",
33-
Object: []interface{}{1, 2, 3},
33+
Object: []any{1, 2, 3},
3434
},
3535
}
3636

cbor/value.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
// Helpful wrapper for parsing arbitrary CBOR data which may contain types that
2828
// cannot be easily represented in Go (such as maps with bytestring keys)
2929
type Value struct {
30-
value interface{}
30+
value any
3131
// We store this as a string so that the type is still hashable for use as map keys
3232
cborData string
3333
}
@@ -78,7 +78,7 @@ func (v *Value) UnmarshalCBOR(data []byte) error {
7878
v.value = tmpTagDecode
7979
}
8080
default:
81-
var tmpValue interface{}
81+
var tmpValue any
8282
if _, err := Decode(data, &tmpValue); err != nil {
8383
return err
8484
}
@@ -91,7 +91,7 @@ func (v Value) Cbor() []byte {
9191
return []byte(v.cborData)
9292
}
9393

94-
func (v Value) Value() interface{} {
94+
func (v Value) Value() any {
9595
return v.value
9696
}
9797

@@ -160,20 +160,20 @@ func (v *Value) processArray(data []byte) error {
160160
return nil
161161
}
162162

163-
func generateAstJson(obj interface{}) ([]byte, error) {
164-
tmpJsonObj := map[string]interface{}{}
163+
func generateAstJson(obj any) ([]byte, error) {
164+
tmpJsonObj := map[string]any{}
165165
switch v := obj.(type) {
166166
case []byte:
167167
tmpJsonObj["bytes"] = hex.EncodeToString(v)
168168
case ByteString:
169169
tmpJsonObj["bytes"] = hex.EncodeToString(v.Bytes())
170170
case WrappedCbor:
171171
tmpJsonObj["bytes"] = hex.EncodeToString(v.Bytes())
172-
case []interface{}:
172+
case []any:
173173
return generateAstJsonList[[]any](v)
174174
case Set:
175175
return generateAstJsonList[Set](v)
176-
case map[interface{}]interface{}:
176+
case map[any]any:
177177
return generateAstJsonMap[map[any]any](v)
178178
case Map:
179179
return generateAstJsonMap[Map](v)
@@ -376,12 +376,12 @@ func (l *LazyValue) MarshalJSON() ([]byte, error) {
376376
return l.value.MarshalJSON()
377377
}
378378

379-
func (l *LazyValue) Decode() (interface{}, error) {
379+
func (l *LazyValue) Decode() (any, error) {
380380
err := l.value.UnmarshalCBOR([]byte(l.value.cborData))
381381
return l.Value(), err
382382
}
383383

384-
func (l *LazyValue) Value() interface{} {
384+
func (l *LazyValue) Value() any {
385385
return l.value.Value()
386386
}
387387

cbor/value_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import (
3030

3131
var testDefs = []struct {
3232
cborHex string
33-
expectedObject interface{}
33+
expectedObject any
3434
expectedAstJson string
3535
expectedDecodeError error
3636
}{

cmd/gouroboros/chainsync.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func newChainSyncFlags() *chainSyncFlags {
7070
}
7171

7272
// Intersect points (last block of previous era) for each era on testnet/mainnet
73-
var eraIntersect = map[string]map[string][]interface{}{
73+
var eraIntersect = map[string]map[string][]any{
7474
"unknown": {
7575
"genesis": {},
7676
},
@@ -145,7 +145,7 @@ func testChainSync(f *globalFlags) {
145145
os.Exit(1)
146146
}
147147

148-
var intersectPoint []interface{}
148+
var intersectPoint []any
149149
if _, ok := eraIntersect[f.network]; !ok {
150150
if chainSyncFlags.startEra != "genesis" {
151151
fmt.Printf(
@@ -255,7 +255,7 @@ func chainSyncRollBackwardHandler(
255255
func chainSyncRollForwardHandler(
256256
ctx chainsync.CallbackContext,
257257
blockType uint,
258-
blockData interface{},
258+
blockData any,
259259
tip chainsync.Tip,
260260
) error {
261261
var block ledger.Block

connection.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ type Connection struct {
6464
muxer *muxer.Muxer
6565
errorChan chan error
6666
protoErrorChan chan error
67-
handshakeFinishedChan chan interface{}
67+
handshakeFinishedChan chan any
6868
handshakeVersion uint16
6969
handshakeVersionData protocol.VersionData
70-
doneChan chan interface{}
70+
doneChan chan any
7171
connClosedChan chan struct{}
7272
waitGroup sync.WaitGroup
7373
onceClose sync.Once
@@ -101,7 +101,7 @@ type Connection struct {
101101
func NewConnection(options ...ConnectionOptionFunc) (*Connection, error) {
102102
c := &Connection{
103103
protoErrorChan: make(chan error, 10),
104-
handshakeFinishedChan: make(chan interface{}),
104+
handshakeFinishedChan: make(chan any),
105105
connClosedChan: make(chan struct{}),
106106
// Create a discard logger to throw away logs. We do this so
107107
// we don't have to add guards around every log operation if
@@ -261,7 +261,7 @@ func (c *Connection) setupConnection() error {
261261
)
262262
}
263263
// Start Goroutine to shutdown when doneChan is closed
264-
c.doneChan = make(chan interface{})
264+
c.doneChan = make(chan any)
265265
go func() {
266266
<-c.doneChan
267267
c.shutdown()

internal/test/helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ func JsonStringsEqual(jsonData1 []byte, jsonData2 []byte) bool {
3030
return true
3131
}
3232
// Decode provided JSON strings
33-
var tmpObj1 interface{}
33+
var tmpObj1 any
3434
if err := json.Unmarshal(jsonData1, &tmpObj1); err != nil {
3535
return false
3636
}
37-
var tmpObj2 interface{}
37+
var tmpObj2 any
3838
if err := json.Unmarshal(jsonData2, &tmpObj2); err != nil {
3939
return false
4040
}

ledger/babbage/babbage.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -404,11 +404,11 @@ func (d *BabbageTransactionOutputDatumOption) UnmarshalCBOR(data []byte) error {
404404
}
405405

406406
func (d *BabbageTransactionOutputDatumOption) MarshalCBOR() ([]byte, error) {
407-
var tmpObj []interface{}
407+
var tmpObj []any
408408
if d.hash != nil {
409-
tmpObj = []interface{}{DatumOptionTypeHash, d.hash}
409+
tmpObj = []any{DatumOptionTypeHash, d.hash}
410410
} else if d.data != nil {
411-
tmpObj = []interface{}{DatumOptionTypeData, cbor.Tag{Number: 24, Content: d.data.Cbor()}}
411+
tmpObj = []any{DatumOptionTypeData, cbor.Tag{Number: 24, Content: d.data.Cbor()}}
412412
} else {
413413
return nil, errors.New("unknown datum option type")
414414
}

0 commit comments

Comments
 (0)