Skip to content

Commit b00166e

Browse files
authored
Improve error annotations in read.go, rename interface{} to any (#333)
1 parent b8bf692 commit b00166e

File tree

9 files changed

+106
-100
lines changed

9 files changed

+106
-100
lines changed

dataset.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (d *Dataset) FindElementByTag(tag tag.Tag) (*Element, error) {
3333
return e, nil
3434
}
3535
}
36-
return nil, ErrorElementNotFound
36+
return nil, fmt.Errorf("unable to find %v element: %w", tag, ErrorElementNotFound)
3737
}
3838

3939
func (d *Dataset) transferSyntax() (binary.ByteOrder, bool, error) {
@@ -57,7 +57,7 @@ func (d *Dataset) FindElementByTagNested(tag tag.Tag) (*Element, error) {
5757
return e, nil
5858
}
5959
}
60-
return nil, ErrorElementNotFound
60+
return nil, fmt.Errorf("unable to find %v element: %w", tag, ErrorElementNotFound)
6161
}
6262

6363
// FlatIterator will be deprecated soon in favor of

element.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ type Value interface {
9191
ValueType() ValueType
9292
// GetValue returns the underlying value that this Value holds. What type is returned here can be determined exactly
9393
// from the ValueType() of this Value (see the ValueType godoc).
94-
GetValue() interface{} // TODO: rename to Get to read cleaner
94+
GetValue() any // TODO: rename to Get to read cleaner
9595
String() string
9696
MarshalJSON() ([]byte, error)
9797
// Equals returns true if this value equals the input Value.
@@ -107,7 +107,7 @@ type Value interface {
107107
// Acceptable types: []int, []string, []byte, []float64, PixelDataInfo,
108108
// [][]*Element (represents a sequence, which contains several
109109
// items which each contain several elements).
110-
func NewValue(data interface{}) (Value, error) {
110+
func NewValue(data any) (Value, error) {
111111
switch data.(type) {
112112
case []int:
113113
return &intsValue{value: data.([]int)}, nil
@@ -127,11 +127,11 @@ func NewValue(data interface{}) (Value, error) {
127127
}
128128
return &sequencesValue{value: sequenceItems}, nil
129129
default:
130-
return nil, ErrorUnexpectedDataType
130+
return nil, fmt.Errorf("NewValue: unexpected data type %T: %w", data, ErrorUnexpectedDataType)
131131
}
132132
}
133133

134-
func mustNewValue(data interface{}) Value {
134+
func mustNewValue(data any) Value {
135135
v, err := NewValue(data)
136136
if err != nil {
137137
panic(err)
@@ -142,7 +142,7 @@ func mustNewValue(data interface{}) Value {
142142
// NewElement creates a new DICOM Element with the supplied tag and with a value
143143
// built from the provided data. The data can be one of the types that is
144144
// acceptable to NewValue.
145-
func NewElement(t tag.Tag, data interface{}) (*Element, error) {
145+
func NewElement(t tag.Tag, data any) (*Element, error) {
146146
tagInfo, err := tag.Find(t)
147147
if err != nil {
148148
return nil, err
@@ -164,15 +164,15 @@ func NewElement(t tag.Tag, data interface{}) (*Element, error) {
164164
}, nil
165165
}
166166

167-
func mustNewElement(t tag.Tag, data interface{}) *Element {
167+
func mustNewElement(t tag.Tag, data any) *Element {
168168
elem, err := NewElement(t, data)
169169
if err != nil {
170170
log.Panic(err)
171171
}
172172
return elem
173173
}
174174

175-
func mustNewPrivateElement(t tag.Tag, rawVR string, data interface{}) *Element {
175+
func mustNewPrivateElement(t tag.Tag, rawVR string, data any) *Element {
176176
value, err := NewValue(data)
177177
if err != nil {
178178
log.Panic(fmt.Errorf("error creating value: %w", err))
@@ -217,9 +217,9 @@ type bytesValue struct {
217217
value []byte
218218
}
219219

220-
func (b *bytesValue) isElementValue() {}
221-
func (b *bytesValue) ValueType() ValueType { return Bytes }
222-
func (b *bytesValue) GetValue() interface{} { return b.value }
220+
func (b *bytesValue) isElementValue() {}
221+
func (b *bytesValue) ValueType() ValueType { return Bytes }
222+
func (b *bytesValue) GetValue() any { return b.value }
223223
func (b *bytesValue) String() string {
224224
return fmt.Sprintf("%v", b.value)
225225
}
@@ -242,9 +242,9 @@ type stringsValue struct {
242242
value []string
243243
}
244244

245-
func (s *stringsValue) isElementValue() {}
246-
func (s *stringsValue) ValueType() ValueType { return Strings }
247-
func (s *stringsValue) GetValue() interface{} { return s.value }
245+
func (s *stringsValue) isElementValue() {}
246+
func (s *stringsValue) ValueType() ValueType { return Strings }
247+
func (s *stringsValue) GetValue() any { return s.value }
248248
func (s *stringsValue) String() string {
249249
return fmt.Sprintf("%v", s.value)
250250
}
@@ -273,9 +273,9 @@ type intsValue struct {
273273
value []int
274274
}
275275

276-
func (i *intsValue) isElementValue() {}
277-
func (i *intsValue) ValueType() ValueType { return Ints }
278-
func (i *intsValue) GetValue() interface{} { return i.value }
276+
func (i *intsValue) isElementValue() {}
277+
func (i *intsValue) ValueType() ValueType { return Ints }
278+
func (i *intsValue) GetValue() any { return i.value }
279279
func (i *intsValue) String() string {
280280
return fmt.Sprintf("%v", i.value)
281281
}
@@ -304,9 +304,9 @@ type floatsValue struct {
304304
value []float64
305305
}
306306

307-
func (f *floatsValue) isElementValue() {}
308-
func (f *floatsValue) ValueType() ValueType { return Floats }
309-
func (f *floatsValue) GetValue() interface{} { return f.value }
307+
func (f *floatsValue) isElementValue() {}
308+
func (f *floatsValue) ValueType() ValueType { return Floats }
309+
func (f *floatsValue) GetValue() any { return f.value }
310310
func (f *floatsValue) String() string {
311311
return fmt.Sprintf("%v", f.value)
312312
}
@@ -345,7 +345,7 @@ func (s *SequenceItemValue) ValueType() ValueType { return SequenceItem }
345345
// GetValue returns the underlying value that this Value holds. What type is
346346
// returned here can be determined exactly from the ValueType() of this Value
347347
// (see the ValueType godoc).
348-
func (s *SequenceItemValue) GetValue() interface{} { return s.elements }
348+
func (s *SequenceItemValue) GetValue() any { return s.elements }
349349

350350
// String is used to get a string representation of this struct.
351351
func (s *SequenceItemValue) String() string {
@@ -379,9 +379,9 @@ type sequencesValue struct {
379379
value []*SequenceItemValue
380380
}
381381

382-
func (s *sequencesValue) isElementValue() {}
383-
func (s *sequencesValue) ValueType() ValueType { return Sequences }
384-
func (s *sequencesValue) GetValue() interface{} { return s.value }
382+
func (s *sequencesValue) isElementValue() {}
383+
func (s *sequencesValue) ValueType() ValueType { return Sequences }
384+
func (s *sequencesValue) GetValue() any { return s.value }
385385
func (s *sequencesValue) String() string {
386386
// TODO: consider adding more sophisticated formatting
387387
return fmt.Sprintf("%+v", s.value)
@@ -440,9 +440,9 @@ type pixelDataValue struct {
440440
PixelDataInfo
441441
}
442442

443-
func (p *pixelDataValue) isElementValue() {}
444-
func (p *pixelDataValue) ValueType() ValueType { return PixelData }
445-
func (p *pixelDataValue) GetValue() interface{} { return p.PixelDataInfo }
443+
func (p *pixelDataValue) isElementValue() {}
444+
func (p *pixelDataValue) ValueType() ValueType { return PixelData }
445+
func (p *pixelDataValue) GetValue() any { return p.PixelDataInfo }
446446
func (p *pixelDataValue) String() string {
447447
if len(p.Frames) == 0 {
448448
return "empty pixel data"
@@ -530,7 +530,7 @@ func MustGetPixelDataInfo(v Value) PixelDataInfo {
530530

531531
// allValues is used for tests that need to pass in instances of the unexported
532532
// value structs to cmp.AllowUnexported.
533-
var allValues = []interface{}{
533+
var allValues = []any{
534534
floatsValue{},
535535
intsValue{},
536536
stringsValue{},

element_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package dicom
22

33
import (
44
"encoding/json"
5+
"errors"
56
"testing"
67

78
"github.com/google/go-cmp/cmp"
@@ -62,7 +63,7 @@ func TestElement_String(t *testing.T) {
6263
func TestNewValue(t *testing.T) {
6364
cases := []struct {
6465
name string
65-
data interface{}
66+
data any
6667
wantValue Value
6768
wantError error
6869
}{
@@ -141,7 +142,7 @@ func TestNewValue(t *testing.T) {
141142
func TestNewValue_UnexpectedType(t *testing.T) {
142143
data := 10
143144
_, err := NewValue(data)
144-
if err != ErrorUnexpectedDataType {
145+
if !errors.Is(err, ErrorUnexpectedDataType) {
145146
t.Errorf("NewValue(%v) expected an error. got: %v, want: %v", data, err, ErrorUnexpectedDataType)
146147
}
147148
}

pkg/debug/debug.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build debug
12
// +build debug
23

34
package debug
@@ -10,6 +11,6 @@ func Log(data string) {
1011
}
1112

1213
// Logf only logs with a formatted string when the debug build flag is present.
13-
func Logf(format string, args ...interface{}) {
14+
func Logf(format string, args ...any) {
1415
log.Printf(format, args...)
1516
}

pkg/debug/default.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build !debug
12
// +build !debug
23

34
package debug
@@ -6,4 +7,4 @@ package debug
67
func Log(data string) {}
78

89
// Logf only logs with a formatted string when the debug build flag is present.
9-
func Logf(format string, args ...interface{}) {}
10+
func Logf(format string, args ...any) {}

pkg/personname/groupInfo_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ func TestGroupInfo_DCM_panic(t *testing.T) {
396396
TrailingNullLevel: 5,
397397
}
398398

399-
var recovered interface{}
399+
var recovered any
400400

401401
func() {
402402
defer func() {

pkg/personname/info_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ func TestInfo_DCM_panic(t *testing.T) {
493493
TrailingNullLevel: 3,
494494
}
495495

496-
var recovered interface{}
496+
var recovered any
497497

498498
func() {
499499
defer func() {

0 commit comments

Comments
 (0)