/
fn_len.go
40 lines (34 loc) · 1.08 KB
/
fn_len.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the MIT License.
// This product includes software developed at Guance Cloud (https://www.guance.com/).
// Copyright 2021-present Guance, Inc.
package funcs
import (
"fmt"
"github.com/GuanceCloud/ppl/pkg/ast"
"github.com/GuanceCloud/ppl/pkg/engine/runtime"
)
func LenChecking(ctx *runtime.Context, funcExpr *ast.CallExpr) *runtime.RuntimeError {
if len(funcExpr.Param) != 1 {
return runtime.NewRunError(ctx, fmt.Sprintf(
"func %s expected 1", funcExpr.Name), funcExpr.NamePos)
}
return nil
}
func Len(ctx *runtime.Context, funcExpr *ast.CallExpr) *runtime.RuntimeError {
val, dtype, err := runtime.RunStmt(ctx, funcExpr.Param[0])
if err != nil {
return err
}
switch dtype { //nolint:exhaustive
case ast.Map:
ctx.Regs.ReturnAppend(int64(len(val.(map[string]any))), ast.Int)
case ast.List:
ctx.Regs.ReturnAppend(int64(len(val.([]any))), ast.Int)
case ast.String:
ctx.Regs.ReturnAppend(int64(len(val.(string))), ast.Int)
default:
ctx.Regs.ReturnAppend(int64(0), ast.Int)
}
return nil
}