-
Notifications
You must be signed in to change notification settings - Fork 4
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
2f9d9ce
commit d9535b1
Showing
7 changed files
with
178 additions
and
49 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
inputconfig: | ||
csvsourcefilename: sample.csv | ||
inputmethod: CSV | ||
collection: sampledata | ||
credentialfileaddr: firebaseConfig.json | ||
document: "1" | ||
inputmethod: Firebase | ||
outputconfig: | ||
csvdestinationfilename: samplenew.csv | ||
outputmethod: CSV | ||
cronjob: | ||
cronexpression: "@every 5s" # Every 5 seconds | ||
jobname: "Data Fetch and Process" | ||
task: "process_csv" | ||
collection: abcd | ||
connstring: mongodb://localhost:27017/ | ||
database: test1 | ||
outputmethod: MongoDB | ||
transformations: | | ||
ADD_FIELD("processed_at", '2004-10-22') | ||
validations: | | ||
FIELD("age") RANGE(30, 35) |
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
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,76 @@ | ||
package language | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// TokenType represents the type of a token | ||
type TokenType string | ||
|
||
const ( | ||
TokenField TokenType = "FIELD" | ||
TokenCondition TokenType = "CONDITION" | ||
TokenOperator TokenType = "OPERATOR" | ||
TokenValue TokenType = "VALUE" | ||
TokenLogical TokenType = "LOGICAL" | ||
TokenSeparator TokenType = "SEPARATOR" | ||
TokenTransform TokenType = "TRANSFORM" | ||
TokenInvalid TokenType = "INVALID" | ||
) | ||
|
||
// Token represents a single token | ||
type Token struct { | ||
Type TokenType | ||
Value string | ||
} | ||
|
||
// Lexer for parsing rules | ||
type Lexer struct { | ||
input string | ||
pos int | ||
} | ||
|
||
// NewLexer initializes a lexer with the input string | ||
func NewLexer(input string) *Lexer { | ||
return &Lexer{ | ||
input: strings.TrimSpace(input), | ||
pos: 0, | ||
} | ||
} | ||
|
||
// Tokenize splits the input into tokens | ||
func (l *Lexer) Tokenize(input string) ([]Token, error) { | ||
var tokens []Token | ||
pos := 0 | ||
patterns := map[TokenType]*regexp.Regexp{ | ||
TokenField: regexp.MustCompile(`^FIELD\("([^"]+)"\)`), // Match FIELD("field_name") | ||
TokenCondition: regexp.MustCompile(`^(TYPE|RANGE|MATCHES|IN|REQUIRED)`), // Custom conditions | ||
TokenValue: regexp.MustCompile(`^"([^"]*)"|'([^']*)'|[\d\.]+|\([^)]*\)`), // Match strings, numbers, lists | ||
TokenLogical: regexp.MustCompile(`^(AND|OR|NOT)`), // Logical operators | ||
TokenSeparator: regexp.MustCompile(`^,`), // Separators | ||
} | ||
|
||
for pos < len(input) { | ||
input = strings.TrimSpace(input[pos:]) | ||
pos = 0 | ||
|
||
matched := false | ||
for tokenType, pattern := range patterns { | ||
if loc := pattern.FindStringIndex(input); loc != nil && loc[0] == 0 { | ||
value := input[loc[0]:loc[1]] | ||
tokens = append(tokens, Token{Type: tokenType, Value: value}) | ||
pos += len(value) | ||
matched = true | ||
break | ||
} | ||
} | ||
|
||
if !matched { | ||
return nil, fmt.Errorf("unexpected token at: %s", input) | ||
} | ||
} | ||
|
||
return tokens, nil | ||
} |
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,62 @@ | ||
package language | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// Node represents a node in the Abstract Syntax Tree (AST) | ||
type Node struct { | ||
Type TokenType | ||
Value string | ||
Children []*Node | ||
} | ||
|
||
// Parser for validation and transformation rules | ||
type Parser struct{} | ||
|
||
// NewParser initializes a parser | ||
func NewParser() *Parser { | ||
return &Parser{} | ||
} | ||
|
||
func (p *Parser) ParseRules(tokens []Token) (*Node, error) { | ||
if len(tokens) < 3 { | ||
return nil, errors.New("insufficient parameters") | ||
} | ||
|
||
root := &Node{Type: "ROOT", Children: []*Node{}} | ||
var currentField string | ||
|
||
for i := 0; i < len(tokens); i++ { | ||
token := tokens[i] | ||
|
||
if token.Type == "FIELD" { | ||
// Set the current field and continue to the next token | ||
currentField = token.Value | ||
} else if token.Type == "CONDITION" { | ||
// Ensure there is a following value | ||
if i+1 >= len(tokens) { | ||
return nil, errors.New("expected value after condition") | ||
} | ||
|
||
condition := token | ||
value := tokens[i+1] // Next token is the value | ||
|
||
node := &Node{Type: "EXPRESSION", Children: []*Node{ | ||
{Type: "FIELD", Value: currentField}, | ||
{Type: "CONDITION", Value: condition.Value}, | ||
{Type: "VALUE", Value: value.Value}, | ||
}} | ||
|
||
root.Children = append(root.Children, node) | ||
|
||
// Move past the value token | ||
i++ | ||
} else { | ||
return nil, fmt.Errorf("unexpected token: %s", token.Value) | ||
} | ||
} | ||
|
||
return root, nil | ||
} |
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