-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a4bd170
commit c3639d7
Showing
16 changed files
with
1,360 additions
and
1,039 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package epsearchast_v3 | ||
|
||
// ApplyAliases will return a new AST where all aliases have been resolved to their new value. | ||
// This function should be called after validating it. | ||
func ApplyAliases(a *AstNode, aliases map[string]string) (*AstNode, error) { | ||
aliasFunc := func(a *AstNode, children []*AstNode) (*AstNode, error) { | ||
|
||
newArgs := make([]string, len(a.Args)) | ||
copy(newArgs, a.Args) | ||
|
||
if len(newArgs) > 0 { | ||
if v, ok := aliases[newArgs[0]]; ok { | ||
newArgs[0] = v | ||
} | ||
} else { | ||
newArgs = nil | ||
} | ||
|
||
// When we unmarshal the JSON AST a node with no children has nil for the field. | ||
// Reduce would get messy if you could pass in a nil. | ||
// if we want to do equality testing in Tests we need to not set empty children. | ||
// Or maybe make it a non pointer type or something. | ||
var childrenNodes []*AstNode = nil | ||
|
||
if len(children) > 0 { | ||
childrenNodes = children | ||
} | ||
|
||
return &AstNode{ | ||
NodeType: a.NodeType, | ||
Children: childrenNodes, | ||
Args: newArgs, | ||
}, nil | ||
} | ||
|
||
return ReduceAst(a, aliasFunc) | ||
} |
Oops, something went wrong.