From c167c0691b36b7b84af862561fd0b5c36c09afd8 Mon Sep 17 00:00:00 2001 From: klarysz Date: Mon, 15 Aug 2022 21:45:20 +0200 Subject: [PATCH] added logger --- executor/service.go | 2 - go.mod | 4 +- logger/printer.go | 9 ++-- router/router_test.go | 13 ++++- .../testdata/031_multiple_execs/resource.yaml | 4 +- .../testdata/035_logger/populate/events.json | 45 ++++++++++++++++++ router/testdata/035_logger/resource.yaml | 47 +++++++++++++++++++ view/placeholders.go | 44 +++++------------ 8 files changed, 123 insertions(+), 45 deletions(-) create mode 100644 router/testdata/035_logger/populate/events.json create mode 100644 router/testdata/035_logger/resource.yaml diff --git a/executor/service.go b/executor/service.go index 29992ef5..4460947d 100644 --- a/executor/service.go +++ b/executor/service.go @@ -3,7 +3,6 @@ package executor import ( "context" "database/sql" - "fmt" "github.com/viant/datly/shared" "sync" ) @@ -59,7 +58,6 @@ func (e *Executor) execData(ctx context.Context, wg *sync.WaitGroup, tx *sql.Tx, } func (e *Executor) executeStatement(ctx context.Context, tx *sql.Tx, stmt *SQLStatment) error { - fmt.Printf("executing: %v %v\n", stmt.SQL, stmt.Args) _, err := tx.ExecContext(ctx, stmt.SQL, stmt.Args...) return err } diff --git a/go.mod b/go.mod index 3cccae6d..2f650941 100644 --- a/go.mod +++ b/go.mod @@ -30,12 +30,10 @@ 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.20220815170357-0518dfc48b9d + github.com/viant/velty v0.1.1-0.20220815184724-5837b86c2a72 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 gopkg.in/go-playground/assert.v1 v1.2.1 // indirect gopkg.in/yaml.v3 v3.0.1 ) - -replace github.com/viant/sqlx v0.0.0-20220811162450-fda00ea81681 => /Users/awitas/go/src/github.com/viant/sqlx diff --git a/logger/printer.go b/logger/printer.go index 1ceea784..129ec3d9 100644 --- a/logger/printer.go +++ b/logger/printer.go @@ -6,6 +6,7 @@ import ( "github.com/viant/velty/est/op" "github.com/viant/xunsafe" "reflect" + "strings" ) var stringType = reflect.TypeOf("") @@ -17,7 +18,7 @@ func (p Printer) Discover(aFunc interface{}) (func(operands []*op.Operand, state 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 + return actual(p, p.asInterfaces(operands[1:], state)), nil }, stringType, true case func(_ Printer, message string, args ...interface{}) string: @@ -26,8 +27,8 @@ func (p Printer) Discover(aFunc interface{}) (func(operands []*op.Operand, state 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) + format := *(*string)(operands[1].Exec(state)) + args := p.asInterfaces(operands[2:], state) return actual(p, format, args...), nil }, stringType, true @@ -55,6 +56,6 @@ func (p Printer) Println(args ...interface{}) string { } func (p Printer) Printf(format string, args ...interface{}) string { - fmt.Printf(format, args...) + fmt.Printf(strings.ReplaceAll(format, "\\n", "\n"), args...) return "" } diff --git a/router/router_test.go b/router/router_test.go index 230a3979..3d9d5b5b 100644 --- a/router/router_test.go +++ b/router/router_test.go @@ -654,8 +654,8 @@ func TestRouter(t *testing.T) { visitors: map[string]codec.LifecycleVisitor{}, afterInsertUri: "/api/events?_criteria=Quantity=40", afterInsertMethod: http.MethodGet, - requestBody: `{"ID": [1,10,103], "Quantity": 40}`, afterInsertExpected: `[{"Id":1,"Timestamp":"2019-03-11T02:20:33Z","EventTypeId":2,"Quantity":40,"UserId":1},{"Id":10,"Timestamp":"2019-03-15T12:07:33Z","EventTypeId":11,"Quantity":40,"UserId":2},{"Id":103,"Timestamp":"2019-04-10T05:15:33Z","EventTypeId":111,"Quantity":40,"UserId":3}]`, + requestBody: `{"ID": [1,10,103], "Quantity": 40}`, }, { description: "extract values from Request Body", @@ -684,6 +684,17 @@ func TestRouter(t *testing.T) { requestBody: `[1,10,103]`, expected: `[{"Id":1,"Timestamp":"2019-03-11T02:20:33Z","EventTypeId":2,"Quantity":33.23432374000549,"UserId":1},{"Id":10,"Timestamp":"2019-03-15T12:07:33Z","EventTypeId":11,"Quantity":21.957962334156036,"UserId":2},{"Id":103,"Timestamp":"2019-04-10T05:15:33Z","EventTypeId":111,"Quantity":5.084940046072006,"UserId":3}]`, }, + { + description: "executor with param slice", + resourceURI: "035_logger", + uri: "/api/events", + method: http.MethodPost, + visitors: map[string]codec.LifecycleVisitor{}, + requestBody: `{"ID": [1,10,103], "Quantity": 0}`, + afterInsertUri: "/api/events", + afterInsertMethod: http.MethodGet, + afterInsertExpected: `[{"Id":1,"Timestamp":"2019-03-11T02:20:33Z","EventTypeId":2,"Quantity":0,"UserId":1},{"Id":10,"Timestamp":"2019-03-15T12:07:33Z","EventTypeId":11,"Quantity":0,"UserId":2},{"Id":100,"Timestamp":"2019-04-10T05:15:33Z","EventTypeId":111,"Quantity":5.084940046072006,"UserId":3},{"Id":101,"Timestamp":"2019-04-10T05:15:33Z","EventTypeId":111,"Quantity":5.084940046072006,"UserId":3},{"Id":102,"Timestamp":"2019-04-10T05:15:33Z","EventTypeId":111,"Quantity":5.084940046072006,"UserId":3},{"Id":103,"Timestamp":"2019-04-10T05:15:33Z","EventTypeId":111,"Quantity":0,"UserId":3}]`, + }, } //for i, tCase := range testcases[len(testcases)-1:] { diff --git a/router/testdata/031_multiple_execs/resource.yaml b/router/testdata/031_multiple_execs/resource.yaml index c70d3974..11e91b70 100644 --- a/router/testdata/031_multiple_execs/resource.yaml +++ b/router/testdata/031_multiple_execs/resource.yaml @@ -33,10 +33,8 @@ Routes: $errors.RegisterError("invalid status") #else - #set($index = 0) #foreach($ID in $Unsafe.Body.ID) - UPDATE events SET quantity = $criteria.Add($index, $Unsafe.Body.Quantity) WHERE id = $criteria.Add($index, $ID); - #set($index = $index+1) + UPDATE events SET quantity = $criteria.Add(0, $Unsafe.Body.Quantity) WHERE id = $criteria.Add(0, $ID); #end #end diff --git a/router/testdata/035_logger/populate/events.json b/router/testdata/035_logger/populate/events.json new file mode 100644 index 00000000..28ddad08 --- /dev/null +++ b/router/testdata/035_logger/populate/events.json @@ -0,0 +1,45 @@ +[ + {}, + { + "id": 1, + "event_type_id": 2, + "quantity": 33.23432374000549, + "timestamp": "2019-03-11 02:20:33", + "user_id": 1 + }, + { + "id": 10, + "event_type_id": 11, + "quantity": 21.957962334156036, + "timestamp": "2019-03-15 12:07:33", + "user_id": 2 + }, + { + "id": 100, + "event_type_id": 111, + "quantity": 5.084940046072006, + "timestamp": "2019-04-10 05:15:33", + "user_id": 3 + }, + { + "id": 101, + "event_type_id": 111, + "quantity": 5.084940046072006, + "timestamp": "2019-04-10 05:15:33", + "user_id": 3 + }, + { + "id": 102, + "event_type_id": 111, + "quantity": 5.084940046072006, + "timestamp": "2019-04-10 05:15:33", + "user_id": 3 + }, + { + "id": 103, + "event_type_id": 111, + "quantity": 5.084940046072006, + "timestamp": "2019-04-10 05:15:33", + "user_id": 3 + } +] \ No newline at end of file diff --git a/router/testdata/035_logger/resource.yaml b/router/testdata/035_logger/resource.yaml new file mode 100644 index 00000000..c5b7bde9 --- /dev/null +++ b/router/testdata/035_logger/resource.yaml @@ -0,0 +1,47 @@ +Routes: + - URI: "/api/events" + Method: POST + Service: Executor + + View: + Ref: events_ref + Name: events + Mode: SQLExec + Template: + Parameters: + - Name: Body + In: + Kind: body + Schema: + DataType: Body + Source: ' + #foreach($ID in $Unsafe.Body.ID) + UPDATE events SET quantity = $Unsafe.Body.Quantity WHERE id = $ID; + $logger.Printf("executing update stmt with params: [%v,%v]\n", $Unsafe.Body.Quantity, $ID) + #end + ' + - URI: "/api/events" + Method: GET + View: + Ref: events_ref + Name: events + +Resource: + Views: + - Name: events_ref + Connector: + Ref: db + Table: events + Selector: + Constraints: + Criteria: true + Filterable: [ '*' ] + + Types: + - Name: Body + DataType: 'struct {ID []int; Quantity float64 }' + + Connectors: + - Name: db + Driver: sqlite3 + DSN: "./testdata/db/db.db" \ No newline at end of file diff --git a/view/placeholders.go b/view/placeholders.go index 5613f572..157aa007 100644 --- a/view/placeholders.go +++ b/view/placeholders.go @@ -10,9 +10,8 @@ import ( type CriteriaSanitizer struct { Columns ColumnIndex - ParamsGroup [][]interface{} + ParamsGroup []interface{} Mock bool - GroupCounter int PlaceholderCounter int sliceIndex map[reflect.Type]*xunsafe.Slice } @@ -21,6 +20,10 @@ func (p *CriteriaSanitizer) AsBinding(value interface{}) string { return p.Add(0, value) } +func (p *CriteriaSanitizer) AppendBinding(value interface{}) string { + return p.Add(0, value) +} + func (p *CriteriaSanitizer) AsColumn(columnName string) (string, error) { lookup, err := p.Columns.Lookup(columnName) if err != nil { @@ -30,7 +33,7 @@ func (p *CriteriaSanitizer) AsColumn(columnName string) (string, error) { return lookup.Name, nil } -func (p *CriteriaSanitizer) Add(at int, value interface{}) string { +func (p *CriteriaSanitizer) Add(_ int, value interface{}) string { if value == nil { return "" } @@ -40,8 +43,7 @@ func (p *CriteriaSanitizer) Add(at int, value interface{}) string { return "" } - p.growIfNeeded(at) - p.ParamsGroup[at] = append(p.ParamsGroup[at], valueCopy...) + p.ParamsGroup = append(p.ParamsGroup, valueCopy...) return expanded } @@ -85,25 +87,8 @@ func (p *CriteriaSanitizer) copyAndExpandSlice(valueType reflect.Type, valuePtr } } -func (p *CriteriaSanitizer) growIfNeeded(at int) { - if len(p.ParamsGroup) > at { - return - } - - newParams := make([][]interface{}, at+1) - for i, group := range p.ParamsGroup { - newParams[i] = append(newParams[i], group...) - } - - p.ParamsGroup = newParams -} - -func (p *CriteriaSanitizer) At(i int) []interface{} { - if len(p.ParamsGroup) <= i { - return []interface{}{} - } - - return p.ParamsGroup[i] +func (p *CriteriaSanitizer) At(_ int) []interface{} { + return p.ParamsGroup } func (p *CriteriaSanitizer) Next() (interface{}, error) { @@ -112,18 +97,13 @@ func (p *CriteriaSanitizer) Next() (interface{}, error) { } for { - if p.GroupCounter >= len(p.ParamsGroup) { - return nil, fmt.Errorf("not found next binding variable") - } - - if p.PlaceholderCounter < len(p.ParamsGroup[p.GroupCounter]) { + if p.PlaceholderCounter < len(p.ParamsGroup) { index := p.PlaceholderCounter p.PlaceholderCounter++ - return p.ParamsGroup[p.GroupCounter][index], nil + return p.ParamsGroup[index], nil } - p.GroupCounter++ - p.PlaceholderCounter = 0 + return nil, fmt.Errorf("expected to got binding parameter, but noone was found") } }