Skip to content

Commit cdaed89

Browse files
Chris. Pjohnweldon
andauthored
Refactor search attribute extraction (#321)
Co-authored-by: John Weldon <[email protected]>
1 parent 441148e commit cdaed89

File tree

2 files changed

+54
-20
lines changed

2 files changed

+54
-20
lines changed

search.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -376,16 +376,9 @@ func (l *Conn) Search(searchRequest *SearchRequest) (*SearchResult, error) {
376376

377377
switch packet.Children[1].Tag {
378378
case 4:
379-
entry := new(Entry)
380-
entry.DN = packet.Children[1].Children[0].Value.(string)
381-
for _, child := range packet.Children[1].Children[1].Children {
382-
attr := new(EntryAttribute)
383-
attr.Name = child.Children[0].Value.(string)
384-
for _, value := range child.Children[1].Children {
385-
attr.Values = append(attr.Values, value.Value.(string))
386-
attr.ByteValues = append(attr.ByteValues, value.ByteValue)
387-
}
388-
entry.Attributes = append(entry.Attributes, attr)
379+
entry := &Entry{
380+
DN: packet.Children[1].Children[0].Value.(string),
381+
Attributes: unpackAttributes(packet.Children[1].Children[1].Children),
389382
}
390383
result.Entries = append(result.Entries, entry)
391384
case 5:
@@ -408,3 +401,27 @@ func (l *Conn) Search(searchRequest *SearchRequest) (*SearchResult, error) {
408401
}
409402
}
410403
}
404+
405+
// unpackAttributes will extract all given LDAP attributes and it's values
406+
// from the ber.Packet
407+
func unpackAttributes(children []*ber.Packet) []*EntryAttribute {
408+
entries := make([]*EntryAttribute, len(children))
409+
for i, child := range children {
410+
length := len(child.Children[1].Children)
411+
entry := &EntryAttribute{
412+
Name: child.Children[0].Value.(string),
413+
// pre-allocate the slice since we can determine
414+
// the number of attributes at this point
415+
Values: make([]string, length),
416+
ByteValues: make([][]byte, length),
417+
}
418+
419+
for i, value := range child.Children[1].Children {
420+
entry.ByteValues[i] = value.ByteValue
421+
entry.Values[i] = value.Value.(string)
422+
}
423+
entries[i] = entry
424+
}
425+
426+
return entries
427+
}

v3/search.go

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -376,16 +376,9 @@ func (l *Conn) Search(searchRequest *SearchRequest) (*SearchResult, error) {
376376

377377
switch packet.Children[1].Tag {
378378
case 4:
379-
entry := new(Entry)
380-
entry.DN = packet.Children[1].Children[0].Value.(string)
381-
for _, child := range packet.Children[1].Children[1].Children {
382-
attr := new(EntryAttribute)
383-
attr.Name = child.Children[0].Value.(string)
384-
for _, value := range child.Children[1].Children {
385-
attr.Values = append(attr.Values, value.Value.(string))
386-
attr.ByteValues = append(attr.ByteValues, value.ByteValue)
387-
}
388-
entry.Attributes = append(entry.Attributes, attr)
379+
entry := &Entry{
380+
DN: packet.Children[1].Children[0].Value.(string),
381+
Attributes: unpackAttributes(packet.Children[1].Children[1].Children),
389382
}
390383
result.Entries = append(result.Entries, entry)
391384
case 5:
@@ -408,3 +401,27 @@ func (l *Conn) Search(searchRequest *SearchRequest) (*SearchResult, error) {
408401
}
409402
}
410403
}
404+
405+
// unpackAttributes will extract all given LDAP attributes and it's values
406+
// from the ber.Packet
407+
func unpackAttributes(children []*ber.Packet) []*EntryAttribute {
408+
entries := make([]*EntryAttribute, len(children))
409+
for i, child := range children {
410+
length := len(child.Children[1].Children)
411+
entry := &EntryAttribute{
412+
Name: child.Children[0].Value.(string),
413+
// pre-allocate the slice since we can determine
414+
// the number of attributes at this point
415+
Values: make([]string, length),
416+
ByteValues: make([][]byte, length),
417+
}
418+
419+
for i, value := range child.Children[1].Children {
420+
entry.ByteValues[i] = value.ByteValue
421+
entry.Values[i] = value.Value.(string)
422+
}
423+
entries[i] = entry
424+
}
425+
426+
return entries
427+
}

0 commit comments

Comments
 (0)