Skip to content

Commit 7e26918

Browse files
committed
Update comments to use newer terminology (Go 1.18)
This commit updates code comments to migrate from old terminology to use newer terminology introduced in 2022. For example, "empty interface" -> "a value of type any".
1 parent c427e21 commit 7e26918

7 files changed

Lines changed: 95 additions & 96 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ This library checks tag validity for built-in tags (currently tag numbers 0, 1,
854854

855855
Unknown tag data items (not tag number 0, 1, 2, 3, or 55799) are handled in two ways:
856856

857-
* When decoding into an empty interface, unknown tag data item will be decoded into `cbor.Tag` data type, which contains tag number and tag content. The tag content will be decoded into the default Go data type for the CBOR data type.
857+
* When decoding into a value of type any, unknown tag data item will be decoded into `cbor.Tag` data type, which contains tag number and tag content. The tag content will be decoded into the default Go data type for the CBOR data type.
858858
* When decoding into other Go types, unknown tag data item is decoded into the specified Go type. If Go type is registered with a tag number, the tag number can optionally be verified.
859859

860860
Decoder also has an option to forbid tag data items (treat any tag data item as error) which is specified by protocols such as CTAP2 Canonical CBOR.

bench_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -302,9 +302,9 @@ func BenchmarkUnmarshal(b *testing.B) {
302302
data []byte
303303
decodeToType reflect.Type
304304
}{
305-
// Unmarshal CBOR map with string key to map[string]interface{}.
305+
// Unmarshal CBOR map with string key to map[string]any.
306306
{
307-
name: "CBOR map to Go map[string]interface{}",
307+
name: "CBOR map to Go map[string]any",
308308
data: mustHexDecode("a86154f56255691bffffffffffffffff61493903e76146fbc0106666666666666142581a0102030405060708090a0b0c0d0e0f101112131415161718191a6153782b54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f6764536c6369981a0102030405060708090a0b0c0d0e0f101112131415161718181819181a634d7373ad6163614361656145616661466167614761686148616e614e616d614d61616141616261426164614461696149616a614a616c614c"),
309309
decodeToType: reflect.TypeFor[map[string]any](),
310310
},
@@ -314,9 +314,9 @@ func BenchmarkUnmarshal(b *testing.B) {
314314
data: mustHexDecode("a86154f56255491bffffffffffffffff61493903e76146fbc0106666666666666142581a0102030405060708090a0b0c0d0e0f101112131415161718191a6153782b54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f6764536c6369981a0102030405060708090a0b0c0d0e0f101112131415161718181819181a634d7373ad6163614361656145616661466167614761686148616e614e616d614d61616141616261426164614461696149616a614a616c614c"),
315315
decodeToType: reflect.TypeFor[T1](),
316316
},
317-
// Unmarshal CBOR map with integer key, such as COSE Key and SenML, to map[int]interface{}.
317+
// Unmarshal CBOR map with integer key, such as COSE Key and SenML, to map[int]any.
318318
{
319-
name: "CBOR map to Go map[int]interface{}",
319+
name: "CBOR map to Go map[int]any",
320320
data: mustHexDecode("a801f5021bffffffffffffffff033903e704fbc01066666666666605581a0102030405060708090a0b0c0d0e0f101112131415161718191a06782b54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f6707981a0102030405060708090a0b0c0d0e0f101112131415161718181819181a08ad61646144616661466167614761686148616d614d616e614e6161614161626142616361436165614561696149616a614a616c614c"),
321321
decodeToType: reflect.TypeFor[map[int]any](),
322322
},
@@ -326,9 +326,9 @@ func BenchmarkUnmarshal(b *testing.B) {
326326
data: mustHexDecode("a801f5021bffffffffffffffff033903e704fbc01066666666666605581a0102030405060708090a0b0c0d0e0f101112131415161718191a06782b54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f6707981a0102030405060708090a0b0c0d0e0f101112131415161718181819181a08ad61646144616661466167614761686148616d614d616e614e6161614161626142616361436165614561696149616a614a616c614c"),
327327
decodeToType: reflect.TypeFor[T2](),
328328
},
329-
// Unmarshal CBOR array of known sequence of data types, such as signed/maced/encrypted CWT, to []interface{}.
329+
// Unmarshal CBOR array of known sequence of data types, such as signed/maced/encrypted CWT, to []any.
330330
{
331-
name: "CBOR array to Go []interface{}",
331+
name: "CBOR array to Go []any",
332332
data: mustHexDecode("88f51bffffffffffffffff3903e7fbc010666666666666581a0102030405060708090a0b0c0d0e0f101112131415161718191a782b54686520717569636b2062726f776e20666f78206a756d7073206f76657220746865206c617a7920646f67981a0102030405060708090a0b0c0d0e0f101112131415161718181819181aad616261426163614361646144616561456166614661696149616e614e616161416167614761686148616a614a616c614c616d614d"),
333333
decodeToType: reflect.TypeFor[[]any](),
334334
},
@@ -462,7 +462,7 @@ func BenchmarkMarshal(b *testing.B) {
462462
})
463463
}
464464
}
465-
// Marshal map[string]interface{} to CBOR map
465+
// Marshal map[string]any to CBOR map
466466
m1 := map[string]any{
467467
"T": true,
468468
"UI": uint(18446744073709551615),
@@ -484,7 +484,7 @@ func BenchmarkMarshal(b *testing.B) {
484484
Slci: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26},
485485
Mss: map[string]string{"a": "A", "b": "B", "c": "C", "d": "D", "e": "E", "f": "F", "g": "G", "h": "H", "i": "I", "j": "J", "l": "L", "m": "M", "n": "N"},
486486
}
487-
// Marshal map[int]interface{} to CBOR map
487+
// Marshal map[int]any to CBOR map
488488
m2 := map[int]any{
489489
1: true,
490490
2: uint(18446744073709551615),
@@ -506,7 +506,7 @@ func BenchmarkMarshal(b *testing.B) {
506506
Slci: []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26},
507507
Mss: map[string]string{"a": "A", "b": "B", "c": "C", "d": "D", "e": "E", "f": "F", "g": "G", "h": "H", "i": "I", "j": "J", "l": "L", "m": "M", "n": "N"},
508508
}
509-
// Marshal []interface to CBOR array.
509+
// Marshal []any to CBOR array.
510510
slc := []any{
511511
true,
512512
uint(18446744073709551615),
@@ -533,7 +533,7 @@ func BenchmarkMarshal(b *testing.B) {
533533
value any
534534
}{
535535
{
536-
name: "Go map[string]interface{} to CBOR map",
536+
name: "Go map[string]any to CBOR map",
537537
value: m1,
538538
},
539539
{
@@ -570,15 +570,15 @@ func BenchmarkMarshal(b *testing.B) {
570570
value: SomeFieldsOneOmitEmpty{},
571571
},
572572
{
573-
name: "Go map[int]interface{} to CBOR map",
573+
name: "Go map[int]any to CBOR map",
574574
value: m2,
575575
},
576576
{
577577
name: "Go struct keyasint to CBOR map",
578578
value: v2,
579579
},
580580
{
581-
name: "Go []interface{} to CBOR map",
581+
name: "Go []any to CBOR map",
582582
value: slc,
583583
},
584584
{

decode.go

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import (
3939
// unmarshals CBOR into the value pointed to by the pointer. If the
4040
// pointer is nil, Unmarshal creates a new value for it to point to.
4141
//
42-
// To unmarshal CBOR into an empty interface value, Unmarshal uses the
42+
// To unmarshal CBOR into a value of type any, Unmarshal uses the
4343
// following rules:
4444
//
4545
// CBOR booleans decode to bool.
@@ -48,8 +48,8 @@ import (
4848
// CBOR floating points decode to float64.
4949
// CBOR byte strings decode to []byte.
5050
// CBOR text strings decode to string.
51-
// CBOR arrays decode to []interface{}.
52-
// CBOR maps decode to map[interface{}]interface{}.
51+
// CBOR arrays decode to []any.
52+
// CBOR maps decode to map[any]any.
5353
// CBOR null and undefined values decode to nil.
5454
// CBOR times (tag 0 and 1) decode to time.Time.
5555
// CBOR bignums (tag 2 and 3) decode to big.Int.
@@ -299,7 +299,7 @@ func (e *InadmissibleTagContentTypeError) Error() string {
299299

300300
// DupMapKeyMode specifies how to enforce duplicate map key. Two map keys are considered duplicates if:
301301
// 1. When decoding into a struct, both keys match the same struct field. The keys are also
302-
// considered duplicates if neither matches any field and decoding to interface{} would produce
302+
// considered duplicates if neither matches any field and decoding to a value of type any would produce
303303
// equal (==) values for both keys.
304304
// 2. When decoding into a map, both keys are equal (==) when decoded into values of the
305305
// destination map's key type.
@@ -360,19 +360,19 @@ func (tm TagsMode) valid() bool {
360360
}
361361

362362
// IntDecMode specifies which Go type (int64, uint64, or big.Int) should
363-
// be used when decoding CBOR integers (major type 0 and 1) to Go interface{}.
363+
// be used when decoding CBOR integers (major type 0 and 1) to a value of type any.
364364
type IntDecMode int
365365

366366
const (
367-
// IntDecConvertNone affects how CBOR integers (major type 0 and 1) decode to Go interface{}.
367+
// IntDecConvertNone affects how CBOR integers (major type 0 and 1) decode to a value of type any.
368368
// It decodes CBOR unsigned integer (major type 0) to:
369369
// - uint64
370370
// It decodes CBOR negative integer (major type 1) to:
371371
// - int64 if value fits
372372
// - big.Int or *big.Int (see BigIntDecMode) if value doesn't fit into int64
373373
IntDecConvertNone IntDecMode = iota
374374

375-
// IntDecConvertSigned affects how CBOR integers (major type 0 and 1) decode to Go interface{}.
375+
// IntDecConvertSigned affects how CBOR integers (major type 0 and 1) decode to a value of type any.
376376
// It decodes CBOR integers (major type 0 and 1) to:
377377
// - int64 if value fits
378378
// - big.Int or *big.Int (see BigIntDecMode) if value < math.MinInt64
@@ -382,13 +382,13 @@ const (
382382
// Please use other options, such as IntDecConvertSignedOrError, IntDecConvertSignedOrBigInt, IntDecConvertNone.
383383
IntDecConvertSigned
384384

385-
// IntDecConvertSignedOrFail affects how CBOR integers (major type 0 and 1) decode to Go interface{}.
385+
// IntDecConvertSignedOrFail affects how CBOR integers (major type 0 and 1) decode to a value of type any.
386386
// It decodes CBOR integers (major type 0 and 1) to:
387387
// - int64 if value fits
388388
// - return UnmarshalTypeError if value doesn't fit into int64
389389
IntDecConvertSignedOrFail
390390

391-
// IntDecConvertSignedOrBigInt affects how CBOR integers (major type 0 and 1) decode to Go interface{}.
391+
// IntDecConvertSignedOrBigInt affects how CBOR integers (major type 0 and 1) decode to a value of type any.
392392
// It makes CBOR integers (major type 0 and 1) decode to:
393393
// - int64 if value fits
394394
// - big.Int or *big.Int (see BigIntDecMode) if value doesn't fit into int64
@@ -402,10 +402,10 @@ func (idm IntDecMode) valid() bool {
402402
}
403403

404404
// MapKeyByteStringMode specifies how to decode CBOR byte string (major type 2)
405-
// as Go map key when decoding CBOR map key into an empty Go interface value.
405+
// as Go map key when decoding CBOR map key into a value of type any.
406406
// Specifically, this option applies when decoding CBOR map into
407-
// - Go empty interface, or
408-
// - Go map with empty interface as key type.
407+
// - any, or
408+
// - Go map with type any as key type.
409409
// The CBOR map key types handled by this option are
410410
// - byte string
411411
// - tagged byte string
@@ -420,7 +420,7 @@ const (
420420
MapKeyByteStringAllowed MapKeyByteStringMode = iota
421421

422422
// MapKeyByteStringForbidden forbids CBOR byte string being decoded as Go map key.
423-
// Attempting to decode CBOR byte string as map key into empty interface value
423+
// Attempting to decode CBOR byte string as map key into a value of type any
424424
// returns a decoding error.
425425
MapKeyByteStringForbidden
426426

@@ -489,16 +489,16 @@ func (fnmm FieldNameMatchingMode) valid() bool {
489489
return fnmm >= 0 && fnmm < maxFieldNameMatchingMode
490490
}
491491

492-
// BigIntDecMode specifies how to decode CBOR bignum to Go interface{}.
492+
// BigIntDecMode specifies how to decode CBOR bignum to a value of type any.
493493
type BigIntDecMode int
494494

495495
const (
496496
// BigIntDecodeValue makes CBOR bignum decode to big.Int (instead of *big.Int)
497-
// when unmarshaling into a Go interface{}.
497+
// when unmarshaling into a value of type any.
498498
BigIntDecodeValue BigIntDecMode = iota
499499

500500
// BigIntDecodePointer makes CBOR bignum decode to *big.Int when
501-
// unmarshaling into a Go interface{}.
501+
// unmarshaling into a value of type any.
502502
BigIntDecodePointer
503503

504504
maxBigIntDecMode
@@ -549,17 +549,17 @@ func (fnbsm FieldNameByteStringMode) valid() bool {
549549
return fnbsm >= 0 && fnbsm < maxFieldNameByteStringMode
550550
}
551551

552-
// UnrecognizedTagToAnyMode specifies how to decode unrecognized CBOR tag into an empty interface (any).
552+
// UnrecognizedTagToAnyMode specifies how to decode unrecognized CBOR tag into a value of type any.
553553
// Currently, recognized CBOR tag numbers are 0, 1, 2, 3, or registered by TagSet.
554554
type UnrecognizedTagToAnyMode int
555555

556556
const (
557557
// UnrecognizedTagNumAndContentToAny decodes CBOR tag number and tag content to cbor.Tag
558-
// when decoding unrecognized CBOR tag into an empty interface.
558+
// when decoding unrecognized CBOR tag into a value of type any.
559559
UnrecognizedTagNumAndContentToAny UnrecognizedTagToAnyMode = iota
560560

561561
// UnrecognizedTagContentToAny decodes only CBOR tag content (into its default type)
562-
// when decoding unrecognized CBOR tag into an empty interface.
562+
// when decoding unrecognized CBOR tag into a value of type any.
563563
UnrecognizedTagContentToAny
564564

565565
maxUnrecognizedTagToAny
@@ -569,21 +569,21 @@ func (uttam UnrecognizedTagToAnyMode) valid() bool {
569569
return uttam >= 0 && uttam < maxUnrecognizedTagToAny
570570
}
571571

572-
// TimeTagToAnyMode specifies how to decode CBOR tag 0 and 1 into an empty interface (any).
572+
// TimeTagToAnyMode specifies how to decode CBOR tag 0 and 1 into a value of type any.
573573
// Based on the specified mode, Unmarshal can return a time.Time value or a time string in a specific format.
574574
type TimeTagToAnyMode int
575575

576576
const (
577577
// TimeTagToTime decodes CBOR tag 0 and 1 into a time.Time value
578-
// when decoding tag 0 or 1 into an empty interface.
578+
// when decoding tag 0 or 1 into a value of type any.
579579
TimeTagToTime TimeTagToAnyMode = iota
580580

581581
// TimeTagToRFC3339 decodes CBOR tag 0 and 1 into a time string in RFC3339 format
582-
// when decoding tag 0 or 1 into an empty interface.
582+
// when decoding tag 0 or 1 into a value of type any.
583583
TimeTagToRFC3339
584584

585585
// TimeTagToRFC3339Nano decodes CBOR tag 0 and 1 into a time string in RFC3339Nano format
586-
// when decoding tag 0 or 1 into an empty interface.
586+
// when decoding tag 0 or 1 into a value of type any.
587587
TimeTagToRFC3339Nano
588588

589589
maxTimeTagToAnyMode
@@ -818,11 +818,11 @@ type DecOptions struct {
818818
TagsMd TagsMode
819819

820820
// IntDec specifies which Go integer type (int64, uint64, or [big.Int]) to use
821-
// when decoding CBOR int (major type 0 and 1) to Go interface{}.
821+
// when decoding CBOR int (major type 0 and 1) to a value of type any.
822822
IntDec IntDecMode
823823

824824
// MapKeyByteString specifies how to decode CBOR byte string as map key
825-
// when decoding CBOR map with byte string key into an empty interface value.
825+
// when decoding CBOR map with byte string key into a value of type any.
826826
// By default, an error is returned when attempting to decode CBOR byte string
827827
// as map key because Go doesn't allow []byte as map key.
828828
MapKeyByteString MapKeyByteStringMode
@@ -831,8 +831,8 @@ type DecOptions struct {
831831
ExtraReturnErrors ExtraDecErrorCond
832832

833833
// DefaultMapType specifies Go map type to create and decode to
834-
// when unmarshaling CBOR into an empty interface value.
835-
// By default, unmarshal uses map[interface{}]interface{}.
834+
// when unmarshaling CBOR into a value of type any.
835+
// By default, unmarshal uses map[any]any.
836836
DefaultMapType reflect.Type
837837

838838
// UTF8 specifies if decoder should decode CBOR Text containing invalid UTF-8.
@@ -842,11 +842,11 @@ type DecOptions struct {
842842
// FieldNameMatching specifies how string keys in CBOR maps are matched to Go struct field names.
843843
FieldNameMatching FieldNameMatchingMode
844844

845-
// BigIntDec specifies how to decode CBOR bignum to Go interface{}.
845+
// BigIntDec specifies how to decode CBOR bignum to a value of type any.
846846
BigIntDec BigIntDecMode
847847

848848
// DefaultByteStringType is the Go type that should be produced when decoding a CBOR byte
849-
// string into an empty interface value. Types to which a []byte is convertible are valid
849+
// string into a value of type any. Types to which a []byte is convertible are valid
850850
// for this option, except for array and pointer-to-array types. If nil, the default is
851851
// []byte.
852852
DefaultByteStringType reflect.Type
@@ -858,11 +858,11 @@ type DecOptions struct {
858858
// Go struct field name.
859859
FieldNameByteString FieldNameByteStringMode
860860

861-
// UnrecognizedTagToAny specifies how to decode unrecognized CBOR tag into an empty interface.
861+
// UnrecognizedTagToAny specifies how to decode unrecognized CBOR tag into a value of type any.
862862
// Currently, recognized CBOR tag numbers are 0, 1, 2, 3, or registered by TagSet.
863863
UnrecognizedTagToAny UnrecognizedTagToAnyMode
864864

865-
// TimeTagToAny specifies how to decode CBOR tag 0 and 1 into an empty interface (any).
865+
// TimeTagToAny specifies how to decode CBOR tag 0 and 1 into a value of type any.
866866
// Based on the specified mode, Unmarshal can return a time.Time value or a time string in a specific format.
867867
TimeTagToAny TimeTagToAnyMode
868868

@@ -2486,7 +2486,7 @@ func (d *decoder) parseMapToMap(v reflect.Value, tInfo *typeInfo) error { //noli
24862486
keyType, eleType := tInfo.keyTypeInfo.typ, tInfo.elemTypeInfo.typ
24872487
reuseKey, reuseEle := isImmutableKind(tInfo.keyTypeInfo.kind), isImmutableKind(tInfo.elemTypeInfo.kind)
24882488
var keyValue, eleValue reflect.Value
2489-
keyIsInterfaceType := keyType == typeIntf // If key type is interface{}, need to check if key value is hashable.
2489+
keyIsInterfaceType := keyType == typeIntf // If key type is any, need to check if key value is hashable.
24902490
var err, lastErr error
24912491
keyCount := v.Len()
24922492
var existingKeys map[any]bool // Store existing map keys, used for detecting duplicate map key.

0 commit comments

Comments
 (0)