Adding url decoding function to extend template functionality in easegress builder filters
KevinQiangK opened this issue · 5 comments
I have submitted an issue regarding the content of the template field in the builder filters' spec. The template is defined using Golang's text/template package, with additional functions from the sprig package and custom functions defined by Easegress.
I encountered a situation where I received URL encoded data and needed to decode it within the builder filter's template. However, neither the sprig package nor the extra functions provided by Easegress have a built-in function for URL decoding.
To resolve this issue, I have added a custom 'urldecode' function to the extra functions as follows:
var extraFuncs = template.FuncMap{
"addf": func(a, b interface{}) float64 {
x, y := toFloat64(a), toFloat64(b)
return x + y
},
........
"urldecode": func(s string) string {
ds, _ := url.QueryUnescape(s)
return ds
},
}
Within the template, I can now use the 'urldecode' function as follows:
{{.responses.DEFAULT.JSONBody.data | urldecode}}
My question is: Are there any other recommended approaches or best practices for extending template functions in a more elegant and maintainable manner?
URL decoding (encoding) is a common scenario in the traffic orchestration system. I suggest providing users with helper functions for use in builder filters or predefined filter options.
As far as I know, I can't find a better way to extend the template functions except make a new pr for it? So, if you like, feel free to make a pr for it and please make sure to update the corresponding documentation.
By the way, with newest easegress, you can simplify
{{.responses.DEFAULT.JSONBody.data | urldecode}}
to
{{.resp.JSONBody.data | urldecode}}
Please check https://github.com/easegress-io/easegress/blob/main/docs/07.Reference/7.02.Filters.md#template-of-builder-filters for more details.
As far as I know, I can't find a better way to extend the template functions except make a new pr for it? So, if you like, feel free to make a pr for it and please make sure to update the corresponding documentation.
By the way, with newest easegress, you can simplify
{{.responses.DEFAULT.JSONBody.data | urldecode}}
to
{{.resp.JSONBody.data | urldecode}}
Please check https://github.com/easegress-io/easegress/blob/main/docs/07.Reference/7.02.Filters.md#template-of-builder-filters for more details.
Thank you for your suggestion. I will create a pull request to address this issue by adding the 'urlQueryEscape' and 'urlQueryUnescape' template functions to the builtin extraFuncs .
#1194 I have implemented the 'urlQueryEscape' and 'urlQueryUnescape' template functions and added them to the builtin extraFuncs. You can now utilize them as follows:
{{.resp.JSONBody.data | urlQueryUnescape}}