tcdi / plrust

A Rust procedural language handler for PostgreSQL
PostgreSQL License
1.12k stars 32 forks source link

Improve on using features of a dependency in plrust function #325

Closed anth0nyleung closed 1 year ago

anth0nyleung commented 1 year ago

PL/Rust does not handle using features for a dependencies quite well currently.

For instance, I have made serde an allowed dependency with the feature derive

$ > cat plrust_allowed_dependencies.toml
serde = { version = "1.0", features = ["derive"] }

But when i try to define a function with serde as dependency and enable the derive marco, it errors out because it expect the value of a dependency to be a string not a toml table

postgres=# CREATE FUNCTION foo (r INT, g INT, b INT) RETURNS INT AS
$$
CREATE FUNCTION table_deps (r INT, g INT, b INT) RETURNS TEXT STRICT AS
$$
                  [dependencies]
                  serde = { version = "1.0", features = ["derive"] }
                  rand = "*"

                  [code]
                  use serde::{Serialize, Deserialize};

                  #[derive(Debug, Serialize, Deserialize)]
                  struct Color {
                       r: u8,
                       g: u8,
                       b: u8,
                  }

                  let r = r as u8;
                  let g = g as u8;
                  let b = b as u8;

                  Ok(Some(format!("{:?}", Color{ r, g, b})))
                  $$ language plrust;        
$$ language plrust;

ERROR:
   0: dependency serde with values Table({"features": Array([String("derive")]), "version": String("1.0")}) is malformatted. Only strings are supported

Location:
   plrust/src/user_crate/mod.rs:355

Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.
postgres=#
eeeebbbbrrrr commented 1 year ago

got a fix for this. Simple oversight from #103.

Also note, I updated your example above so it compiles and executes with what I assume your intent was. I didn't change it to specifically exercise serialization, other than keeping the #[derive(Serialize, Deserialize)], which definitely engages the serde compile-time machinery!

eeeebbbbrrrr commented 1 year ago

got a fix for this. Simple oversight from https://github.com/tcdi/plrust/pull/103.

correction: it’s much more complex than I first thought. I think @thomcc and I have a path forward tho.

eeeebbbbrrrr commented 1 year ago

Resolved in v1.2.0.