Skip to content

Commit

Permalink
make json rowwise parsing more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Jun 19, 2024
1 parent 0933e09 commit 8ccd58f
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/FsSpreadsheet/Json/Cell.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ let encodeRows (cell:FsCell) =

let decodeRows rowNumber : Decoder<FsCell> =
Decode.object (fun builder ->
let v,dt = builder.Required.Field value (Value.decode)
let c = builder.Required.Field column Decode.int
new FsCell(v,dt,FsAddress(rowNumber,c))
let v,dt = builder.Optional.Field value (Value.decode) |> Option.defaultValue ("", DataType.Empty)
let c = builder.Optional.Field column Decode.int |> Option.defaultValue 0
new FsCell(v,dt,FsAddress(Option.defaultValue 0 rowNumber,c))
)


Expand Down
6 changes: 3 additions & 3 deletions src/FsSpreadsheet/Json/Row.fs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ let encode (row:FsRow) =
cells, Encode.seq (row.Cells |> Seq.map Cell.encodeRows)
]

let decode : Decoder<int*FsCell seq> =
let decode : Decoder<int option*FsCell seq> =
Decode.object (fun builder ->
let n = builder.Required.Field number Decode.int
let cs = builder.Required.Field cells (Decode.seq (Cell.decodeRows n))
let n = builder.Optional.Field number Decode.int
let cs = builder.Optional.Field cells (Decode.seq (Cell.decodeRows n)) |> Option.defaultValue Seq.empty
n,cs
)
17 changes: 15 additions & 2 deletions src/FsSpreadsheet/Json/Worksheet.fs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,29 @@ let encodeRows (sheet:FsWorksheet) =

let decodeRows : Decoder<FsWorksheet> =
Decode.object (fun builder ->
let mutable rowIndex = 0

let n = builder.Required.Field name Decode.string
let ts = builder.Optional.Field tables (Decode.seq Table.decode)
let rs = builder.Required.Field rows (Decode.seq Row.decode)
let rs = builder.Optional.Field rows (Decode.seq Row.decode) |> Option.defaultValue Seq.empty
let sheet = new FsWorksheet(n)
rs
|> Seq.iter (fun (rowI,cells) ->
let mutable colIndex = 0
let rowI =
match rowI with
| Some i -> i
| None -> rowIndex + 1
rowIndex <- rowI
let r = sheet.Row(rowI)
cells
|> Seq.iter (fun cell ->
let c = r[cell.ColumnNumber]
let colI =
match cell.ColumnNumber with
| 0 -> colIndex + 1
| i -> i
colIndex <- colI
let c = r[colI]
c.Value <- cell.Value
c.DataType <- cell.DataType
)
Expand Down
46 changes: 46 additions & 0 deletions tests/FsSpreadsheet.Js.Tests/Json.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,52 @@ open Fable.Pyxpecto
let rows =
testList "Rows" [

testCase "Read Standard" <| fun _ ->
// Read object taken from https://spreadsheet.dsl.builders/#_sheets_and_rows
let s = """{
"sheets": [
{
"name": "Sample",
"rows": [
{
"number": 5,
"cells": [
{
"value": "Line Number Five"
}
]
},
{
},
{
"cells": [
{
"value": "Line Number Seven"
}
]
}
]
}
]
}"""
let wb = FsWorkbook.fromRowsJsonString(s)
let sheet = Expect.wantSome (wb.TryGetWorksheetByName "Sample") "Sheet Sample"
Expect.equal sheet.Rows.Count 3 "Row count"

Expect.isTrue (sheet.ContainsRowAt 5) "Row 5"
let row1 = sheet.Row(5)
Expect.isTrue (row1.HasCellAt 1) "Row 5 cell"
Expect.equal (row1.[1].Value) "Line Number Five" "Row 5 cell value"

Expect.isTrue (sheet.ContainsRowAt 6) "Row 6"
let row1 = sheet.Row(6)
Expect.hasLength row1.Cells 0 "Row 6 cell count"

Expect.isTrue (sheet.ContainsRowAt 7) "Row 7"
let row2 = sheet.Row(7)
Expect.isTrue (row2.HasCellAt 1) "Row 7 cell"
Expect.equal (row2.[1].Value) "Line Number Seven" "Row 7 cell value"

testCase "Read-Write DefaultTestObject" <| fun _ ->
let dto = DefaultTestObject.defaultTestObject()
let s = dto.ToRowsJsonString()
Expand Down
45 changes: 45 additions & 0 deletions tests/FsSpreadsheet.Net.Tests/Json.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,51 @@ open Fable.Pyxpecto

let rows =
testList "Rows" [
testCase "Read Standard" <| fun _ ->
// Read object taken from https://spreadsheet.dsl.builders/#_sheets_and_rows
let s = """{
"sheets": [
{
"name": "Sample",
"rows": [
{
"number": 5,
"cells": [
{
"value": "Line 5"
}
]
},
{
},
{
"cells": [
{
"value": "Line 7"
}
]
}
]
}
]
}"""
let wb = FsWorkbook.fromRowsJsonString(s)
let sheet = Expect.wantSome (wb.TryGetWorksheetByName "Sample") "Sheet Sample"
Expect.equal sheet.Rows.Count 3 "Row count"

Expect.isTrue (sheet.ContainsRowAt 5) "Row 5"
let row1 = sheet.Row(5)
Expect.isTrue (row1.HasCellAt 1) "Row 5 cell"
Expect.equal (row1.[1].Value) "Line 5" "Row 5 cell value"

Expect.isTrue (sheet.ContainsRowAt 6) "Row 6"
let row1 = sheet.Row(6)
Expect.hasLength row1.Cells 0 "Row 6 cell count"

Expect.isTrue (sheet.ContainsRowAt 7) "Row 7"
let row2 = sheet.Row(7)
Expect.isTrue (row2.HasCellAt 1) "Row 7 cell"
Expect.equal (row2.[1].Value) "Line 7" "Row 7 cell value"

testCase "Read-Write DefaultTestObject" <| fun _ ->
let dto = DefaultTestObject.defaultTestObject()
Expand Down
45 changes: 45 additions & 0 deletions tests/FsSpreadsheet.Py.Tests/Json.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,52 @@ open Fable.Pyxpecto

let rows =
testList "Rows" [
testCase "Read Standard" <| fun _ ->
// Read object taken from https://spreadsheet.dsl.builders/#_sheets_and_rows
let s = """{
"sheets": [
{
"name": "Sample",
"rows": [
{
"number": 5,
"cells": [
{
"value": "Line 5"
}
]
},
{
},
{
"cells": [
{
"value": "Line 7"
}
]
}
]
}
]
}"""
let wb = FsWorkbook.fromRowsJsonString(s)
let sheet = Expect.wantSome (wb.TryGetWorksheetByName "Sample") "Sheet Sample"
Expect.equal sheet.Rows.Count 3 "Row count"

Expect.isTrue (sheet.ContainsRowAt 5) "Row 5"
let row1 = sheet.Row(5)
Expect.isTrue (row1.HasCellAt 1) "Row 5 cell"
Expect.equal (row1.[1].Value) "Line 5" "Row 5 cell value"

Expect.isTrue (sheet.ContainsRowAt 6) "Row 6"
let row1 = sheet.Row(6)
Expect.hasLength row1.Cells 0 "Row 6 cell count"

Expect.isTrue (sheet.ContainsRowAt 7) "Row 7"
let row2 = sheet.Row(7)
Expect.isTrue (row2.HasCellAt 1) "Row 7 cell"
Expect.equal (row2.[1].Value) "Line 7" "Row 7 cell value"

testCase "Read-Write DefaultTestObject" <| fun _ ->
let dto = DefaultTestObject.defaultTestObject()
let s = dto.ToRowsJsonString()
Expand Down

0 comments on commit 8ccd58f

Please sign in to comment.