@@ -376,16 +376,9 @@ func (l *Conn) Search(searchRequest *SearchRequest) (*SearchResult, error) {
376
376
377
377
switch packet .Children [1 ].Tag {
378
378
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 ),
389
382
}
390
383
result .Entries = append (result .Entries , entry )
391
384
case 5 :
@@ -408,3 +401,27 @@ func (l *Conn) Search(searchRequest *SearchRequest) (*SearchResult, error) {
408
401
}
409
402
}
410
403
}
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