Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file shape_structure.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365open!Coreopen!Importtypeconstr=|Int_minofint|Int_maxofint|Int64_minofint64|Int64_maxofint64|String_minofint|String_maxofint|Float_minoffloat|Float_maxoffloat|Patternofstring|List_minofint|List_maxofint[@@derivingvariants,sexp_of]letapply_constraintcons=letloc=!Ast_helper.default_locinmatchconswith|Int_minx->[%exprcheck_int_mini~min:[%eAst_convenience.intx]]|Int_maxx->[%exprcheck_int_maxi~max:[%eAst_convenience.intx]]|Int64_minm->[%exprcheck_int64_mini~min:[%eAst_convenience.int64m]]|Int64_maxm->[%exprcheck_int64_maxi~max:[%eAst_convenience.int64m]]|String_minx->[%exprcheck_string_mini~min:[%eAst_convenience.intx]]|String_maxx->[%exprcheck_string_maxi~max:[%eAst_convenience.intx]]|Float_minf->[%exprcheck_float_mini~min:[%eAst_convenience.floatf]]|Float_maxf->[%exprcheck_float_mini~min:[%eAst_convenience.floatf]]|Patternx->[%exprcheck_patterni~pattern:[%eAst_convenience.strx]]|List_minx->[%exprcheck_list_mini~min:[%eAst_convenience.intx]]|List_maxx->[%exprcheck_list_maxi~max:[%eAst_convenience.intx]];;letapply_constraints?(is_pipe=false)cons=letloc=!Ast_helper.default_locinmatchconswith|[]->[%exprfuni->i]|cstr0::cstrs->(matchis_pipewith|true->(* FIXME: The interface doesn't allow enforcing constraints on pipes *)[%exprfuni->i]|false->lete=List.fold_rightcstrs~init:(apply_constraintcstr0)~f:(funcstre->[%expr[%eapply_constraintcstr]>>=fun()->[%ee]])in[%exprfuni->letopenResultinok_or_failwith[%ee];i]);;let%expect_test"apply_constraints"=lettestcstrs=letexpr=apply_constraintscstrsinprintf"%s%!"(Util.expression_to_stringexpr)intest[];[%expect{| fun i -> i |}];test[Int_min3];[%expect{| fun i -> let open Result in ok_or_failwith (check_int_min i ~min:3); i |}];test[Int_min3;Int_max5];[%expect{|
fun i ->
let open Result in
ok_or_failwith
((check_int_max i ~max:5) >>= (fun () -> check_int_min i ~min:3));
i |}];;(* helper function to sort and annotate fields of a structure shape. This is
used both for the implementation and the interface. *)letstructure_members(ss:Botodata.structure_shape)=List.mapss.members~f:(fun(field_name,member)->(Shape.structure_shape_required_fieldssfield_name,field_name,Shape.uncapitalized_idfield_name,member))|>List.stable_sort~compare:(fun(x,_,_,_)(y,_,_,_)->Bool.comparexy);;letwrap_resultbody=function|None->body|Someresult_wrapper->letloc=!Ast_helper.default_locinAst_convenience.record[Shape.uncapitalized_idresult_wrapper,body;Shape.uncapitalized_idShape.response_metadata_shape_name,[%expr()]];;letlambdaargsbody=letloc=!Ast_helper.default_locinList.fold_rightargs~init:[%exprfun()->[%ebody]]~f:(fun(required,_,id,_)acc->letlabel=ifrequiredthenLabelledidelseOptionalidinAst_convenience.lam~label(Ast_convenience.pvarid)acc);;letmake_of_structure_shape?result_wrapperss=letloc=!Ast_helper.default_locinletmembers=structure_membersssinletfields=List.mapmembers~f:(fun(_,_,id,_)->id,Ast_convenience.evarid)inletresult=ifList.is_emptyfieldsthen[%expr()]elseAst_convenience.recordfieldsinletbody=wrap_resultresultresult_wrapperinlambdamembersbody;;letshape_membershape={Botodata.shape;deprecated=None;deprecatedMessage=None;location=None;locationName=None;documentation=None;xmlNamespace=None;streaming=None;xmlAttribute=None;queryName=None;box=None;flattened=None;idempotencyToken=None;eventpayload=None;hostLabel=None;jsonvalue=None};;let%expect_test"make_of_structure_shape"=lettest?result_wrappershape=letexpr=make_of_structure_shape?result_wrappershapeinprintf"%s%!"(Util.expression_to_stringexpr)inletrequired_name="required_field"inletstructure_shapemembers:Botodata.structure_shape={Botodata.empty_structure_shapewithrequired=Some[required_name];members}inletmember~name~shape=name,shape_membershapeintest(structure_shape[]);[%expect{| fun () -> () |}];test(structure_shape[member~name:"name_a"~shape:"shape_a";member~name:required_name~shape:"shape_required";member~name:"name_b"~shape:"shape_b"]);[%expect{|
fun ?name_a ->
fun ?name_b ->
fun ~required_field -> fun () -> { name_a; name_b; required_field } |}];test~result_wrapper:"result_wrapper"(structure_shape[member~name:"name_a"~shape:"shape_a";member~name:required_name~shape:"shape_required";member~name:"name_b"~shape:"shape_b"]);[%expect{|
fun ?name_a ->
fun ?name_b ->
fun ~required_field ->
fun () ->
{
result_wrapper = { name_a; name_b; required_field };
responseMetaData = ()
} |}];;typecore_type=Parsetree.core_typeletsexp_of_core_typet=t|>Util.core_type_to_string|>[%sexp_of:string]typekind=|Constraintsof{constraints:constrlist;base_type:core_type}|BuildofBotodata.structure_shape[@@derivingsexp_of]letconstraintsbase_typel=Constraints{constraints=List.filter_optl;base_type}letkindshape=letopenOptioninletloc=!Ast_helper.default_locinmatchshapewith|Botodata.Integer_shapeis->constraints[%type:int][is.min>>|int_min;is.max>>|int_max]|Long_shapels->constraints[%type:int64][ls.min>>|int64_min;ls.max>>|int64_max]|String_shapess->constraints[%type:string][ss.pattern>>|pattern;ss.min>>|string_min;ss.max>>|string_max]|Blob_shapebs->constraints[%type:string][bs.min>>|string_min;bs.max>>|string_max]|List_shapels->letelt_ty=Shape.core_type_of_shapels.member.shapeinconstraints[%type:[%telt_ty]list][ls.min>>|list_min;ls.max>>|list_max]|Map_shapems->letkey_ty=Shape.core_type_of_shapems.keyinletvalue_ty=Shape.core_type_of_shapems.valueinconstraints[%type:([%tkey_ty]*[%tvalue_ty])list][ms.min>>|list_min;ms.max>>|list_max]|Timestamp_shape_->constraints[%type:string][](* FIXME: the format of time stamp should be checked *)|Enum_shape_->constraints[%type:t][]|Boolean_shape_->constraints[%type:bool][]|Float_shapefs->constraints[%type:float][fs.min>>|float_min;fs.max>>|float_max]|Double_shapeds->constraints[%type:float][ds.min>>|float_min;ds.max>>|float_max]|Structure_shapes->Builds;;let%expect_test"kind"=lettestshape=Format.printf!"%{sexp:kind}%!"(kindshape)inletinteger_shape?min?max()=Botodata.Integer_shape{box=None;min;max;documentation=None;deprecated=None;deprecatedMessage=None}intest(integer_shape());[%expect{| (Constraints (constraints ()) (base_type int)) |}];test(integer_shape~min:3());[%expect{| (Constraints (constraints ((Int_min 3))) (base_type int)) |}];test(integer_shape~max:5());[%expect{| (Constraints (constraints ((Int_max 5))) (base_type int)) |}];test(integer_shape~min:3~max:5());[%expect{| (Constraints (constraints ((Int_min 3) (Int_max 5))) (base_type int)) |}];letlong_shape?min?max()=Botodata.Long_shape{box=None;min;max;documentation=None}intest(long_shape());[%expect{| (Constraints (constraints ()) (base_type int64)) |}];test(long_shape~min:3L~max:5L());[%expect{| (Constraints (constraints ((Int64_min 3) (Int64_max 5))) (base_type int64)) |}];letstring_shape?min?max?pattern()=Botodata.String_shape{pattern;min;max;sensitive=None;documentation=None;deprecated=None;deprecatedMessage=None}intest(string_shape());[%expect{| (Constraints (constraints ()) (base_type string)) |}];test(string_shape~min:3~max:5~pattern:"PATTERN"());[%expect{|
(Constraints (constraints ((Pattern PATTERN) (String_min 3) (String_max 5)))
(base_type string)) |}];letblob_shape?min?max()=Botodata.Blob_shape{min;max;sensitive=None;streaming=None;documentation=None}intest(blob_shape());[%expect{| (Constraints (constraints ()) (base_type string)) |}];test(blob_shape~min:3~max:5());[%expect{|
(Constraints (constraints ((String_min 3) (String_max 5)))
(base_type string)) |}];letlist_shape?min?max()=Botodata.List_shape{min;max;member=shape_member"shape";documentation=None;flattened=None;sensitive=None;deprecatedMessage=None;deprecated=None}intest(list_shape());[%expect{| (Constraints (constraints ()) (base_type "Shape.t list")) |}];test(list_shape~min:3~max:5());[%expect{|
(Constraints (constraints ((List_min 3) (List_max 5)))
(base_type "Shape.t list")) |}];letmap_shape?min?max()=Botodata.Map_shape{min;max;key="key";value="value";locationName=None;documentation=None;flattened=None;sensitive=None}intest(map_shape());[%expect{| (Constraints (constraints ()) (base_type "(Key.t * Value.t) list")) |}];test(map_shape~min:3~max:5());[%expect{|
(Constraints (constraints ((List_min 3) (List_max 5)))
(base_type "(Key.t * Value.t) list")) |}];;letbody?result_wrappershape=matchkindshapewith|Builds->make_of_structure_shape?result_wrappers|Constraints{constraints;_}->letis_pipe=matchshapewith|Blob_shape_->true|_->falseinapply_constraints~is_pipeconstraints;;letstructure_item_of_shape?result_wrappershape=letloc=!Ast_helper.default_locin[%striletmake=[%ebody?result_wrappershape]];;letstructure_make_typess=letloc=!Ast_helper.default_locinletmembers=structure_membersssinletinit=[%type:unit->t]inList.fold_rightmembers~init~f:(fun(required,_,id,member)acc->letlabel=ifrequiredthenLabelledidelseOptionalidinletty=Shape.core_type_of_shapemember.shapeinAst_helper.Typ.arrowlabeltyacc);;lettype_of_shapes=letloc=!Ast_helper.default_locinmatchkindswith|Constraints{base_type;_}->[%type:[%tbase_type]->t]|Buildss->structure_make_typess;;letprivate_flag_of_shapeshape=matchshape,kindshapewith|List_shape_,_->(* Make exception for list shapes so we can destruct them naturally *)Public|_,Constraints{constraints=[];_}->Public|_,Constraints_->Private|_,Build_->Public;;