Declarative CSV codecs
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

OCaml 97.4%
Dune 0.2%
Other 2.4%
29 1 0

Clone this repository

https://tangled.org/gazagnaire.org/ocaml-csvt https://tangled.org/did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-csvt
git@git.recoil.org:gazagnaire.org/ocaml-csvt git@git.recoil.org:did:plc:jhift2vwcxhou52p3sewcrpx/ocaml-csvt

For self-hosted knots, clone URLs may differ based on your setup.

Download tar.gz
README.md

csvt#

Declarative CSV codecs with bytesrw streaming.

Overview#

Csvt provides bidirectional CSV encoding and decoding using a combinator-based approach inspired by Jsont. Define a typed row codec once and use it for both decoding CSV files into OCaml records and encoding records back to CSV. Column names are resolved at decode time for O(1) per-row field access.

Streaming I/O is built on bytesrw: decode reads from a Bytes.Reader.t and encode writes to a Bytes.Writer.t, so CSV processing composes with other bytesrw-based codecs in a pipeline. For simple cases, decode_file, decode_string, and fold_file provide direct access.

Installation#

opam install csvt

Usage#

type point = { x : float; y : float; label : string }

let point_codec =
  Csvt.(Row.(
    obj (fun x y label -> { x; y; label })
    |> col "x" float ~enc:(fun p -> p.x)
    |> col "y" float ~enc:(fun p -> p.y)
    |> col "label" string ~enc:(fun p -> p.label)
    |> finish
  ))

(* Decode from a file *)
let () =
  match Csvt.decode_file point_codec "points.csv" with
  | Ok points -> List.iter (fun p -> Printf.printf "%s\n" p.label) points
  | Error e -> prerr_endline (Csvt.error_to_string e)

(* Streaming fold without building a list *)
let sum =
  Csvt.fold_file point_codec "points.csv"
    (fun acc p -> acc +. p.x) 0.0

(* Encode to a bytesrw writer *)
let () =
  let buf = Buffer.create 256 in
  let w = Bytesrw.Bytes.Writer.of_buffer buf in
  Csvt.encode point_codec points w

API Overview#

  • Field codecs -- string, int, float, bool, option, nullable_float, nullable_int
  • col_map -- Custom field codec from dec/enc functions
  • Row module -- Row builder: obj, col, finish
  • decode_file, decode_channel, decode_string -- Batch decoding
  • fold_file, fold_channel -- Streaming fold over rows
  • decode -- Streaming decode from a bytesrw reader
  • encode -- Streaming encode to a bytesrw writer
  • col -- Single-column projection query
  • update_col, delete_col -- Column-level transforms
  • col_names, col_count -- Codec introspection

Licence#

ISC License. See LICENSE.md for details.