123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400(*
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_util(** Re-construct nested messages out of field path *)letnormalize_option_nameoption_namevalue=List.fold_right(funname_partacc->matchname_partwith|Pb_raw_option.Simple_namename->Pb_option.Message_literal[name,acc]|Pb_raw_option.Extension_namename->(* TODO: Consider supporting Extension_name in option names, as in
(foo).bar.(.baz.bob) *)failwith(Printf.sprintf"normalize_option_name: Extension_name '%s' is not supported in \
option_name"name))option_namevalueletoption_name_from_part=function|Pb_raw_option.Simple_namex->Pb_option.Simple_namex|Pb_raw_option.Extension_namex->Pb_option.Extension_namexletnormalize_optionoption_namevalue=matchoption_namewith|[]->failwith"option_name can't be an empty list!"|[single_item]->(* Only one top level component in option name - nothing fancy is required
in this case *)option_name_from_partsingle_item,value|top_level_item::rest->(* Top level option is a message, and we need to reconstruct nested messages
inside the value, and leave only top level name component as actual option
name *)letnew_value=normalize_option_namerestvalueinoption_name_from_parttop_level_item,new_value(** [compile_options set] is compiling raw options into Pb_option.set *)letcompile_optionsoption_set=(* Handle destructured lists *)letoption_set=Pb_raw_option.group_list_valuesoption_setinList.fold_left(funset(option_name,value)->(* Normalize option names into nested messages within values *)letoption_name,value=normalize_optionoption_namevaluein(* Pb_option.add is smart to merge nested messages, reconstructing the
final message values *)Pb_option.addsetoption_namevalue)Pb_option.emptyoption_setletscope_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_raw_option.get_simplefield_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_optionsinletfield_options=compile_optionsfield_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_parsedinletmap_options=compile_optionsmap_optionsinTt.{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;}in(* Compile one-of options separately, as compilation requires to see all raw
options at once *)letoneof_options=oneof_parsed.Pt.oneof_body|>Pb_util.List.filter_map(function|Pt.Oneof_optiono->Someo|_->None)|>List.fold_left(funoneof_options(name,value)->Pb_raw_option.addoneof_optionsnamevalue)Pb_raw_option.empty|>compile_optionsinletoneof=List.fold_left(funacc->function|Pt.Oneof_fieldf->{accwithTt.oneof_fields=compile_field_p1f::acc.Tt.oneof_fields;}|_->acc)initoneof_parsed.Pt.oneof_bodyin(* now reverse the fields so they're back in the original order *){oneofwithTt.oneof_fields=List.revoneof.oneof_fields;Tt.oneof_options}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_p1file_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}->letenum_value_options=compile_optionsenum_value_optionsinSomeTt.{enum_value_name;enum_value_int;enum_value_options}|_->None)enum_bodyin(* Compile enum options separately, as compilation requires to see all raw
options at once *)letenum_options=enum_body|>Pb_util.List.filter_map(function|Pt.Enum_optiono->Someo|_->None)|>List.fold_left(funenum_options(name,value)->Pb_raw_option.addenum_optionsnamevalue)Pb_raw_option.empty|>compile_optionsinletspec=Tt.Enum{Tt.enum_name;Tt.enum_values;Tt.enum_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_messagefile_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_raw_option.set;all_types:'dlist;}lete0={message_body=[];extensions=[];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->letall_sub_types=validate_messagefile_namefile_optionssub_scopemin{accwithAcc.all_types=all_types@all_sub_types}|Pt.Message_enumparsed_enum->letenum=compile_enum_p1file_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_option(name,value)->letoptions=Pb_raw_option.addoptionsnamevaluein{accwithAcc.options})Acc.e0message_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|>compile_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_options=compile_optionsrpc_optionsinletrpc={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_packagepackageinletfile_options=compile_optionsfile_optionsinletpbtt_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}