diff --git a/search.go b/search.go index 8d3967da..9dbc581d 100644 --- a/search.go +++ b/search.go @@ -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 { @@ -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) @@ -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) @@ -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) diff --git a/search_test.go b/search_test.go index 5f77b22e..fca09b5f 100644 --- a/search_test.go +++ b/search_test.go @@ -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") + } +}