Allow substituting fresh IDs in rewrite (#168)

This commit is contained in:
Rijnard van Tonder 2020-01-31 19:22:48 -07:00 committed by GitHub
parent 37de5e0c4e
commit d5d8a5cce4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 0 deletions

View File

@ -6,6 +6,15 @@ let debug =
Sys.getenv "DEBUG_COMBY"
|> Option.is_some
let substitute_fresh template =
let pattern = ":[id()]" in
let template_ref = ref template in
while Option.is_some (String.substr_index !template_ref ~pattern) do
let uuid = Uuid_unix.(Fn.compose Uuid.to_string create ()) in
let id = String.suffix uuid 12 in
template_ref := String.substr_replace_first !template_ref ~pattern ~with_:id
done;
!template_ref
let substitute template env =
let substitution_formats =
@ -23,6 +32,7 @@ let substitute template env =
; ":[?", "]"
]
in
let template = substitute_fresh template in
Environment.vars env
|> List.fold ~init:(template, []) ~f:(fun (acc, vars) variable ->
match Environment.lookup env variable with

View File

@ -1,5 +1,9 @@
open Match
(** substitute the pattern :[id()] with a fresh hex string based on the last
48-bit part of a UUID v3 identifier *)
val substitute_fresh : string -> string
(** substitute returns the result and variables substituted for *)
val substitute : string -> Environment.t -> (string * string list)