package bonsai

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
module Path_order (M : Typed_fields_lib.S) : sig ... end

Path order is one of the scarier parts of the API and is explained further in the explanation for S.path_order.

module type S = sig ... end
val make : ?namespace:string list -> (module S with type Typed_field.derived_on = 'a) -> 'a t

Makes a 'a t where 'a is a record.

When dealing with nested records, namespaces are automatically inferred, but can be overriden with the optional namespace parameter.

Example:

      module Nested = struct
        type t =
          { foo : int
          ; bar : int
          }
        [@@deriving typed_fields, sexp, equal]

        let parser_for_field : type a. a Typed_field.t -> a Parser.t = function
          | Foo -> Parser.from_query_required Value_parser.int
          | Bar -> Parser.from_query_required Value_parser.int
        ;;

        let path_order = []
      end

      module My_url = struct
        type t =
          { a : Nested.t
          ; b : Nested.t
          }
        [@@deriving typed_fields, sexp, equal]

        let parser_for_field : type a. a Typed_field.t -> a Parser.t = function
          | A -> Parser.Record.make (module Nested)
          | B -> Parser.Record.make (module Nested)
        ;;

        let path_order = []
      end

      let%expect_test _ =
        let parser = Parser.Record.make (module My_url) in
        Parser.check_ok_and_print_urls_or_errors parser;
        [%expect
          {|
URL parser looks good!
┌───────────────────────────────────────────────────┐
│ All urls                                          │
├───────────────────────────────────────────────────┤
│ /?a.bar=<int>&a.foo=<int>&b.bar=<int>&b.foo=<int> │
└───────────────────────────────────────────────────┘ |}]
      ;;

      (*Doing this instead results in: *)
      let parser_for_field : type a. a Typed_field.t -> a Parser.t = function
        | A -> Parser.Record.make ~namespace:[ "my_a" ] (module Nested)
        | B -> Parser.Record.make ~namespace:[ "my_b" ] (module Nested)
      ;;

      let%expect_test _ =
        Parser.check_ok_and_print_urls_or_errors parser;
        [%expect
          {|
URL parser looks good!
┌───────────────────────────────────────────────────────────────┐
│ All urls                                                      │
├───────────────────────────────────────────────────────────────┤
│ /?my_a.bar=<int>&my_a.foo=<int>&my_b.bar=<int>&my_b.foo=<int> │
└───────────────────────────────────────────────────────────────┘ |}]
OCaml

Innovation. Community. Security.