Files
core/solutions/catalog/cond.go
T

323 lines
6.8 KiB
Go
Raw Normal View History

package catalog
import (
"fmt"
"sort"
"strconv"
"strings"
)
// Cond is a condition over wizard answers (ADR-0003). It is plain data:
//
// {q: v} answer equals v (a multiselect answer: contains v)
// {q: [a, b]} answer is one of a, b (multiselect: contains one)
// {q: {in: [..]}} same as a list
// {q: {not: v}} answer is not v (not one of a list)
// {q: {set: true|false}} answer is (not) given and non-empty
// {q: {gt|gte|lt|lte: n}} numeric comparison
// {q: {count_gte|count_lte: n}} number of list items
// {all: [c, ..]} {any: [c, ..]} {not: c}
// {$mode: guided} wizard mode; {$platform: linux}
//
// Several keys in one map must all hold. An empty condition is true.
type Cond map[string]any
// Env is what a condition is evaluated against.
type Env struct {
Answers map[string]any
Mode Level
Platform string
}
// MaxCondDepth bounds nesting (RULE-08).
const MaxCondDepth = 16
// Eval evaluates c. Errors are malformed conditions (the linter reports
// them before a catalog is used).
func (c Cond) Eval(env Env) (bool, error) { return evalMap(map[string]any(c), env, 0) }
// Holds is Eval for callers that validated the catalog: a malformed
// condition counts as false.
func (c Cond) Holds(env Env) bool {
ok, err := c.Eval(env)
return err == nil && ok
}
func evalMap(m map[string]any, env Env, depth int) (bool, error) {
if depth > MaxCondDepth {
return false, fmt.Errorf("condition nested deeper than %d", MaxCondDepth)
}
keys := sortedKeys(m)
for _, k := range keys {
v := m[k]
var ok bool
var err error
switch k {
case "all", "any":
list, isList := v.([]any)
if !isList {
return false, fmt.Errorf("%s: want a list of conditions", k)
}
ok = k == "all"
for _, item := range list {
sub, isMap := asMap(item)
if !isMap {
return false, fmt.Errorf("%s: want a list of conditions", k)
}
r, err := evalMap(sub, env, depth+1)
if err != nil {
return false, err
}
if k == "all" && !r {
ok = false
break
}
if k == "any" && r {
ok = true
break
}
}
case "not":
sub, isMap := asMap(v)
if !isMap {
return false, fmt.Errorf("not: want a condition")
}
r, err := evalMap(sub, env, depth+1)
if err != nil {
return false, err
}
ok = !r
case "$mode":
ok, err = match(string(env.Mode), v)
case "$platform":
ok, err = match(env.Platform, v)
default:
ok, err = match(env.Answers[k], v)
}
if err != nil {
return false, fmt.Errorf("%s: %w", k, err)
}
if !ok {
return false, nil
}
}
return true, nil
}
// match compares one answer with a condition value.
func match(answer any, want any) (bool, error) {
if ops, ok := asMap(want); ok {
for _, op := range sortedKeys(ops) {
arg := ops[op]
var r bool
switch op {
case "in":
r = matchAny(answer, arg)
case "not":
r = !matchAny(answer, arg)
case "set":
b, isBool := arg.(bool)
if !isBool {
return false, fmt.Errorf("set: want true or false")
}
r = IsSet(answer) == b
case "gt", "gte", "lt", "lte":
a, aok := toFloat(answer)
n, nok := toFloat(arg)
if !nok {
return false, fmt.Errorf("%s: want a number", op)
}
if !aok {
r = false
break
}
switch op {
case "gt":
r = a > n
case "gte":
r = a >= n
case "lt":
r = a < n
case "lte":
r = a <= n
}
case "count_gte", "count_lte":
n, nok := toFloat(arg)
if !nok {
return false, fmt.Errorf("%s: want a number", op)
}
cnt := float64(len(asList(answer)))
if op == "count_gte" {
r = cnt >= n
} else {
r = cnt <= n
}
default:
return false, fmt.Errorf("unknown operator %q", op)
}
if !r {
return false, nil
}
}
return true, nil
}
return matchAny(answer, want), nil
}
// matchAny: want is a scalar or a list of scalars; answer a scalar or list.
func matchAny(answer any, want any) bool {
wants := asList(want)
if want != nil && len(wants) == 0 {
if _, isList := want.([]any); !isList {
wants = []any{want}
}
}
answers := asList(answer)
if _, isList := answer.([]any); !isList {
if _, isStrList := answer.([]string); !isStrList {
answers = []any{answer}
}
}
for _, a := range answers {
for _, w := range wants {
if Equal(a, w) {
return true
}
}
}
return false
}
// Equal compares two scalar answer values loosely: numbers by value,
// everything else by its string form ("true" == true).
func Equal(a, b any) bool {
if a == nil || b == nil {
return a == nil && b == nil
}
if fa, ok := toFloat(a); ok {
if fb, ok := toFloat(b); ok {
return fa == fb
}
}
return Scalar(a) == Scalar(b)
}
// Scalar renders a scalar answer as text.
func Scalar(v any) string {
switch x := v.(type) {
case nil:
return ""
case string:
return x
case bool:
return strconv.FormatBool(x)
case int:
return strconv.Itoa(x)
case int64:
return strconv.FormatInt(x, 10)
case float64:
return strconv.FormatFloat(x, 'f', -1, 64)
}
return fmt.Sprint(v)
}
// IsSet reports whether an answer is present and not empty.
func IsSet(v any) bool {
switch x := v.(type) {
case nil:
return false
case string:
return strings.TrimSpace(x) != ""
case []any:
return len(x) > 0
case []string:
return len(x) > 0
case map[string]any:
return len(x) > 0
}
return true
}
func toFloat(v any) (float64, bool) {
switch x := v.(type) {
case int:
return float64(x), true
case int64:
return float64(x), true
case float64:
return x, true
case uint64:
return float64(x), true
}
return 0, false
}
func asList(v any) []any {
switch x := v.(type) {
case []any:
return x
case []string:
out := make([]any, len(x))
for i, s := range x {
out[i] = s
}
return out
}
return nil
}
func asMap(v any) (map[string]any, bool) {
switch x := v.(type) {
case map[string]any:
return x, true
case Cond:
return map[string]any(x), true
}
return nil, false
}
func sortedKeys(m map[string]any) []string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
// Refs returns the question IDs a condition reads (for the linter).
func (c Cond) Refs() []string {
seen := map[string]bool{}
var walk func(m map[string]any, depth int)
walk = func(m map[string]any, depth int) {
if depth > MaxCondDepth {
return
}
for k, v := range m {
switch k {
case "all", "any":
if list, ok := v.([]any); ok {
for _, item := range list {
if sub, ok := asMap(item); ok {
walk(sub, depth+1)
}
}
}
case "not":
if sub, ok := asMap(v); ok {
walk(sub, depth+1)
}
case "$mode", "$platform":
default:
seen[k] = true
}
}
}
walk(map[string]any(c), 0)
out := make([]string, 0, len(seen))
for k := range seen {
out = append(out, k)
}
sort.Strings(out)
return out
}