forked from sqlc-dev/sqlc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_call.go
80 lines (70 loc) · 2.04 KB
/
func_call.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package validate
import (
"errors"
"fmt"
"github.com/michaelzx/sqlc-stand-alone-queries-struct/internal/config"
"github.com/michaelzx/sqlc-stand-alone-queries-struct/internal/sql/ast"
"github.com/michaelzx/sqlc-stand-alone-queries-struct/internal/sql/astutils"
"github.com/michaelzx/sqlc-stand-alone-queries-struct/internal/sql/catalog"
"github.com/michaelzx/sqlc-stand-alone-queries-struct/internal/sql/sqlerr"
)
type funcCallVisitor struct {
catalog *catalog.Catalog
settings config.CombinedSettings
err error
}
func (v *funcCallVisitor) Visit(node ast.Node) astutils.Visitor {
if v.err != nil {
return nil
}
call, ok := node.(*ast.FuncCall)
if !ok {
return v
}
fn := call.Func
if fn == nil {
return v
}
// Custom validation for sqlc.arg, sqlc.narg and sqlc.slice
// TODO: Replace this once type-checking is implemented
if fn.Schema == "sqlc" {
if !(fn.Name == "arg" || fn.Name == "narg" || fn.Name == "slice" || fn.Name == "embed") {
v.err = sqlerr.FunctionNotFound("sqlc." + fn.Name)
return nil
}
if len(call.Args.Items) != 1 {
v.err = &sqlerr.Error{
Message: fmt.Sprintf("expected 1 parameter to sqlc.%s; got %d", fn.Name, len(call.Args.Items)),
Location: call.Pos(),
}
return nil
}
switch n := call.Args.Items[0].(type) {
case *ast.A_Const:
case *ast.ColumnRef:
default:
v.err = &sqlerr.Error{
Message: fmt.Sprintf("expected parameter to sqlc.%s to be string or reference; got %T", fn.Name, n),
Location: call.Pos(),
}
return nil
}
// If we have sqlc.arg or sqlc.narg, there is no need to resolve the function call.
// It won't resolve anyway, sinc it is not a real function.
return nil
}
fun, err := v.catalog.ResolveFuncCall(call)
if fun != nil {
return v
}
if errors.Is(err, sqlerr.NotFound) && !v.settings.Package.StrictFunctionChecks {
return v
}
v.err = err
return nil
}
func FuncCall(c *catalog.Catalog, cs config.CombinedSettings, n ast.Node) error {
visitor := funcCallVisitor{catalog: c, settings: cs}
astutils.Walk(&visitor, n)
return visitor.err
}