-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathdefault_funcs.go
More file actions
78 lines (62 loc) · 1.49 KB
/
Copy pathdefault_funcs.go
File metadata and controls
78 lines (62 loc) · 1.49 KB
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
package jsonschema
import (
"strconv"
"strings"
"time"
)
// FunctionCall represents a parsed function call with name and arguments
type FunctionCall struct {
Name string
Args []any
}
// parseFunctionCall parses a string to determine if it's a function call.
// It returns nil when the input is not in function-call form.
func parseFunctionCall(input string) *FunctionCall {
if len(input) < 3 || !strings.HasSuffix(input, ")") {
return nil
}
parenIndex := strings.IndexByte(input, '(')
if parenIndex <= 0 {
return nil
}
name := strings.TrimSpace(input[:parenIndex])
rawArgs := strings.TrimSpace(input[parenIndex+1 : len(input)-1])
var args []any
if rawArgs != "" {
args = parseArgs(rawArgs)
}
return &FunctionCall{
Name: name,
Args: args,
}
}
func parseArgs(raw string) []any {
args := make([]any, 0)
for part := range strings.SplitSeq(raw, ",") {
part = strings.TrimSpace(part)
if part == "" {
continue
}
if i, err := strconv.ParseInt(part, 10, 64); err == nil {
args = append(args, i)
continue
}
if f, err := strconv.ParseFloat(part, 64); err == nil {
args = append(args, f)
continue
}
args = append(args, part)
}
return args
}
// DefaultNowFunc generates current timestamp in various formats
// This function must be manually registered by developers
func DefaultNowFunc(args ...any) (any, error) {
format := time.RFC3339
if len(args) > 0 {
if f, ok := args[0].(string); ok {
format = f
}
}
return time.Now().Format(format), nil
}