diff options
| author | 2026-02-21 18:34:50 +0300 | |
|---|---|---|
| committer | 2026-02-21 18:34:50 +0300 | |
| commit | 815bd56f4160859644f7897082e6b171a51617ed (patch) | |
| tree | 57175fe5469c545903dba59586d265cb259c5357 | |
| parent | Сделал $ разрешенным символом в словах (diff) | |
| download | conf-815bd56f4160859644f7897082e6b171a51617ed.tar.gz conf-815bd56f4160859644f7897082e6b171a51617ed.tar.bz2 conf-815bd56f4160859644f7897082e6b171a51617ed.tar.xz conf-815bd56f4160859644f7897082e6b171a51617ed.zip | |
| -rw-r--r-- | model/lookup.go | 9 | ||||
| -rw-r--r-- | model/value.go | 43 |
2 files changed, 51 insertions, 1 deletions
diff --git a/model/lookup.go b/model/lookup.go index e8cc8bf..880a1f8 100644 --- a/model/lookup.go +++ b/model/lookup.go @@ -19,7 +19,7 @@ func chainLookup(lookups ...WordLookup) WordLookup { return v } } - return string(word) + return "" } } @@ -47,3 +47,10 @@ func LookupSubst(subst map[Word]string) WordLookup { return "" } } + +// Origin возвращает просто строковое представления слова. Если поставить в +// конце цепочки - то вместо пустоты (если предыдущие фильтры не сработали) +// вернётся оригинальное имя слова. +func Origin(word Word) string { + return string(word) +} diff --git a/model/value.go b/model/value.go index 4ec7344..9fa8e9b 100644 --- a/model/value.go +++ b/model/value.go @@ -1,6 +1,7 @@ package model import ( + "fmt" "strconv" "strings" ) @@ -39,4 +40,46 @@ func (v Values) BuildString(lookups ...WordLookup) string { 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 |
