12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879(*****************************************************************************)(* *)(* Open Source License *)(* Copyright (c) 2018 Nomadic Labs, <contact@nomadic-labs.com> *)(* *)(* Permission is hereby granted, free of charge, to any person obtaining a *)(* copy of this software and associated documentation files (the "Software"),*)(* to deal in the Software without restriction, including without limitation *)(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)(* and/or sell copies of the Software, and to permit persons to whom the *)(* Software is furnished to do so, subject to the following conditions: *)(* *)(* The above copyright notice and this permission notice shall be included *)(* in all copies or substantial portions of the Software. *)(* *)(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)(* DEALINGS IN THE SOFTWARE. *)(* *)(*****************************************************************************)openEncodingletversion_caseencchoosewrapnamenth=case~title:(Printf.sprintf"%s version %d"namenth)Json_only(obj1(req(Printf.sprintf"%s.v%d"namenth)enc))choosewrapletmake_encoding~namel=union~tag_size:`Uint8(List.mapi(funnthf->fnamenth)l)type_t=|Version_0:'v0encoding->'v0t|Version_S:{previous:'vnt;encoding:'vnp1encoding;upgrade:'vn->'vnp1;}->'vnp1tletfirst_versione=Version_0eletnext_versionencodingupgradeprevious=Version_S{encoding;upgrade;previous}letencoding:typea.name:string->at->aencoding=fun~nameversion->matchversionwith|Version_0e->make_encoding~name[version_casee(funx->Somex)(funx->x)]|Version_S{previous;encoding;upgrade}->letrecmk_nones:type(* This function generates encoding cases for all the
outdated versions.
These versions are never encoded to
(hence [fun _ -> None]) but are safely decoded with
the use of the upgrade functions. *)b.(b->a)->bt->(string->int->acase)list=funupgr->function|Version_0e->[version_casee(fun_->None)(funx->upgrx)]|Version_S{previous;encoding;upgrade}->letothers=mk_nones(funx->upgr(upgradex))previousinversion_caseencoding(fun_->None)(funx->upgrx)::othersinletnones=mk_nonesupgradepreviousinletcases=version_caseencoding(funx->Somex)(funx->x)::nones|>List.revinmake_encoding~namecases