Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file read_intf.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257openCore(** If we can read a row correctly but the ['a t] provided can't convert it,
your ['a On_invalid_row.t] will define what happens.
Here, 'read a row correctly' means that it's valid CSV (subject to your
delimiter etc) & it has the correct number of columns. If that's not the
case the parsers will just raise. *)moduletypeOn_invalid_row=sigtype'at(** The default. Raise with additional context. *)valraise:_t(** Skip the bad row *)valskip:_t(** Do something else!
- [`Skip]: skip the line. Same as [skip] above.
- [`Yield]: return the given value for this row.
- [`Raise]: raise the given exception
- [`Fallback]: invoke the given handler instead
*)valcreate:(line_number:int(** The line number of the bad row. *)->intString.Map.t(** Map from header to position. *)->stringAppend_only_buffer.t(** Value at each position. *)->exn(** Exception raised when trying to convert this row. *)->[`Skip|`Yieldof'a|`Raiseofexn|`Fallbackof'at])->'atendmoduletypeOpen_on_rhs=sigtype'atvalat_index:int->f:(string->'a)->'atvalat_header:string->f:(string->'a)->'atvalat_header_opt:string->f:(stringoption->'a)->'atendmoduletypeRoot=sig(** Row up to the error, and the field with the error up to the point of
failure. Same as [Expert.Parse_state.Bad_csv_formatting]. *)exceptionBad_csv_formattingofstringlist*string(** This provides an applicative interface for constructing values from a csv file.
An ['a t] describes how to build an OCaml model ['a] for each row.
Simple example:
{[
type t =
{ foo : int
; bar : string
}
(* Describes how to generate a [t] from a row of a csv file *)
let parse : t Delimited_kernel.Read.t =
let open Delimited_kernel.Read.Let_syntax in
let%map_open foo = at_header "foo" ~f:Int.of_string
and bar = at_header "bar" ~f:String.of_string in
{ foo; bar }
;;
let _ =
Delimited_kernel.Read.list_of_string ~header:`Yes parse
"foo,bar\n2,\"hello, world\"\n"
;;
]}
*)type'atincludeApplicative.Swithtype'at:='atmoduleOpen_on_rhs_intf:sigmoduletypeS=Open_on_rhswithtype'at:='atendincludeApplicative.Let_syntaxwithtype'at:='atwithmoduleOpen_on_rhs_intf:=Open_on_rhs_intf(** Read a field at the given index. Use [f] to convert the field from string. *)valat_index:int->f:(string->'a)->'at(** Read a field at the given header. Use [f] to convert the field from string.
Note that if the given header is not provided through either the file or
the [~header] argument to the parsers, this will fail at runtime. *)valat_header:string->f:(string->'a)->'at(** Read a field at the given header, if it exists. Use [f] to convert the field from
string. *)valat_header_opt:string->f:(stringoption->'a)->'atmoduleRecord_builder:Record_builder.Swithtype'aapplicative='atmoduleFields_O:sig(** The following are convenience functions that build on [Record_builder.field] to
make it easy to define a [t Delimited.Read.t] for some record type [t].
Example usage:
{[
type t =
{ foo : int
; bar : bool
}
[@@deriving fields]
let read : t Delimited.Read.t =
Delimited.Read.Fields_O.(
Fields.make_creator
~foo:!!Int.of_string
~bar:!?(Option.value_map ~default:true ~f:Bool.of_string)
|> Delimited.Read.Record_builder.build_for_record)
]}
*)(** Reads a single column from a field of a record. *)val(!!):(string->'a)->(_,'a)Field.t->('a,_,_,_)Record_builder.Make_creator_types.handle_one_field(** Reads a single column from a field of a record, if the header exists.
[( !? )] is to [at_header_opt] as [( !! )] is to [at_header].
*)val(!?):(stringoption->'a)->(_,'a)Field.t->('a,_,_,_)Record_builder.Make_creator_types.handle_one_fieldendmoduleOn_invalid_row:On_invalid_row(** Header parsing control *)moduleHeader=Header(** Whole-row parsing. *)moduleRow:sigtype'abuilder_t='atincludemoduletypeofRow(** A builder for [Row.t]s.
As this parses the whole row it's slower than using the builder
interface directly, but simpler to use. *)valbuilder:tbuilder_tend(** Fold the CSV rows contained in the given string. *)valfold_string:?strip:bool->?sep:char->?quote:[`No_quoting|`Usingofchar]->?header:Header.t->?on_invalid_row:'aOn_invalid_row.t->'at->init:'b->f:('b->'a->'b)->string->'b(** Load the CSV as a list *)vallist_of_string:?strip:bool->?sep:char->?quote:[`No_quoting|`Usingofchar]->?header:Header.t->?on_invalid_row:'aOn_invalid_row.t->'at->string->'alist(** Read CSV file. *)valread_lines:?strip:bool->?sep:char->?quote:[`No_quoting|`Usingofchar]->?header:Header.t->?on_invalid_row:'aOn_invalid_row.t->'at->In_channel.t->'alist(** Read CSV file, processing line by line. *)valfold_lines:?buffer_size:int->?strip:bool->?sep:char->?quote:[`No_quoting|`Usingofchar]->?header:Header.t->?on_invalid_row:'aOn_invalid_row.t->'at->init:'b->f:('b->'a->'b)->In_channel.t->'bmoduleStreaming:sigtype'abuilder_t='attype'atvalcreate:?strip:bool->?sep:char->?quote:[`No_quoting|`Usingofchar]->?start_line_number:int->?on_invalid_row:'aOn_invalid_row.t->?header:Header.t->'abuilder_t->init:'b->f:('b->'a->'b)->'bt(** Like [create], but additionally passes the line number of the current row to [f] *)valcreate_indexed:?strip:bool->?sep:char->?quote:[`No_quoting|`Usingofchar]->?start_line_number:int->?on_invalid_row:'aOn_invalid_row.t->?header:Header.t->'abuilder_t->init:'b->f:(int->'b->'a->'b)->'btvalinput_string:'at->?pos:int->?len:int->string->'atvalinput:'at->?pos:int->?len:int->bytes->'atvalfinish:'at->'atvalacc:'at->'avalheaders:'at->String.Set.toptionvalstate:'at->[`Parsing_header|`Parsing_rows]endendmoduletypeExpert=sigmoduleAppend_only_buffer=Append_only_buffermoduleParse_state=Parse_statemoduleOn_invalid_row:On_invalid_rowtype'atmoduleBuilder:sigtypenonrec'at='atvallambda:(intString.Map.t->stringAppend_only_buffer.t->'a)->'atvalreturn:'a->'atendend