-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtypeparser.go
94 lines (89 loc) · 1.59 KB
/
typeparser.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
package godatabend
import "fmt"
// TypeDesc describes a (possibly nested) data type returned by Databend.
type TypeDesc struct {
Name string
Nullable bool
Args []*TypeDesc
}
func ParseTypeDesc(s string) (*TypeDesc, error) {
var (
name = ""
args = []*TypeDesc{}
depth = 0
start = 0
nullable = false
)
for i, c := range s {
switch c {
case '(':
if depth == 0 {
name = s[start:i]
start = i + 1
}
depth++
case ')':
depth--
if depth == 0 {
s := s[start:i]
if s != "" {
desc, err := ParseTypeDesc(s)
if err != nil {
return nil, err
}
args = append(args, desc)
}
start = i + 1
}
case ',':
if depth == 1 {
s := s[start:i]
if s != "" {
desc, err := ParseTypeDesc(s)
if err != nil {
return nil, err
}
args = append(args, desc)
}
start = i + 1
}
case ' ':
if depth == 0 {
s := s[start:i]
if s != "" {
name = s
}
start = i + 1
}
}
}
if depth != 0 {
return nil, fmt.Errorf("invalid type desc: %s", s)
}
if start < len(s) {
s := s[start:]
if s != "" {
if name == "" {
name = s
} else if s == "NULL" {
nullable = true
} else {
return nil, fmt.Errorf("invalid type arg for %s: %s", name, s)
}
}
}
return &TypeDesc{Name: name, Nullable: nullable, Args: args}, nil
}
func (desc *TypeDesc) Normalize() *TypeDesc {
switch desc.Name {
case "Nullable":
sub := desc.Args[0].Normalize()
sub.Nullable = true
return sub
case "DateTime":
desc.Name = "Timestamp"
return desc
default:
return desc
}
}