Skip to content

Instantly share code, notes, and snippets.

@stuartelimu
Created October 7, 2019 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartelimu/cf249ee80ed706292d8941b1749bbdd5 to your computer and use it in GitHub Desktop.
Save stuartelimu/cf249ee80ed706292d8941b1749bbdd5 to your computer and use it in GitHub Desktop.
"$matchOne": func(template, context map[string]interface{}) (interface{}, error) {
if err := restrictProperties(template, "$matchOne"); err != nil {
return nil, err
}
match, ok := template["$matchOne"].(map[string]interface{})
if !ok {
return nil, TemplateError{
Message: "$matchOne can evaluate objects only",
Template: template,
}
}
// get the sorted list of conditions
conditions := make([]string, 0, len(match))
for condition := range match {
conditions = append(conditions, condition)
}
sort.Strings(conditions)
result := ""
for _, key := range conditions {
check, err := i.Execute(key, 0, context)
if err != nil {
return nil, TemplateError{
Message: err.Error(),
Template: template,
}
}
if i.IsTruthy(check) {
value := match[key]
r, err := render(value, context)
if err != nil {
return nil, TemplateError{
Message: err.Error(),
Template: template,
}
}
result = fmt.Sprintf("%v", r)
break
}
}
return result, nil
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment