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

make Get(Raw)AttributeValues compare case insensitive #111

Merged
merged 4 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ func (e *Entry) GetAttributeValues(attribute string) []string {
return []string{}
}

// GetEqualFoldAttributeValues returns the values for the named attribute, or an
// empty list. Attribute matching is done with strings.EqualFold.
func (e *Entry) GetEqualFoldAttributeValues(attribute string) []string {
for _, attr := range e.Attributes {
if strings.EqualFold(attribute, attr.Name) {
return attr.Values
}
}
return []string{}
}

// GetRawAttributeValues returns the byte values for the named attribute, or an empty list
func (e *Entry) GetRawAttributeValues(attribute string) [][]byte {
for _, attr := range e.Attributes {
Expand All @@ -142,6 +153,16 @@ func (e *Entry) GetRawAttributeValues(attribute string) [][]byte {
return [][]byte{}
}

// GetEqualFoldRawAttributeValues returns the byte values for the named attribute, or an empty list
func (e *Entry) GetEqualFoldRawAttributeValues(attribute string) [][]byte {
for _, attr := range e.Attributes {
if strings.EqualFold(attr.Name, attribute) {
return attr.ByteValues
}
}
return [][]byte{}
}

// GetAttributeValue returns the first value for the named attribute, or ""
func (e *Entry) GetAttributeValue(attribute string) string {
values := e.GetAttributeValues(attribute)
Expand All @@ -151,6 +172,16 @@ func (e *Entry) GetAttributeValue(attribute string) string {
return values[0]
}

// GetEqualFoldAttributeValue returns the first value for the named attribute, or "".
// Attribute comparison is done with strings.EqualFold.
func (e *Entry) GetEqualFoldAttributeValue(attribute string) string {
values := e.GetEqualFoldAttributeValues(attribute)
if len(values) == 0 {
return ""
}
return values[0]
}

// GetRawAttributeValue returns the first value for the named attribute, or an empty slice
func (e *Entry) GetRawAttributeValue(attribute string) []byte {
values := e.GetRawAttributeValues(attribute)
Expand All @@ -160,6 +191,15 @@ func (e *Entry) GetRawAttributeValue(attribute string) []byte {
return values[0]
}

// GetEqualFoldRawAttributeValue returns the first value for the named attribute, or an empty slice
func (e *Entry) GetEqualFoldRawAttributeValue(attribute string) []byte {
values := e.GetEqualFoldRawAttributeValues(attribute)
if len(values) == 0 {
return []byte{}
}
return values[0]
}

// Print outputs a human-readable description
func (e *Entry) Print() {
fmt.Printf("DN: %s\n", e.DN)
Expand Down
19 changes: 19 additions & 0 deletions search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,22 @@ func TestNewEntry(t *testing.T) {
iteration = iteration + 1
}
}

func TestGetAttributeValue(t *testing.T) {
dn := "testDN"
attributes := map[string][]string{
"Alpha": {"value"},
Copy link
Contributor

Choose a reason for hiding this comment

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

what should happen if you have {"alpha":{"a","b"}, "ALPHA": {"c", "d"}} and you search for "Alpha"?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

is that possible? isn't the attribute name case insensitive?

Copy link
Member

Choose a reason for hiding this comment

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

Shall we at least add a test case with attribute names differing only in case and document via the tests how we'd handle it? That way at least it's more obvious.

"bEta": {"value"},
"gaMma": {"value"},
"delTa": {"value"},
"epsiLon": {"value"},
}
entry := NewEntry(dn, attributes)
if entry.GetAttributeValue("Alpha") != "value" {
t.Errorf("failed to get attribute in original case")
}

if entry.GetEqualFoldAttributeValue("alpha") != "value" {
t.Errorf("failed to get attribute in changed case")
}
}