Skip to content

Commit

Permalink
Merge branch 'main' into s/pixeldata-rep
Browse files Browse the repository at this point in the history
  • Loading branch information
suyashkumar committed Aug 8, 2024
2 parents eb8fb2b + b00166e commit 680f125
Show file tree
Hide file tree
Showing 29 changed files with 8,798 additions and 9,164 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/bench_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ jobs:
env:
GOBIN: /home/runner/go
steps:
- name: Set up Go 1.18
- name: Set up Go 1.22
uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.22
id: go

- name: Check out code
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/bench_push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ jobs:
env:
GOBIN: /home/runner/go
steps:
- name: Set up Go 1.18
- name: Set up Go 1.22
uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.22
id: go

- name: Check out code
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ jobs:
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.18
- name: Set up Go 1.22
uses: actions/setup-go@v3
with:
go-version: 1.18
go-version: 1.22
id: go

- name: Check out code
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
BINARY = dicomutil
VERSION = `git describe --tags --always`

.PHONY: codegen
codegen:
- go generate -x ./...
- gofmt -s -w ./pkg/tag

.PHONY: build
build:
go mod download
Expand Down
6 changes: 3 additions & 3 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 Expand Up @@ -175,7 +175,7 @@ func (d *Dataset) String() string {
tabs := buildTabs(elem.l)
var tagName string
if tagInfo, err := tag.Find(elem.e.Tag); err == nil {
tagName = tagInfo.Name
tagName = tagInfo.Keyword
}

b.WriteString(fmt.Sprintf("%s[\n", tabs))
Expand Down
17 changes: 17 additions & 0 deletions dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@ func makeSequenceElement(tg tag.Tag, items [][]*Element) *Element {
}
}

func makeUNSequenceElement(tg tag.Tag, items [][]*Element) *Element {
sequenceItems := make([]*SequenceItemValue, 0, len(items))
for _, item := range items {
sequenceItems = append(sequenceItems, &SequenceItemValue{elements: item})
}

return &Element{
Tag: tg,
ValueRepresentation: tag.VRUnknown,
RawValueRepresentation: "UN",
Value: &sequencesValue{
value: sequenceItems,
},
ValueLength: tag.VLUndefinedLength,
}
}

func TestDataset_FindElementByTag(t *testing.T) {
data := Dataset{
Elements: []*Element{
Expand Down
62 changes: 32 additions & 30 deletions element.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (e *Element) Equals(target *Element) bool {
func (e *Element) String() string {
var tagName string
if tagInfo, err := tag.Find(e.Tag); err == nil {
tagName = tagInfo.Name
tagName = tagInfo.Keyword
}
return fmt.Sprintf("[\n Tag: %s\n Tag Name: %s\n VR: %s\n VR Raw: %s\n VL: %d\n Value: %s\n]\n\n",
e.Tag.String(),
Expand Down 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,13 +142,15 @@ 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
}
rawVR := tagInfo.VR

rawVR := tagInfo.VRs[0]
if t == tag.PixelData {
rawVR = "OW"
}
value, err := NewValue(data)
if err != nil {
return nil, err
Expand All @@ -162,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 @@ -215,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 @@ -240,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 @@ -271,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 @@ -302,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 @@ -343,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 @@ -377,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 +442,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 +532,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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/suyashkumar/dicom

go 1.18
go 1.22

require (
github.com/google/go-cmp v0.6.0
Expand Down
Loading

0 comments on commit 680f125

Please sign in to comment.