Skip to content

Commit

Permalink
fix json column reader for numberless json
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Jun 28, 2024
1 parent 09c017e commit 0b64894
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
6 changes: 3 additions & 3 deletions src/FsSpreadsheet/Json/Cell.fs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ let encodeCols (cell:FsCell) =

let decodeCols colNumber : Decoder<FsCell> =
Decode.object (fun builder ->
let v,dt = builder.Required.Field value (Value.decode)
let r = builder.Required.Field row Decode.int
new FsCell(v,dt,FsAddress(r,colNumber))
let v,dt = builder.Optional.Field value (Value.decode) |> Option.defaultValue ("", DataType.Empty)
let r = builder.Optional.Field row Decode.int |> Option.defaultValue 0
new FsCell(v,dt,FsAddress(r,Option.defaultValue 0 colNumber))
)
6 changes: 3 additions & 3 deletions src/FsSpreadsheet/Json/Column.fs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ let encodeNoNumbers (col: FsCell seq) =
cells, Encode.seq (col |> Seq.map Cell.encodeNoNumber)
]

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.decodeCols n))
let n = builder.Optional.Field number Decode.int
let cs = builder.Optional.Field cells (Decode.seq (Cell.decodeCols n)) |> Option.defaultValue Seq.empty
n,cs
)
21 changes: 19 additions & 2 deletions src/FsSpreadsheet/Json/Worksheet.fs
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,33 @@ let encodeColumns noNumbering (sheet:FsWorksheet) =

let decodeColumns : Decoder<FsWorksheet> =
Decode.object (fun builder ->
printfn "Decode Worksheet"
let mutable colIndex = 0
let n = builder.Required.Field name Decode.string
let ts = builder.Optional.Field tables (Decode.seq Table.decode)
let cs = builder.Required.Field columns (Decode.seq Column.decode)
let sheet = new FsWorksheet(n)
printfn "Start columning"
cs
|> Seq.iter (fun (colI,cells) ->
printfn "\tColumn: %OcolI" colI
let mutable rowIndex = 0
let colI =
match colI with
| Some i -> i
| None -> colIndex + 1
colIndex <- colI
let col = sheet.Column(colI)
cells
|> Seq.iter (fun cell ->
let c = col[cell.RowNumber]
|> Seq.iter (fun cell ->
printfn "\t\tstart Cell: %O" cell
let rowI =
match cell.RowNumber with
| 0 -> rowIndex + 1
| i -> i
printfn "\t\trow: %i" rowI
rowIndex <- rowI
let c = col[rowIndex]
c.Value <- cell.Value
c.DataType <- cell.DataType
)
Expand Down
21 changes: 13 additions & 8 deletions tests/FsSpreadsheet.Net.Tests/Json.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ let getFilledTestWb() =
let wb = new FsWorkbook()
let ws = FsWorkbook.initWorksheet "MySheet" wb
let r1 = ws.Row(1)
r1.[1].Value <- "A1"
r1.[2].Value <- "B1"
r1.[1].SetValueAs "A1"
r1.[2].SetValueAs "B1"
let r2 = ws.Row(2)
r2.[1].Value <- "A2"
r2.[2].Value <- "B2"
r2.[1].SetValueAs "A2"
r2.[2].SetValueAs "B2"
wb

let rows =
Expand Down Expand Up @@ -102,8 +102,13 @@ let rows =
let wb = getFilledTestWb()
let s = wb.ToRowsJsonString(noNumbering = true)
let wb2 = FsWorkbook.fromRowsJsonString(s)
Expect.workSheetEqual (wb.GetWorksheetAt(1)) (wb2.GetWorksheetAt(1)) "NoNumber Filled Write-Read"
Expect.workSheetEqual (wb2.GetWorksheetAt(1)) (wb.GetWorksheetAt(1)) "NoNumber Filled Write-Read"

testCase "NoNumber DefaultTestObject Write-Read_Success" <| fun _ ->
let dto = DefaultTestObject.defaultTestObject()
let s = dto.ToRowsJsonString(noNumbering = true)
let dto2 = FsWorkbook.fromRowsJsonString(s)
()

testCase "Write-Read DefaultTestObject" <| fun _ ->
let dto = DefaultTestObject.defaultTestObject()
Expand Down Expand Up @@ -154,13 +159,13 @@ let columns =
let wb = getFilledTestWb()
let s = wb.ToColumnsJsonString(noNumbering = true)
let wb2 = FsWorkbook.fromColumnsJsonString(s)
Expect.workSheetEqual (wb.GetWorksheetAt(1)) (wb2.GetWorksheetAt(1)) "NoNumber Filled Write-Read"
Expect.workSheetEqual (wb2.GetWorksheetAt(1)) (wb.GetWorksheetAt(1)) "NoNumber Filled Write-Read"

testCase "NoNumber Write-Read DefaultTestObject" <| fun _ ->
testCase "NoNumber DefaultTestObject Write-Read_Success" <| fun _ ->
let dto = DefaultTestObject.defaultTestObject()
let s = dto.ToColumnsJsonString(noNumbering = true)
let dto2 = FsWorkbook.fromColumnsJsonString(s)
Expect.isDefaultTestObject dto2
()

testCase "Write-Read DefaultTestObject" <| fun _ ->
let dto = DefaultTestObject.defaultTestObject()
Expand Down
2 changes: 1 addition & 1 deletion tests/TestUtils/TestingUtils.fs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ module Expect =
let workSheetEqual (actual : FsWorksheet) (expected : FsWorksheet) message =
let f (ws : FsWorksheet) =
ws.RescanRows()
ws.Rows |> Seq.map (fun r -> r.Cells)
ws.Rows |> Seq.map (fun r -> r.Cells |> Seq.filter (fun c -> c.Value <> ""))
if actual.Name <> expected.Name then
failwithf $"{message}. Worksheet names do not match. Expected {expected.Name} but got {actual.Name}"
columnsEqual (f actual) (f expected) $"{message}. Worksheet does not match"
Expand Down

0 comments on commit 0b64894

Please sign in to comment.