Skip to content

Commit

Permalink
Merge pull request #6 from lenscas/feature/better_tl_generation
Browse files Browse the repository at this point in the history
make generated teal nicer to read
  • Loading branch information
lenscas authored Feb 5, 2022
2 parents 7c1d0f6 + ec5e50e commit 6b3e9df
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
2 changes: 1 addition & 1 deletion pgteal_cli/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn get_app() -> Params {
let matches = App::new("PG Teal")
.version("0.0")
.author("Lenscas <[email protected]>")
.about("Generates teal types and functions based on sql.")
.about("Generates teal types and functions based on sql files.")
.arg(
Arg::with_name("connection_string")
.short("c")
Expand Down
37 changes: 20 additions & 17 deletions pgteal_cli/src/tl_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,25 @@ fn make_function(
let params: String = query
.params
.iter()
.map(|v| " \"".to_owned() + v + "\"")
.map(|v| " \"".to_owned() + v + "\"")
.collect::<Vec<_>>()
.join(",\n");
let params = format!("local param_order:{{string}} = {{\n{}\n}}", params);
let params = format!(
"local param_order:{{string}} = {{\n{}\n }}",
params
);
format!(
" {}
{}
local query_params = {{}}
for k,v in ipairs(param_order) do
query_params[k] = (params as {{string:{}}})[v]
end
return connection:{}(
[[{}]],
query_params
) as {}
end",
"{}
{}
local query_params = {{}}
for k,v in ipairs(param_order) do
query_params[k] = (params as {{string:{}}})[v]
end
return connection:{}(
[[{}\n]],
query_params
) as {}
end",
function_header,
params,
shared::Input::get_type_name(tealr::Direction::ToLua),
Expand Down Expand Up @@ -221,19 +224,19 @@ pub(crate) fn write_to_file(
part.output_type.name,
part.output_type.name
);
let functions = part.functions.join(",\n ");
reexported + &functions + "}"
let functions = part.functions.join(",\n");
reexported + &functions + "\n }"
})
.collect::<Vec<_>>()
.join(",");
.join(",\n");

let path = get_path(teal_pattern, original_file_path);
let path = Path::new(&path);

std::fs::create_dir_all(path.parent().unwrap())?;

let to_write = format!(
"local libpgteal = require(\"libpgteal\")\nlocal Connection = libpgteal.Connection\n{}\nreturn {{{}}}",
"local libpgteal = require(\"libpgteal\")\nlocal Connection = libpgteal.Connection\n{}\nreturn {{\n{}\n}}",
glued_types, modules
);
std::fs::write(path, to_write)?;
Expand Down

0 comments on commit 6b3e9df

Please sign in to comment.