Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tag.Add: allows users to register custom or missing tags into the tags registry #342

Merged
merged 11 commits into from
Nov 29, 2024
14 changes: 14 additions & 0 deletions pkg/tag/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package tag
//go:generate stringer -type VRKind

import (
"errors"
"fmt"
"strconv"
"strings"
Expand Down Expand Up @@ -245,3 +246,16 @@ func parseTag(tag string) (Tag, error) {
}
return Tag{Group: uint16(group), Element: uint16(elem)}, nil
}

// Add adds a custom tag to the lsit of registered tags. This enables users to work around missing tag definitions
suyashkumar marked this conversation as resolved.
Show resolved Hide resolved
// and to create private tags.
// If force == true existing tags will be overwritten.
// Otherwise an error is returned when attempting to add an already existing tag.
func Add(info Info, force bool) error {
_, found := tagDict[info.Tag]
if found && !force {
return errors.New("tag already exists")
}
tagDict[info.Tag] = info
return nil
}
88 changes: 88 additions & 0 deletions pkg/tag/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,94 @@ func TestUint32Conversion(t *testing.T) {
}
}

func TestRegisterCustom(t *testing.T) {

flicaflow marked this conversation as resolved.
Show resolved Hide resolved
t.Run("Add new tag", func(t *testing.T) {
testInfo := Info{
Tag: Tag{Group: 0x0063, Element: 0x0020},
VRs: []string{"UT"},
Name: "TestTag",
VM: "1",
}
// Given a certain tag does not exist
_, err := FindByName("TestTag")
if err == nil {
t.Fatalf("expected TestTag to not exist")
}

// When a new tag is registered
err = Add(testInfo, false)
if err != nil {
t.Fatalf("SetInfo(TestInfo, false) == <error> , expected SetInfo to return nil when a unknown tag is added")
flicaflow marked this conversation as resolved.
Show resolved Hide resolved
}

// Then the tag is now part of the tag collection
_, err = FindByName("TestTag")
if err != nil {
t.Errorf("expected TestTag to be accessible with FindByName")
flicaflow marked this conversation as resolved.
Show resolved Hide resolved
}
info, err := Find(testInfo.Tag)
if err != nil {
t.Fatalf("expected TestTag to be accessible with Find")
flicaflow marked this conversation as resolved.
Show resolved Hide resolved
}
if diff := cmp.Diff(testInfo, info); diff != "" {
t.Fatal("info of new registered tag is wrong: \n", diff)
flicaflow marked this conversation as resolved.
Show resolved Hide resolved
}
})
t.Run("force overwrite existing tag", func(t *testing.T) {
// setup a test tag
testInfo := Info{
Tag: Tag{Group: 0x0073, Element: 0x0021},
VRs: []string{"UT"},
Name: "TestTag",
VM: "1",
}
err := Add(testInfo, false)
if err != nil {
t.Fatalf("Add(testInfo, false) = error(%v), expected nil for unknown tag", err)
flicaflow marked this conversation as resolved.
Show resolved Hide resolved
}

// now change the info
testInfo.VRs = []string{"LO"}
// and try to overwrite it with force
err = Add(testInfo, true)
if err != nil {
t.Fatalf("Add(testInfo, true) = error(%v), Add with force = true should never return an error", err)
flicaflow marked this conversation as resolved.
Show resolved Hide resolved
}

// validate that the tag has been overwritten
info, err := Find(testInfo.Tag)
if err != nil {
t.Fatalf("expected added tag to be accessible with Find")
flicaflow marked this conversation as resolved.
Show resolved Hide resolved
}
if info.VRs[0] != "LO" {
t.Fatal("expected the VR to have changed to LO")
flicaflow marked this conversation as resolved.
Show resolved Hide resolved
}
})
t.Run("fail to overwrite existing tag", func(t *testing.T) {
// setup a test tag
testInfo := Info{
Tag: Tag{Group: 0x0083, Element: 0x0031},
VRs: []string{"UT"},
Name: "TestTag",
VM: "1",
}
err := Add(testInfo, false)
if err != nil {
t.Fatalf("Add(testInfo, false) = error(%v), expected nil for unknown tag", err)
}

// now change the info
testInfo.VRs = []string{"LO"}
// and try to overwrite it with force
err = Add(testInfo, false)
if err == nil {
t.Fatalf("Add(testInfo, true) = nil, Add with force = false should return an error when trying to overwrite a tag")
}
flicaflow marked this conversation as resolved.
Show resolved Hide resolved
})

}

func BenchmarkFindMetaGroupLengthTag(b *testing.B) {
for i := 0; i < b.N; i++ {
if _, err := Find(Tag{2, 0}); err != nil {
Expand Down
Loading