123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370(*
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_treemoduleString_map=Pb_util.Str_mapmoduleTypes_by_scope=structtypetype_=Pb_field_type.unresolvedTt.proto_typeletname_of_type{Tt.spec;_}=matchspecwith|Tt.Enum{Tt.enum_name;_}->enum_name|Tt.Message{Tt.message_name;_}->message_nametypet={name:string;types:type_String_map.t;subs:tString_map.t;}letempty?(name="")()={name;types=String_map.empty;subs=String_map.empty}letaddttype_=letrecauxtpackagesmessage_names=let{types;subs;_}=tinmatchpackages,message_nameswith|[],[]->{twithtypes=String_map.add(name_of_typetype_)type_types}|name::packages,message_names|([]aspackages),name::message_names->letsub=matchString_map.findnamesubswith|sub->auxsubpackagesmessage_names|exceptionNot_found->aux(empty~name())packagesmessage_namesinletsubs=subs|>String_map.removename|>String_map.addnamesubin(* remove + add = replace *){twithsubs}inlet{Tt.scope={Tt.packages;message_names};_}=type_inauxtpackagesmessage_namesletfindtpathtype_name=letrecauxt=function|[]->String_map.findtype_namet.types|name::path->aux(String_map.findnamet.subs)pathinauxtpathletfpf=Printf.fprintfletrecprint?(level=0)oc{types;subs;name}:unit=fpfoc"%s %s\n"(Pb_util.indentation_prefixlevel)name;String_map.iter(fun_t->fpfoc"%s +-%s \n"(Pb_util.indentation_prefixlevel)(name_of_typet))types;String_map.iter(fun_sub->printoc~level:(level+1)sub)subsletprintoct=printoctletempty=empty()end(* Types_by_scope *)(* this function returns the type path of a message which is the
packages followed by the enclosing message names and eventually
the message name of the given type.
If the type is an enum then [Failure] is raised.
TODO: change [Failure] to a [Pb_exception.Compilation_error] *)lettype_path_of_type{Tt.scope;spec;_}=matchspecwith|Tt.Enum_->assertfalse|Tt.Message{Tt.message_name;_}->let{Tt.packages;message_names}=scopeinpackages@message_names@[message_name](* this function returns all the scope to search for a type starting
by the most innner one first.
If [message_scope] = ['Msg1'; 'Msg2'] and [field_scope] = ['Msg3'] then
the following scopes will be returned:
[
['Msg1'; 'Msg2'; 'Msg3']; // This would be the scope of the current msg
['Msg1'; 'Msg3'; ]; // Outer message scope
['Msg3'; ] // Top level scope
] *)letcompute_search_type_pathsunresolved_field_typemessage_type_path=let{Pb_field_type.type_path;type_name=_;from_root}=unresolved_field_typeiniffrom_rootthen[type_path]else(letreclooptype_paths=function|[]->type_path::type_paths|l->loop((l@type_path)::type_paths)(Pb_util.List.pop_lastl)inList.rev@@loop[]message_type_path)(** this function ensure that the default value of the field is correct
with respect to its type when this latter is a builtin one.
in case the default value is invalid then an
[Pb_exception.Compilation_error] is raised.
Note that this function also does type coersion when the default value
is an int and the builtin type is a float or double. *)letresolve_builtin_type_field_defaultfield_namebuiltin_typefield_default=matchfield_defaultwith|None->None|Someconstant->(matchbuiltin_typewith|#Pb_field_type.builtin_type_floating_point->(matchconstantwith|Pb_option.Constant_inti->Some(Pb_option.Constant_float(float_of_inti))|Pb_option.Constant_float_->Someconstant|_->E.invalid_default_value~field_name~info:"invalid default type (float/int expected)"())|#Pb_field_type.builtin_type_signed_int->(matchconstantwith|Pb_option.Constant_int_->Someconstant|_->letinfo="invalid default type (int expected)"inE.invalid_default_value~field_name~info())|#Pb_field_type.builtin_type_unsigned_int->(matchconstantwith|Pb_option.Constant_inti->ifi>=0thenSomeconstantelseE.invalid_default_value~field_name~info:"negative default value for unsigned int"()|_->E.invalid_default_value~field_name~info:"invalid default type (int expected)"())|`Bool->(matchconstantwith|Pb_option.Constant_bool_->Someconstant|_->letinfo="invalid default type (bool expected)"inE.invalid_default_value~field_name~info())|`String->(matchconstantwith|Pb_option.Constant_string_->Someconstant|_->letinfo="invalid default type (string expected)"inE.invalid_default_value~field_name~info())|`Bytes->E.invalid_default_value~field_name~info:"default value not supported for bytes"())(** This function verifies that the default value for a used defined
field is correct.
In protobuf, only field which type is [enum] can have a default
value. Field of type [message] can't.
In the case the field is an enum then the default value must be
a litteral value which is one of the enum value.
If the validation fails then [Pb_exception.Compilation_error] is raised *)letresolve_enum_field_defaultfield_nametype_field_default=matchfield_defaultwith|None->None|Some(Pb_option.Constant_literaldefault_enum_valueasconstant)->let{Tt.spec;_}=type_in(matchspecwith|Tt.Message_->letinfo="field of type message cannot have a "^"default litteral value"inE.invalid_default_value~field_name~info()|Tt.Enum{Tt.enum_values;_}->letdefault_enum_value=Pb_util.List.apply_until(funenum->let{Tt.enum_value_name;_}=enuminifenum_value_name=default_enum_valuethenSomeenum_value_nameelseNone)enum_valuesin(matchdefault_enum_valuewith|Some_->Someconstant|None->E.invalid_default_value~field_name~info:"Invalid default enum value"()))|_->E.invalid_default_value~field_name~info:"default value not supported for message"()letresolve_user_defined_typetfield_name(unresolved_field_type:Pb_field_type.unresolved)field_defaultmessage_type_path:int*_=let{Pb_field_type.type_name;_}=unresolved_field_typeinletrecaux=function|[]->raiseNot_found|type_path::tl->(matchTypes_by_scope.findttype_pathtype_namewith|type_->letid=type_.Tt.idinletfield_default=resolve_enum_field_defaultfield_nametype_field_defaultinid,field_default|exceptionNot_found->auxtl)inaux(compute_search_type_pathsunresolved_field_typemessage_type_path)(** this function resolves both the type and the defaut value of a field
type. Note that it is necessary to verify both at the same time since
the default value must be of the same type as the field type in order
to be valid.
For builtin the type the validation is trivial while for user defined
type a search must be done for all the possible scopes the type
might be in. *)letresolve_field_type_and_defaulttfield_namefield_typefield_defaultmessage_type_path=matchfield_typewith|#Pb_field_type.builtin_typeasbuiltin_type->letfield_default=resolve_builtin_type_field_defaultfield_namebuiltin_typefield_defaultinbuiltin_type,field_default|`User_definedunresolved_field_type->letid,default=resolve_user_defined_typetfield_nameunresolved_field_typefield_defaultmessage_type_pathin`User_definedid,default(** this function resolves all the field type of the given type *)letresolve_typettype_:intTt.proto_type=let{Tt.scope;id;file_name;file_options;spec}=type_inmatchspecwith|Tt.Enume->{Tt.scope;id;file_name;file_options;spec=Tt.Enume}|Tt.Messagemessage->let{Tt.extensions;message_options;message_name;message_body}=messageinletmessage_type_path=type_path_of_typetype_inletresolve_fieldfield=let{Tt.field_parsed;field_type;field_default;field_options}=fieldinletfield_name=field_parsed.Pt.field_nameinletfield_type,field_default=letdo_resolve()=resolve_field_type_and_defaulttfield_namefield_typefield_defaultmessage_type_pathinmatchdo_resolve()with|ret->ret|exceptionNot_found->E.unresolved_type~field_name~type_:""~message_name()in{Tt.field_parsed;field_type;field_default;field_options}inletmessage_body=List.map(function|Tt.Message_fieldfield->Tt.Message_field(resolve_fieldfield)|Tt.Message_oneof_fieldoneof->let{Tt.oneof_name;oneof_fields;oneof_options}=oneofinletoneof_fields=List.mapresolve_fieldoneof_fieldsinTt.Message_oneof_field{Tt.oneof_name;oneof_fields;oneof_options}|Tt.Message_map_fieldmap->let{Tt.map_name;map_number;map_key_type;map_value_type;map_options;}=mapinletfield_default=Noneinletmap_value_type,_=resolve_field_type_and_defaulttmap_namemap_value_typefield_defaultmessage_type_pathinTt.Message_map_field{Tt.map_name;map_number;map_options;map_key_type;map_value_type;})message_bodyinletspec=Tt.Message{Tt.extensions;message_options;message_name;message_body}in{Tt.scope;id;file_name;file_options;spec}letresolve_typestypes:Types_by_scope.t*_list=lett=List.fold_leftTypes_by_scope.addTypes_by_scope.emptytypesint,List.map(resolve_typet)typesletresolve_servicet(service:_Tt.service):_Tt.service=letresolve_ty~rpc_name~namety:Pb_field_type.resolved=letrpc_type,_field_default=letdo_resolve()=resolve_user_defined_typetnametyNoneservice.service_packagesinmatchdo_resolve()with|ret->ret|exceptionNot_found->E.unresolved_type~field_name:name~type_:""~message_name:rpc_name()inrpc_typeinletresolve_rpc(rpc:_Tt.rpc):_Tt.rpc=letrpc_name=rpc.rpc_namein{rpcwithTt.rpc_req=resolve_ty~rpc_name~name:"req"rpc.rpc_req;rpc_res=resolve_ty~rpc_name~name:"res"rpc.rpc_res;}in{servicewithTt.service_body=List.mapresolve_rpcservice.service_body}letresolve_services(t:Types_by_scope.t)(services:_Tt.servicelist):_Tt.servicelist=List.map(resolve_servicet)services