Skip to content

Commit

Permalink
add CaseInsensitive var
Browse files Browse the repository at this point in the history
  • Loading branch information
vetinari committed Oct 2, 2017
1 parent b9616cb commit 440fb56
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
24 changes: 20 additions & 4 deletions search.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ var DerefMap = map[int]string{
DerefAlways: "DerefAlways",
}

// CaseInsensitive sets whether attribute names in Get(Raw)AttributeValue(s) are
// compared case insensitive
var CaseInsensitive = false

// NewEntry returns an Entry object with the specified distinguished name and attribute key-value pairs.
// The map of attributes is accessed in alphabetical order of the keys in order to ensure that, for the
// same input map of attributes, the output entry will contain the same order of attributes
Expand Down Expand Up @@ -126,11 +130,18 @@ type Entry struct {
Attributes []*EntryAttribute
}

func noChanges(s string) string { return s }

// GetAttributeValues returns the values for the named attribute, or an empty list
func (e *Entry) GetAttributeValues(attribute string) []string {
attribute = strings.ToLower(attribute)
var transform func(string) string = noChanges
if CaseInsensitive {
transform = strings.ToLower
}

attribute = transform(attribute)
for _, attr := range e.Attributes {
if strings.ToLower(attr.Name) == attribute {
if transform(attr.Name) == attribute {
return attr.Values
}
}
Expand All @@ -139,9 +150,14 @@ func (e *Entry) GetAttributeValues(attribute string) []string {

// GetRawAttributeValues returns the byte values for the named attribute, or an empty list
func (e *Entry) GetRawAttributeValues(attribute string) [][]byte {
attribute = strings.ToLower(attribute)
var transform func(string) string = noChanges
if CaseInsensitive {
transform = strings.ToLower
}

attribute = transform(attribute)
for _, attr := range e.Attributes {
if strings.ToLower(attr.Name) == attribute {
if transform(attr.Name) == attribute {
return attr.ByteValues
}
}
Expand Down
2 changes: 2 additions & 0 deletions search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ func TestGetAttributeValue(t *testing.T) {
if entry.GetAttributeValue("Alpha") != "value" {
t.Errorf("failed to get attribute in original case")
}
CaseInsensitive = true
if entry.GetAttributeValue("alpha") != "value" {
t.Errorf("failed to get attribute in changed case")
}
CaseInsensitive = false
}

0 comments on commit 440fb56

Please sign in to comment.