aboutsummaryrefslogtreecommitdiff
path: root/model/value.go
diff options
context:
space:
mode:
Diffstat (limited to 'model/value.go')
-rw-r--r--model/value.go43
1 files changed, 43 insertions, 0 deletions
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