Skip to content

Commit

Permalink
tag.Add: allows users to register custom or missing tags into the tag…
Browse files Browse the repository at this point in the history
…s registry (#342)

* adds RegisterCustom function to tag package

* extends doc of tag.RegisterCustom to clarify bahavior

* add unit test for tag.RegisterCustom

* Apply suggestions from code review

change comment wording of RegisterCustom function

Co-authored-by: Suyash Kumar <suyashkumar2003@gmail.com>

* Use Errof where appriate

* use cmp.Diff

* renamed RegisterCustom to Add with additional force flag

* Apply suggestions from code review

added stylistic improvements

Co-authored-by: Suyash Kumar <suyashkumar2003@gmail.com>

* fix unti test

* Fix doc string typo

---------

Co-authored-by: Suyash Kumar <suyashkumar2003@gmail.com>
  • Loading branch information
flicaflow and suyashkumar authored Nov 29, 2024
1 parent 9a71c1f commit 37f0671
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
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 list of registered tags. This enables users to work around missing tag definitions
// 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 @@ -2,6 +2,7 @@ package tag

import (
"fmt"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
Expand Down Expand Up @@ -100,6 +101,93 @@ func TestUint32Conversion(t *testing.T) {
}
}

func TestRegisterCustom(t *testing.T) {
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("Add(TestInfo, false) = %v, want: nil", err)
}

// Then the tag is now part of the tag collection
_, err = FindByName("TestTag")
if err != nil {
t.Errorf("error when finding newly added tag (FindByName\"TestTag\"), got: %v, want: nil", err)
}
info, err := Find(testInfo.Tag)
if err != nil {
t.Fatalf("unexpected error in Find(\"TestTag\"). got err: %v, want err: nil", err)
}
if diff := cmp.Diff(testInfo, info); diff != "" {
t.Errorf("unexpected diff in retrieved tag info after Add: %v: \n", diff)
}
})
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) = %v, want: nil for unknown tag", err)
}

// 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) = %v, want: nil. Add with force = true should never return an error", err)
}

// validate that the tag has been overwritten
info, err := Find(testInfo.Tag)
if err != nil {
t.Fatalf("unexpected err on Find, got: %v, want: nil", err)
}
if info.VRs[0] != "LO" {
t.Errorf("unexpected VR on updated tag, got: %v, want: LO", info.VRs[0])
}
})
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 || !strings.Contains(err.Error(), "tag already exists") {
t.Fatalf("Add(testInfo, true) = %v, want: error(\"tag already exists\"). Add with force = false should return an error when trying to overwrite an existing tag", err)
}
})

}

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

0 comments on commit 37f0671

Please sign in to comment.