forked from fraugster/parquet-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshaller.go
196 lines (164 loc) · 4.69 KB
/
marshaller.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package interfaces
import "github.com/fraugster/parquet-go/parquetschema"
// Marshaller is the interface necessary for objects to be
// marshalled when passed to the (*Writer).WriteRecord method.
type Marshaller interface {
MarshalParquet(obj MarshalObject) error
}
// MarshalObject is the interface a Marshaller needs to marshall its data
// to.
type MarshalObject interface {
AddField(field string) MarshalElement
GetData() map[string]interface{}
}
// MarshalElement describes the interface to set the value of an element in a Marshaller
// implementation.
type MarshalElement interface {
Group() MarshalObject
SetInt32(i int32)
SetInt64(i int64)
SetInt96(i [12]byte)
SetFloat32(f float32)
SetFloat64(f float64)
SetBool(b bool)
SetByteArray(data []byte)
List() MarshalList
Map() MarshalMap
}
// MarshalList describes the interface to add a list of values in a Marshaller
// implementation.
type MarshalList interface {
Add() MarshalElement
}
// MarshalMap describes the interface to add a map of keys and values in a Marshaller
// implementation.
type MarshalMap interface {
Add() MarshalMapElement
}
// MarshalMapElement describes the interfaces to set an individual pair of keys and
// values in a Marshaller implementation.
type MarshalMapElement interface {
Key() MarshalElement
Value() MarshalElement
}
type object struct {
data map[string]interface{}
schema *parquetschema.SchemaDefinition
}
func (o *object) GetData() map[string]interface{} {
return o.data
}
func (o *object) AddField(field string) MarshalElement {
return &element{data: o.data, f: field, schema: o.schema.SubSchema(field)}
}
type element struct {
data map[string]interface{}
f string
schema *parquetschema.SchemaDefinition
}
func (e *element) SetInt32(i int32) {
e.data[e.f] = i
}
func (e *element) SetInt64(i int64) {
e.data[e.f] = i
}
func (e *element) SetInt96(i [12]byte) {
e.data[e.f] = i
}
func (e *element) SetFloat32(f float32) {
e.data[e.f] = f
}
func (e *element) SetFloat64(f float64) {
e.data[e.f] = f
}
func (e *element) SetBool(b bool) {
e.data[e.f] = b
}
func (e *element) SetByteArray(data []byte) {
e.data[e.f] = data
}
func (e *element) List() MarshalList {
listName := "list"
elemName := "element"
bagSchema := e.schema.SubSchema("bag")
if bagSchema != nil {
listName = "bag"
elemName = "array_element"
}
data := map[string]interface{}{listName: []map[string]interface{}{}}
e.data[e.f] = data
return &list{data: data, listName: listName, elemName: elemName, schema: e.schema.SubSchema(listName).SubSchema(elemName)}
}
func (e *element) Map() MarshalMap {
data := map[string]interface{}{"key_value": []map[string]interface{}{}}
e.data[e.f] = data
return &marshMap{data: data, schema: e.schema}
}
func (e *element) Group() MarshalObject {
obj := map[string]interface{}{}
e.data[e.f] = obj
return &object{data: obj}
}
type list struct {
data map[string]interface{}
schema *parquetschema.SchemaDefinition
listName string
elemName string
}
func (l *list) Add() MarshalElement {
listData := l.data[l.listName].([]map[string]interface{})
elemData := map[string]interface{}{}
l.data[l.listName] = append(listData, elemData)
e := &element{data: elemData, f: l.elemName, schema: l.schema}
return e
}
type marshMap struct {
data map[string]interface{}
schema *parquetschema.SchemaDefinition
}
func (l *marshMap) Add() MarshalMapElement {
kvData := l.data["key_value"].([]map[string]interface{})
elemData := map[string]interface{}{}
l.data["key_value"] = append(kvData, elemData)
me := &mapElement{data: elemData, schema: l.schema.SubSchema("key_value")}
return me
}
type mapElement struct {
data map[string]interface{}
schema *parquetschema.SchemaDefinition
}
func (m *mapElement) Key() MarshalElement {
return &element{data: m.data, f: "key", schema: m.schema.SubSchema("key")}
}
func (m *mapElement) Value() MarshalElement {
return &element{data: m.data, f: "value", schema: m.schema.SubSchema("value")}
}
// NewMarshallObject creates a new marshaller object
func NewMarshallObject(data map[string]interface{}) MarshalObject {
if data == nil {
data = make(map[string]interface{})
}
return &object{
data: data,
}
}
// NewMarshallObjectWithSchema creates a new marshaller object with a particular schema.
func NewMarshallObjectWithSchema(data map[string]interface{}, schemaDef *parquetschema.SchemaDefinition) MarshalObject {
if data == nil {
data = make(map[string]interface{})
}
return &object{
data: data,
schema: schemaDef,
}
}
// NewMarshalElement creates new marshall element object
func NewMarshalElement(data map[string]interface{}, name string) MarshalElement {
if data == nil {
data = make(map[string]interface{})
}
return &element{
data: data,
f: name,
}
}