Toml.LensesPartial lenses (returns option) for accessing TOML structures. They make it possible to read/write deeply nested immutable values.
val get : 'a -> ( 'a, 'b ) lens -> 'b optionExtracts a value from some Toml data using a lens. Returns Some value if the underlying data matched the lens, or None.
Example:
utop # let toml_data = Toml.Parser.(from_string "
[this.is.a.deeply.nested.table]
answer=42" |> unsafe);;
val toml_data : Types.table = <abstr>
utop # Toml.Lenses.(get toml_data (
key "this" |-- table
|-- key "is" |-- table
|-- key "a" |-- table
|-- key "deeply" |-- table
|-- key "nested" |-- table
|-- key "table" |-- table
|-- key "answer"|-- int ));;
- : int option = Some 42val set : 'b -> 'a -> ( 'a, 'b ) lens -> 'a optionReplaces an existing value in some Toml data using a lens. Returns Some value if the underlying data matched the lens, or None.
Example:
utop # let toml_data = Toml.Parser.(from_string "
[this.is.a.deeply.nested.table]
answer=42" |> unsafe);;
val toml_data : Types.table = <abstr>
utop # let maybe_toml_data' = Toml.Lenses.(set 2015 toml_data (
key "this" |-- table
|-- key "is" |-- table
|-- key "a" |-- table
|-- key "deeply" |-- table
|-- key "nested" |-- table
|-- key "table" |-- table
|-- key "answer"|-- int ));;
val maybe_toml_data' : Types.table option = Some <abstr>
utop # let (Some toml_data') = maybe_toml_data';;
val toml_data' : Types.table = <abstr>
utop # Toml.Printer.string_of_table toml_data';;
- : bytes = "[this.is.a.deeply.nested.table]\nanswer = 2015\n"val update : ( 'b -> 'b option ) -> 'a -> ( 'a, 'b ) lens -> 'a optionApplies a function on an existing value in some Toml data. Returns Some value if the underlying data matched the lens, or None.
Example:
utop # let toml_data = Toml.Parser.(from_string "
[[table.of.mixed_values]]
int=0
str=\"a\"
[[table.of.mixed_values]]
str=\"b\"
[[table.of.mixed_values]]
int=3
" |> unsafe);;
val toml_data : Types.table = <abstr>
utop # let maybe_toml_data' = Toml.Lenses.(update
(fun ts -> Some (
List.map (fun t ->
(** Add 'int=42' to each table which doesn't already have an 'int' key *)
if not (Types.Table.mem (Toml.key "int") t) then
Types.Table.add (Toml.key "int") (Types.TInt 42) t
else t) ts))
toml_data
(key "table" |-- table
|-- key "of" |-- table
|-- key "mixed_values" |-- array |-- tables));;
val maybe_toml_data' : Types.table option = Some <abstr>
utop # let (Some toml_data') = maybe_toml_data';;
val toml_data' : Types.table = <abstr>
utop # print_endline @@ Toml.Printer.string_of_table toml_data';;
[[table.of.mixed_values]]
int = 0
str = "a"
[[table.of.mixed_values]]
int = 42
str = "b"
[[table.of.mixed_values]]
int = 3
- : unit = ()val key : string -> ( Types.table, Types.value ) lensLens to a particular value in a table.
val field : string -> ( Types.table, Types.table ) lensCombines the key and table lenses, for the common case of nested tables.
val string : ( Types.value, string ) lensLens to a string value.
val bool : ( Types.value, bool ) lensLens to a boolean value.
val int : ( Types.value, int ) lensLens to an int value.
val float : ( Types.value, float ) lensLens to a float value.
val date : ( Types.value, float ) lensLens to a date value.
val array : ( Types.value, Types.array ) lensLens to an array value.
val table : ( Types.value, Types.table ) lensLens to a table value.
val strings : ( Types.array, string list ) lensLens to an array of strings.
val bools : ( Types.array, bool list ) lensLens to an array of booleans.
val ints : ( Types.array, int list ) lensLens to an array of ints.
val floats : ( Types.array, float list ) lensLens to an array of floats.
val dates : ( Types.array, float list ) lensLens to an array of dates.
val tables : ( Types.array, Types.table list ) lensLens to an array of tables.
val arrays : ( Types.array, Types.array list ) lensLens to an array of arrays.