Skip to content

Commit

Permalink
add GetEqualFold(Raw|)AttributeValue(s|)
Browse files Browse the repository at this point in the history
these are the case insensitive counter parts, e.g.
  GetAttributeValue <=> GetEqualFoldAttributeValue
using strings.EqualFold to compare the attrribute names

fixes go-ldap#109 go-ldap#129
  • Loading branch information
vetinari committed Jun 7, 2018
1 parent 99171d7 commit 9c998c4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
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"},
"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")
}
}

0 comments on commit 9c998c4

Please sign in to comment.