aboutsummaryrefslogtreecommitdiff
path: root/write.go
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2021-03-09 19:00:45 +0100
committerAlex Auvolat <alex@adnab.me>2021-03-09 19:00:45 +0100
commit99d8955ab3b5e24552d1f2b621744d07013f58ac (patch)
tree5820b5b594aeea4d4bb366055efef62046221119 /write.go
parent1a20a64eff34336789c0e3b41db1de727e3fe998 (diff)
downloadbottin-99d8955ab3b5e24552d1f2b621744d07013f58ac.tar.gz
bottin-99d8955ab3b5e24552d1f2b621744d07013f58ac.zip
Refactor & add case normalization logic to putAttributes
Diffstat (limited to 'write.go')
-rw-r--r--write.go36
1 files changed, 35 insertions, 1 deletions
diff --git a/write.go b/write.go
index e4c7de1..482f971 100644
--- a/write.go
+++ b/write.go
@@ -19,7 +19,31 @@ func (server *Server) putAttributes(dn string, attrs Entry) error {
return err
}
- for k, valuesNC := range attrs {
+ // Normalize attribute names: if we have several times the same attr
+ // but with different cases, put that in the same attr
+ normalized := make(Entry)
+ for k, values := range attrs {
+ found := false
+ for k2 := range normalized {
+ if strings.EqualFold(k, k2) {
+ normalized[k2] = append(normalized[k2], values...)
+ found = true
+ break
+ }
+ }
+ if !found {
+ normalized[k] = values
+ }
+ }
+
+ // Retreieve previously existing attributes, which we will use to delete
+ // entries with the wrong case
+ previous_pairs, _, err := server.kv.List(prefix + "/attribute=", &server.readOpts)
+ if err != nil {
+ return err
+ }
+
+ for k, valuesNC := range normalized {
path := prefix + "/attribute=" + k
// Trim spaces and remove empty values
@@ -31,6 +55,16 @@ func (server *Server) putAttributes(dn string, attrs Entry) error {
}
}
+ // If previously existing pairs with the wrong case exist, delete them
+ for _, prev_pair := range previous_pairs {
+ if strings.EqualFold(prev_pair.Key, path) && prev_pair.Key != path {
+ _, err := server.kv.Delete(prev_pair.Key, nil)
+ if err != nil {
+ return err
+ }
+ }
+ }
+
// If we have zero values, delete associated k/v pair
// Otherwise, write new values
if len(values) == 0 {