-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.go
108 lines (86 loc) · 2.61 KB
/
type.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
package main
import (
"fmt"
"regexp"
"strings"
)
type baseType string
const (
typeUnknown baseType = ""
typeText baseType = "text"
typeNumber baseType = "number"
typeDate baseType = "date"
typeTime baseType = "time"
typeDatetime baseType = "datetime"
typeBool baseType = "bool"
typeFormula baseType = "formula"
)
func (t baseType) derive(explicitInputFormat, explicitOutputFormat string) derivedType {
derived := derivedType{
baseType: t,
}
implicit, found := implicitInputFormats[t]
if found {
derived.implicitInputFormat = implicit
}
implicit, found = implicitOutputFormats[t]
if found {
derived.implicitOutputFormat = implicit
}
derived.explicitInputFormat = explicitInputFormat
derived.explicitOutputFormat = explicitOutputFormat
return derived
}
type derivedType struct {
baseType baseType
explicitInputFormat string
implicitInputFormat string
explicitOutputFormat string
implicitOutputFormat string
}
func (t derivedType) String() string {
s := string(t.baseType)
if t.explicitInputFormat != "" || t.implicitInputFormat != "" || t.explicitOutputFormat != "" || t.implicitOutputFormat != "" {
s += "("
if t.explicitInputFormat != "" && t.implicitInputFormat != "" {
s += t.explicitInputFormat + " or " + t.implicitInputFormat
} else if t.explicitInputFormat != "" {
s += t.explicitInputFormat
} else if t.implicitInputFormat != "" {
s += t.implicitInputFormat
}
if t.explicitOutputFormat != "" || t.implicitOutputFormat != "" {
s += "->"
}
if t.explicitOutputFormat != "" {
s += t.explicitOutputFormat
} else if t.implicitOutputFormat != "" {
s += t.implicitOutputFormat
}
s += ")"
}
return s
}
var implicitInputFormats map[baseType]string
var implicitOutputFormats map[baseType]string
func parseType(s string) (derivedType, error) {
declRE := regexp.MustCompile(`(text|number|datetime|date|time|bool|formula)(?:\((.*?)(?:->(.+))?\))?`)
subs := declRE.FindStringSubmatch(s)
if subs == nil {
return derivedType{}, fmt.Errorf("invalid type declaration %q", s)
}
base := baseType(subs[1])
derived := base.derive(strings.TrimSpace(subs[2]), strings.TrimSpace(subs[3]))
return derived, nil
}
func initImplicitDecls(dil, dol, til, tol, dtil, dtol, nol string) {
implicitInputFormats = make(map[baseType]string)
implicitInputFormats[typeDate] = dil
implicitInputFormats[typeTime] = til
implicitInputFormats[typeDatetime] = dtil
implicitOutputFormats = make(map[baseType]string)
implicitOutputFormats[typeDate] = dol
implicitOutputFormats[typeTime] = tol
implicitOutputFormats[typeDatetime] = dtol
implicitOutputFormats[typeNumber] = nol
}