Skip to content

Commit

Permalink
Refactored some calls
Browse files Browse the repository at this point in the history
  • Loading branch information
lkarlslund committed Sep 13, 2022
1 parent e4d7840 commit e531f82
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 54 deletions.
37 changes: 14 additions & 23 deletions modules/engine/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,40 +417,31 @@ func (o *Object) AttrString(attr Attribute) []string {
return o.Attr(attr).StringSlice()
}

func (o *Object) AttrRendered(attr Attribute) []string {
func (o *Object) AttrRendered(attr Attribute) AttributeValues {
switch attr {
case ObjectCategory:
// ocguid := o.ObjectCategoryGUID()
// if ocguid != UnknownGUID {
// if schemaobject, found := AllObjects.Find(SchemaIDGUID, AttributeValueGUID(o.ObjectCategoryGUID())); found {
// // fmt.Println(schemaobject)
// return []string{schemaobject.OneAttrString(Name)}
// }
// }

cat := o.OneAttrString(ObjectCategory)
if cat != "" {

splitted := strings.Split(cat, ",")
vals := o.Attr(attr)
if vals.Len() == 1 {
cat := vals.First()
splitted := strings.Split(cat.String(), ",")
if len(splitted) > 1 {
// This is a DN pointing to a category - otherwise it's just something we made up! :)
return []string{splitted[0][3:]}
return AttributeValueOne{
Value: AttributeValueString(splitted[0][3:]),
}
}
return []string{cat}
}

return []string{"Unknown"}
return NoValues{}
default:
return o.Attr(attr).StringSlice()
return o.Attr(attr)
}
}

func (o *Object) OneAttrRendered(attr Attribute) string {
r := o.AttrRendered(attr)
if len(r) == 0 {
if r.Len() == 0 {
return ""
}
return r[0]
return r.First().String()
}

// Returns synthetic blank attribute value if it isn't set
Expand Down Expand Up @@ -510,7 +501,7 @@ func (o *Object) OneAttrRaw(attr Attribute) interface{} {
return nil
}
if a.Len() == 1 {
return a.Slice()[0].Raw()
return a.First().Raw()
}
return nil
}
Expand All @@ -521,7 +512,7 @@ func (o *Object) OneAttr(attr Attribute) AttributeValue {
return nil
}
if a.Len() == 1 {
return a.Slice()[0]
return a.First()
}
return nil
}
Expand Down
28 changes: 14 additions & 14 deletions modules/engine/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func (os *Objects) refreshIndex(attribute Attribute, index *Index) {
// add all existing stuff to index
for _, o := range os.asarray {
for _, value := range o.Attr(attribute).Slice() {
key := attributeValueToIndex(value)
key := AttributeValueToIndex(value)

// Add to index
index.Add(key, o, false)
Expand All @@ -247,9 +247,9 @@ func (os *Objects) refreshMultiIndex(attribute, attribute2 Attribute, index *Ind
values := o.Attr(attribute).Slice()
values2 := o.Attr(attribute2).Slice()
for _, value := range values {
key := attributeValueToIndex(value)
key := AttributeValueToIndex(value)
for _, value2 := range values2 {
key2 := attributeValueToIndex(value2)
key2 := AttributeValueToIndex(value2)

// Add to index
index.Add(multiindexkey{key, key2}, o, false)
Expand Down Expand Up @@ -286,9 +286,9 @@ func (os *Objects) ReindexObject(o *Object, isnew bool) {
for i, index := range os.indexes {
attribute := Attribute(i)
if index != nil {
for _, value := range o.Attr(attribute).Slice() {
for _, value := range o.AttrRendered(attribute).Slice() {
// If it's a string, lowercase it before adding to index, we do the same on lookups
indexval := attributeValueToIndex(value)
indexval := AttributeValueToIndex(value)

unique := attribute.IsUnique()

Expand Down Expand Up @@ -319,9 +319,9 @@ func (os *Objects) ReindexObject(o *Object, isnew bool) {
values := o.Attr(attribute).Slice()
values2 := o.Attr(attribute2).Slice()
for _, value := range values {
key := attributeValueToIndex(value)
key := AttributeValueToIndex(value)
for _, value2 := range values2 {
key2 := attributeValueToIndex(value2)
key2 := AttributeValueToIndex(value2)

index.Add(multiindexkey{key, key2}, o, !isnew)
}
Expand All @@ -330,7 +330,7 @@ func (os *Objects) ReindexObject(o *Object, isnew bool) {
os.indexlock.RUnlock()
}

func attributeValueToIndex(value AttributeValue) interface{} {
func AttributeValueToIndex(value AttributeValue) interface{} {
if vs, ok := value.(AttributeValueString); ok {
return strings.ToLower(string(vs))
}
Expand Down Expand Up @@ -394,9 +394,9 @@ func (os *Objects) Merge(attrtomerge []Attribute, o *Object) bool {
for _, mergetarget := range mergetargets {
for attr, values := range o.AttributeValueMap() {
if attr.IsSingle() && mergetarget.HasAttr(attr) {
if !CompareAttributeValues(values.Slice()[0], mergetarget.Attr(attr).Slice()[0]) {
if !CompareAttributeValues(values.First(), mergetarget.Attr(attr).First()) {
// Conflicting attribute values, we can't merge these
ui.Debug().Msgf("Not merging %v into %v on %v with value '%v', as attribute %v is different", o.Label(), mergetarget.Label(), mergeattr.String(), lookfor.String(), attr.String())
ui.Debug().Msgf("Not merging %v into %v on %v with value '%v', as attribute %v is different (%v != %v)", o.Label(), mergetarget.Label(), mergeattr.String(), lookfor.String(), attr.String(), values.First().String(), mergetarget.Attr(attr).First().String())
// if attr == WhenCreated {
// ui.Debug().Msgf("Object details: %v", o.StringNoACL())
// ui.Debug().Msgf("Mergetarget details: %v", mergetarget.StringNoACL())
Expand Down Expand Up @@ -577,11 +577,11 @@ func (os *Objects) FindTwoMultiOrAdd(attribute Attribute, value AttributeValue,
if addifnotfound == nil {
if attribute2 == NonExistingAttribute {
// Lookup by one attribute
matches, found := os.GetIndex(attribute).Lookup(attributeValueToIndex(value))
matches, found := os.GetIndex(attribute).Lookup(AttributeValueToIndex(value))
return matches, found
} else {
// Lookup by two attributes
matches, found := os.GetMultiIndex(attribute, attribute2).Lookup(multiindexkey{attributeValueToIndex(value), attributeValueToIndex(value2)})
matches, found := os.GetMultiIndex(attribute, attribute2).Lookup(multiindexkey{AttributeValueToIndex(value), AttributeValueToIndex(value2)})
return matches, found
}
}
Expand All @@ -591,14 +591,14 @@ func (os *Objects) FindTwoMultiOrAdd(attribute Attribute, value AttributeValue,

if attribute2 == NonExistingAttribute {
// Lookup by one attribute
matches, found := os.GetIndex(attribute).Lookup(attributeValueToIndex(value))
matches, found := os.GetIndex(attribute).Lookup(AttributeValueToIndex(value))
if found {
os.objectmutex.Unlock()
return matches, found
}
} else {
// Lookup by two attributes
matches, found := os.GetMultiIndex(attribute, attribute2).Lookup(multiindexkey{attributeValueToIndex(value), attributeValueToIndex(value2)})
matches, found := os.GetMultiIndex(attribute, attribute2).Lookup(multiindexkey{AttributeValueToIndex(value), AttributeValueToIndex(value2)})
if found {
os.objectmutex.Unlock()
return matches, found
Expand Down
6 changes: 3 additions & 3 deletions modules/engine/processing.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func Merge(aos []*Objects) (*Objects, error) {

for mergeobject, _ := range needsmerge {
if mergeobject.HasAttr(UniqueSource) {
us := attributeValueToIndex(mergeobject.OneAttr(UniqueSource))
us := AttributeValueToIndex(mergeobject.OneAttr(UniqueSource))
if sourcemap[us] == nil {
sourcemap[us] = NewObjects()
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func Merge(aos []*Objects) (*Objects, error) {
pb.Add(1)
// Here we'll deduplicate DNs, because sometimes schema and config context slips in twice
if dn := addobject.OneAttr(DistinguishedName); dn != nil {
if existing, exists := dnindex.Lookup(attributeValueToIndex(dn)); exists {
if existing, exists := dnindex.Lookup(AttributeValueToIndex(dn)); exists {
existing[0].AbsorbEx(addobject, true)
continue
}
Expand All @@ -108,7 +108,7 @@ func Merge(aos []*Objects) (*Objects, error) {
pb.Add(1)
// Here we'll deduplicate DNs, because sometimes schema and config context slips in twice
if dn := addobject.OneAttr(DistinguishedName); dn != nil {
if existing, exists := dnindex.Lookup(attributeValueToIndex(dn)); exists {
if existing, exists := dnindex.Lookup(AttributeValueToIndex(dn)); exists {
existing[0].AbsorbEx(addobject, true)
continue
}
Expand Down
29 changes: 15 additions & 14 deletions modules/query/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,11 @@ type LowerStringAttribute engine.Attribute

func (a LowerStringAttribute) Strings(o *engine.Object) []string {
l := o.AttrRendered(engine.Attribute(a))
for i, s := range l {
l[i] = strings.ToLower(s)
lo := make([]string, l.Len())
for i, s := range l.Slice() {
lo[i] = strings.ToLower(s.String())
}
return l
return lo
}

type andquery struct {
Expand Down Expand Up @@ -497,13 +498,13 @@ type hasStringMatch struct {
}

func (hsm hasStringMatch) Evaluate(a engine.Attribute, o *engine.Object) bool {
for _, value := range o.AttrRendered(a) {
for _, value := range o.AttrRendered(a).Slice() {
if !hsm.casesensitive {
if strings.EqualFold(hsm.m, value) {
if strings.EqualFold(hsm.m, value.String()) {
return true
}
} else {
if hsm.m == value {
if hsm.m == value.String() {
return true
}
}
Expand All @@ -530,13 +531,13 @@ type hasGlobMatch struct {
}

func (hgm hasGlobMatch) Evaluate(a engine.Attribute, o *engine.Object) bool {
for _, value := range o.AttrRendered(a) {
for _, value := range o.AttrRendered(a).Slice() {
if !hgm.casesensitive {
if hgm.m.Match(strings.ToLower(value)) {
if hgm.m.Match(strings.ToLower(value.String())) {
return true
}
} else {
if hgm.m.Match(value) {
if hgm.m.Match(value.String()) {
return true
}
}
Expand All @@ -557,8 +558,8 @@ type hasRegexpMatch struct {
}

func (hrm hasRegexpMatch) Evaluate(a engine.Attribute, o *engine.Object) bool {
for _, value := range o.AttrRendered(a) {
if hrm.m.MatchString(value) {
for _, value := range o.AttrRendered(a).Slice() {
if hrm.m.MatchString(value.String()) {
return true
}
}
Expand Down Expand Up @@ -596,13 +597,13 @@ func recursiveDNmatchFunc(o *engine.Object, a engine.Attribute, dn string, maxde
return false
}
// Check all attribute values for match or ancestry
for _, value := range o.AttrRendered(a) {
for _, value := range o.AttrRendered(a).Slice() {
// We're at the end
if strings.EqualFold(value, dn) {
if strings.EqualFold(value.String(), dn) {
return true
}
// Perhaps parent matches?
if parent, found := ao.Find(activedirectory.DistinguishedName, engine.AttributeValueString(value)); found {
if parent, found := ao.Find(activedirectory.DistinguishedName, engine.AttributeValueString(value.String())); found {
return recursiveDNmatchFunc(parent, a, dn, maxdepth-1, ao)
}
}
Expand Down

0 comments on commit e531f82

Please sign in to comment.