aboutsummaryrefslogtreecommitdiff
path: root/model/value.go
blob: 9fa8e9be4744ec3950dc0620da9b9dda1ed9a3fe (plain) (blame)
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
79
80
81
82
83
84
85
package model

import (
	"fmt"
	"strconv"
	"strings"
)

type Value any

type Values []Value

// BuildString собирает из значений Value цельную строку, при этом приводя все
// значения к типу string. Так же принимает функции типа WordLookup, которые
// последовательно будут пытаться привести значения типа Word к
// контекстозависимым значениям. Например, пытаться находить по имени переменную
// окружения ОС.
func (v Values) BuildString(lookups ...WordLookup) string {
	sw := strings.Builder{}

	for _, v := range v {
		switch v := v.(type) {
		case string:
			sw.WriteString(v)
		case float64:
			sw.WriteString(strconv.FormatFloat(v, 'f', 5, 64))
		case int:
			sw.WriteString(strconv.Itoa(v))
		case bool:
			if v {
				sw.WriteString("true")
				continue
			}
			sw.WriteString("false")
		case Word:
			sw.WriteString(chainLookup(lookups...)(v))
		}
	}

	return sw.String()
}

func (v Values) String() string {
	result := make([]string, 0, len(v))

	for _, v := range v {
		switch v := v.(type) {
		case string:
			result = append(result, v)
		case float64:
			result = append(result, strconv.FormatFloat(v, 'f', 5, 64))
		case int:
			result = append(result, strconv.Itoa(v))
		case bool:
			if v {
				result = append(result, "true")
				continue
			}
			result = append(result, "false")
		case Word:
			result = append(result, string(v))
		}
	}

	return strings.Join(result, " ")
}

func (v Values) Int() (int, error) {
	if len(v) != 1 {
		return 0, fmt.Errorf("AsInt can return only single value (there is %d values)", len(v))
	}
	val := v[0]
	switch val := val.(type) {
	case int:
		return val, nil
	case string:
		return strconv.Atoi(val)
	case float64:
		return int(val), nil
	default:
		return 0, fmt.Errorf("invalid type for convert to int: %t", val)
	}
}

type Word string