text
stringlengths 0
601k
|
---|
let warn_if_unable_to_pay_account_creation_fee ~ get_account ( ~ constraint_constants : Genesis_constants . Constraint_constants . t ) t ~ logger user_command_payload = let receiver_pk = Signed_command_payload . receiver_pk user_command_payload in let token = Signed_command_payload . token user_command_payload in let receiver = Account_id . create receiver_pk token in let receiver_account = get_account receiver in let amount = Signed_command_payload . amount user_command_payload in match ( receiver_account , amount ) amount with | ` Bootstrapping , _ | ` Active ( Some _ ) _ , _ | _ , None -> ( ) | ` Active None , Some amount -> let open Currency . Amount in let account_creation_fee = of_fee constraint_constants . account_creation_fee in if amount < account_creation_fee then [ % log warn ] warn " A transaction was submitted that is likely to fail because the \ receiver account doesn ' t appear to have been created already and \ the transaction amount of % s is smaller than the account creation \ fee of % s . " ( to_formatted_string amount ) amount ( to_formatted_string account_creation_fee ) account_creation_fee ; ( ) |
let to_user_command ( ? nonce_map = Account_id . Map . empty ) empty ~ get_current_nonce ~ get_account ~ constraint_constants ~ logger ( client_input : t ) t = Deferred . map ~ f : ( Result . map_error ~ f ( : fun str -> Error . createf " Error creating user command : % s Error : % s " ( Yojson . Safe . to_string ( to_yojson client_input ) client_input ) client_input str ) ) @@ let open Deferred . Result . Let_syntax in let fee_payer = fee_payer client_input in let % bind minimum_nonce , inferred_nonce , updated_nonce_map = inferred_nonce ~ get_current_nonce ~ fee_payer ~ nonce_map |> Deferred . return in let % bind user_command_payload = Payload . to_user_command_payload client_input . payload ~ minimum_nonce ~ inferred_nonce |> Deferred . return in let ( ) = warn_if_unable_to_pay_account_creation_fee ~ get_account ~ constraint_constants ~ logger user_command_payload in let % map signed_user_command = sign ~ signer : client_input . signer ~ user_command_payload client_input . signature in ( Signed_command . forget_check signed_user_command , updated_nonce_map ) updated_nonce_map |
let to_user_commands ( ? nonce_map = Account_id . Map . empty ) empty ~ get_current_nonce ~ get_account ~ constraint_constants ~ logger uc_inputs : Signed_command . t list Deferred . Or_error . t = let open Deferred . Or_error . Let_syntax in let % map user_commands , _ = Deferred . Or_error . List . fold ~ init ( [ ] , : nonce_map ) nonce_map uc_inputs ~ f ( : fun ( valid_user_commands , nonce_map ) nonce_map uc_input -> let % map res , updated_nonce_map = to_user_command ~ nonce_map ~ get_current_nonce ~ get_account ~ constraint_constants ~ logger uc_input in ( res :: valid_user_commands , updated_nonce_map ) updated_nonce_map ) in List . rev user_commands |
type t = { author_name : string option ; email : string option ; github_username : string option ; create_switch : bool option } |
let of_dec ( dec : Dec_user_config . t ) t = { author_name = dec . author_name ; email = dec . email ; github_username = dec . github_username ; create_switch = dec . create_switch } |
let path_of_opt value = value |> Option . map Result . ok |> Option . value ~ default : ( Config . spin_config_dir |> Result . map ( fun p -> Filename . concat p " default ) ) " |
let read ? path ( ) = let decode_if_exists path = if Sys . file_exists path then Decoder . decode_sexps_file path Dec_user_config . decode |> Result . map of_dec |> Result . map Option . some |> Result . map_error ( Spin_error . of_decoder_error ~ file : path ) path else Ok None in Result . bind ( path_of_opt path ) path decode_if_exists |
let save ? path ( t : t ) t = let open Result . Syntax in let + path = path_of_opt path in let ( ) = Sys . mkdir_p ( Filename . dirname path ) path in Encoder . encode_file path Dec_user_config . { author_name = t . author_name ; email = t . email ; github_username = t . github_username ; create_switch = t . create_switch } Dec_user_config . encode |
let validate_strip s = match String . trim s with " " -> Error " Enter a value . " | s -> Ok s |
let prompt ? default : d ( ) = let author_name = Inquire . input " Your name " ? default ( : Option . bind d ( fun d -> d . author_name ) author_name ) author_name ~ validate : validate_strip in let email = Inquire . input " Your email " ? default ( : Option . bind d ( fun d -> d . email ) email ) email ~ validate : validate_strip in let github_username = Inquire . input " Your GitHub username " ? default ( : Option . bind d ( fun d -> d . github_username ) github_username ) github_username ~ validate : validate_strip in let create_switch = Inquire . confirm " Create switches when generating projects " ? default ( : Option . bind d ( fun d -> d . create_switch ) create_switch ) create_switch in { author_name = Some author_name ; email = Some email ; github_username = Some github_username ; create_switch = Some create_switch } |
let to_context t = let context = Hashtbl . create 256 in Option . iter ( fun v -> Hashtbl . add context " author_name " v |> ignore ) ignore t . author_name ; Option . iter ( fun v -> Hashtbl . add context " email " v |> ignore ) ignore t . email ; Option . iter ( fun v -> Hashtbl . add context " github_username " v |> ignore ) ignore t . github_username ; context |
module Search = struct module Query = Empty . Query module Request = struct type t = { search_term : string ; limited : int option } [ @@ deriving accessor ] let encoding = let to_tuple t = t . search_term , t . limited in let of_tuple v = let search_term , limited = v in { search_term ; limited } in let with_tuple = obj2 ( req " search_term " string ) ( opt " limited " int ) in conv to_tuple of_tuple with_tuple end module Response = struct module User = struct type t = { user_id : string ; display_name : string option ; avatar_url : string option ; } [ @@ deriving accessor ] let encoding = let to_tuple t = t . user_id , t . display_name , t . avatar_url in let of_tuple v = let user_id , display_name , avatar_url = v in { user_id ; display_name ; avatar_url } in let with_tuple = obj3 ( req " user_id " string ) ( opt " display_name " string ) ( opt " avatar_url " string ) in conv to_tuple of_tuple with_tuple end type t = { results : User . t list ; limited : bool } [ @@ deriving accessor ] let encoding = let to_tuple t = t . results , t . limited in let of_tuple v = let results , limited = v in { results ; limited } in let with_tuple = obj2 ( req " results " ( list User . encoding ) ) ( req " limited " bool ) in conv to_tuple of_tuple with_tuple end end |
type links = | LINKS of ( ( int * int ) * int ) list | WHATEVER | SOME | TYPE of string * string |
type cc_port = { port_links : links ; port_states : string list option ; } |
type site = | Port of cc_port | Counter of int |
type cc_site = { site_name : string ; site_type : site } |
type cc_node = { node_type : string ; node_id : int option ; node_sites : cc_site array ; } |
type connected_component = cc_node option array array |
module LinkSetMap = SetMap . Make ( struct type t = ( int * int ) * int let print = Pp . pair ( Pp . pair Format . pp_print_int Format . pp_print_int ) Format . pp_print_int let compare ( x , y ) ( x ' , y ' ) = let c = Mods . int_pair_compare x x ' in if c = 0 then Mods . int_compare y y ' else c end ) |
let print_link ( dangling , free_id ) p f = function | WHATEVER -> Format . pp_print_string f " [ ] " # | SOME -> Format . pp_print_string f " [ _ ] " | TYPE ( si , ty ) -> Format . fprintf f " [ % s . % s ] " si ty | LINKS [ ] -> Format . pp_print_string f " [ . ] " | LINKS l -> let myself = ref ( LinkSetMap . Map . find_default LinkSetMap . Map . empty p ! dangling ) in let ( ) = Format . fprintf f " [ % a ] " ( Pp . list Pp . space ( fun f p ' -> let i = if p = p ' then - 1 else match Option_util . bind ( LinkSetMap . Map . find_option p ) ( LinkSetMap . Map . find_option p ' ! dangling ) with | None -> let ( ) = incr free_id in let ( ) = myself := LinkSetMap . Map . add p ' ! free_id ! myself in ! free_id | Some va -> va in Format . fprintf f " % i " i ) ) l in dangling := LinkSetMap . Map . add p ! myself ! dangling |
let print_port with_link node p id f = Format . fprintf f " % a % a " ( fun f -> function | None -> Format . pp_print_string f " { } " # | Some [ ] -> ( ) | Some l -> Format . fprintf f " { % a } " ( Pp . list Pp . space ( fun f i -> Format . fprintf f " % s " i ) ) l ) p . port_states ( match with_link with | Some pack -> print_link pack ( node , id ) | None -> ( fun _ _ -> ( ) ) ) p . port_links |
let print_intf with_link node = Pp . array Pp . space ( fun id f si -> let ( ) = Format . fprintf f " % s " si . site_name in ( match si . site_type with | Port p -> print_port with_link node p id f | Counter i -> Format . fprintf f " { =% i } " i ) ) |
let print_agent with_id link node f = function | None -> Format . pp_print_string f " . " | Some ag -> Format . fprintf f " % a % s ( [ @< h >% a ] ) " @ ( Pp . option ~ with_space : false ( fun f i -> Format . fprintf f " x % i " : i ) ) ( if with_id then ag . node_id else None ) ag . node_type ( print_intf link node ) ag . node_sites |
let print_cc f mix = let link = Some ( ref ( LinkSetMap . Map . empty ) , ref 0 ) in Pp . array ( fun f -> Format . fprintf f " \\@ " ) ( fun al -> Pp . array Pp . comma ( fun ar -> print_agent true link ( al , ar ) ) ) f mix |
let get_color = let store = Hashtbl . create 10 in fun i -> try Hashtbl . find store i with Not_found -> let v = Format . sprintf " #% x % x % x " ( Random . int 255 ) ( Random . int 255 ) ( Random . int 255 ) in let ( ) = Hashtbl . add store i v in v |
let print_dot_cc nb_cc f mix = Pp . array Pp . empty ( fun il -> Pp . array Pp . empty ( fun ir f -> function | None -> ( ) | Some ag -> Format . fprintf f " node % d_ % d_ % d [ label = " [ \@< h >% a ] " , @\ color = " \% s " , \ style = filled ] ; , " @ nb_cc il ir ( print_agent false None ( il , ir ) ) ( Some ag ) ( get_color ag . node_type ) ; Format . fprintf f " node % d_ % d_ % d -> counter % d [ style = invis ] ; , " @ nb_cc il ir nb_cc ) ) f mix ; ignore @@ Array . iteri ( fun al -> Array . iteri ( fun ar -> function | None -> ( ) | Some ag -> Array . iteri ( fun s si -> match si . site_type with | Counter _ -> ( ) | Port p -> match p . port_links with | WHATEVER -> assert false | SOME -> assert false | TYPE ( _si , _ty ) -> assert false | LINKS links -> Pp . list Pp . empty ( fun f ( ( al ' , ar ' ) , s ' ) -> if al < al ' || ( al = al ' && ( ar < ar ' || ( ar = ar ' && s < s ' ) ) ) then match mix . ( al ' ) . ( ar ' ) with | None -> assert false | Some ag ' -> Format . fprintf f " node % d_ % d_ % d -> node % d_ % d_ % d \ [ taillabel " =\% s " , \ headlabel " =\% s " , \ dir = none ] ; , " @ nb_cc al ar nb_cc al ' ar ' si . site_name ag ' . node_sites . ( s ' ) . site_name ) f links ) ag . node_sites ) ) mix |
let write_cc_port ob p = let ( ) = Bi_outbuf . add_char ob ' { ' in let ( ) = JsonUtil . write_field " port_links " ( fun ob -> function | WHATEVER -> Yojson . Basic . write_null ob ( ) | SOME -> Yojson . Basic . write_bool ob true | TYPE ( si , ty ) -> let ( ) = Bi_outbuf . add_string ob " { " \ site_name " " " \:\ in let ( ) = Bi_outbuf . add_string ob si in let ( ) = Bi_outbuf . add_string ob " " , " \\ agent_type " " " \:\ in let ( ) = Bi_outbuf . add_string ob ty in Bi_outbuf . add_string ob " " } " \ | LINKS l -> JsonUtil . write_list ( JsonUtil . write_compact_pair ( JsonUtil . write_compact_pair Yojson . Basic . write_int Yojson . Basic . write_int ) Yojson . Basic . write_int ) ob l ) ob p . port_links in let ( ) = JsonUtil . write_comma ob in let ( ) = JsonUtil . write_field " port_states " ( JsonUtil . write_option ( JsonUtil . write_list Yojson . Basic . write_string ) ) ob p . port_states in Bi_outbuf . add_char ob ' } ' |
let write_site ob f = let ( ) = Bi_outbuf . add_char ob ' [ ' in let ( ) = match f . site_type with | Counter i -> let ( ) = Yojson . Basic . write_string ob " counter " in let ( ) = Bi_outbuf . add_char ob ' , ' in Yojson . Basic . write_int ob i | Port p -> let ( ) = Yojson . Basic . write_string ob " port " in let ( ) = Bi_outbuf . add_char ob ' , ' in write_cc_port ob p in Bi_outbuf . add_char ob ' ] ' |
let write_cc_site ob f = let ( ) = Bi_outbuf . add_char ob ' { ' in let ( ) = JsonUtil . write_field " site_name " Yojson . Basic . write_string ob f . site_name in let ( ) = JsonUtil . write_comma ob in let ( ) = JsonUtil . write_field " site_type " write_site ob f in Bi_outbuf . add_char ob ' } ' |
let links_of_yojson = function | ` Null -> WHATEVER | ` Bool b -> let ( ) = assert b in SOME | ` Assoc [ " site_name " , ` String si ; " agent_type " , ` String ty ] | ` Assoc [ " agent_type " , ` String ty ; " site_name " , ` String si ] -> TYPE ( si , ty ) | ` List _ as x -> let error_msg = None in LINKS ( JsonUtil . to_list ( JsonUtil . compact_to_pair ( JsonUtil . compact_to_pair ( JsonUtil . to_int ? error_msg ) ( JsonUtil . to_int ? error_msg ) ) ( JsonUtil . to_int ? error_msg ) ) x ) | x -> raise ( Yojson . Basic . Util . Type_error ( " Incorrect cc_port " , x ) ) |
let read_cc_port p lb = let ( port_links , port_states ) = Yojson . Basic . read_fields ( fun ( s , i ) key p lb -> if key = " port_links " then ( links_of_yojson ( Yojson . Basic . read_json p lb ) , i ) else let ( ) = assert ( key = " port_states " ) in ( s , JsonUtil . read_option ( Yojson . Basic . read_list Yojson . Basic . read_string ) p lb ) ) ( LINKS [ ] , Some [ ] ) p lb in { port_links ; port_states } |
let read_site p lb = let ( ) = Yojson . Basic . read_lbr p lb in let key = JsonUtil . read_between_spaces Yojson . Basic . read_string p lb in let ( ) = Yojson . Basic . read_comma p lb in let out = JsonUtil . read_between_spaces ( fun p lb -> if key = " counter " then Counter ( Yojson . Basic . read_int p lb ) else let ( ) = assert ( key = " port " ) in Port ( read_cc_port p lb ) ) p lb in let ( ) = Yojson . Basic . read_rbr p lb in out |
let read_cc_site p lb = let ( site_name , site_type ) = Yojson . Basic . read_fields ( fun ( n , s ) key p lb -> if key = " site_name " then ( Yojson . Basic . read_string p lb , s ) else let ( ) = assert ( key = " site_type " ) in ( n , read_site p lb ) ) ( " " , Counter ( - 1 ) ) p lb in { site_name ; site_type } |
let write_cc_node ob x = JsonUtil . write_option ( fun ob f -> let ( ) = Bi_outbuf . add_char ob ' { ' in let ( ) = JsonUtil . write_field " node_type " Yojson . Basic . write_string ob f . node_type in let ( ) = JsonUtil . write_comma ob in let ( ) = match f . node_id with | None -> ( ) | Some node_id -> let ( ) = JsonUtil . write_field " node_id " Yojson . Basic . write_int ob node_id in JsonUtil . write_comma ob in let ( ) = JsonUtil . write_field " node_sites " ( JsonUtil . write_array write_cc_site ) ob f . node_sites in Bi_outbuf . add_char ob ' } ' ) ob x |
let read_cc_node p lb = JsonUtil . read_option ( fun p lb -> let ( node_id , node_type , node_sites ) = Yojson . Basic . read_fields ( fun ( id , n , s ) key p lb -> if key = " node_id " then ( Some ( Yojson . Basic . read_int p lb ) , n , s ) else if key = " node_type " then ( id , Yojson . Basic . read_string p lb , s ) else let ( ) = assert ( key = " node_sites " ) in ( id , n , Yojson . Basic . read_array read_cc_site p lb ) ) ( None , " " , [ ] ) || p lb in { node_id ; node_type ; node_sites } ) p lb |
let write_connected_component ob f = JsonUtil . write_array ( JsonUtil . write_array write_cc_node ) ob f |
let read_connected_component ob f = Yojson . Basic . read_array ( Yojson . Basic . read_array read_cc_node ) ob f |
let string_of_connected_component ( ? len = 1024 ) x = let ob = Bi_outbuf . create len in let ( ) = write_connected_component ob x in Bi_outbuf . contents ob |
let connected_component_of_string s = read_connected_component ( Yojson . Safe . init_lexer ( ) ) ( Lexing . from_string s ) |
module Style = struct type t = | Loc | Error | Warning | Kwd | Id | Prompt | Details | Ok | Debug | Success | Ansi_styles of Ansi_color . Style . t list end |
module Print_config = struct type t = Style . t -> Ansi_color . Style . t list open Ansi_color . Style let default : Style . t -> _ = function | Loc -> [ bold ] | Error -> [ bold ; fg_red ] | Warning -> [ bold ; fg_magenta ] | Kwd -> [ bold ; fg_blue ] | Id -> [ bold ; fg_yellow ] | Prompt -> [ bold ; fg_green ] | Details -> [ dim ; fg_white ] | Ok -> [ dim ; fg_green ] | Debug -> [ underlined ; fg_bright_cyan ] | Success -> [ bold ; fg_green ] | Ansi_styles l -> l end |
type t = { loc : Loc0 . t option ; paragraphs : Style . t Pp . t list ; hints : Style . t Pp . t list } |
let make ? loc ? prefix ( ? hints = [ ] ) paragraphs = let paragraphs = match ( prefix , paragraphs ) with | None , l -> l | Some p , [ ] -> [ p ] | Some p , x :: l -> Pp . concat ~ sep : Pp . space [ p ; x ] :: l in { loc ; hints ; paragraphs } |
let pp { loc ; paragraphs ; hints } = let open Pp . O in let paragraphs = match hints with | [ ] -> paragraphs | _ -> List . append paragraphs ( List . map hints ~ f ( : fun hint -> Pp . verbatim " Hint " : ++ Pp . space ++ hint ) ) in let paragraphs = List . map paragraphs ~ f : Pp . box in let paragraphs = match loc with | None -> paragraphs | Some { Loc0 . start ; stop } -> let start_c = start . pos_cnum - start . pos_bol in let stop_c = stop . pos_cnum - start . pos_bol in Pp . tag Style . Loc ( Pp . textf " File % S , line % d , characters % d -% d " : start . pos_fname start . pos_lnum start_c stop_c ) :: paragraphs in Pp . vbox ( Pp . concat_map paragraphs ~ sep : Pp . nop ~ f ( : fun pp -> Pp . seq pp Pp . cut ) ) |
let print ( ? config = Print_config . default ) t = Ansi_color . print ( Pp . map_tags ( pp t ) ~ f : config ) |
let prerr ( ? config = Print_config . default ) t = Ansi_color . prerr ( Pp . map_tags ( pp t ) ~ f : config ) |
let levenshtein_distance s t = let m = String . length s and n = String . length t in let d = Array . make_matrix ~ dimx ( : m + 1 ) ~ dimy ( : n + 1 ) 0 in for i = 0 to m do d . ( i ) . ( 0 ) <- i done ; for j = 0 to n do d . ( 0 ) . ( j ) <- j done ; for j = 1 to n do for i = 1 to m do if s . [ i - 1 ] = t . [ j - 1 ] then d . ( i ) . ( j ) <- d . ( i - 1 ) . ( j - 1 ) else d . ( i ) . ( j ) <- min ( d . ( i - 1 ) . ( j ) + 1 ) ( min ( d . ( i ) . ( j - 1 ) + 1 ) ( d . ( i - 1 ) . ( j - 1 ) + 1 ) ) done done ; d . ( m ) . ( n ) |
let did_you_mean s ~ candidates = let candidates = List . filter candidates ~ f ( : fun candidate -> levenshtein_distance s candidate < 3 ) in match candidates with | [ ] -> [ ] | l -> [ Pp . textf " did you mean % s " ? ( String . enumerate_or l ) ] |
let to_string t = Format . asprintf " % a " Pp . render_ignore_tags ( pp { t with loc = None } ) |> String . drop_prefix ~ prefix " : Error : " |> Option . value_exn |> String . trim |
let json = ` O [ ( " name " , ` String " OCaml ) " ; ( " qualities " , ` A [ ` O [ ( " name " , ` String " awesome ) " ] ; ` O [ ( " name " , ` String " simple ) " ] ; ` O [ ( " name " , ` String " fun ) " ] ] ) ] |
let section heading = print_newline ( ) ; print_string " # " ; print_endline heading |
let subsection heading = print_newline ( ) ; print_string " ## " ; print_endline heading |
let test tmpl = subsection " parsed " ; print_endline " " ; --- print_endline ( Mustache . to_string tmpl ) tmpl ; print_endline " " ; --- subsection " rendered " ; print_endline " " ; --- print_endline ( Mustache . render tmpl json ) json ; print_endline " " ; --- ( ) |
let ( ) = section " Parsed template " |
let parsed_template = Mustache . of_string " Hello { { name } name } name \ nMustache is :\ n { n { n # qualities } qualities } qualities \ n * { { name } name } name \ n { n { n / qualities } qualities } qualities \ n " |
let ( ) = test parsed_template |
let ( ) = section " Programmed template " |
let programmed_template = let open Mustache in concat [ raw " Hello " ; escaped " name " ; raw " \ n " ; raw " Mustache is " : ; raw " \ n " ; section " qualities " @@ concat [ raw " * " ; escaped " name " ; raw " \ n " ] ] |
let ( ) = test programmed_template |
let ( ) = section " Output comparison " |
let ( ) = let parsed_output = Mustache . render parsed_template json in let programmed_output = Mustache . render programmed_template json in if String . equal parsed_output programmed_output then print_endline " Outputs match as expected . " else print_endline " Outputs DO NOT match , this is suspcious . " |
module type Sig = sig val register_migration : unit -> unit val register_cleaner : unit -> unit val lifecycles : Sihl . Container . lifecycle list val search : ? ctx ( : string * string ) list -> [ ` Desc | ` Asc ] -> string option -> limit : int -> offset : int -> ( Model . t list * int ) Lwt . t val get : ? ctx ( : string * string ) list -> string -> Model . t option Lwt . t val get_by_email : ? ctx ( : string * string ) list -> string -> Model . t option Lwt . t val insert : ? ctx ( : string * string ) list -> Model . t -> unit Lwt . t val update : ? ctx ( : string * string ) list -> Model . t -> unit Lwt . t end |
let status = let encode m = m |> Model . status_to_string |> Result . ok in let decode = Model . status_of_string in Caqti_type . ( custom ~ encode ~ decode string ) ; ; |
let user = let open Sihl . Contract . User in let encode m = Ok ( m . id , ( m . email , ( m . username , ( m . name , ( m . given_name , ( m . password , ( m . status , ( m . admin , ( m . confirmed , ( m . created_at , m . updated_at ) ) ) ) ) ) ) ) ) ) in let decode ( id , ( email , ( username , ( name , ( given_name , ( password , ( status , ( admin , ( confirmed , ( created_at , updated_at ) ) ) ) ) ) ) ) ) ) = Ok { id ; email ; username ; name ; given_name ; password ; status ; admin ; confirmed ; created_at ; updated_at } in Caqti_type . ( custom ~ encode ~ decode ( tup2 string ( tup2 string ( tup2 ( option string ) ( tup2 ( option string ) ( tup2 ( option string ) ( tup2 string ( tup2 status ( tup2 bool ( tup2 bool ( tup2 ptime ptime ) ) ) ) ) ) ) ) ) ) ) ; ; |
module MakeMariaDb ( MigrationService : Sihl . Contract . Migration . Sig ) : Sig = struct let lifecycles = [ Database . lifecycle ; MigrationService . lifecycle ] module Migration = struct let fix_collation = Migration . create_step ~ label " : fix collation " { sql | SET collation_server = ' utf8mb4_unicode_ci ' | sql } ; ; let create_users_table = Migration . create_step ~ label " : create users table " { sql | CREATE TABLE IF NOT EXISTS user_users ( id BIGINT UNSIGNED AUTO_INCREMENT , uuid BINARY ( 16 ) NOT NULL , email VARCHAR ( 128 ) NOT NULL , password VARCHAR ( 128 ) NOT NULL , username VARCHAR ( 128 ) , status VARCHAR ( 128 ) NOT NULL , admin BOOLEAN NOT NULL DEFAULT false , confirmed BOOLEAN NOT NULL DEFAULT false , created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP , PRIMARY KEY ( id ) , CONSTRAINT unique_uuid UNIQUE KEY ( uuid ) , CONSTRAINT unique_email UNIQUE KEY ( email ) ) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci | sql } ; ; let add_updated_at_column = Sihl . Database . Migration . create_step ~ label " : add updated_at column " { sql | ALTER TABLE user_users ADD COLUMN updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP | sql } ; ; let add_name_columns = Sihl . Database . Migration . create_step ~ label " : add name columns " { sql | ALTER TABLE user_users ADD COLUMN name VARCHAR ( 128 ) NULL , ADD COLUMN given_name VARCHAR ( 128 ) NULL | sql } ; ; let migration ( ) = Migration . ( empty " user " |> add_step fix_collation |> add_step create_users_table |> add_step add_updated_at_column |> add_step add_name_columns ) ; ; end let filter_fragment = { sql | WHERE user_users . email LIKE $ 1 OR user_users . username LIKE $ 1 OR user_users . name LIKE $ 1 OR user_users . given_name LIKE $ 1 OR user_users . status LIKE $ 1 | sql } ; ; let search_query = { sql | SELECT COUNT OVER ( ) as total , LOWER ( CONCAT ( SUBSTR ( HEX ( uuid ) , 1 , 8 ) , ' ' , - SUBSTR ( HEX ( uuid ) , 9 , 4 ) , ' ' , - SUBSTR ( HEX ( uuid ) , 13 , 4 ) , ' ' , - SUBSTR ( HEX ( uuid ) , 17 , 4 ) , ' ' , - SUBSTR ( HEX ( uuid ) , 21 ) ) ) , email , username , name , given_name , password , status , admin , confirmed , created_at , updated_at FROM user_users | sql } ; ; let request = Sihl . Database . prepare_search_request ~ search_query ~ filter_fragment ~ sort_by_field " : id " user ; ; let search ? ctx sort filter ~ limit ~ offset = Sihl . Database . run_search_request ? ctx request sort filter ~ limit ~ offset ; ; let get_request = let open Caqti_request . Infix in { sql | SELECT LOWER ( CONCAT ( SUBSTR ( HEX ( uuid ) , 1 , 8 ) , ' ' , - SUBSTR ( HEX ( uuid ) , 9 , 4 ) , ' ' , - SUBSTR ( HEX ( uuid ) , 13 , 4 ) , ' ' , - SUBSTR ( HEX ( uuid ) , 17 , 4 ) , ' ' , - SUBSTR ( HEX ( uuid ) , 21 ) ) ) , email , username , name , given_name , password , status , admin , confirmed , created_at , updated_at FROM user_users WHERE user_users . uuid = UNHEX ( REPLACE ( , ? ' ' , - ' ' ) ) | sql } |> Caqti_type . string ->? user ; ; let get ? ctx id = Database . find_opt ? ctx get_request id let get_by_email_request = let open Caqti_request . Infix in { sql | SELECT LOWER ( CONCAT ( SUBSTR ( HEX ( uuid ) , 1 , 8 ) , ' ' , - SUBSTR ( HEX ( uuid ) , 9 , 4 ) , ' ' , - SUBSTR ( HEX ( uuid ) , 13 , 4 ) , ' ' , - SUBSTR ( HEX ( uuid ) , 17 , 4 ) , ' ' , - SUBSTR ( HEX ( uuid ) , 21 ) ) ) , email , username , name , given_name , password , status , admin , confirmed , created_at , updated_at FROM user_users WHERE user_users . email LIKE ? | sql } |> Caqti_type . string ->? user ; ; let get_by_email ? ctx email = Database . find_opt ? ctx get_by_email_request email ; ; let insert_request = let open Caqti_request . Infix in { sql | INSERT INTO user_users ( uuid , email , username , name , given_name , password , status , admin , confirmed , created_at , updated_at ) VALUES ( UNHEX ( REPLACE ( $ 1 , ' ' , - ' ' ) ) , LOWER ( $ 2 ) , $ 3 , $ 4 , $ 5 , $ 6 , $ 7 , $ 8 , $ 9 , $ 10 , $ 11 ) | sql } |> user . -> Caqti_type . unit ; ; let insert ? ctx user = Database . exec ? ctx insert_request user let update_request = let open Caqti_request . Infix in { sql | UPDATE user_users SET email = LOWER ( $ 2 ) , username = $ 3 , name = $ 4 , given_name = $ 5 , password = $ 6 , status = $ 7 , admin = $ 8 , confirmed = $ 9 , created_at = $ 10 , updated_at = $ 11 WHERE user_users . uuid = UNHEX ( REPLACE ( $ 1 , ' ' , - ' ' ) ) | sql } |> user . -> Caqti_type . unit ; ; let update ? ctx user = Database . exec ? ctx update_request user let clean_request = let open Caqti_request . Infix in " TRUNCATE user_users " |> Caqti_type . ( unit . -> unit ) ; ; let clean ? ctx ( ) = Database . exec ? ctx clean_request ( ) let register_migration ( ) = MigrationService . register_migration ( Migration . migration ( ) ) ; ; let register_cleaner ( ) = Cleaner . register_cleaner clean end |
module MakePostgreSql ( MigrationService : Sihl . Contract . Migration . Sig ) : Sig = struct let lifecycles = [ Database . lifecycle ; MigrationService . lifecycle ] module Migration = struct let create_users_table = Migration . create_step ~ label " : create users table " { sql | CREATE TABLE IF NOT EXISTS user_users ( id serial , uuid uuid NOT NULL , email VARCHAR ( 128 ) NOT NULL , password VARCHAR ( 128 ) NOT NULL , username VARCHAR ( 128 ) , status VARCHAR ( 128 ) NOT NULL , admin BOOLEAN NOT NULL DEFAULT false , confirmed BOOLEAN NOT NULL DEFAULT false , created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP , PRIMARY KEY ( id ) , UNIQUE ( uuid ) , UNIQUE ( email ) ) | sql } ; ; let add_updated_at_column = Sihl . Database . Migration . create_step ~ label " : add updated_at column " { sql | ALTER TABLE user_users ADD COLUMN updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW ( ) | sql } ; ; let remove_timezone = Sihl . Database . Migration . create_step ~ label " : remove timezone info from timestamps " { sql | ALTER TABLE user_users ALTER COLUMN created_at TYPE TIMESTAMP , ALTER COLUMN updated_at TYPE TIMESTAMP | sql } ; ; let add_name_columns = Sihl . Database . Migration . create_step ~ label " : add name columns " { sql | ALTER TABLE user_users ADD COLUMN name VARCHAR ( 128 ) NULL , ADD COLUMN given_name VARCHAR ( 128 ) NULL | sql } ; ; let migration ( ) = Migration . ( empty " user " |> add_step create_users_table |> add_step add_updated_at_column |> add_step remove_timezone |> add_step add_name_columns ) ; ; end let filter_fragment = { sql | WHERE user_users . email LIKE $ 1 OR user_users . username LIKE $ 1 OR user_users . name LIKE $ 1 OR user_users . given_name LIKE $ 1 OR user_users . status LIKE $ 1 | sql } ; ; let search_query = { sql | SELECT COUNT OVER ( ) as total , uuid , email , username , name , given_name , password , status , admin , confirmed , created_at , updated_at FROM user_users | sql } ; ; let request = Sihl . Database . prepare_search_request ~ search_query ~ filter_fragment ~ sort_by_field " : id " user ; ; let search ? ctx sort filter ~ limit ~ offset = Sihl . Database . run_search_request ? ctx request sort filter ~ limit ~ offset ; ; let get_request = let open Caqti_request . Infix in { sql | SELECT uuid as id , email , username , name , given_name , password , status , admin , confirmed , created_at , updated_at FROM user_users WHERE user_users . uuid = $ 1 :: uuid | sql } |> Caqti_type . string ->? user ; ; let get ? ctx id = Database . find_opt ? ctx get_request id let get_by_email_request = let open Caqti_request . Infix in { sql | SELECT uuid as id , email , username , name , given_name , password , status , admin , confirmed , created_at , updated_at FROM user_users WHERE LOWER ( user_users . email ) = LOWER ( ) ? | sql } |> Caqti_type . string ->? user ; ; let get_by_email ? ctx email = Database . find_opt ? ctx get_by_email_request email ; ; let insert_request = let open Caqti_request . Infix in { sql | INSERT INTO user_users ( uuid , email , username , name , given_name , password , status , admin , confirmed , created_at , updated_at ) VALUES ( $ 1 :: uuid , LOWER ( $ 2 ) , $ 3 , $ 4 , $ 5 , $ 6 , $ 7 , $ 8 , $ 9 , $ 10 AT TIME ZONE ' UTC ' , $ 11 AT TIME ZONE ' UTC ' ) | sql } |> user . -> Caqti_type . unit ; ; let insert ? ctx user = Database . exec ? ctx insert_request user let update_request = let open Caqti_request . Infix in { sql | UPDATE user_users SET email = LOWER ( $ 2 ) , username = $ 3 , name = $ 4 , given_name = $ 5 , password = $ 6 , status = $ 7 , admin = $ 8 , confirmed = $ 9 , created_at = $ 10 , updated_at = $ 11 WHERE user_users . uuid = $ 1 :: uuid | sql } |> user . -> Caqti_type . unit ; ; let update ? ctx user = Database . exec ? ctx update_request user let clean_request = let open Caqti_request . Infix in " TRUNCATE TABLE user_users CASCADE " |> Caqti_type . ( unit . -> unit ) ; ; let clean ? ctx ( ) = Database . exec ? ctx clean_request ( ) let register_migration ( ) = MigrationService . register_migration ( Migration . migration ( ) ) ; ; let register_cleaner ( ) = Cleaner . register_cleaner clean end |
let test_current_user = let current_user = Lwt_main . run begin Github . ( Monad . ( run ( User . current_info ~ token ( ) >|= Response . value ) ) ) end in assert ( current_user . Github_t . user_info_ty = ` User ) ; Format . printf " Check current user : OK . . " @ |
let test_current_user_first_org = let org = Lwt_main . run begin Github . ( Monad . ( run begin let orgs = Organization . current_user_orgs ~ token ( ) in Stream . next orgs >>= function | None -> Printf . eprintf " No organizations for the current user . \ n " ; exit 1 | Some ( first_org , _ ) -> return first_org end ) ) end in assert ( org . Github_t . org_ty = ` Org ) ; Format . printf " Check current user first org : OK . . " @ |
let test_ocaml_organization = let ocaml_user = Lwt_main . run begin Github . ( Monad . ( run begin User . info ~ token ~ user " : ocaml " ( ) >|= Response . value end ) ) end in assert ( ocaml_user . Github_t . user_info_ty = ` Org ) ; Format . printf " Check OCaml org : OK . . " @ |
let test_ocaml_repository = let opam_repository = Lwt_main . run begin Github . ( Monad . ( run begin Repo . info ~ token ~ user " : ocaml " ~ repo " : opam " ( ) >|= Response . value end ) ) end in assert Github_t . ( opam_repository . repository_owner . user_ty = ` Org ) ; Format . printf " Check OCaml repo : OK . . " @ |
let mem u s = ISet . mem ( UChar . uint_code u ) u s |
let add u s = ISet . add ( UChar . uint_code u ) u s |
let add_range u1 u2 s = ISet . add_range ( UChar . uint_code u1 ) u1 ( UChar . uint_code u2 ) u2 s |
let singleton u = ISet . singleton ( UChar . uint_code u ) u |
let remove u s = ISet . remove ( UChar . uint_code u ) u s |
let remove_range u1 u2 s = ISet . remove_range ( UChar . uint_code u1 ) u1 ( UChar . uint_code u2 ) u2 s |
let from u s = ISet . from ( UChar . uint_code u ) u s |
let after u s = ISet . after ( UChar . uint_code u ) u s |
let until u s = ISet . until ( UChar . uint_code u ) u s |
let before u s = ISet . before ( UChar . uint_code u ) u s |
let iter f s = ISet . iter ( fun n -> f ( UChar . chr_of_uint n ) n ) n s |
let iter_range f s = let f ' n1 n2 = f ( UChar . chr_of_uint n1 ) n1 ( UChar . chr_of_uint n2 ) n2 in ISet . iter_range f ' s |
let fold f s a = let f ' n a = f ( UChar . chr_of_uint n ) n a in ISet . fold f ' s a |
let fold_range f s a = let f ' n1 n2 a = f ( UChar . chr_of_uint n1 ) n1 ( UChar . chr_of_uint n2 ) n2 a in ISet . fold_range f ' s a |
let for_all p s = let p ' n = p ( UChar . chr_of_uint n ) n in ISet . for_all p ' s |
let exists p s = let p ' n = p ( UChar . chr_of_uint n ) n in ISet . exists p ' s |
let filter p s = let p ' n = p ( UChar . chr_of_uint n ) n in ISet . filter p ' s |
let partition p s = let p ' n = p ( UChar . chr_of_uint n ) n in ISet . partition p ' s |
let elements s = List . map UChar . chr_of_uint ( ISet . elements s ) s |
let ranges s = let f ( n1 , n2 ) n2 = ( UChar . chr_of_uint n1 , UChar . chr_of_uint n2 ) n2 in List . map f ( ISet . ranges s ) s |
let min_elt s = UChar . chr_of_uint ( ISet . min_elt s ) s |
let max_elt s = UChar . chr_of_uint ( ISet . max_elt s ) s |
let choose s = UChar . chr_of_uint ( ISet . choose s ) s |
let uset_of_iset s = s |
let iset_of_uset s = s |
let get_files_in_directory dir = let len = String . length dir in let dir = if len > 0 && Sys . os_type = " Win32 " && ( dir . [ len - 1 ] = ' ' / || dir . [ len - 1 ] = ' ' ) \\ then String . sub dir ~ pos : 0 ~ len ( : len - 1 ) else dir in match try Some ( opendir dir ) with Unix_error _ -> None with None -> [ ] | Some dirh -> let rec get_them l = match try Some ( readdir dirh ) with _ -> None with | Some x -> get_them ( x :: l ) | None -> closedir dirh ; l in List . sort ~ cmp : compare ( get_them [ ] ) |
let is_directory name = try ( stat name ) . st_kind = S_DIR with _ -> false |
let concat dir name = let len = String . length dir in if len = 0 then name else if dir . [ len - 1 ] = ' ' / then dir ^ name else dir ^ " " / ^ name |
let get_directories_in_files ~ path = List . filter ~ f ( : fun x -> is_directory ( concat path x ) ) |
let subshell ~ cmd = let rc = open_process_in cmd in let rec it l = match try Some ( input_line rc ) with _ -> None with Some x -> it ( x :: l ) | None -> List . rev l in let answer = it [ ] in ignore ( close_process_in rc ) ; answer |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.