Commits

Taylor Venable committed 3c39b4d

Add support for filtering based on values.

Comments (0)

Files changed (3)

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

                      replacer)
   }
 
+  /** Filters out the attributes to be replaced by checking to make sure that
+   they have a certain value. */
+
   def withValue(value: String) = {
-    new AttrReplacer(filter,
-                     replacer)
+    new AttrReplacer(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)
+        }
+      }
+    },
+    replacer)
   }
 
-  def withValue(value: Regex) = {
-    new AttrReplacer(filter,
-                     replacer)
+  def withValue(valueRegex: Regex) = {
+    new AttrReplacer(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)
+        }
+      }
+    },
+    replacer)
   }
 
   def using(newValue: String) = {

src/test/resources/transform-00.ldif

 objectclass: person
 cn: Taylor Venable
 password: I am a secret!
+groupMembership: Secret_Alpha
+groupMembership: Secret_Beta
+groupMembership: Normal_Folks
+groupMembership: Awesome_Folks

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

             assert(newEntry("password").length === 1)
             assert(newEntry("password")(0) === "CENSORED")
           }
+
+          "do transformations 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")(1).value === "Normal_Folks")
+            assert(newEntry("groupMembership")(2).value === "Awesome_Folks")
+            assert(newEntry("groupMembership")(3).value === "CENSORED")
+          }
+
+          "do transformations 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")
+          }
         }
       }
     }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.