-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
39 lines (36 loc) · 1.2 KB
/
util.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
package eval
import (
"fmt"
"go/ast"
"go/token"
"strings"
)
func DetectRootAggFunc(exp ast.Expr) (string, AggEnabledType, AggFuncType, []ast.Expr) {
if callExp, ok := exp.(*ast.CallExpr); ok {
funExp := callExp.Fun
if funIdentExp, ok := funExp.(*ast.Ident); ok {
if StringToAggFunc(funIdentExp.Name) != AggUnknown {
return funIdentExp.Name, AggFuncEnabled, StringToAggFunc(funIdentExp.Name), callExp.Args
}
}
}
return "", AggFuncDisabled, AggUnknown, nil
}
func GetAggStringSeparator(funcName string, aggFuncArgs []ast.Expr) (string, error) {
if funcName == string(AggStringAgg) && len(aggFuncArgs) != 2 {
return "", fmt.Errorf("%s must have two parameters", funcName)
} else if funcName == string(AggStringAggIf) && len(aggFuncArgs) != 3 {
return "", fmt.Errorf("%s must have three parameters", funcName)
}
switch separatorExpTyped := aggFuncArgs[1].(type) {
case *ast.BasicLit:
switch separatorExpTyped.Kind {
case token.STRING:
return strings.Trim(separatorExpTyped.Value, "\""), nil
default:
return "", fmt.Errorf("string_agg/if second parameter must be a constant string")
}
default:
return "", fmt.Errorf("string_agg/if second parameter must be a basic literal")
}
}