Skip to content

Commit

Permalink
Improve some errors, rename interface{} to any
Browse files Browse the repository at this point in the history
  • Loading branch information
suyashkumar committed Jun 16, 2024
1 parent b8bf692 commit e9f813a
Show file tree
Hide file tree
Showing 9 changed files with 106 additions and 100 deletions.
4 changes: 2 additions & 2 deletions dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (d *Dataset) FindElementByTag(tag tag.Tag) (*Element, error) {
return e, nil
}
}
return nil, ErrorElementNotFound
return nil, fmt.Errorf("unable to find %v element: %w", tag, ErrorElementNotFound)
}

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

// FlatIterator will be deprecated soon in favor of
Expand Down
54 changes: 27 additions & 27 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ type Value interface {
ValueType() ValueType
// GetValue returns the underlying value that this Value holds. What type is returned here can be determined exactly
// from the ValueType() of this Value (see the ValueType godoc).
GetValue() interface{} // TODO: rename to Get to read cleaner
GetValue() any // TODO: rename to Get to read cleaner
String() string
MarshalJSON() ([]byte, error)
// Equals returns true if this value equals the input Value.
Expand All @@ -107,7 +107,7 @@ type Value interface {
// Acceptable types: []int, []string, []byte, []float64, PixelDataInfo,
// [][]*Element (represents a sequence, which contains several
// items which each contain several elements).
func NewValue(data interface{}) (Value, error) {
func NewValue(data any) (Value, error) {
switch data.(type) {
case []int:
return &intsValue{value: data.([]int)}, nil
Expand All @@ -127,11 +127,11 @@ func NewValue(data interface{}) (Value, error) {
}
return &sequencesValue{value: sequenceItems}, nil
default:
return nil, ErrorUnexpectedDataType
return nil, fmt.Errorf("NewValue: unexpected data type %T: %w", data, ErrorUnexpectedDataType)
}
}

func mustNewValue(data interface{}) Value {
func mustNewValue(data any) Value {
v, err := NewValue(data)
if err != nil {
panic(err)
Expand All @@ -142,7 +142,7 @@ func mustNewValue(data interface{}) Value {
// NewElement creates a new DICOM Element with the supplied tag and with a value
// built from the provided data. The data can be one of the types that is
// acceptable to NewValue.
func NewElement(t tag.Tag, data interface{}) (*Element, error) {
func NewElement(t tag.Tag, data any) (*Element, error) {
tagInfo, err := tag.Find(t)
if err != nil {
return nil, err
Expand All @@ -164,15 +164,15 @@ func NewElement(t tag.Tag, data interface{}) (*Element, error) {
}, nil
}

func mustNewElement(t tag.Tag, data interface{}) *Element {
func mustNewElement(t tag.Tag, data any) *Element {
elem, err := NewElement(t, data)
if err != nil {
log.Panic(err)
}
return elem
}

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

func (b *bytesValue) isElementValue() {}
func (b *bytesValue) ValueType() ValueType { return Bytes }
func (b *bytesValue) GetValue() interface{} { return b.value }
func (b *bytesValue) isElementValue() {}
func (b *bytesValue) ValueType() ValueType { return Bytes }
func (b *bytesValue) GetValue() any { return b.value }
func (b *bytesValue) String() string {
return fmt.Sprintf("%v", b.value)
}
Expand All @@ -242,9 +242,9 @@ type stringsValue struct {
value []string
}

func (s *stringsValue) isElementValue() {}
func (s *stringsValue) ValueType() ValueType { return Strings }
func (s *stringsValue) GetValue() interface{} { return s.value }
func (s *stringsValue) isElementValue() {}
func (s *stringsValue) ValueType() ValueType { return Strings }
func (s *stringsValue) GetValue() any { return s.value }
func (s *stringsValue) String() string {
return fmt.Sprintf("%v", s.value)
}
Expand Down Expand Up @@ -273,9 +273,9 @@ type intsValue struct {
value []int
}

func (i *intsValue) isElementValue() {}
func (i *intsValue) ValueType() ValueType { return Ints }
func (i *intsValue) GetValue() interface{} { return i.value }
func (i *intsValue) isElementValue() {}
func (i *intsValue) ValueType() ValueType { return Ints }
func (i *intsValue) GetValue() any { return i.value }
func (i *intsValue) String() string {
return fmt.Sprintf("%v", i.value)
}
Expand Down Expand Up @@ -304,9 +304,9 @@ type floatsValue struct {
value []float64
}

func (f *floatsValue) isElementValue() {}
func (f *floatsValue) ValueType() ValueType { return Floats }
func (f *floatsValue) GetValue() interface{} { return f.value }
func (f *floatsValue) isElementValue() {}
func (f *floatsValue) ValueType() ValueType { return Floats }
func (f *floatsValue) GetValue() any { return f.value }
func (f *floatsValue) String() string {
return fmt.Sprintf("%v", f.value)
}
Expand Down Expand Up @@ -345,7 +345,7 @@ func (s *SequenceItemValue) ValueType() ValueType { return SequenceItem }
// GetValue returns the underlying value that this Value holds. What type is
// returned here can be determined exactly from the ValueType() of this Value
// (see the ValueType godoc).
func (s *SequenceItemValue) GetValue() interface{} { return s.elements }
func (s *SequenceItemValue) GetValue() any { return s.elements }

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

func (s *sequencesValue) isElementValue() {}
func (s *sequencesValue) ValueType() ValueType { return Sequences }
func (s *sequencesValue) GetValue() interface{} { return s.value }
func (s *sequencesValue) isElementValue() {}
func (s *sequencesValue) ValueType() ValueType { return Sequences }
func (s *sequencesValue) GetValue() any { return s.value }
func (s *sequencesValue) String() string {
// TODO: consider adding more sophisticated formatting
return fmt.Sprintf("%+v", s.value)
Expand Down Expand Up @@ -440,9 +440,9 @@ type pixelDataValue struct {
PixelDataInfo
}

func (p *pixelDataValue) isElementValue() {}
func (p *pixelDataValue) ValueType() ValueType { return PixelData }
func (p *pixelDataValue) GetValue() interface{} { return p.PixelDataInfo }
func (p *pixelDataValue) isElementValue() {}
func (p *pixelDataValue) ValueType() ValueType { return PixelData }
func (p *pixelDataValue) GetValue() any { return p.PixelDataInfo }
func (p *pixelDataValue) String() string {
if len(p.Frames) == 0 {
return "empty pixel data"
Expand Down Expand Up @@ -530,7 +530,7 @@ func MustGetPixelDataInfo(v Value) PixelDataInfo {

// allValues is used for tests that need to pass in instances of the unexported
// value structs to cmp.AllowUnexported.
var allValues = []interface{}{
var allValues = []any{
floatsValue{},
intsValue{},
stringsValue{},
Expand Down
5 changes: 3 additions & 2 deletions element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dicom

import (
"encoding/json"
"errors"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -62,7 +63,7 @@ func TestElement_String(t *testing.T) {
func TestNewValue(t *testing.T) {
cases := []struct {
name string
data interface{}
data any
wantValue Value
wantError error
}{
Expand Down Expand Up @@ -141,7 +142,7 @@ func TestNewValue(t *testing.T) {
func TestNewValue_UnexpectedType(t *testing.T) {
data := 10
_, err := NewValue(data)
if err != ErrorUnexpectedDataType {
if !errors.Is(err, ErrorUnexpectedDataType) {
t.Errorf("NewValue(%v) expected an error. got: %v, want: %v", data, err, ErrorUnexpectedDataType)
}
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/debug/debug.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build debug
// +build debug

package debug
Expand All @@ -10,6 +11,6 @@ func Log(data string) {
}

// Logf only logs with a formatted string when the debug build flag is present.
func Logf(format string, args ...interface{}) {
func Logf(format string, args ...any) {
log.Printf(format, args...)
}
3 changes: 2 additions & 1 deletion pkg/debug/default.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build !debug
// +build !debug

package debug
Expand All @@ -6,4 +7,4 @@ package debug
func Log(data string) {}

// Logf only logs with a formatted string when the debug build flag is present.
func Logf(format string, args ...interface{}) {}
func Logf(format string, args ...any) {}
2 changes: 1 addition & 1 deletion pkg/personname/groupInfo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ func TestGroupInfo_DCM_panic(t *testing.T) {
TrailingNullLevel: 5,
}

var recovered interface{}
var recovered any

func() {
defer func() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/personname/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func TestInfo_DCM_panic(t *testing.T) {
TrailingNullLevel: 3,
}

var recovered interface{}
var recovered any

func() {
defer func() {
Expand Down
Loading

0 comments on commit e9f813a

Please sign in to comment.