MCPcopy Create free account
hub / github.com/araddon/qlbridge / Validate

Method Validate

expr/builtins/time.go:369–411  ·  view source on GitHub ↗
(n *expr.FuncNode)

Source from the content-addressed store, hash-verified

367// Type time
368func (m *ToDateIn) Type() value.ValueType { return value.TimeType }
369func (m *ToDateIn) Validate(n *expr.FuncNode) (expr.EvaluatorFunc, error) {
370 if len(n.Args) != 2 {
371 return nil, fmt.Errorf(`Expected args for todatein( (field | "now-3h" ), location) but got %s`, n)
372 }
373
374 sn, ok := n.Args[1].(*expr.StringNode)
375 if !ok {
376 return nil, fmt.Errorf("Expected a string literal value for location like America/Los_Angeles")
377 }
378
379 loc, err := time.LoadLocation(sn.Text)
380 if err != nil {
381 return nil, err
382 }
383
384 if sn, ok = n.Args[0].(*expr.StringNode); ok {
385 dateStr := sn.Text
386 if ok && len(dateStr) > 3 && strings.ToLower(dateStr[:3]) == "now" {
387 _, err = datemath.Eval(dateStr)
388 if err != nil {
389 return nil, fmt.Errorf("%q was not able to be parsed %v", dateStr, err)
390 }
391 // Is date math
392 return func(_ expr.EvalContext, _ []value.Value) (value.Value, bool) {
393 t, _ := datemath.Eval(dateStr)
394 return value.NewTimeValue(t), true
395 }, nil
396 }
397 }
398
399 // Return the Evaluator
400 return func(_ expr.EvalContext, args []value.Value) (value.Value, bool) {
401 valueDateStr, ok := value.ValueToString(args[0])
402 if !ok {
403 return value.TimeZeroValue, false
404 }
405 if t, err := dateparse.ParseIn(valueDateStr, loc); err == nil {
406 // We are going to correct back to UTC. so all fields are in UTC.
407 return value.NewTimeValue(t.In(time.UTC)), true
408 }
409 return value.TimeZeroValue, false
410 }, nil
411}
412
413// TimeSeconds time in Seconds, parses a variety of formats looking for seconds
414// See github.com/araddon/dateparse for formats supported on date parsing

Callers

nothing calls this directly

Calls 4

NewTimeValueFunction · 0.92
ValueToStringFunction · 0.92
ErrorfMethod · 0.80
EvalMethod · 0.80

Tested by

no test coverage detected