-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprinter.go
148 lines (127 loc) · 4.03 KB
/
printer.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
package main
import (
"fmt"
"strings"
)
// Printer is an interface that prints ddl of hive.
type Printer interface {
Print() string
}
// PrimitivePrinter is a printer structure corresponding to the primitive type of hive.
type PrimitivePrinter struct {
depth int
colName string
typeName string
delimiter string
}
// StructPrinter is a printer structure corresponding to the struct type of hive.
type StructPrinter struct {
depth int
colName string
typeName string
delimiter string
members []Printer
}
// ArrayPrinter is a printer structure corresponding to the array type of hive.
type ArrayPrinter struct {
headerDepth int
fotterDepth int
colName string
typeName string
delimiter string
headerNewline string
fotterNewline string
member Printer
}
// NewPrimitivePrinter creates and returns a new PrimitivePrinter.
func NewPrimitivePrinter(depth int, colName, typeName, delimiter string) *PrimitivePrinter {
return &PrimitivePrinter{
depth: depth,
colName: colName,
typeName: typeName,
delimiter: delimiter,
}
}
// NewStructPrinter creates and returns a new StructPrinter.
func NewStructPrinter(depth int, colName, delimiter string, plist []Printer) *StructPrinter {
return &StructPrinter{
depth: depth,
colName: colName,
typeName: "struct",
delimiter: delimiter,
members: plist,
}
}
// NewPrimitiveArrayPrinter creates and returns a new ArrayPrinter whose element is a primitive type.
func NewPrimitiveArrayPrinter(depth int, colName, delimiter, elementType string) *ArrayPrinter {
p := NewPrimitivePrinter(0, "", elementType, "")
return &ArrayPrinter{
headerDepth: depth,
fotterDepth: 0,
colName: colName,
typeName: "array",
delimiter: delimiter,
headerNewline: "",
fotterNewline: "",
member: p,
}
}
// NewStructArrayPrinter creates and returns a new ArrayPrinter whose element is a struct type.
func NewStructArrayPrinter(depth int, colName, delimiter string, plist []Printer) *ArrayPrinter {
p := NewStructPrinter(depth+1, "", "", plist)
return &ArrayPrinter{
headerDepth: depth,
fotterDepth: depth,
colName: colName,
typeName: "array",
delimiter: delimiter,
headerNewline: "\n",
fotterNewline: "\n",
member: p,
}
}
// NewMultipleArrayPrinter creates and returns a new ArrayPrinter whose element is a array type.
func NewMultipleArrayPrinter(depth int, colName, delimiter string, p Printer) *ArrayPrinter {
return &ArrayPrinter{
headerDepth: depth,
fotterDepth: depth,
colName: colName,
typeName: "array",
delimiter: delimiter,
headerNewline: "\n",
fotterNewline: "\n",
member: p,
}
}
// PrintHeader prints header of hive ddl.
func PrintHeader() string {
return fmt.Sprint("create external table json_data(")
}
// PrintFooter prints footer of hive ddl.
func PrintFooter() string {
return fmt.Sprint(")")
}
// Print prints one line of hive ddl corresponding to the primitive type.
func (p PrimitivePrinter) Print() string {
return fmt.Sprintf("%s%s%s%s", printIndent(p.depth), p.colName, p.delimiter, p.typeName)
}
// Print prints one line of hive ddl corresponding to the primitive type.
func (p StructPrinter) Print() string {
structPirntHeader := fmt.Sprintf("%s%s%s%s<\n", printIndent(p.depth), p.colName, p.delimiter, p.typeName)
structPirntFooter := fmt.Sprintf("\n%s>", printIndent(p.depth))
var mPrints []string
for _, v := range p.members {
mPrints = append(mPrints, v.Print())
}
mPrint := strings.Join(mPrints, ",\n")
return structPirntHeader + mPrint + structPirntFooter
}
// Print prints one line of hive ddl corresponding to the array type.
func (p ArrayPrinter) Print() string {
structPirntHeader := fmt.Sprintf("%s%s%s%s<%s", printIndent(p.headerDepth), p.colName, p.delimiter, p.typeName, p.headerNewline)
structPirntFooter := fmt.Sprintf("%s%s>", p.fotterNewline, printIndent(p.fotterDepth))
return structPirntHeader + p.member.Print() + structPirntFooter
}
func printIndent(depth int) string {
return strings.Repeat(" ", depth)
}