Skip to content

feat(spin_sdk::pg): Extract type conversions into sdk #813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ default = []
e2e-tests = []
outbound-redis-tests = []
config-provider-tests = []
outbound-pg-tests = []

[workspace]
members = [
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ test-outbound-redis:
test-config-provider:
RUST_LOG=$(LOG_LEVEL) cargo test --test integration --features config-provider-tests --no-fail-fast -- integration_tests::config_provider_tests --nocapture

.PHONY: test-outbound-pg
test-outbound-pg:
RUST_LOG=$(LOG_LEVEL) cargo test --test integration --features outbound-pg-tests --no-fail-fast -- --nocapture

.PHONY: test-sdk-go
test-sdk-go:
$(MAKE) -C sdk/go test
Expand Down
2 changes: 2 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const RUST_HTTP_INTEGRATION_TEST: &str = "tests/http/simple-spin-rust";
const RUST_HTTP_INTEGRATION_ENV_TEST: &str = "tests/http/headers-env-routes-test";
const RUST_HTTP_VAULT_CONFIG_TEST: &str = "tests/http/vault-config-test";
const RUST_OUTBOUND_REDIS_INTEGRATION_TEST: &str = "tests/outbound-redis/http-rust-outbound-redis";
const RUST_OUTBOUND_PG_INTEGRATION_TEST: &str = "tests/outbound-pg/http-rust-outbound-pg";

fn main() {
let mut config = vergen::Config::default();
Expand Down Expand Up @@ -72,6 +73,7 @@ error: the `wasm32-wasi` target is not installed
cargo_build(RUST_HTTP_INTEGRATION_ENV_TEST);
cargo_build(RUST_HTTP_VAULT_CONFIG_TEST);
cargo_build(RUST_OUTBOUND_REDIS_INTEGRATION_TEST);
cargo_build(RUST_OUTBOUND_PG_INTEGRATION_TEST);
}

fn build_wasm_test_program(name: &'static str, root: &'static str) {
Expand Down
16 changes: 4 additions & 12 deletions crates/outbound-pg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ fn convert_data_type(pg_type: &Type) -> DbDataType {
Type::INT2 => DbDataType::Int16,
Type::INT4 => DbDataType::Int32,
Type::INT8 => DbDataType::Int64,
Type::TEXT => DbDataType::Str,
Type::VARCHAR => DbDataType::Str,
Type::TEXT | Type::VARCHAR | Type::BPCHAR => DbDataType::Str,
_ => {
tracing::debug!("Couldn't convert Postgres type {} to WIT", pg_type.name(),);
DbDataType::Other
Expand Down Expand Up @@ -208,17 +207,10 @@ fn convert_entry(row: &Row, index: usize) -> Result<DbValue, tokio_postgres::Err
None => DbValue::DbNull,
}
}
&Type::TEXT => {
let value: Option<&str> = row.try_get(index)?;
&Type::TEXT | &Type::VARCHAR | &Type::BPCHAR => {
let value: Option<String> = row.try_get(index)?;
match value {
Some(v) => DbValue::Str(v.to_owned()),
None => DbValue::DbNull,
}
}
&Type::VARCHAR => {
let value: Option<&str> = row.try_get(index)?;
match value {
Some(v) => DbValue::Str(v.to_owned()),
Some(v) => DbValue::Str(v),
None => DbValue::DbNull,
}
}
Expand Down
21 changes: 21 additions & 0 deletions examples/config-rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions examples/http-rust-outbound-http/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions examples/http-rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions examples/redis-rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions examples/rust-outbound-pg/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion examples/rust-outbound-pg/db/testdata.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ CREATE TABLE articletest (
id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
title varchar(40) NOT NULL,
content text NOT NULL,
authorname varchar(40) NOT NULL
authorname varchar(40) NOT NULL,
coauthor text
);

INSERT INTO articletest (title, content, authorname) VALUES
Expand Down
85 changes: 33 additions & 52 deletions examples/rust-outbound-pg/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#![allow(dead_code)]
use anyhow::{anyhow, Result};
use anyhow::Result;
use spin_sdk::{
http::{Request, Response},
http_component, pg,
http_component,
pg::{self, Decode},
};

// The environment variable set in `spin.toml` that points to the
Expand All @@ -15,6 +16,27 @@ struct Article {
title: String,
content: String,
authorname: String,
coauthor: Option<String>,
}

impl TryFrom<&pg::Row> for Article {
type Error = anyhow::Error;

fn try_from(row: &pg::Row) -> Result<Self, Self::Error> {
let id = i32::decode(&row[0])?;
let title = String::decode(&row[1])?;
let content = String::decode(&row[2])?;
let authorname = String::decode(&row[3])?;
let coauthor = Option::<String>::decode(&row[4])?;

Ok(Self {
id,
title,
content,
authorname,
coauthor,
})
}
}

#[http_component]
Expand All @@ -32,9 +54,8 @@ fn process(req: Request) -> Result<Response> {
fn read(_req: Request) -> Result<Response> {
let address = std::env::var(DB_URL_ENV)?;

let sql = "SELECT id, title, content, authorname FROM articletest";
let rowset = pg::query(&address, sql, &[])
.map_err(|e| anyhow!("Error executing Postgres query: {:?}", e))?;
let sql = "SELECT id, title, content, authorname, coauthor FROM articletest";
let rowset = pg::query(&address, sql, &[])?;

let column_summary = rowset
.columns
Expand All @@ -46,17 +67,7 @@ fn read(_req: Request) -> Result<Response> {
let mut response_lines = vec![];

for row in rowset.rows {
let id = as_int(&row[0])?;
let title = as_owned_string(&row[1])?;
let content = as_owned_string(&row[2])?;
let authorname = as_owned_string(&row[3])?;

let article = Article {
id,
title,
content,
authorname,
};
let article = Article::try_from(&row)?;

println!("article: {:#?}", article);
response_lines.push(format!("article: {:#?}", article));
Expand All @@ -80,16 +91,14 @@ fn write(_req: Request) -> Result<Response> {
let address = std::env::var(DB_URL_ENV)?;

let sql = "INSERT INTO articletest (title, content, authorname) VALUES ('aaa', 'bbb', 'ccc')";
let nrow_executed =
pg::execute(&address, sql, &[]).map_err(|_| anyhow!("Error execute pg command"))?;
let nrow_executed = pg::execute(&address, sql, &[])?;

println!("nrow_executed: {}", nrow_executed);

let sql = "SELECT COUNT(id) FROM articletest";
let rowset = pg::query(&address, sql, &[])
.map_err(|e| anyhow!("Error executing Postgres query: {:?}", e))?;
let rowset = pg::query(&address, sql, &[])?;
let row = &rowset.rows[0];
let count = as_bigint(&row[0])?;
let count = i64::decode(&row[0])?;
let response = format!("Count: {}\n", count);

Ok(http::Response::builder()
Expand All @@ -102,11 +111,10 @@ fn pg_backend_pid(_req: Request) -> Result<Response> {
let sql = "SELECT pg_backend_pid()";

let get_pid = || {
let rowset = pg::query(&address, sql, &[])
.map_err(|e| anyhow!("Error executing Postgres query: {:?}", e))?;

let rowset = pg::query(&address, sql, &[])?;
let row = &rowset.rows[0];
as_int(&row[0])

i32::decode(&row[0])
};

assert_eq!(get_pid()?, get_pid()?);
Expand All @@ -118,33 +126,6 @@ fn pg_backend_pid(_req: Request) -> Result<Response> {
.body(Some(response.into()))?)
}

fn as_owned_string(value: &pg::DbValue) -> anyhow::Result<String> {
match value {
pg::DbValue::Str(s) => Ok(s.to_owned()),
_ => Err(anyhow!("Expected string from database but got {:?}", value)),
}
}

fn as_int(value: &pg::DbValue) -> anyhow::Result<i32> {
match value {
pg::DbValue::Int32(n) => Ok(*n),
_ => Err(anyhow!(
"Expected integer from database but got {:?}",
value
)),
}
}

fn as_bigint(value: &pg::DbValue) -> anyhow::Result<i64> {
match value {
pg::DbValue::Int64(n) => Ok(*n),
_ => Err(anyhow!(
"Expected integer from database but got {:?}",
value
)),
}
}

fn format_col(column: &pg::Column) -> String {
format!("{}:{:?}", column.name, column.data_type)
}
Loading