-
Notifications
You must be signed in to change notification settings - Fork 2
/
convert.go
160 lines (151 loc) · 3.54 KB
/
convert.go
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Copyright 2021 Josh Deprez
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package yarn
import (
"fmt"
"math"
"strconv"
yarnpb "drjosh.dev/yarn/bytecode"
)
// ConvertToBool attempts conversion of the standard Yarn Spinner VM types
// (bool, number, string, null) to bool.
func ConvertToBool(x interface{}) (bool, error) {
if x == nil {
return false, nil
}
switch x := x.(type) {
case bool:
return x, nil
case float32:
return !math.IsNaN(float64(x)) && x != 0, nil
case float64:
return !math.IsNaN(x) && x != 0, nil
case int:
return x != 0, nil
case string:
return x != "", nil
default:
return false, fmt.Errorf("%T %w to bool", x, ErrNotConvertible)
}
}
// ConvertToInt attempts conversion of the standard Yarn Spinner VM types to
// (bool, number, string, null) to int.
func ConvertToInt(x interface{}) (int, error) {
if x == nil {
return 0, nil
}
switch t := x.(type) {
case bool:
if t {
return 1, nil
}
return 0, nil
case float32:
return int(t), nil
case float64:
return int(t), nil
case int:
return t, nil
case string:
return strconv.Atoi(t)
default:
if t == nil {
return 0, nil
}
return 0, fmt.Errorf("%T %w to int", x, ErrNotConvertible)
}
}
// ConvertToFloat32 attempts conversion of the standard Yarn Spinner VM types
// (bool, number, string, null) to a float32.
func ConvertToFloat32(x interface{}) (float32, error) {
if x == nil {
return 0, nil
}
switch t := x.(type) {
case bool:
if t {
return 1, nil
}
return 0, nil
case float32:
return t, nil
case float64:
return float32(t), nil
case int:
return float32(t), nil
case string:
y, err := strconv.ParseFloat(t, 32)
if err != nil {
return 0, err
}
return float32(y), nil
default:
if t == nil {
return 0, nil
}
return 0, fmt.Errorf("%T %w to float32", x, ErrNotConvertible)
}
}
// ConvertToFloat64 attempts conversion of the standard Yarn Spinner VM types
// (bool, number, string, null) to a float64.
func ConvertToFloat64(x interface{}) (float64, error) {
if x == nil {
return 0, nil
}
switch t := x.(type) {
case bool:
if t {
return 1, nil
}
return 0, nil
case float32:
return float64(t), nil
case float64:
return t, nil
case int:
return float64(t), nil
case string:
return strconv.ParseFloat(t, 64)
default:
if t == nil {
return 0, nil
}
return 0, fmt.Errorf("%T %w to float64", x, ErrNotConvertible)
}
}
// ConvertToString converts a value to a string, in a way that matches what Yarn
// Spinner does. nil becomes "null", and booleans are title-cased.
func ConvertToString(x interface{}) string {
if x == nil {
return "null"
}
if x, ok := x.(bool); ok {
if x {
return "True"
}
return "False"
}
return fmt.Sprint(x)
}
// operandToInt is a helper for turning a number value into an int.
func operandToInt(op *yarnpb.Operand) (int, error) {
if op == nil {
return 0, ErrNilOperand
}
f, ok := op.Value.(*yarnpb.Operand_FloatValue)
if !ok {
return 0, fmt.Errorf("%w for operand [%T != Operand_FloatValue]", ErrWrongType, op.Value)
}
return int(f.FloatValue), nil
}