-
Notifications
You must be signed in to change notification settings - Fork 0
/
column.go
164 lines (146 loc) · 4.6 KB
/
column.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
// Copyright 2019 Melvin Davis<hi@melvindavis.me>. All rights reserved.
// Use of this source code is governed by a Melvin Davis<hi@melvindavis.me>
// license that can be found in the LICENSE file.
package interpreter
import "encoding/json"
/*
* This file contains the defnition of column type node
*/
const (
//AggregationFnCount for count aggregation funtion
AggregationFnCount = "COUNT"
//AggregationFnSum for sum aggregation funtion
AggregationFnSum = "SUM"
//AggregationFnAvg for average aggregation funtion
AggregationFnAvg = "AVG"
)
//DefaultAggregationFn is the default aggregation function for all the columns
const DefaultAggregationFn = AggregationFnCount
const (
//DataTypeInt denotes integer data type
DataTypeInt = "INT"
//DataTypeFloat denotes float data type
DataTypeFloat = "FLOAT"
//DataTypeString denotes string data type
DataTypeString = "STRING"
//DataTypeDate denotes date data type
DataTypeDate = "DATE"
)
//ColumnNode is the node storing the information about a column.
//It can be the child of a Table Node and can have value as children
type ColumnNode struct {
//UID is the unique id of the column node
UID string
//Word is the word with which the column node has to be matched
Word []rune
//PN is the parent node of the column. It will be a table node
PN *TableNode
//PUID is the UID of column's parent node
PUID string
//Name is the name of the node
Name string
//Children are the children node of the column node
Children []ValueNode
//Resolved indicates that the node is resolved
Resolved bool
//Dimension indicates that the column can be used as dimension
Dimension bool
//Measure indicates that the column can be used as measure
Measure bool
//AggregationFn is preferred aggregation with the column node if used as a measure across a dimension
AggregationFn string
//DataType of the column
DataType string
//Description is the description about the column
Description string
//DateFormat is the format of the data if date type
DateFormat string
}
type columnNode struct {
UID string `json:"uid,omitempty"`
Word string `json:"word,omitempty"`
PUID string `json:"puid,omitempty"`
Name string `json:"name,omitempty"`
Children []ValueNode `json:"children,omitempty"`
Resolved bool `json:"resolved,omitempty"`
Type string `json:"type,omitempty"`
Dimension bool `json:"dimension,omitempty"`
Measure bool `json:"measure,omitempty"`
AggregationFn string `json:"aggregation_fn,omitempty"`
DataType string `json:"data_type,omitempty"`
Description string `json:"description"`
DateFormat string `json:"date_format"`
}
//Copy will return a copy of the node
func (c *ColumnNode) Copy() Node {
return &ColumnNode{
UID: c.UID,
Word: c.Word,
PN: c.PN,
PUID: c.PUID,
Name: c.Name,
Children: c.Children,
Resolved: c.Resolved,
Dimension: c.Dimension,
Measure: c.Measure,
AggregationFn: c.AggregationFn,
DataType: c.DataType,
Description: c.Description,
DateFormat: c.DateFormat,
}
}
//ID returns the unique id of the node
func (c *ColumnNode) ID() string {
return c.UID
}
//Type returns Column Type
func (c *ColumnNode) Type() Type {
return Column
}
//TokenWord returns the word property of the node
func (c *ColumnNode) TokenWord() []rune {
return c.Word
}
//PID returns the PUID if the node
func (c *ColumnNode) PID() string {
return c.PUID
}
//Parent returns the PN of the node
func (c *ColumnNode) Parent() Node {
return c.PN
}
//MarshalJSON encodes the node into a serializable json
func (c *ColumnNode) MarshalJSON() ([]byte, error) {
return json.Marshal(&columnNode{
c.UID, string(c.Word), c.PUID, c.Name, c.Children, c.Resolved, "Column", c.Dimension, c.Measure, c.AggregationFn, c.DataType, c.Description, c.DateFormat,
})
}
//UnmarshalJSON decodes the node from a json
func (c *ColumnNode) UnmarshalJSON(data []byte) error {
m := &ColumnNode{}
err := json.Unmarshal(data, m)
if err != nil {
return err
}
c.UID = m.UID
c.Word = []rune(m.Word)
c.PUID = m.PUID
c.Name = m.Name
c.Children = m.Children
c.Resolved = m.Resolved
c.Dimension = m.Dimension
c.Measure = m.Measure
c.AggregationFn = m.AggregationFn
c.DataType = m.DataType
c.Description = m.Description
c.DateFormat = m.DateFormat
return nil
}
//IsResolved will return true if the node is resolved
func (c *ColumnNode) IsResolved() bool {
return c.Resolved
}
//SetResolved will set the resolved state of the node
func (c *ColumnNode) SetResolved(state bool) {
c.Resolved = state
}