123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354(*
The MIT License (MIT)
Copyright (c) 2016 Maxime Ransan <maxime.ransan@gmail.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.
*)moduleE=Pb_exceptionmodulePt=Pb_parsing_parse_treemoduleTt=Pb_typing_type_treemoduleTyping_util=Pb_typing_utilletscope_of_package:stringoption->Tt.type_scope=function|Somes->{Typing_util.empty_scopewithTt.packages=List.rev@@Pb_util.rev_split_by_char'.'s;}|None->Typing_util.empty_scopeletget_defaultfield_namefield_options:Pb_option.constantoption=matchPb_option.getfield_options"default"with|Some(Pb_option.Scalar_valueconstant)->Someconstant|Some(Pb_option.Message_literal_)->E.invalid_default_value~field_name~info:"message literals are unsupported for default values"()|Some(Pb_option.List_literal_)->E.invalid_default_value~field_name~info:"list literals are unsupported for default values"()|None->Noneletcompile_field_p1field_parsed:_Tt.field=let{Pt.field_type;Pt.field_options;Pt.field_name;_}=field_parsedinletfield_default=get_defaultfield_namefield_optionsin{Tt.field_parsed;Tt.field_type;Tt.field_default;Tt.field_options}letcompile_map_p1map_parsed:_Tt.map_field=let{Pt.map_name;Pt.map_number;Pt.map_key_type;Pt.map_value_type;Pt.map_options;}=map_parsedinTt.{map_name;map_number;map_key_type;map_value_type;map_options}letcompile_oneof_p1oneof_parsed:_Tt.oneof=letinit={Tt.oneof_name=oneof_parsed.Pt.oneof_name;Tt.oneof_fields=[];Tt.oneof_options=Pb_option.empty;}inletoneof=List.fold_left(funacc->function|Pt.Oneof_fieldf->{accwithTt.oneof_fields=compile_field_p1f::acc.Tt.oneof_fields;}|Pt.Oneof_option(name,value)->{accwithTt.oneof_options=Pb_option.addacc.Tt.oneof_optionsnamevalue;})initoneof_parsed.Pt.oneof_bodyin(* now reverse the fields so they're back in the original order *){oneofwithTt.oneof_fields=List.revoneof.oneof_fields}letnot_foundf:bool=tryf();falsewithNot_found->trueletreclist_assoc2x=function|[]->raiseNot_found|(a,b)::l->ifcomparebx=0thenaelselist_assoc2xlletmake_proto_type~file_name~file_options~id~scope~spec:_Tt.proto_type={Tt.id;Tt.scope;Tt.file_name;Tt.file_options;Tt.spec}(** compile a [Pbpt] enum to a [Pbtt] type *)letcompile_enum_p1?(parent_options=Pb_option.empty)file_namefile_optionsscopeparsed_enum=let{Pt.enum_id;enum_name;enum_body}=parsed_enuminletenum_values=Pb_util.List.filter_map(function|Pt.Enum_value{Pt.enum_value_name;enum_value_int;enum_value_options}->SomeTt.{enum_value_name;enum_value_int;enum_value_options}|_->None)enum_bodyinletenum_options=enum_body|>Pb_util.List.filter_map(function|Pt.Enum_optiono->Someo|_->None)|>List.fold_left(funenum_options(name,value)->Pb_option.addenum_optionsnamevalue)Pb_option.emptyinletspec=Tt.Enum{Tt.enum_name;Tt.enum_values;Tt.enum_options=Pb_option.mergeparent_optionsenum_options;}inmake_proto_type~file_name~file_options~id:enum_id~scope~spec(** compile a [Pbpt] message a list of [Pbtt] types (ie messages can
defined more than one type). *)letrecvalidate_message?(parent_options=Pb_option.empty)file_namefile_optionsmessage_scopeparsed_message:_Tt.proto_typelist=let{Pt.id;Pt.message_name;Pt.message_body}=parsed_messageinlet{Tt.message_names;_}=message_scopeinletsub_scope={message_scopewithTt.message_names=message_names@[message_name]}inletmoduleAcc=struct(* Ad-hoc module for the "large" accumulated data during the
fold_left below.
*)type('a,'b,'d)t={message_body:'alist;extensions:'blist;options:Pb_option.set;all_types:'dlist;}lete0parent_options={message_body=[];extensions=[];options=parent_options;all_types=[];}endinletacc=List.fold_left(funaccfield->let{Acc.message_body;extensions;options;all_types}=accinmatchfieldwith|Pt.Message_fieldf->letfield=Tt.Message_field(compile_field_p1f)in{accwithAcc.message_body=field::message_body}|Pt.Message_map_fieldm->letfield=Tt.Message_map_field(compile_map_p1m)in{accwithAcc.message_body=field::message_body}|Pt.Message_oneof_fieldo->letfield=Tt.Message_oneof_field(compile_oneof_p1o)in{accwithAcc.message_body=field::message_body}|Pt.Message_subm->letparent_options=optionsinletall_sub_types=validate_message~parent_optionsfile_namefile_optionssub_scopemin{accwithAcc.all_types=all_types@all_sub_types}|Pt.Message_enumparsed_enum->letparent_options=optionsinletenum=compile_enum_p1~parent_optionsfile_namefile_optionssub_scopeparsed_enumin{accwithAcc.all_types=all_types@[enum]}|Pt.Message_extensionextension_ranges->{accwithAcc.extensions=extensions@extension_ranges}|Pt.Message_reserved_->acc(* TODO add support for checking reserved fields *)|Pt.Message_optionmessage_option->letoptions=Pb_option.addoptions(fstmessage_option)(sndmessage_option)in{accwithAcc.options})(Acc.e0parent_options)message_bodyinletmessage_body=List.revacc.Acc.message_bodyin(* TODO: Maybe [validate_duplicate] should be in
[Pb_parsing_util.verify_message] along with
the proto3 invariant. *)(* Both field name and field number must be unique
within a message scope. This includes the field in a
oneof field inside the message.
This function verifies this constrain and raises
the corresponding Duplicated_field_number exception in
case it is violated. *)letvalidate_duplicate(number_index:(int*string)list)namenumber=ifnot_found(fun()->ignore@@List.assocnumbernumber_index)&¬_found(fun()->ignore@@list_assoc2namenumber_index)then(number,name)::number_indexelseE.duplicated_field_number~field_name:name~previous_field_name:""~message_name()inletvalidate_duplicate_fieldnumber_indexfield=letnumber=Typing_util.field_numberfieldinletname=Typing_util.field_namefieldinvalidate_duplicatenumber_indexnamenumberinignore(List.fold_left(funnumber_index->function|Tt.Message_fieldfield->validate_duplicate_fieldnumber_indexfield|Tt.Message_oneof_field{Tt.oneof_fields;_}->List.fold_leftvalidate_duplicate_fieldnumber_indexoneof_fields|Tt.Message_map_fieldm->let{Tt.map_name;map_number;_}=minvalidate_duplicatenumber_indexmap_namemap_number)[]message_body:_list);letspec=Tt.Message{Tt.extensions=acc.Acc.extensions;message_options=acc.Acc.options;message_name;message_body;}inacc.Acc.all_types@[make_proto_type~file_name~file_options~id~scope:message_scope~spec]letvalidate_service(scope:Tt.type_scope)~file_name(service:Pt.service):_Tt.service=let{Pt.service_name;service_body}=serviceinletservice_body=List.filter_map(function|Pt.Service_option_->None|Pt.Service_rpc{rpc_name;rpc_options;rpc_req_stream;rpc_req;rpc_res_stream;rpc_res;}->letrpc_req=matchrpc_reqwith|`User_definedty->ty|_->E.invalid_rpc_req_type~service_name~rpc_name()inletrpc_res=matchrpc_reswith|`User_definedty->ty|_->E.invalid_rpc_res_type~service_name~rpc_name()inletrpc={Tt.rpc_name;rpc_options;rpc_req_stream;rpc_req;rpc_res_stream;rpc_res;}inSomerpc)service_bodyin{Tt.service_packages=scope.packages;service_file_name=file_name;service_name;service_body;}letvalidate(proto:Pt.proto):_Tt.proto=let{Pt.package;Pt.proto_file_name;messages;enums;file_options;services;_;}=protoinletfile_name=Pb_util.Option.default""proto_file_nameinletscope=scope_of_packagepackageinletpbtt_msgs=List.fold_right(funepbtt_msgs->[compile_enum_p1file_namefile_optionsscopee]::pbtt_msgs)enums[]inletproto_types=List.fold_left(funpbtt_msgspbpt_msg->lettys=validate_messagefile_namefile_optionsscopepbpt_msgintys::pbtt_msgs)pbtt_msgsmessagesinletproto_services=List.map(validate_servicescope~file_name)servicesin{Tt.proto_types;proto_services}