github.com/kaptinlin/jsonschema@v0.4.6/default_funcs.go (about) 1 package jsonschema 2 3 import ( 4 "strconv" 5 "strings" 6 "time" 7 ) 8 9 // FunctionCall represents a parsed function call with name and arguments 10 type FunctionCall struct { 11 Name string 12 Args []any 13 } 14 15 // parseFunctionCall parses a string to determine if it's a function call 16 // Returns nil if the string is not a function call format 17 func parseFunctionCall(input string) (*FunctionCall, error) { 18 // Check if it's in function call format: functionName() 19 if len(input) < 3 || !strings.HasSuffix(input, ")") { 20 return nil, nil // Not a function call 21 } 22 23 parenIndex := strings.IndexByte(input, '(') 24 if parenIndex <= 0 { 25 return nil, nil // Not a function call 26 } 27 28 name := strings.TrimSpace(input[:parenIndex]) 29 argsStr := strings.TrimSpace(input[parenIndex+1 : len(input)-1]) 30 31 // Parse arguments 32 var args []any 33 if argsStr != "" { 34 args = parseArgs(argsStr) 35 } 36 37 return &FunctionCall{ 38 Name: name, 39 Args: args, 40 }, nil 41 } 42 43 // parseArgs parses function arguments from a string 44 // Simple implementation that handles basic types 45 func parseArgs(argsStr string) []any { 46 parts := strings.Split(argsStr, ",") 47 args := make([]any, 0, len(parts)) 48 49 for _, part := range parts { 50 part = strings.TrimSpace(part) 51 if part == "" { 52 continue 53 } 54 55 // Try to parse as integer 56 if i, err := strconv.ParseInt(part, 10, 64); err == nil { 57 args = append(args, i) 58 continue 59 } 60 61 // Try to parse as float 62 if f, err := strconv.ParseFloat(part, 64); err == nil { 63 args = append(args, f) 64 continue 65 } 66 67 // Default to string 68 args = append(args, part) 69 } 70 71 return args 72 } 73 74 // DefaultNowFunc generates current timestamp in various formats 75 // This function must be manually registered by developers 76 func DefaultNowFunc(args ...any) (any, error) { 77 format := time.RFC3339 // Default format 78 79 if len(args) > 0 { 80 if f, ok := args[0].(string); ok { 81 format = f 82 } 83 } 84 85 return time.Now().Format(format), nil 86 }