Skip to content

Commit

Permalink
added value_extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
kgoins committed Apr 12, 2021
1 parent 774f2d1 commit b0fbcde
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/audibleblink/msldapuac v0.2.0
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/icza/backscanner v0.0.0-20200205093934-2120fccb01f7
github.com/kgoins/hashset v0.1.0
github.com/magiconair/properties v1.8.4 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.3.3 // indirect
Expand All @@ -17,7 +18,7 @@ require (
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.6.1
github.com/stretchr/testify v1.7.0
go.uber.org/zap v1.10.0
golang.org/x/sys v0.0.0-20200923182605-d9f96fdee20d // indirect
gopkg.in/ini.v1 v1.61.0 // indirect
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kgoins/hashset v0.1.0 h1:fTE0PSDGSnpidJd7tT7tw/vdCCtOZ0OpTO6x53w2upg=
github.com/kgoins/hashset v0.1.0/go.mod h1:La39wQwoV2fuwcVhBbROEdyVY+/3lhlVlCb08jd+4Pc=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
Expand Down Expand Up @@ -206,8 +208,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
Expand Down
41 changes: 41 additions & 0 deletions pkg/value_extractor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ldsview

import (
hashset "github.com/kgoins/hashset/pkg"
)

func GetValues(source *LdifParser, attrName string) ([]string, error) {
filterParts := []string{attrName}
filter := BuildAttributeFilter(filterParts)
source.SetAttributeFilter(filter)

entities, done, cont := make(chan Entity), make(chan bool), make(chan bool)

valSet := hashset.NewStrHashset()
go func(ents chan Entity, done chan bool, list *hashset.StrHashset) {
for entity := range ents {
if entity.IsEmpty() {
continue
}

attr, found := entity.GetAttribute(attrName)
if !found {
continue
}

valSet.Add(attr.Value.Values()...)
}

done <- true //signals to BuildEntities we're done processing
cont <- true //signals to self we're done processing

}(entities, done, &valSet)

err := source.BuildEntities(entities, done)
if err != nil {
return nil, err
}

<-cont // wait for dnList to be populated
return valSet.Values(), nil
}
17 changes: 17 additions & 0 deletions pkg/value_extractor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ldsview

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestValueExtractor_GetValues(t *testing.T) {
parser := NewLdifParser(TESTFILE)

t.Run("parses the ldif objects correctly", func(t *testing.T) {
structure, err := GetValues(&parser, "cn")
assert.Nil(t, err)
assert.Greater(t, len(structure), 0)
})
}

0 comments on commit b0fbcde

Please sign in to comment.