package pidgin_pp

All checked builds pass
  1. Overview
  2. Docs

Description

Defined a set of pretty-printers to handle the different types described by the Pidgin library

Added to opam-repository:

README

pidgin

The main idea is to present a minimal representation (very similar to that of JSON) and to provide:

  • A DSL for describing arbitrary data structures in this language.
  • Validation functions that operate on data described using this DSL
  • A bidirectional conversion approach, imposing a cost due to the indirect nature of the generic format (though it is viable in many scenarios).

Pidgin does not statically preserve the type of expressions; instead, it hides them, which allows expressions written in this language to be treated as an untyped runtime representation of arbitrary OCaml values (enabling the derivation of pretty-printers and equality functions, for example).

History and Trivia

Pidgin is derived from the Yocaml.Data module (from the YOCaml project) and the Rensai validation model, which originated from Kohai.

The name "Pidgin", a grammatically simplified form of contact language that develops between two or more groups of people that do not have a language in common (Wikipedia), was suggested by Lorie Den Os.

A simple example

Here is a very short example that demonstrates how to serialize and deserialize Pidgin expressions:

module Gender = struct
  type t =
    | Male
    | Female
    | Other of string

  let to_pidgin x =
    Repr.string
      (match x with
       | Other s -> s
       | Male -> "male"
       | Female -> "female")
  ;;

  let from_pidgin =
    let open Check in
    string
    $ function
    | "male" | "m" -> Male
    | "female" | "f" -> Female
    | other -> Other other
  ;;
end
module Human = struct
  type t =
    { nickname : string
    ; firstname : string option
    ; lastname : string option
    ; age : int option
    ; gender : Gender.t
    }

  let make ?firstname ?lastname ?age ~nickname ~gender () =
    { nickname; firstname; lastname; age; gender }
  ;;

  let to_pidgin { nickname; firstname; lastname; age; gender } =
    let open Repr in
    record
      [ "nickname", string nickname
      ; "firstname", option string firstname
      ; "lastname", option string lastname
      ; "age", option int age
      ; "gender", Gender.to_pidgin gender
      ]
  ;;

  let from_pidgin =
    let open Check in
    record (fun fields ->
      let+ nickname = req ~alt:[ "nick"; "pseudo" ] fields "nickname" string
      and+ gender = req fields "gender" Gender.from_pidgin
      and+ firstname = opt ~alt:[ "first_name" ] fields "firstname" string
      and+ lastname = opt ~alt:[ "last_name"; "name" ] fields "lastname" string
      and+ age = opt fields "age" int in
      make ?firstname ?lastname ?age ~nickname ~gender ())
  ;;
end

Dependencies (3)

  1. pidgin = version
  2. ocaml >= "5.0.0"
  3. dune >= "3.21"

Dev Dependencies (1)

  1. odoc with-doc

Optional Dependencies

None

Used by

None

Conflicts

None

Build status

Results of building pidgin_pp with each compiler on check.ci.ocaml.org, run of against opam-repository 8a77e58. All results for this package.

Each result links to its build log. To keep crawlers from overloading the site, build logs require a GitHub sign-in (see ocurrent/opam-health-check). On your first log, GitHub asks you to “Authorize opam-health-check”, the OCurrent app that runs these checks; it sees only your public profile, not your repositories. The sign-in lasts 30 days.

Version OCaml 4.11 OCaml 4.14 OCaml 5.2 OCaml 5.4 OCaml 5.5
1.2.0 (this version) n/a n/a good good good
  • good Package successfully built
  • partial One of the dependencies failed to build
  • bad Package failed to build
  • n/a Package is not available in this environment
  • internal Internal failure