Created
October 7, 2019 13:25
-
-
Save stuartelimu/cf249ee80ed706292d8941b1749bbdd5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"$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