Detect duplicates in sdkRegistry

Bug: 195754365
Test: m nothing
Change-Id: I67c5022b7cc61891fd6b90365f8271d97d7bcd98
This commit is contained in:
Paul Duffin
2021-09-22 13:25:23 +01:00
parent f04033be81
commit 581f2e5f79
3 changed files with 73 additions and 0 deletions

View File

@@ -396,6 +396,25 @@ type sdkRegistry struct {
func (r *sdkRegistry) copyAndAppend(registerable sdkRegisterable) *sdkRegistry {
oldList := r.list
// Make sure that list does not already contain the property. Uses a simple linear search instead
// of a binary search even though the list is sorted. That is because the number of items in the
// list is small and so not worth the overhead of a binary search.
found := false
newPropertyName := registerable.SdkPropertyName()
for _, r := range oldList {
if r.SdkPropertyName() == newPropertyName {
found = true
break
}
}
if found {
names := []string{}
for _, r := range oldList {
names = append(names, r.SdkPropertyName())
}
panic(fmt.Errorf("duplicate properties found, %q already exists in %q", newPropertyName, names))
}
// Copy the slice just in case this is being read while being modified, e.g. when testing.
list := make([]sdkRegisterable, 0, len(oldList)+1)
list = append(list, oldList...)