-
Notifications
You must be signed in to change notification settings - Fork 4
/
fn_parse_int.go
110 lines (89 loc) · 2.28 KB
/
fn_parse_int.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// 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 (
"strconv"
"github.com/GuanceCloud/platypus/pkg/ast"
"github.com/GuanceCloud/platypus/pkg/engine/runtime"
"github.com/GuanceCloud/platypus/pkg/errchain"
)
func ParseIntChecking(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError {
if err := reindexFuncArgs(funcExpr, []string{
"val", "base",
}, 2); err != nil {
return runtime.NewRunError(ctx, err.Error(), funcExpr.NamePos)
}
return nil
}
func ParseInt(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError {
val, _, err := runtime.RunStmt(ctx, funcExpr.Param[0])
if err != nil {
return err
}
if val == nil {
ctx.Regs.ReturnAppend(int64(0), ast.Int)
return nil
}
valStr, ok := val.(string)
if !ok {
ctx.Regs.ReturnAppend(int64(0), ast.Int)
return nil
}
base, _, err := runtime.RunStmt(ctx, funcExpr.Param[1])
if err != nil {
return err
}
if base == nil {
ctx.Regs.ReturnAppend(int64(0), ast.Int)
return nil
}
baseInt, ok := base.(int64)
if !ok {
ctx.Regs.ReturnAppend(int64(0), ast.Int)
return nil
}
v, _ := strconv.ParseInt(valStr, int(baseInt), 64)
ctx.Regs.ReturnAppend(v, ast.Int)
return nil
}
func FormatIntChecking(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError {
if err := reindexFuncArgs(funcExpr, []string{
"val", "base",
}, 2); err != nil {
return runtime.NewRunError(ctx, err.Error(), funcExpr.NamePos)
}
return nil
}
func FormatInt(ctx *runtime.Context, funcExpr *ast.CallExpr) *errchain.PlError {
val, _, err := runtime.RunStmt(ctx, funcExpr.Param[0])
if err != nil {
return err
}
if val == nil {
ctx.Regs.ReturnAppend("", ast.Int)
return nil
}
valStr, ok := val.(int64)
if !ok {
ctx.Regs.ReturnAppend("", ast.Int)
return nil
}
base, _, err := runtime.RunStmt(ctx, funcExpr.Param[1])
if err != nil {
return err
}
if base == nil {
ctx.Regs.ReturnAppend("", ast.Int)
return nil
}
baseInt, ok := base.(int64)
if !ok {
ctx.Regs.ReturnAppend("", ast.Int)
return nil
}
v := strconv.FormatInt(valStr, int(baseInt))
ctx.Regs.ReturnAppend(v, ast.String)
return nil
}