Skip to content

Commit 83d5203

Browse files
committed
[fix] change
1 parent 38cc5b4 commit 83d5203

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

log.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Logs
2+
3+
- 08/12 - Add option to pass a ̀`list` as `schema`

main.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def createFieldsBinding(schemaObject, startPath=""):
5656
else:
5757
path = '{}{}{}'.format(
5858
startPath, options["privateSeparator"], oneElement)
59-
if isinstance(schemaObject, dict) and (isinstance(schemaObject[oneElement], dict) or isinstance(schemaObject[oneElement], list)):
59+
if isinstance(schemaObject[oneElement], dict) or isinstance(schemaObject[oneElement], list):
6060
if isinstance(schemaObject[oneElement], list):
6161
bindings.append({
6262
"name": oneElement,
@@ -131,28 +131,41 @@ def parseLine(line):
131131
long = len(allPath)
132132
for count in range(0, long):
133133
nextPath = allPath[count]
134+
if isinstance(goodPlace, list):
135+
nextPathInt = int(nextPath)
134136
if count == (long - 1):
135137
if not isinstance(goodPlace, list):
136138
goodPlace[nextPath] = ""
137139
else:
138140
if nextPath not in goodPlace:
139-
goodPlace[nextPath] = {}
140-
goodPlace = goodPlace[nextPath]
141+
if isinstance(goodPlace, list):
142+
goodPlace.insert(nextPathInt, {})
143+
else:
144+
goodPlace[nextPath] = {}
145+
if isinstance(goodPlace, list):
146+
goodPlace = goodPlace[nextPathInt]
147+
else:
148+
goodPlace = goodPlace[nextPath]
141149
if isinstance(goodPlace, list):
142150
goodPlace.append(currentValue)
143151
elif isinstance(goodPlace, dict):
144152
goodPlace[onePathName] = currentValue
145153
else:
146154
goodPlace = currentValue
147155
else:
148-
obj[onePathRow] = currentValue
156+
if isinstance(obj, list):
157+
place = int(onePathRow)
158+
obj.insert(place, currentValue)
159+
elif isinstance(obj, dict):
160+
obj[onePathRow] = currentValue
149161
return obj
150162

151163
def parsefirstLine():
152164
global firstLine
153165
if isinstance(options["overrideFirstLine"], list):
154166
firstLine = options["overrideFirstLine"]
155-
if isinstance(schema, dict):
167+
if schema != None:
168+
# None is default value for schema
156169
cols = createFieldsBinding(schema)
157170
if options["debug"]:
158171
print("BINDINGS:", JSONstringify(cols))

test.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from main import parseFile
2+
from json import dumps as JSONstringify
23

34
schema = {
45
"test1": [
@@ -14,6 +15,14 @@
1415
"test2": "string"
1516
}
1617

18+
schema3 = [
19+
"test1",
20+
{
21+
"test4": "string",
22+
},
23+
"test2"
24+
]
25+
1726

1827
def test(parsedLine=[], a=[]):
1928
return None
@@ -23,6 +32,6 @@ def test(parsedLine=[], a=[]):
2332
"lineCallBack": test
2433
}
2534

26-
res = parseFile("csv.txt", schema, options)
35+
res = parseFile("csv.txt", {}, options)
2736

28-
print(res)
37+
print(JSONstringify(res, indent=4))

0 commit comments

Comments
 (0)