Added ability to customize the resolution of context contained variables.
Added ability to customize the resolution of context contained variables, by:
NamedFieldResolver.GetNamedField(name)andIndexedFieldResolver.GetIndexedField(index)interfaces- Struct field
pongo2:"..."
Plus: Added security enforcement to never use not exported fields.
Excerpt from the documentation
By the default sub variables are resolved by
- Field names inside structs
- Keys of values inside maps
- Indexes of values inside slices
This behavior can be customized using either struct tag "pongo2" like:
type MyStruct struct {
FieldA string `pongo2:"uh"`
FieldB string `pongo2:"yeah"`
}
my.tmpl:
{{ myStruct.uh }} {{ myStruct.yeah }}
...or by implementing NamedFieldResolver or IndexedFieldResolver.
type MyStruct struct {
fieldA string
}
// GetNamedField implements NamedFieldResolver
func (s MyStruct) GetNamedField(s string) (interface{}, error) {
switch s {
case "uh":
return s.fieldA, nil
case "yeah":
return "YEAH!", nil
default:
return nil, pongo2.ErrNoSuchField
}
}
// GetNamedField implements IndexedFieldResolver
func (s MyStruct) GetIndexedField(s int) (interface{}, error) {
switch s {
case 0:
return s.fieldA, nil
case 1:
return "YEAH!", nil
default:
return nil, pongo2.ErrNoSuchField
}
}
my.tmpl:
{{ myStruct.uh }} {{ myStruct.yeah }}
{{ myStruct.0 }} {{ myStruct.1 }}
Kudos, SonarCloud Quality Gate passed! 
0 Bugs
0 Vulnerabilities
0 Security Hotspots
5 Code Smells
No Coverage information
0.0% Duplication
@flosch any updates on this? 🙂
Thank you for your contribution. Please allow some more time for me to go through your PR.