Skip to content

Commit

Permalink
added template logger
Browse files Browse the repository at this point in the history
  • Loading branch information
klarysz committed Aug 15, 2022
1 parent 6243935 commit e4c7859
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 149 deletions.
46 changes: 22 additions & 24 deletions cmd/ast/testdata/case004/output.yaml
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
parameters:
- assumed: false
datatype: int
id: ID
kind: query
name: id
required: false
- assumed: false
datatype: string
id: quantity
kind: path
name: quantity
required: true
- datatype: int
id: ID
kind: query
name: id
required: false
- datatype: string
id: quantity
kind: path
name: quantity
required: true
source: |-
SELECT *, (
SELECT abc
FROM T2
WHERE 1=2
) as abc FROM T1
WHERE 1=1
#if($Has.ID)
AND ID = $ID
#end
#if($Has.quantity)
AND quantity = $quantity
#end
SELECT *, (
SELECT abc
FROM T2
WHERE 1=2
) as abc FROM T1
WHERE 1=1
#if($Has.ID)
AND ID = $ID
#end
#if($Has.quantity)
AND quantity = $quantity
#end
53 changes: 26 additions & 27 deletions cmd/ast/testdata/case005/output.yaml
Original file line number Diff line number Diff line change
@@ -1,29 +1,28 @@
parameters:
- assumed: false
datatype: int
id: ID
kind: query
name: id
required: false
- assumed: true
datatype: string
id: quantity
kind: query
name: quantity
required: false
typer:
columnname: quantity
- datatype: int
id: ID
kind: query
name: id
required: false
- assumed: true
datatype: string
id: quantity
kind: query
name: quantity
required: false
typer:
columnname: quantity
source: |
SELECT *, (
SELECT abc
FROM T2
WHERE 1=2
) as abc FROM T1
WHERE 1=1
#if($Has.ID)
AND ID = $ID
#end
#if($Has.quantity)
AND quantity = $quantity
AND quantity = $Unsafe.quantity
#end
SELECT *, (
SELECT abc
FROM T2
WHERE 1=2
) as abc FROM T1
WHERE 1=1
#if($Has.ID)
AND ID = $ID
#end
#if($Has.quantity)
AND quantity = $quantity
AND quantity = $Unsafe.quantity
#end
73 changes: 40 additions & 33 deletions cmd/ast/testdata/case006/output.yaml
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
parameters:
- assumed: false
datatype: int
id: ID
kind: query
name: id
required: false
- assumed: true
datatype: string
id: quantity
kind: query
name: quantity
required: false
typer:
columnname: quantity
- datatype: int
id: ID
kind: query
name: id
required: false
- assumed: true
cardinality: Many
datatype: string
id: Columns
kind: query
multi: true
name: Columns
required: false
- assumed: true
datatype: string
id: quantity
kind: query
name: quantity
required: false
typer:
columnname: quantity
source: |
SELECT *, (
SELECT abc
FROM T2
WHERE 1=2
) as abc FROM T1
WHERE 1=1
#set($valueSet = 123)
#if($Has.ID)
AND ID = $ID
#end
#foreach($column in $Columns)
$column.Id
#end
#if($Has.quantity)
AND quantity = $quantity
AND quantity = $Unsafe.quantity
#end
SELECT *, (
SELECT abc
FROM T2
WHERE 1=2
) as abc FROM T1
WHERE 1=1
#set($valueSet = 123)
#if($Has.ID)
AND ID = $ID
#end
#foreach($column in $Columns)
$column.Id
#end
#if($Has.quantity)
AND quantity = $quantity
AND quantity = $Unsafe.quantity
#end
74 changes: 41 additions & 33 deletions cmd/ast/testdata/case007/output.yaml
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
parameters:
- assumed: true
datatype: string
id: ID
kind: query
name: ID
required: false
- assumed: true
datatype: string
id: quantity
kind: query
name: quantity
required: false
typer:
columnname: quantity
- assumed: true
datatype: string
id: ID
kind: query
name: ID
required: false
- assumed: true
cardinality: Many
datatype: string
id: Columns
kind: query
multi: true
name: Columns
required: false
- assumed: true
datatype: string
id: quantity
kind: query
name: quantity
required: false
typer:
columnname: quantity
source: |
SELECT *, (
SELECT abc
FROM T2
WHERE 1=2
) as abc FROM T1
WHERE 1=1
#set($valueSet = 123)
#if($Has.ID)
AND ID = 10
#end
#foreach($column in $Columns)
$column.Id
#end
#if($Has.quantity)
AND quantity = $quantity
AND quantity = $Unsafe.quantity
#end
SELECT *, (
SELECT abc
FROM T2
WHERE 1=2
) as abc FROM T1
WHERE 1=1
#set($valueSet = 123)
#if($Has.ID)
AND ID = 10
#end
#foreach($column in $Columns)
$column.Id
#end
#if($Has.quantity)
AND quantity = $quantity
AND quantity = $Unsafe.quantity
#end
69 changes: 38 additions & 31 deletions cmd/ast/testdata/case008/output.yaml
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
parameters:
- assumed: true
datatype: string
id: ID
kind: query
name: ID
required: false
- assumed: false
datatype: time.Time
id: quantity
kind: query
name: quantity
required: false
- assumed: true
datatype: string
id: ID
kind: query
name: ID
required: false
- assumed: true
cardinality: Many
datatype: string
id: Columns
kind: query
multi: true
name: Columns
required: false
- datatype: time.Time
id: quantity
kind: query
name: quantity
required: false
source: |
SELECT *, (
SELECT abc
FROM T2
WHERE 1=2
) as abc FROM T1
WHERE 1=1
#set($valueSet = 123)
#if($Has.ID)
AND ID = 10
#end
#foreach($column in $Columns)
$column.Id
#end
#if($Has.quantity)
AND quantity = $quantity /* {"DataType": "time.Time"} */
AND quantity = $Unsafe.quantity
#end
SELECT *, (
SELECT abc
FROM T2
WHERE 1=2
) as abc FROM T1
WHERE 1=1
#set($valueSet = 123)
#if($Has.ID)
AND ID = 10
#end
#foreach($column in $Columns)
$column.Id
#end
#if($Has.quantity)
AND quantity = $quantity /* {"DataType": "time.Time"} */
AND quantity = $Unsafe.quantity
#end
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ require (
github.com/viant/scy v0.2.1-0.20220812183656-68209588b73e
github.com/viant/sqlx v0.0.0-20220811174716-1b6dd6d967a2
github.com/viant/toolbox v0.34.6-0.20220630003140-fb2bf82657c1
github.com/viant/velty v0.1.1-0.20220810215332-b4095f02bc23
github.com/viant/velty v0.1.1-0.20220815170357-0518dfc48b9d
github.com/viant/xunsafe v0.8.1-0.20220609224231-1d3e1fcf7bb6
golang.org/x/oauth2 v0.0.0-20220608161450-d0670ef3b1eb
google.golang.org/api v0.84.0
Expand Down
60 changes: 60 additions & 0 deletions logger/printer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package logger

import (
"fmt"
"github.com/viant/velty/est"
"github.com/viant/velty/est/op"
"github.com/viant/xunsafe"
"reflect"
)

var stringType = reflect.TypeOf("")

type Printer struct {
}

func (p Printer) Discover(aFunc interface{}) (func(operands []*op.Operand, state *est.State) (interface{}, error), reflect.Type, bool) {
switch actual := aFunc.(type) {
case func(_ Printer, args ...interface{}) string:
return func(operands []*op.Operand, state *est.State) (interface{}, error) {
return actual(p, p.asInterfaces(operands, state)), nil
}, stringType, true

case func(_ Printer, message string, args ...interface{}) string:
return func(operands []*op.Operand, state *est.State) (interface{}, error) {
if len(operands) < 1 {
return nil, fmt.Errorf("expected to get 1 or more arguments but got %v", len(operands))
}

format := *(*string)(operands[0].Exec(state))
args := p.asInterfaces(operands[1:], state)

return actual(p, format, args...), nil
}, stringType, true

}

return nil, nil, false
}

func (p Printer) asInterfaces(operands []*op.Operand, state *est.State) []interface{} {
args := make([]interface{}, len(operands))

for i, operand := range operands {
value := reflect.New(operand.Type).Elem().Interface()
xunsafe.Copy(xunsafe.AsPointer(value), operand.Exec(state), int(operand.Type.Size()))
args[i] = value
}

return args
}

func (p Printer) Println(args ...interface{}) string {
fmt.Println(args...)
return ""
}

func (p Printer) Printf(format string, args ...interface{}) string {
fmt.Printf(format, args...)
return ""
}
Loading

0 comments on commit e4c7859

Please sign in to comment.