1
1
import copy
2
2
import traceback
3
- import rich
4
3
5
4
from pprint import pformat
6
- from lkml2cube .parser .types import type_map , literal_unicode
7
-
8
- console = rich .console .Console ()
5
+ from lkml2cube .parser .types import type_map , literal_unicode , folded_unicode , console
9
6
10
7
11
8
def parse_view (lookml_model , raise_when_views_not_present = True ):
12
9
cubes = []
13
10
cube_def = {"cubes" : cubes }
14
11
rpl_table = lambda s : s .replace ("${TABLE}" , "{CUBE}" ).replace ("${" , "{" )
12
+ convert_to_literal = lambda s : (
13
+ literal_unicode (rpl_table (s )) if "\n " in s else rpl_table (s )
14
+ )
15
15
sets = {}
16
16
17
17
if raise_when_views_not_present and "views" not in lookml_model :
@@ -102,10 +102,21 @@ def parse_view(lookml_model, raise_when_views_not_present=True):
102
102
103
103
cube_dimension = {
104
104
"name" : dimension ["name" ],
105
- "sql" : rpl_table (dimension ["sql" ]),
105
+ "sql" : convert_to_literal (dimension ["sql" ]),
106
106
"type" : type_map [dimension ["type" ]],
107
107
}
108
108
109
+ if "primary_key" in dimension :
110
+ cube_dimension ["primary_key" ] = bool (
111
+ dimension ["primary_key" ] == "yes"
112
+ )
113
+
114
+ if "label" in dimension :
115
+ cube_dimension ["title" ] = dimension ["label" ]
116
+
117
+ if "description" in dimension :
118
+ cube_dimension ["description" ] = dimension ["description" ]
119
+
109
120
if "hidden" in dimension :
110
121
cube_dimension ["public" ] = not bool (dimension ["hidden" ] == "yes" )
111
122
@@ -121,13 +132,17 @@ def parse_view(lookml_model, raise_when_views_not_present=True):
121
132
)
122
133
continue
123
134
if len (bins ) < 2 :
135
+ console .print (
136
+ f'Dimension type: { dimension ["type" ]} requires more than 1 tiers' ,
137
+ style = "bold red" ,
138
+ )
124
139
pass
125
140
else :
126
141
tier_sql = f"CASE "
127
142
for i in range (0 , len (bins ) - 1 ):
128
143
tier_sql += f" WHEN { cube_dimension ['sql' ]} >= { bins [i ]} AND { cube_dimension ['sql' ]} < { bins [i + 1 ]} THEN { bins [i ]} "
129
144
tier_sql += "ELSE NULL END"
130
- cube_dimension ["sql" ] = tier_sql
145
+ cube_dimension ["sql" ] = folded_unicode ( tier_sql )
131
146
cube ["dimensions" ].append (cube_dimension )
132
147
133
148
for measure in view .get ("measures" , []):
@@ -145,7 +160,7 @@ def parse_view(lookml_model, raise_when_views_not_present=True):
145
160
cube_measure ["public" ] = not bool (measure ["hidden" ] == "yes" )
146
161
147
162
if measure ["type" ] != "count" :
148
- cube_measure ["sql" ] = rpl_table (measure ["sql" ])
163
+ cube_measure ["sql" ] = convert_to_literal (measure ["sql" ])
149
164
elif "drill_fields" in measure :
150
165
drill_members = []
151
166
for drill_field in measure ["drill_fields" ]:
0 commit comments