Fix handling of empty set of values as absence of the key

This commit is contained in:
Alex 2020-01-20 09:11:30 +01:00
parent 3b793c90a0
commit a7ccdad378

29
main.go
View file

@ -246,14 +246,23 @@ func (server *Server) addElements(dn string, attrs Entry) error {
} }
for k, v := range attrs { for k, v := range attrs {
json, err := json.Marshal(v) path := prefix + "/attribute=" + k
if err != nil { if len(v) == 0 {
return err // If we have zero values, delete associated k/v pair
} _, err := server.kv.Delete(path, nil)
pair := &consul.KVPair{Key: prefix + "/attribute=" + k, Value: json} if err != nil {
_, err = server.kv.Put(pair, nil) return err
if err != nil { }
return err } else {
json, err := json.Marshal(v)
if err != nil {
return err
}
pair := &consul.KVPair{Key: path, Value: json}
_, err = server.kv.Put(pair, nil)
if err != nil {
return err
}
} }
} }
return nil return nil
@ -454,9 +463,9 @@ func applyFilter(entry Entry, filter message.Filter) (bool, error) {
} else if fPresent, ok := filter.(message.FilterPresent); ok { } else if fPresent, ok := filter.(message.FilterPresent); ok {
what := string(fPresent) what := string(fPresent)
// Case insensitive search // Case insensitive search
for desc := range entry { for desc, values := range entry {
if strings.EqualFold(what, desc) { if strings.EqualFold(what, desc) {
return true, nil return len(values) > 0, nil
} }
} }
return false, nil return false, nil