package pidgin
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=4a7bb0fccdebf5b205d2eeaa8a9b8e50acf267445f949a2b1a8304def9a38548
sha512=2612fc5cbdd6173a0676ae8840158b85358744561245d0b55ce0ebcf41acb26eddbd316be42a60d55e187928182f681cf3efb4f556ddf47d698217e8376633d3
Description
Pidgin is a common language for dealing with serialization an deserialization of complex data structures
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
;;
endmodule 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
Dev Dependencies (7)
-
odoc
with-doc -
ocaml-lsp-server
with-dev-setup -
merlin
with-dev-setup -
ocp-indent
with-dev-setup -
ocamlformat
with-dev-setup -
utop
with-dev-setup -
alcotest
with-test & >= "1.9.1"
Used by (1)
Conflicts
None