-
Notifications
You must be signed in to change notification settings - Fork 4
Support rules with a script node interior type. #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,101 @@ | ||
| package ast | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/prequel-dev/prequel-compiler/pkg/parser" | ||
| "github.com/prequel-dev/prequel-compiler/pkg/schema" | ||
| "github.com/rs/zerolog/log" | ||
| ) | ||
|
|
||
| type AstScriptT struct { | ||
| Code string | ||
| Language string | ||
| Timeout time.Duration | ||
| } | ||
|
|
||
| // Build the child Ast nodes for the script. | ||
| // | ||
| // Script nodes are internal nodes with one input node. | ||
| // The parser node for a script contains a ScriptT struct as its first child, followed by one input node. | ||
| // Build the the child nodes for the script node by building each of the parser node's children; | ||
| // the first child is skipped since it is the script definition, and the remaining child is built as the input to the script node. | ||
|
|
||
| func (b *builderT) buildScriptChildren(parserNode *parser.NodeT, machineAddress *AstNodeAddressT) ([]*AstNodeT, error) { | ||
|
|
||
| if len(parserNode.Children) != 2 { | ||
| log.Error().Int("child_count", len(parserNode.Children)).Msg("Script node must have two children") | ||
| return nil, parserNode.WrapError(ErrInvalidNodeType) | ||
| } | ||
|
|
||
| termIdx := uint32(1) | ||
|
|
||
| child := parserNode.Children[1] | ||
| parserChildNode, ok := child.(*parser.NodeT) | ||
| if !ok { | ||
| log.Error().Any("child", child).Msg("Failed to build Script child node") | ||
| return nil, parserNode.WrapError(ErrInvalidNodeType) | ||
| } | ||
|
|
||
| leaf, err := b.buildLeafChild(parserChildNode, machineAddress, &termIdx) | ||
|
|
||
| var childList []*AstNodeT | ||
|
|
||
| switch { | ||
| case err != nil: | ||
| // fallthrough | ||
| case leaf != nil: | ||
| childList = []*AstNodeT{leaf} | ||
| default: | ||
| err = b.descendTree(func() error { | ||
| node, err := b.buildTree(parserChildNode, machineAddress, &termIdx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| childList = []*AstNodeT{node} | ||
| return nil | ||
| }) | ||
| } | ||
|
|
||
| return childList, err | ||
| } | ||
|
|
||
| // Validate script definitions and build the script node. | ||
|
|
||
| func (b *builderT) buildScriptNode(parserNode *parser.NodeT, parentMachineAddress, machineAddress *AstNodeAddressT) (*AstNodeT, error) { | ||
|
|
||
| // Expects exactly two children, the first should be parser.ScriptT, the following is the script input node. | ||
|
|
||
| if len(parserNode.Children) != 2 { | ||
| log.Error().Int("child_count", len(parserNode.Children)).Msg("Script node must have exactly two children") | ||
| return nil, parserNode.WrapError(ErrInvalidNodeType) | ||
| } | ||
|
|
||
| scriptNode, ok := parserNode.Children[0].(*parser.ScriptT) | ||
|
|
||
| if !ok { | ||
| log.Error().Any("script", parserNode.Children[0]).Msg("Failed to build Script node") | ||
| return nil, parserNode.WrapError(ErrMissingScalar) | ||
| } | ||
|
|
||
| if scriptNode.Code == "" { | ||
| log.Error().Msg("Script code string is empty") | ||
| return nil, parserNode.WrapError(ErrMissingScalar) | ||
| } | ||
|
|
||
| pn := &AstScriptT{ | ||
| Code: scriptNode.Code, | ||
| Language: scriptNode.Language, | ||
| } | ||
|
|
||
| if scriptNode.Timeout != nil { | ||
| pn.Timeout = *scriptNode.Timeout | ||
| } | ||
|
|
||
| var ( | ||
| node = newAstNode(parserNode, parserNode.Metadata.Type, schema.ScopeCluster, parentMachineAddress, machineAddress) | ||
| ) | ||
|
|
||
| node.Object = pn | ||
| return node, nil | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.