diff options
| author | 2026-03-09 23:05:42 +0300 | |
|---|---|---|
| committer | 2026-03-09 23:05:42 +0300 | |
| commit | 00394a80501960ad26787b5c44435ed5ed67ad84 (patch) | |
| tree | 672eb918c552c858f32e9533dc3799af6b75769f /model/value.go | |
| parent | '-' sign in words accepted (diff) | |
| download | conf-0.1.0.tar.gz conf-0.1.0.tar.bz2 conf-0.1.0.tar.xz conf-0.1.0.zip | |
Полностью переписал библиотеку. Перевёл с EBNF на PEG.v0.1.0
Diffstat (limited to '')
| -rw-r--r-- | model/value.go | 85 |
1 files changed, 0 insertions, 85 deletions
diff --git a/model/value.go b/model/value.go deleted file mode 100644 index 9fa8e9b..0000000 --- a/model/value.go +++ /dev/null @@ -1,85 +0,0 @@ -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 |
