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
9 changes: 9 additions & 0 deletions pkg/tag/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,12 @@
}
return Tag{Group: uint16(group), Element: uint16(elem)}, nil
}

// RegisterCustom allows to add a custom tag. This allows to work arond missing tag definitions
// and to create private tags.
flicaflow marked this conversation as resolved.
Show resolved Hide resolved
// If the tag already exists it will be overridden.
func RegisterCustom(info Info) {
maybeInitTagDict()

tagDict[info.Tag] = info
}
64 changes: 64 additions & 0 deletions pkg/tag/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,70 @@ func TestSplitTag(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) {
// 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
TagTestTag := Tag{Group: 0x0063, Element: 0x0020}
RegisterCustom(Info{
Tag: TagTestTag,
VR: "UT",
Name: "TestTag",
VM: "1",
})

// Then the tag is now part of the tag collection
_, err = FindByName("TestTag")
if err != nil {
t.Fatalf("expected TestTag to be accessible with FindByName")
}
info, err := Find(TagTestTag)
if err != nil {
t.Fatalf("expected TestTag to be accessible with Find")
flicaflow marked this conversation as resolved.
Show resolved Hide resolved
}
if info.VR != "UT" ||
info.Name != "TestTag" ||
info.VM != "1" {
t.Fatal("info of new registered tag is wrong")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: prefer t.Errorf here and above to collect all test errors. Typically I use t.Errorf for test failure conditions, and Fatalf for test setup issues where the test needs to stop immediately.

}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you should be able to use cmp.Diff to check the info exactly matches the struct you created above: https://pkg.go.dev/github.com/google/go-cmp/cmp#Diff

You could optionally check that both FindByName and Find returns the correct struct.

})
t.Run("override existing tag", func(t *testing.T) {
// Given a tag already exists
TagTestTag := Tag{Group: 0x0010, Element: 0x0010} // this is the PatientName tag
info, err := Find(TagTestTag)
if err != nil {
t.Fatalf("expected TestTag to be accessible with Find")
}
if info.VR != "PN" {
t.Fatal("expected PatientName VR is originally PN")
}

// When the tag is registered with different content
RegisterCustom(Info{
Tag: TagTestTag,
VR: "LO", // originally this is PN
Name: "PatientName",
VM: "1",
})

// Then the tag information is overridden
info, err = Find(TagTestTag)
if err != nil {
t.Fatalf("expected TestTag to be accessible with Find")
}
if info.VR != "LO" {
t.Fatal("expected the VR to have changed to LO")
}
})

}

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