Commits

Taylor Venable committed 9a57a2d

Implement attribute removal transformation.

Comments (0)

Files changed (2)

src/main/scala/net/metasyntax/ldif/transform/EntryTransformer.scala

 import _root_.net.metasyntax.ldif.ast._
 import _root_.net.metasyntax.ldif.util._
 
-class Parted(val preserve: List[LdifAttribute], val replace: List[LdifAttribute]) { }
+private class Parted(val preserve: List[LdifAttribute], val replace: List[LdifAttribute]) { }
 
 /** A combinator approach to turning one entry into another. */
 
   }
 
   def removeAttrs: AttrRemover = {
-    new AttrRemover
+    new AttrRemover((e: LdifEntry) => new Parted(e.attrList, List()))
   }
 }
 
   }
 }
 
-class AttrRemover extends EntryTransformer {
+class AttrRemover(filter: (LdifEntry => Parted)) extends EntryTransformer {
   def apply(e: LdifEntry): LdifEntry = {
-    e
+    val p: Parted = filter(e)
+    new LdifEntry(e.dn, p.preserve)
+  }
+
+  /** Select the attribute to replace by name. This selector filter will be
+   combined with other attribute name selector filters in an 'or' fashion. */
+
+  def withName(attr: String) = {
+    new AttrRemover(filter chainWith {
+      (p: Parted) => {
+        p.preserve.partition((a) => a.name == attr) match {
+          case (toReplace, toPreserve) => new Parted(toPreserve, p.replace ++ toReplace)
+        }
+      }
+    })
+  }
+
+  def withNames(attrs: String*) = {
+    new AttrRemover(filter)
+  }
+
+  /** Filters out the attributes to be replaced by checking to make sure that
+   they have a certain value. */
+
+  def withValue(value: String) = {
+    new AttrRemover(filter chainWith {
+      (p: Parted) => {
+        p.replace.partition(
+          (a) => a.value match {
+            case (v: LdifStringValue) => v == value
+            case _ => false
+          }
+        ) match {
+          case (toReplace, toPreserve) => new Parted(p.preserve ++ toPreserve, toReplace)
+        }
+      }
+    })
+  }
+
+  /** Replace attributes with values that match the specified regular
+   expression. */
+
+  def withValue(valueRegex: Regex) = {
+    new AttrRemover(filter chainWith {
+      (p: Parted) => {
+        p.replace.partition(
+          (a) => a.value match {
+            case (v: LdifStringValue) => valueRegex.findFirstMatchIn(v.data) != None
+            case _ => false
+          }
+        ) match {
+          case (toReplace, toPreserve) => new Parted(p.preserve ++ toPreserve, toReplace)
+        }
+      }
+    })
   }
 }

src/test/scala/net/metasyntax/ldif/test/EntryTransformerSpec.scala

         case (version, entries) => {
           val e = entries(0)
 
-          "do transformations by attribute name" in {
+          "do replacements by attribute name" in {
             val t = (EntryTransformer replaceAttrs) withName "password" using "CENSORED"
             val newEntry = t(e)
             assert(newEntry("password").length === 1)
             assert(newEntry("password")(0) === "CENSORED")
           }
 
-          "do transformations by attribute name and value" in {
+          "do replacements by attribute name and value" in {
             val t = (EntryTransformer replaceAttrs) withName "groupMembership" withValue "Secret_Alpha" using "CENSORED"
             val newEntry = t(e)
             assert(newEntry("groupMembership")(0).value === "Secret_Beta")
             assert(newEntry("groupMembership")(3).value === "CENSORED")
           }
 
-          "do transformations by attribute name and value regex" in {
+          "do replacements by attribute name and value regex" in {
             val secretGroup = "Secret_.*".r
             val t = (EntryTransformer replaceAttrs) withName "groupMembership" withValue secretGroup using "CENSORED"
             val newEntry = t(e)
-            newEntry.print(java.lang.System.out)
             assert(newEntry("groupMembership")(0).value === "Normal_Folks")
             assert(newEntry("groupMembership")(1).value === "Awesome_Folks")
             assert(newEntry("groupMembership")(2).value === "CENSORED")
             assert(newEntry("groupMembership")(3).value === "CENSORED")
           }
+
+          "do removals by attribute name" in {
+            val t = (EntryTransformer removeAttrs) withName "password"
+            val newEntry = t(e)
+            assert(newEntry("password").length === 0)
+          }
+
+          "do removals by attribute name and value" in {
+            val t = (EntryTransformer removeAttrs) withName "groupMembership" withValue "Secret_Alpha"
+            val newEntry = t(e)
+            assert(newEntry("groupMembership").length === 3)
+            assert(newEntry("groupMembership")(0).value === "Secret_Beta")
+            assert(newEntry("groupMembership")(1).value === "Normal_Folks")
+            assert(newEntry("groupMembership")(2).value === "Awesome_Folks")
+          }
+
+          "do removals by attribute name and value regex" in {
+            val secretGroup = "Secret_.*".r
+            val t = (EntryTransformer removeAttrs) withName "groupMembership" withValue secretGroup
+            val newEntry = t(e)
+            assert(newEntry("groupMembership").length === 2)
+            assert(newEntry("groupMembership")(0).value === "Normal_Folks")
+            assert(newEntry("groupMembership")(1).value === "Awesome_Folks")
+          }
         }
       }
     }