text stringlengths 0 601k |
|---|
let build_schema_inner_module file_lens complex_type = let render_type_t formatter fields = match fields with | [ ] -> Format . fprintf formatter [ " @< v 2 > type t = unit , " @ | _ -> Format . fprintf formatter [ " @< v 2 > type t = { , " ; @ List . iter ( fun { Field . ocaml_name ; ocaml_type ; field_type ; _ } -> i... |
module FieldSet = struct include Set . Make ( struct type t = string * Field . t let compare ( id1 , _ ) _ ( id2 , _ ) _ = compare id1 id2 end ) end let merge element s = if mem element s then let _ , { Field . field_type ; _ } = element in if ComplexType . is_enum field_type then let scalar = match field_type . Comple... |
let generate_enum_module formatter inner_module_lens ( id , { Field . field_type ; _ } ) = let enum_module_lens = inner_module_lens |-- InnerServiceModule . get_enum_lens id in let scalar = match field_type . ComplexType . data_type with | ComplexType . Scalar s -> s | _ -> failwith " Complex type not supported in gene... |
let filter_parameters source methods_lens cond = let parameters = let open GapiMonad . ListM in let open GapiMonad . ListM . Infix in source . | methods_lens . | GapiLens . list_map GapiLens . second >>= fun rest_method -> rest_method . | RestMethod . parameters >>= fun parameter -> let json_schema = snd parameter in g... |
let filter_resource_parameters resource cond = filter_parameters resource RestResource . methods cond |
let generate_enum_modules formatter inner_module_lens filter source = let enum_parameters_set = filter source ( fun json_schema -> json_schema . JsonSchema . enum != [ ] ) in mapM_ ( fun parameter -> generate_enum_module formatter inner_module_lens parameter ) parameter ( FieldSet . elements enum_parameters_set ) enum_... |
let generate_parameters_module filter_parameters formatter inner_module_lens resource_id resource = let render_type_t formatter parameters = Format . fprintf formatter [ " @< v 2 > type t = { , @\ , @\ alt : string ; , @\ fields : string ; , @\ prettyPrint : bool ; , @\ quotaUser : string ; , @\ userIp : string ; , @\ ... |
let forward_slash_regxp = Str . regexp_string " " / |
let generate_rest_method formatter inner_module_lens ( id , rest_method ) rest_method = let generate_method_body methd = GapiLens . get_state ( inner_module_lens |-- InnerServiceModule . parameters_module_name ) parameters_module_name >>= fun parameters_module_name -> let build_path_to_add path = let splitted_path = St... |
let rec build_service_inner_module file_lens current_module_lens is_nested ( resource_id , resource ) resource = let module_name = OCamlName . get_ocaml_name ModuleName ( if is_nested then resource_id else resource_id ^ " Resource ) " in let inner_module = InnerServiceModule . create resource_id module_name in let inne... |
let filter_service_parameters rest cond = filter_parameters rest RestDescription . methods cond |
let rec build_api_level_service_module file_lens = let api_level_module_lens = State . get_service_module |-- ServiceModule . get_api_level_module in GapiLens . get_state ( State . service |-- RestDescription . name ) name >>= fun name -> let ocaml_name = OCamlName . get_ocaml_name ModuleName name in api_level_module_l... |
let build_module file_type generate_body = let file_lens = State . get_file_lens file_type in let formatter_lens = file_lens |-- File . formatter in GapiLens . get_state State . service >>= fun service -> let service_name = service . RestDescription . name in let service_version = service . RestDescription . version in... |
let build_schema_module = let generate_body file_lens = GapiLens . get_state file_lens >>= fun file -> let schema_module = SchemaModule . create file . File . module_name in State . get_schema_module_lens ^=! schema_module >>= fun ( ) -> GapiLens . get_state State . sorted_types >>= fun sorted_types -> mapM_ ( fun comp... |
let build_service_module = let rec get_suffix value = let last_slash_position = String . rindex value ' / ' in if last_slash_position > 0 then let suffix = Str . string_after value ( last_slash_position + 1 ) 1 in if String . length suffix > 0 then suffix else get_suffix ( String . sub value 0 last_slash_position ) las... |
let rec generate_schema_module_signature formatter_lens schema_module is_nested = let type_t = schema_module . InnerSchemaModule . type_t in match type_t with | InnerSchemaModule . Record record -> let fields = record . Record . fields in GapiLens . get_state formatter_lens >>= fun formatter -> lift_io $ Format . fprin... |
let build_schema_module_interface = let generate_body file_lens = let formatter_lens = file_lens |-- File . formatter in GapiLens . get_state formatter_lens >>= fun formatter -> GapiLens . get_state State . service >>= fun service -> lift_io ( Format . fprintf formatter [ " @< hov 2 ] >@@\ n @\ n ) " >>= fun ( ) -> Gap... |
let rec generate_service_module_signature omit_declaration file_lens service_module = let render_enum_module formatter enum_module = Format . fprintf formatter " @\ nmodule % s :@\ n [ @< v 2 > sig [ , @@< v 2 > type t , =@| Default , " @ enum_module . EnumModule . ocaml_name ; List . iter ( fun ( _ , { EnumModule . co... |
let build_service_module_interface = let render_scope formatter scopes = List . iter ( fun ( id , scope ) scope -> Format . fprintf formatter " val % s : string @\ n @\ n @\ n " id scope ) scope scopes in let generate_body file_lens = GapiLens . get_state ( file_lens |-- File . formatter ) formatter >>= fun formatter -... |
let generate_code service = let build_all = State . build_type_table >>= fun ( ) -> State . build_sorted_types >>= fun ( ) -> build_schema_module >>= fun ( ) -> build_service_module >>= fun ( ) -> build_schema_module_interface >>= fun ( ) -> build_service_module_interface in let initial_state = State . create service i... |
let _ = let api = ref " " in let version = ref " " in let nocache = ref false in let usage = " Usage : " ^ Sys . executable_name ^ " - api < apiname > - version < apiver > [ - nocache ] nocache [ - nooverwrite ] nooverwrite [ - outdir \ < path ] " > in let arg_specs = Arg . align [ ( " - api " , Arg . Set_string api , ... |
type name_type = | ModuleName | TypeName | ValueName | ParameterName | FieldName | ConstructorName |
module OCamlName = struct let keywords = [ " type " ; " method " ; " private " ; " end " ; " ref " ; " object " ; " open " ; " to " ] let is_first_character_valid s = match s [ . 0 ] 0 with ' a ' . . ' z ' | ' A ' . . ' Z ' | ' _ ' -> true | _ -> false let replace_invalid_characters s = String . map ( fun c -> match c ... |
let get_anonymous_type_module_name id = OCamlName . get_ocaml_name ModuleName id |
module ScalarType = struct type format_t = | NoFormat | DateTimeFormat | DateFormat | TimeFormat | Int32Format | Int64Format | FloatFormat | DoubleFormat | OtherFormat of string let format_of_string = function | " " -> NoFormat | " date - time " -> DateTimeFormat | " date " -> DateFormat | " time " -> TimeFormat | " in... |
module ComplexType = struct type data_t = | Scalar of ScalarType . t | Object of ( string * t ) t list | AnonymousObject of ( string * ( string * t ) t list ) list | Array of t | Reference of string | Dictionary of t and t = { id : string ; data_type : data_t ; original_type : string ; description : string ; } let id =... |
module Field = struct type t = { original_name : string ; ocaml_name : string ; ocaml_type : string ; ocaml_type_module : string ; field_type : ComplexType . t ; empty_value : string ; default : string ; to_string_function : string ; is_recursive : bool ; is_option : bool ; } let original_name = { GapiLens . get = ( fu... |
module Record = struct type t = { module_name : string ; fields : ( string * Field . t ) t list } let module_name = { GapiLens . get = ( fun x -> x . module_name ) module_name ; GapiLens . set = ( fun v x -> { x with module_name = v } ) ; } let fields = { GapiLens . get = ( fun x -> x . fields ) fields ; GapiLens . set... |
module Method = struct type t = { original_name : string ; ocaml_name : string ; description : string ; parameters : ( string * Field . t ) t list ; parameter_order : string list ; request : Field . t option ; response : Field . t option ; supports_media_upload : bool ; supports_media_download : bool ; } let original_n... |
module InnerSchemaModule = struct type type_t = Record of Record . t | List of t | Alias of string and t = { original_name : string ; ocaml_name : string ; type_t : type_t ; inner_modules : ( string * t ) t list ; } let original_name = { GapiLens . get = ( fun x -> x . original_name ) original_name ; GapiLens . set = (... |
module EnumModule = struct type value = { constructor : string ; value : string ; description : string } type t = { original_name : string ; ocaml_name : string ; values : ( string * value ) value list ; } let original_name = { GapiLens . get = ( fun x -> x . original_name ) original_name ; GapiLens . set = ( fun v x -... |
module InnerServiceModule = struct type t = { original_name : string ; ocaml_name : string ; methods : ( string * Method . t ) t list ; inner_modules : ( string * t ) t list ; enums : ( string * EnumModule . t ) t list ; parameters_module_name : string ; } let original_name = { GapiLens . get = ( fun x -> x . original_... |
module SchemaModule = struct type t = { ocaml_name : string ; inner_modules : ( string * InnerSchemaModule . t ) t list ; } let ocaml_name = { GapiLens . get = ( fun x -> x . ocaml_name ) ocaml_name ; GapiLens . set = ( fun v x -> { x with ocaml_name = v } ) ; } let inner_modules = { GapiLens . get = ( fun x -> x . inn... |
module ServiceModule = struct type t = { ocaml_name : string ; inner_modules : ( string * InnerServiceModule . t ) t list ; scopes : ( string * string ) string list ; api_level_module : InnerServiceModule . t option ; } let ocaml_name = { GapiLens . get = ( fun x -> x . ocaml_name ) ocaml_name ; GapiLens . set = ( fun ... |
let get_full_path output_path file_name = if GapiUtils . string_ends_with output_path " " / then output_path ^ file_name else output_path ^ " " / ^ file_name |
type file_type = | SchemaModule | SchemaModuleInterface | ServiceModule | ServiceModuleInterface |
let string_of_file_type = function | SchemaModule -> " schema module " | SchemaModuleInterface -> " schema module interface " | ServiceModule -> " service module " | ServiceModuleInterface -> " service module interface " |
module File = struct type t = { file_type : file_type ; service_name : string ; service_version : string ; module_name : string ; file_name : string ; formatter : Format . formatter ; } let file_type = { GapiLens . get = ( fun x -> x . file_type ) file_type ; GapiLens . set = ( fun v x -> { x with file_type = v } ) ; }... |
module StringSet = struct include Set . Make ( struct type t = string let compare = compare end ) end let add_list xs s = List . fold_left ( fun s ' x -> add x s ' ) s ' s xs end |
module TypeTable = struct type t = ( string , ComplexType . t ) t Hashtbl . t let create ( ) = Hashtbl . create 64 let fold f v table = Hashtbl . fold f table v let build complex_types = let table = Hashtbl . create 64 in List . iter ( fun complex_type -> Hashtbl . add table complex_type . ComplexType . id complex_type... |
module State = struct type t = { service : RestDescription . t ; files : ( file_type , File . t ) t Hashtbl . t ; schema_module : SchemaModule . t option ; service_module : ServiceModule . t option ; type_table : TypeTable . t ; sorted_types : ComplexType . t list ; } let service = { GapiLens . get = ( fun x -> x . ser... |
module Forge = struct let block custom_root = let open Data_encoding in RPC_service . post_service ~ description : " Forge a block " ~ query : RPC_query . empty ~ input : ( merge_objs ( obj6 ( req " level " int32 ) ( req " proto_level " uint8 ) ( req " predecessor " Block_hash . encoding ) ( req " timestamp " Time . en... |
let int64_to_bytes i = let b = MBytes . create 8 in MBytes . set_int64 b 0 i ; b |
let operations_hash = Operation_list_list_hash . compute [ ] |
let rpc_services : Updater . rpc_context RPC_directory . t = let dir = RPC_directory . empty in let dir = RPC_directory . register dir ( Forge . block RPC_path . open_root ) ( fun _ctxt ( ) ( ( level , proto_level , predecessor , timestamp , fitness , context ) , command ) -> let shell = { Block_header . level ; proto_... |
type rpc_context = { block_hash : Block_hash . t ; block_header : Block_header . shell_header ; context : Alpha_context . t ; } |
let rpc_init ( { block_hash ; block_header ; context } : Updater . rpc_context ) = let level = block_header . level in let timestamp = block_header . timestamp in Alpha_context . prepare ~ level ~ predecessor_timestamp : timestamp ~ timestamp context >|=? fun ( context , _ , _ ) -> { block_hash ; block_header ; context... |
let rpc_services = ref ( RPC_directory . empty : Updater . rpc_context RPC_directory . t ) |
let register0_fullctxt ~ chunked s f = rpc_services := RPC_directory . register ~ chunked ! rpc_services s ( fun ctxt q i -> rpc_init ctxt >>=? fun ctxt -> f ctxt q i ) |
let register0 ~ chunked s f = register0_fullctxt ~ chunked s ( fun { context ; _ } -> f context ) |
let register0_noctxt ~ chunked s f = rpc_services := RPC_directory . register ~ chunked ! rpc_services s ( fun _ q i -> f q i ) |
let register1_fullctxt ~ chunked s f = rpc_services := RPC_directory . register ~ chunked ! rpc_services s ( fun ( ctxt , arg ) q i -> rpc_init ctxt >>=? fun ctxt -> f ctxt arg q i ) |
let register1 ~ chunked s f = register1_fullctxt ~ chunked s ( fun { context ; _ } x -> f context x ) |
let register2_fullctxt ~ chunked s f = rpc_services := RPC_directory . register ~ chunked ! rpc_services s ( fun ( ( ctxt , arg1 ) , arg2 ) q i -> rpc_init ctxt >>=? fun ctxt -> f ctxt arg1 arg2 q i ) |
let register2 ~ chunked s f = register2_fullctxt ~ chunked s ( fun { context ; _ } a1 a2 q i -> f context a1 a2 q i ) |
let opt_register0_fullctxt ~ chunked s f = rpc_services := RPC_directory . opt_register ~ chunked ! rpc_services s ( fun ctxt q i -> rpc_init ctxt >>=? fun ctxt -> f ctxt q i ) |
let opt_register0 ~ chunked s f = opt_register0_fullctxt ~ chunked s ( fun { context ; _ } -> f context ) |
let opt_register1_fullctxt ~ chunked s f = rpc_services := RPC_directory . opt_register ~ chunked ! rpc_services s ( fun ( ctxt , arg ) q i -> rpc_init ctxt >>=? fun ctxt -> f ctxt arg q i ) |
let opt_register1 ~ chunked s f = opt_register1_fullctxt ~ chunked s ( fun { context ; _ } x -> f context x ) |
let opt_register2_fullctxt ~ chunked s f = rpc_services := RPC_directory . opt_register ~ chunked ! rpc_services s ( fun ( ( ctxt , arg1 ) , arg2 ) q i -> rpc_init ctxt >>=? fun ctxt -> f ctxt arg1 arg2 q i ) |
let opt_register2 ~ chunked s f = opt_register2_fullctxt ~ chunked s ( fun { context ; _ } a1 a2 q i -> f context a1 a2 q i ) |
let get_rpc_services ( ) = let p = RPC_directory . map ( fun c -> rpc_init c >|= function | Error t -> raise ( Failure ( Format . asprintf " % a " Error_monad . pp_trace t ) ) | Ok c -> c . context ) ( Storage_description . build_directory Alpha_context . description ) in RPC_directory . register_dynamic_directory ! rp... |
module StrMap = Map . Make ( String ) |
let of_yojson yojson = let open Yojson . Safe . Util in let session_list = try Some ( yojson |> to_assoc |> List . map ( fun ( k , v ) -> k , to_string v ) ) with | _ -> None in session_list |> Option . map ( fun s -> s |> List . to_seq |> StrMap . of_seq ) ; ; |
let of_json json = try of_yojson ( Yojson . Safe . from_string json ) with | _ -> None ; ; |
let to_yojson session = ` Assoc ( session |> StrMap . to_seq |> List . of_seq |> List . map ( fun ( k , v ) -> k , ` String v ) ) ; ; |
let to_json session = session |> to_yojson |> Yojson . Safe . to_string |
let to_sexp session = let open Sexplib0 . Sexp_conv in let open Sexplib0 . Sexp in let data = session |> StrMap . to_seq |> List . of_seq |> sexp_of_list ( sexp_of_pair sexp_of_string sexp_of_string ) in List [ List [ Atom " data " ; data ] ] ; ; |
let decode_session resp = let signed_with = Opium . Cookie . Signer . make ( Core_configuration . read_secret ( ) ) in match Opium . Response . cookie ~ signed_with " _session " resp with | None -> None | Some cookie_value -> let _ , value = cookie_value . Opium . Cookie . value in of_json value ; ; |
let get_all_resp resp = let session = decode_session resp in session |> CCOption . map ( fun s -> s |> StrMap . to_seq |> List . of_seq ) ; ; |
let find_resp key resp = let session = decode_session resp in Option . bind session ( StrMap . find_opt key ) |> Option . get ; ; |
let set_value_req session req = let signed_with = Opium . Cookie . Signer . make ( Core_configuration . read_secret ( ) ) in let req_session = CCOption . bind ( Opium . Request . cookie ~ signed_with " _session " req ) of_json in let session = session |> List . to_seq |> StrMap . of_seq in let session = match req_sessi... |
module type Backend = Session . S . Future with type key = string and type value = string and type period = int64 |
module type S = sig type ' + a io type backend type key type value type period type t = private { key : key ; mutable value : value ; mutable expiry_period : period ; mutable modified : bool } val of_key : backend -> key -> ( t , Session . S . error ) result io val of_header : backend -> string -> Header . t -> ( t opt... |
module Make ( IO : Session . S . IO ) ( B : Backend with type ' + a io = ' a IO . t ) = struct type ' + a io = ' a B . io type backend = B . t type key = B . key type value = B . value type period = B . period type t = { key : key ; mutable value : value ; mutable expiry_period : period ; mutable modified : bool } open... |
module W = struct type data = { conn : Postgresql . connection ; mutable period : period } module T : Weak . S with type data := data = Weak . Make ( struct type t = data let equal a b = a . conn = b . conn let hash a = Hashtbl . hash a . conn end ) include T end |
let _default_period = Int64 . of_int ( 60 * 60 * 24 * 7 ) |
let _default_period_table = W . create 5 |
let connect ? host ? hostaddr ? port ? dbname ? user ? password ? options ? tty ? requiressl ? conninfo ? startonly ( ) = new Postgresql . connection ? host ? hostaddr ? port ? dbname ? user ? password ? options ? tty ? requiressl ? conninfo ? startonly ( ) |
let gensym ( ) = Base64 . encode_string ( Cstruct . to_string ( Mirage_crypto_rng . generate 30 ) ) |
let now ( ) = Int64 . of_float ( Unix . time ( ) ) |
let default_period t = try let w = W . find _default_period_table { W . conn = t ; period = _default_period } in w . W . period with Not_found -> _default_period |
let set_default_period t period = let w = W . find _default_period_table { W . conn = t ; period } in w . W . period <- period |
let clear ( t : t ) key = let params = [ | key ] | in let _ = t # exec ~ expect [ : Command_ok ] ~ params " DELETE FROM session WHERE session_key = $ 1 ; " in ( ) |
let get ( t : t ) key = let params = [ | key ] | in let result = t # exec ~ expect [ : Tuples_ok ] ~ params " SELECT CAST ( EXTRACT ( epoch FROM expire_date ) AS INT8 ) , session_data FROM session WHERE session_key = $ 1 " in if result # ntuples = 0 then Error Session . S . Not_found else begin assert ( result # ntuple... |
let _prepare_expiry t = function | None -> Printf . sprintf " % Ld seconds " ( default_period t ) | Some expiry -> Printf . sprintf " % Ld seconds " expiry |
let _set ? expiry ( ? value = Postgresql . null ) ( t : t ) key = let params = [ | key ; _prepare_expiry t expiry ; value ] | in let _ = t # exec ~ expect [ : Command_ok ] ~ params " UPDATE session SET session_data = $ 3 , expire_date = NOW ( ) + CAST ( $ 2 AS INTERVAL ) WHERE session_key = $ 1 " in ( ) |
let set ? expiry t key value = _set ? expiry ~ value t key |
let generate ? expiry ( ? value = Postgresql . null ) ( t : t ) = let key : key = gensym ( ) in let params = [ | key ; _prepare_expiry t expiry ; value ] | in let _ = t # exec ~ expect [ : Command_ok ] ~ params " INSERT INTO session ( session_key , expire_date , session_data ) VALUES ( $ 1 , NOW ( ) + CAST ( $ 2 AS INT... |
include Deferred let in_thread f = In_thread . run f end ) ( Session_postgresql ) |
let connect ? host ? hostaddr ? port ? dbname ? user ? password ? options ? tty ? requiressl ? conninfo ? startonly ( ) = In_thread . run ( fun ( ) -> Session_postgresql . connect ? host ? hostaddr ? port ? dbname ? user ? password ? options ? tty ? requiressl ? conninfo ? startonly ( ) ) |
let set_default_period t period = Session_postgresql . set_default_period t period |
module Pool = struct type ' + a io = ' a Deferred . t type t = { pool : Postgresql . connection Throttle . t ; mutable default_period : period } type key = string type value = string type period = int64 let __de_default t = function | None -> t . default_period | Some expiry -> expiry let generate ? expiry ? value t = ... |
type ' a io = ' a Lwt . t |
type t = { pool : R . connection Lwt_pool . t ; mutable period : period ; } |
let _default_period = Int64 . of_int ( 60 * 60 * 24 * 7 ) |
let gensym ( ) = Base64 . encode_string ( Cstruct . to_string ( Mirage_crypto_rng . generate 30 ) ) |
let redis_key k = " session " : ^ k |
let of_connection_pool pool = { pool ; period = _default_period ; } |
let default_period t = t . period |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.