Added string case operators

This commit is contained in:
Mike Farah
2022-02-22 16:17:23 +11:00
parent 71706af3d4
commit d7b158f855
5 changed files with 106 additions and 0 deletions
+31
View File
@@ -9,6 +9,37 @@ import (
"gopkg.in/yaml.v3"
)
type changeCasePrefs struct {
ToUpperCase bool
}
func changeCaseOperator(d *dataTreeNavigator, context Context, expressionNode *ExpressionNode) (Context, error) {
results := list.New()
prefs := expressionNode.Operation.Preferences.(changeCasePrefs)
for el := context.MatchingNodes.Front(); el != nil; el = el.Next() {
candidate := el.Value.(*CandidateNode)
node := unwrapDoc(candidate.Node)
if guessTagFromCustomType(node) != "!!str" {
return Context{}, fmt.Errorf("cannot change case with %v, can only operate on strings. ", node.Tag)
}
newStringNode := &yaml.Node{Kind: yaml.ScalarNode, Tag: node.Tag, Style: node.Style}
if prefs.ToUpperCase {
newStringNode.Value = strings.ToUpper(node.Value)
} else {
newStringNode.Value = strings.ToLower(node.Value)
}
results.PushBack(candidate.CreateReplacement(newStringNode))
}
return context.ChildContext(results), nil
}
func getSubstituteParameters(d *dataTreeNavigator, block *ExpressionNode, context Context) (string, string, error) {
regEx := ""
replacementText := ""