/
types.go
50 lines (40 loc) · 1.03 KB
/
types.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
package execution
import (
"encoding/json"
"strconv"
"time"
"github.com/cube2222/octosql"
)
type Datatype string
const (
DatatypeBoolean Datatype = "boolean"
DatatypeInt Datatype = "int"
DatatypeFloat64 Datatype = "float64"
DatatypeString Datatype = "string"
DatatypeTuple Datatype = "octosql.Value"
)
// ParseType tries to parse the given string into any type it succeeds to. Returns back the string on failure.
func ParseType(str string) octosql.Value {
integer, err := strconv.ParseInt(str, 10, 64)
if err == nil {
return octosql.MakeInt(int(integer))
}
float, err := strconv.ParseFloat(str, 64)
if err == nil {
return octosql.MakeFloat(float)
}
boolean, err := strconv.ParseBool(str)
if err == nil {
return octosql.MakeBool(boolean)
}
var jsonObject map[string]interface{}
err = json.Unmarshal([]byte(str), &jsonObject)
if err == nil {
return octosql.NormalizeType(jsonObject)
}
t, err := time.Parse(time.RFC3339Nano, str)
if err == nil {
return octosql.MakeTime(t)
}
return octosql.MakeString(str)
}