Skip to content

Instantly share code, notes, and snippets.

@lmammino
Last active October 8, 2024 11:51
Show Gist options
  • Save lmammino/a4dc917f66c6c10e415250304a4a692f to your computer and use it in GitHub Desktop.
Save lmammino/a4dc917f66c6c10e415250304a4a692f to your computer and use it in GitHub Desktop.
Ldap filter beautifier
package main
import (
"fmt"
"./ldap"
)
var filter = "(&(&(&(& (mailnickname=*) (| (&(objectCategory=person)(objectClass=user)(|(homeMDB=*)(msExchHomeServerName=*))) )))(objectCategory=user)(department=IT)(physicalDeliveryOfficeName=Amsterdam)))"
func main() {
fmt.Printf(ldap.FormatFilter(filter))
}
package ldap
import (
"strings"
)
// inspired by https://blogs.technet.microsoft.com/389thoughts/2016/11/24/ldap-query-prettyfier/
func FormatFilter(filter string) string {
buffer := new(strings.Builder)
noSpaces := strings.Replace(filter, " ", "", -1)
noSpaces = strings.Replace(noSpaces, "\n", "", -1)
tabLevel := 0
tabString := " "
inline := false
for _, char := range noSpaces {
switch char {
case '(':
buffer.WriteString("\n")
buffer.WriteString(strings.Repeat(tabString, tabLevel))
buffer.WriteString("(")
tabLevel = tabLevel + 1
inline = false
case ')':
tabLevel = tabLevel - 1
if !inline {
buffer.WriteString(strings.Repeat(tabString, tabLevel))
}
buffer.WriteString(")\n")
inline = false
default:
inline = true
buffer.WriteRune(char)
}
}
return buffer.String()
}
@saltlakeryan
Copy link

Thanks for creating this! In case anyone comes across this and needs something similar in PHP like I did, here's another port:

function prettify($filter)
  {

    $buffer = "";

    $noSpaces = str_replace(" ", "", $filter);
    $noSpaces = str_replace("\n", "", $noSpaces);

    $tabLevel = 0;
    $tabString = "  ";
    $inline = false;
    $chars = str_split($noSpaces);
    foreach ($chars as $char) {
      switch ($char) {
        case '(':
          $buffer .= "\n";
          $buffer .= str_repeat($tabString, $tabLevel);
          $buffer .= "(";
          $tabLevel++;
          $inline = false;
          break;
        case ')':
          $tabLevel--;
          if (!$inline) {
            $buffer .= str_repeat($tabString, $tabLevel);
          }
          $buffer .= ")\n";
          $inline = false;
          break;
        default:
          $inline = true;
          $buffer .= $char;
      }
    }

    $buffer = preg_replace('/^[ \t]*[\r\n]+/m', '', $buffer);
    return $buffer;
  }

@jrwarwick
Copy link

jrwarwick commented May 2, 2022

I was looking for PowerShell in the first place, but the original inspiration cited has disappeared from MS site. So here is a re-re-write in PowerShell:

# inspired by https://blogs.technet.microsoft.com/389thoughts/2016/11/24/ldap-query-prettyfier/
function Format-LDAPFilter($ldapFilterString) {
	$tabLevel = 0
	$indentationString = "  " #you can do a single tab, two spaces, four spaces, etc.
	$inline = $false
	$ldapFilterStringRewritten = ""
	#Eliminate whitespaces first.
	$ldapFilterString = $ldapFilterString -replace "\s+",""

	for ($i = 0; $i -lt $ldapFilterString.length; $i++) {
		switch ($ldapFilterString[$i]) {
			'('{
				$ldapFilterStringRewritten += "`n"
				$ldapFilterStringRewritten += $indentationString * $tabLevel
				$ldapFilterStringRewritten += "("
				$tabLevel += 1
				$inline = $false
			}
			')'{
				$tabLevel -= 1
				if (-NOT $inline) {
					$ldapFilterStringRewritten += $indentationString * $tabLevel
				}
				$ldapFilterStringRewritten += ")" #`n
				if (($ldapFilterString.length - $i) -gt 1 -AND $ldapFilterString[$i+1] -ne '(') {
					$ldapFilterStringRewritten += "`n"
				}                
				$inline = $false
			}
			default{
				$inline = $true
				$ldapFilterStringRewritten += $ldapFilterString[$i]
			}
		}
	}

	$ldapFilterStringRewritten  #implicit return
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment