package runs import ( "errors" "strings" ) // condition is a step's when: a reference that must be truthy, optionally // negated with !, or compared with == or != to a literal. type condition struct { path string negate bool operator string operand string } func parseCondition(source string) (condition, error) { source = strings.TrimSpace(source) for _, operator := range []string{"==", "!="} { if left, right, ok := strings.Cut(source, operator); ok { return condition{path: strings.TrimSpace(left), operator: operator, operand: strings.Trim(strings.TrimSpace(right), `"'`)}, nil } } c := condition{path: source} if rest, ok := strings.CutPrefix(source, "!"); ok { c = condition{path: strings.TrimSpace(rest), negate: true} } if c.path == "" { return condition{}, errors.New("when names nothing") } return c, nil } func (c condition) holds(values map[string]any) bool { value := lookupPath(values, c.path) switch c.operator { case "==": return stringify(value) == c.operand case "!=": return stringify(value) != c.operand } return truthy(value) != c.negate } func truthy(value any) bool { switch v := value.(type) { case nil: return false case bool: return v case string: return v != "" && v != "false" case float64: return v != 0 case int: return v != 0 } return true }