Skip to content

Commit

Permalink
feat(deepql): add initial setup for deepql
Browse files Browse the repository at this point in the history
  • Loading branch information
Umaaz committed Mar 25, 2024
1 parent 9c5f1e4 commit 8d5b3a4
Show file tree
Hide file tree
Showing 11 changed files with 1,764 additions and 0 deletions.
32 changes: 32 additions & 0 deletions pkg/ql/ast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package ql

import "errors"

type RootExpr struct {
trigger *trigger
command *command
}

func (e RootExpr) validate() error {
var errs []error
if e.trigger != nil && e.command != nil {
return errors.New("fatal error: cannot define a trigger and a command")
}
if e.trigger != nil {
err := e.trigger.validate()
if err != nil {
errs = append(errs, err)
}
}
if e.command != nil {
err := e.command.validate()
if err != nil {
errs = append(errs, err)
}
}

if len(errs) > 0 {
return errors.Join(errs...)
}
return nil
}
88 changes: 88 additions & 0 deletions pkg/ql/ast_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (C) 2024 Intergral GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package ql

import (
"errors"
"fmt"
)

const (
list = "list"
deleteType = "delete"
)

var (
commandTypes = map[string]int{
list: COMMAND,
deleteType: COMMAND,
}
)

type command struct {
command string

file string
id string

errors []error
}

func (c *command) validate() error {
if len(c.errors) > 0 {
return errors.Join(c.errors...)
}
return nil
}

func newCommand(typ string, opts []configOption) command {
var cmd command
switch typ {
case list:
cmd = command{command: list}
case deleteType:
cmd = command{command: deleteType}
}

for _, cfg := range opts {
err := cfg.apply(&cmd)
if err != nil {
cmd.errors = append(cmd.errors, err)
}
}

return cmd
}

func applyFuncForCommand(lhs string) func(c *configOption, cmd *command) error {
switch lhs {
case "file":
return func(c *configOption, cmd *command) error {
cmd.file = c.rhs.S
return nil
}
case "id":
return func(c *configOption, cmd *command) error {
cmd.id = c.rhs.S
return nil
}
}
return func(c *configOption, cmd *command) error {
return errors.New(fmt.Sprintf("parse error unrecognized option: %s", lhs))
}
}
59 changes: 59 additions & 0 deletions pkg/ql/ast_options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2024 Intergral GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package ql

import "strings"

type configOption struct {
op Operator
lhs string
rhs Static
fnc func(c *configOption, target interface{}) error
}

func (c *configOption) apply(cfg interface{}) error {
return c.fnc(c, cfg)
}

func newConfigOption(op Operator, lhs string, rhs Static) configOption {
return configOption{
op: op,
lhs: lhs,
rhs: rhs,
fnc: applyFuncFor(lhs),
}
}

func applyFuncFor(lhs string) func(c *configOption, tri interface{}) error {
return func(c *configOption, tri interface{}) error {
if v, ok := tri.(*trigger); ok {
return applyFuncForTrigger(lhs)(c, v)
}

if v, ok := tri.(*command); ok {
return applyFuncForCommand(lhs)(c, v)
}

return nil
}
}

func stripPrefix(lhs string, s string) string {
after, _ := strings.CutPrefix(lhs, s)
return after
}
Loading

0 comments on commit 8d5b3a4

Please sign in to comment.