apple / pkl-pantry

Shared Pkl packages
Apache License 2.0
234 stars 32 forks source link

[csv] Parser failed when a cell doesn't have value #76

Open namm2 opened 3 weeks ago

namm2 commented 3 weeks ago

HI, I'm trying to parse a csv file which was exported from spreadsheet, but the csv parser failed when a cell doesn't have any value:

import "package://pkg.pkl-lang.org/pkl-pantry/pkl.csv@1.0.0#/csv.pkl"

data = """
  Column1, Column2, Column3, Column 4
  Value 1, Value 2,, Value 4
  """

csvData = new csv.Parser {
  input = data
  includeHeader = true
}.parsed

But got pkl error:

–– Pkl Error ––
Expected value of type `String`, but got `null`.

188 | let (header: List<String>? = (if (includeHeader) stringyResult[0] else properties?.keys)?.toList() as List<String>?)
                                                                                                                 ^^^^^^
at pkl.csv.csv#Parser._parsed.<function#3> (https://github.com/apple/pkl-pantry/blob/pkl.csv@1.0.0/packages/pkl.csv/csv.pkl#L188-188)

187 | let (properties: Map<String,reflect.Property>? = rowClass.ifNonNull((clazz) -> allProps(reflect.Class(clazz as Class))))
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at pkl.csv.csv#Parser._parsed.<function#2> (https://github.com/apple/pkl-pantry/blob/pkl.csv@1.0.0/packages/pkl.csv/csv.pkl#L187-203)

182 | let (stringyResult = new StringyTableParser {
      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at pkl.csv.csv#Parser._parsed.<function#1> (https://github.com/apple/pkl-pantry/blob/pkl.csv@1.0.0/packages/pkl.csv/csv.pkl#L182-203)

181 | let (self = this)
      ^^^^^^^^^^^^^^^^^
at pkl.csv.csv#Parser._parsed (https://github.com/apple/pkl-pantry/blob/pkl.csv@1.0.0/packages/pkl.csv/csv.pkl#L181-203)

164 | _parsed
      ^^^^^^^
at pkl.csv.csv#Parser.parsed (https://github.com/apple/pkl-pantry/blob/pkl.csv@1.0.0/packages/pkl.csv/csv.pkl#L164-164)

8 | csvData = new csv.Parser {
              ^^^^^^^^^^^^^^^^
at kv2#csvData (file:///Users/namnguyen/scripts/vault/kv2.pkl, line 8)

106 | text = renderer.renderDocument(value)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at pkl.base#Module.output.text (https://github.com/apple/pkl/blob/0.26.3/stdlib/base.pkl#L106)

Coming from python's csv (I know it's apple to orange comparison, but anyway), I don't expect this error.

holzensp commented 3 weeks ago

I can reproduce and agree this is surprising and undesirable behaviour. Thanks for filing the issue.