Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | |||
cookbooks:transformations:devel [2011/10/22 10:29] 109.230.216.225 XaWgApfdrFUczaM |
cookbooks:transformations:devel [2011/11/09 12:34] (current) 193.146.38.124 old revision restored |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | Ab fab my godloy man. | + | ====== Transformations ====== |
+ | **Development Version** | ||
+ | |||
+ | < | ||
+ | Main author: | ||
+ | | ||
+ | </ | ||
+ | |||
+ | [b]Transformation[/ | ||
+ | |||
+ | Transformations are implemented by various modules, most of them being in **pv** module. | ||
+ | |||
+ | The transformations are intended to facilitate access to different attributes of PV (like strlen of value, parts of value, substrings) or complete different value of PV (encoded in hexa, md5 value, escape/ | ||
+ | |||
+ | A transformation is represented in between [b]' | ||
+ | |||
+ | < | ||
+ | # the length of From URI ($fu is PV for From URI) | ||
+ | |||
+ | $(fu{s.len}) | ||
+ | </ | ||
+ | |||
+ | Many transformations can be applied in the same time to a PV. | ||
+ | |||
+ | < | ||
+ | # the length of escaped ' | ||
+ | |||
+ | $(hdr(Test){s.escape.common}{s.len}) | ||
+ | </ | ||
+ | |||
+ | The transformations can be used anywhere, being considered parts of PV -- in xlog, avpops or other modules' | ||
+ | |||
+ | ===== String Transformations ===== | ||
+ | |||
+ | The name of these transformation starts with ' | ||
+ | |||
+ | Available transformations in this class: | ||
+ | |||
+ | |||
+ | ==== {s.len} ==== | ||
+ | |||
+ | Return strlen of PV value | ||
+ | |||
+ | < | ||
+ | $var(x) = " | ||
+ | if($(var(x){s.len}) == 3) | ||
+ | { | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== {s.int} ==== | ||
+ | |||
+ | Return integer value of a string-represented number | ||
+ | |||
+ | < | ||
+ | $var(x) = " | ||
+ | if($(var(x){s.int})==1234) { | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== {s.md5} ==== | ||
+ | |||
+ | Return md5 over PV value | ||
+ | |||
+ | < | ||
+ | xlog(" | ||
+ | </ | ||
+ | |||
+ | ==== {s.substr, | ||
+ | |||
+ | Return substring starting at offset having size of ' | ||
+ | |||
+ | Example: | ||
+ | < | ||
+ | $var(x) = " | ||
+ | $(var(x){s.substr, | ||
+ | </ | ||
+ | |||
+ | ==== {s.select, | ||
+ | |||
+ | Return a field from PV value. The field is selected based on separator and index. The separator must be a character used to identify the fields. Index must be a integer value or a PV. If index is negative, the count of fields starts from end of PV value, -1 being last field. If index is positive, 0 is the first field. | ||
+ | |||
+ | Example: | ||
+ | < | ||
+ | $var(x) = " | ||
+ | $(var(x){s.select, | ||
+ | |||
+ | $var(x) = " | ||
+ | $(var(x){s.select, | ||
+ | </ | ||
+ | |||
+ | ==== {s.encode.hexa} ==== | ||
+ | |||
+ | Return encoding in hexa of PV value | ||
+ | |||
+ | ==== {s.decode.hexa} ==== | ||
+ | |||
+ | Return decoding from hexa of PV value | ||
+ | |||
+ | |||
+ | |||
+ | ==== {s.escape.common} ==== | ||
+ | |||
+ | Return escaped string of PV value. Characters escaped are ', ", \ and 0. Useful when doing DB queries (care should be taken for non Latin character set). | ||
+ | |||
+ | |||
+ | |||
+ | ==== {s.unescape.common} ==== | ||
+ | |||
+ | Return unescaped string of PV value. Reverse of above transformation. | ||
+ | |||
+ | |||
+ | ==== {s.escape.user} ==== | ||
+ | |||
+ | Return escaped string of PV value, changing to ' | ||
+ | |||
+ | ==== {s.unescape.user} ==== | ||
+ | |||
+ | Return unescaped string of PV value, changing ' | ||
+ | |||
+ | |||
+ | ==== {s.escape.param} ==== | ||
+ | |||
+ | Return escaped string of PV value, changing to ' | ||
+ | |||
+ | |||
+ | ==== {s.unescape.param} ==== | ||
+ | |||
+ | Return unescaped string of PV value, changing ' | ||
+ | |||
+ | ==== {s.tolower} ==== | ||
+ | |||
+ | Return string with lower case ASCII letters. | ||
+ | |||
+ | |||
+ | ==== {s.toupper} ==== | ||
+ | |||
+ | Return string with upper case ASCII letters. | ||
+ | |||
+ | ==== {s.strip, | ||
+ | |||
+ | Return string after removing starting ' | ||
+ | |||
+ | Example: | ||
+ | |||
+ | <code c> | ||
+ | $var(x) = " | ||
+ | $var(y) = $(var(x){s.strip, | ||
+ | </ | ||
+ | |||
+ | ==== {s.striptail, | ||
+ | |||
+ | Return string after removing ending ' | ||
+ | |||
+ | ==== {s.prefixes[, | ||
+ | |||
+ | Return series of comma separated prefixes of the pv. Parameter ' | ||
+ | |||
+ | Example: | ||
+ | <code c> | ||
+ | $var(x) = " | ||
+ | $(var(x){s.prefixes}) => 1, | ||
+ | $(var(x){s.prefixes, | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== {s.prefixes.quoted[, | ||
+ | |||
+ | Return series of comma separated quoted prefixes of the pv. Parameter ' | ||
+ | |||
+ | Example: | ||
+ | <code c> | ||
+ | $var(x) = " | ||
+ | $(var(x){s.prefixes.quoted} => ' | ||
+ | $(var(x){s.prefixes.quoted, | ||
+ | </ | ||
+ | |||
+ | ===== URI Transformations ===== | ||
+ | |||
+ | The name of transformation starts with ' | ||
+ | |||
+ | Available transformations in this class: | ||
+ | |||
+ | ==== {uri.user} ==== | ||
+ | |||
+ | Return the user part | ||
+ | |||
+ | ==== {uri.host} ==== | ||
+ | |||
+ | (same as [b]{uri.domain}[/ | ||
+ | |||
+ | Return the domain part | ||
+ | |||
+ | ==== {uri.passwd} ==== | ||
+ | |||
+ | Return the password | ||
+ | |||
+ | ==== {uri.port} ==== | ||
+ | |||
+ | Return the port | ||
+ | |||
+ | ==== {uri.params} ==== | ||
+ | |||
+ | Return the URI parameters in a string | ||
+ | |||
+ | ==== {uri.param, | ||
+ | |||
+ | Return the value of parameter with name ' | ||
+ | |||
+ | ==== {uri.headers} ==== | ||
+ | |||
+ | Return URI headers | ||
+ | |||
+ | ==== {uri.transport} ==== | ||
+ | |||
+ | Return the value of transport parameter | ||
+ | |||
+ | ==== {uri.ttl} ==== | ||
+ | |||
+ | Return the value of ttl parameter | ||
+ | |||
+ | ==== {uri.uparam} ==== | ||
+ | |||
+ | Return the value of user parameter | ||
+ | |||
+ | ==== {uri.maddr} ==== | ||
+ | |||
+ | Return the value of maddr parameter | ||
+ | |||
+ | ==== {uri.method} ==== | ||
+ | |||
+ | Return the value of method parameter | ||
+ | |||
+ | ==== {uri.lr} ==== | ||
+ | |||
+ | Return the value of lr parameter | ||
+ | |||
+ | ==== {uri.r2} ==== | ||
+ | |||
+ | Return the value of r2 parameter | ||
+ | |||
+ | ===== Parameters List Transformations ===== | ||
+ | |||
+ | The name of the transformation starts with ' | ||
+ | |||
+ | Available transformations in this class: | ||
+ | |||
+ | |||
+ | ==== {param.value, | ||
+ | |||
+ | Return the value of parameter ' | ||
+ | |||
+ | Example: | ||
+ | < | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | ' | ||
+ | |||
+ | ==== {param.valueat, | ||
+ | |||
+ | Return the value of parameter at position give by ' | ||
+ | |||
+ | Example: | ||
+ | < | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | ' | ||
+ | |||
+ | ==== {param.name, | ||
+ | |||
+ | Return the name of parameter at position ' | ||
+ | |||
+ | Example: | ||
+ | < | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== {param.count} ==== | ||
+ | |||
+ | Return the number of parameters in the list. | ||
+ | |||
+ | Example: | ||
+ | < | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | ===== Name-address Transformations ===== | ||
+ | |||
+ | The name of the transformation starts with ' | ||
+ | |||
+ | Available transformations in this class: | ||
+ | |||
+ | |||
+ | ==== {nameaddr.name} ==== | ||
+ | |||
+ | Return the value of display name | ||
+ | |||
+ | Example: | ||
+ | < | ||
+ | '" | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== {nameaddr.uri} ==== | ||
+ | |||
+ | Return the value of URI | ||
+ | |||
+ | Example: | ||
+ | < | ||
+ | '" | ||
+ | </ | ||
+ | |||
+ | |||
+ | ==== {nameaddr.len} ==== | ||
+ | |||
+ | Return the length of the entire name-addr part from the value. | ||
+ | |||
+ | ===== To-Body Transformations ===== | ||
+ | |||
+ | <hi # | ||
+ | |||
+ | Access parts of a ToBody-like structure. | ||
+ | |||
+ | ==== {tobody.uri} ==== | ||
+ | |||
+ | * return URI from To body | ||
+ | |||
+ | ==== {tobody.display} ==== | ||
+ | |||
+ | * return Display name from To body | ||
+ | |||
+ | ==== {tobody.tag} ==== | ||
+ | |||
+ | * return Tag parameter from To body | ||
+ | |||
+ | ==== {tobody.user} ==== | ||
+ | |||
+ | * return URI User from To body | ||
+ | |||
+ | ==== {tobody.host} ==== | ||
+ | |||
+ | * return URI Host from To body | ||
+ | |||
+ | ==== {tobody.params} ==== | ||
+ | |||
+ | * return parameters part from To body | ||
+ | |||
+ | |||
+ | ===== Regular Expression Transformations ===== | ||
+ | |||
+ | <hi # | ||
+ | |||
+ | |||
+ | ==== {re.subst, | ||
+ | |||
+ | Perform Perl-like substitutions on string value pseudo-variables. | ||
+ | |||
+ | <code c> | ||
+ | # Assign Request-URI user to PV | ||
+ | $var(user) = $(ru{re.subst,/ | ||
+ | </ | ||
+ | |||
+ | <code c> | ||
+ | # Assign Request-URI user to PV, where every ' | ||
+ | $var(user) = $(rU{re.subst,/ | ||
+ | </ | ||
+ | |||
+ | The prototype is: | ||
+ | |||
+ | <code c> | ||
+ | {re.subst,/ | ||
+ | </ | ||
+ | |||
+ | * match_expression - Posix regular expression | ||
+ | * replacement_expression - substitution expression with back references to matched tokes: \1, \2, ..., \9 | ||
+ | * flags: | ||
+ | * i - match ignore case | ||
+ | * s - match within multi-lines strings | ||
+ | * g - replace all matches | ||
+ | |||
+ | ===== SQL Transformations ===== | ||
+ | |||
+ | <hi # | ||
+ | |||
+ | ==== {sql.val} ==== | ||
+ | |||
+ | This transformation outputs valid SQL values for various PV values: | ||
+ | * < | ||
+ | * integers are output as integers | ||
+ | * everything else is output as quoted and escaped string | ||
+ | |||
+ | <code c> | ||
+ | $var(null) = $null; | ||
+ | $avp(null) = $null; | ||
+ | $avp(str) = " | ||
+ | $avp(nr) = 12345; | ||
+ | $avp(strnr) = " | ||
+ | |||
+ | xlog(" | ||
+ | xlog(" | ||
+ | xlog(" | ||
+ | xlog(" | ||
+ | xlog(" | ||
+ | xlog(" | ||
+ | |||
+ | Output: | ||
+ | $rm = ACK = ' | ||
+ | $var(null) = 0 = 0 | ||
+ | $avp(null) = < | ||
+ | $avp(str) = String with \ illegal \characters = ' | ||
+ | $avp(nr) = 12345 = 12345 | ||
+ | $avp(strnr) = 12345 = ' | ||
+ | </ | ||
+ | |||
+ | ==== {sql.val.int} ==== | ||
+ | |||
+ | Like sql.val, but output number 0 for null values. | ||
+ | |||
+ | ==== {sql.val.str} ==== | ||
+ | |||
+ | Like sql.val, but output string '' | ||
+ | |||
+ | ===== Examples ===== | ||
+ | |||
+ | Within a PV, many transformation can be applied, being executed from left to right. | ||
+ | |||
+ | * The length of the value of parameter at postion 1 (remember 0 is first position, 1 is second position) | ||
+ | |||
+ | < | ||
+ | $var(x) = " | ||
+ | $(var(x){param.value, | ||
+ | </ | ||
+ | |||
+ | * Test if whether is un-registration or not | ||
+ | |||
+ | < | ||
+ | if(is_method(" | ||
+ | xlog(" | ||
+ | </ |