Skip to content

Commit

Permalink
Merge pull request #1958 from fermyon/final-wit-changes
Browse files Browse the repository at this point in the history
Potentially final wit changes for 2.0.0
  • Loading branch information
rylev authored Oct 27, 2023
2 parents fe20501 + 0873a64 commit 7396a78
Show file tree
Hide file tree
Showing 32 changed files with 406 additions and 94 deletions.
40 changes: 20 additions & 20 deletions crates/outbound-mysql/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ use mysql_async::{consts::ColumnType, from_value_opt, prelude::*, Opts, OptsBuil
use spin_core::wasmtime::component::Resource;
use spin_core::{async_trait, HostComponent};
use spin_world::v1::mysql as v1;
use spin_world::v1::rdbms_types::{Column, DbDataType, DbValue, ParameterValue, RowSet};
use spin_world::v2::mysql::{self as v2, Connection};
use spin_world::v2::rdbms_types as v2_types;
use spin_world::v2::rdbms_types::{Column, DbDataType, DbValue, ParameterValue};
use std::sync::Arc;
use url::Url;

Expand Down Expand Up @@ -85,7 +86,7 @@ impl v2::HostConnection for OutboundMysql {
connection: Resource<Connection>,
statement: String,
params: Vec<ParameterValue>,
) -> Result<Result<RowSet, v2::Error>> {
) -> Result<Result<v2_types::RowSet, v2::Error>> {
Ok(async {
let db_params = params.into_iter().map(to_sql_parameter).collect::<Vec<_>>();
let parameters = mysql_async::Params::Positional(db_params);
Expand All @@ -108,7 +109,7 @@ impl v2::HostConnection for OutboundMysql {
.map(|row| convert_row(row, &columns))
.collect::<Result<Vec<_>, _>>()?;

Ok(RowSet { columns, rows })
Ok(v2_types::RowSet { columns, rows })
}
}
}
Expand All @@ -126,11 +127,11 @@ macro_rules! delegate {
($self:ident.$name:ident($address:expr, $($arg:expr),*)) => {{
let connection = match <Self as v2::HostConnection>::open($self, $address).await? {
Ok(c) => c,
Err(e) => return Ok(Err(to_legacy_error(e))),
Err(e) => return Ok(Err(e.into())),
};
Ok(<Self as v2::HostConnection>::$name($self, connection, $($arg),*)
.await?
.map_err(|e| to_legacy_error(e)))
.map_err(Into::into))
}};
}

Expand All @@ -140,28 +141,27 @@ impl v1::Host for OutboundMysql {
&mut self,
address: String,
statement: String,
params: Vec<ParameterValue>,
params: Vec<v1::ParameterValue>,
) -> Result<Result<(), v1::MysqlError>> {
delegate!(self.execute(address, statement, params))
delegate!(self.execute(
address,
statement,
params.into_iter().map(Into::into).collect()
))
}

async fn query(
&mut self,
address: String,
statement: String,
params: Vec<ParameterValue>,
) -> Result<Result<RowSet, v1::MysqlError>> {
delegate!(self.query(address, statement, params))
}
}

fn to_legacy_error(error: v2::Error) -> v1::MysqlError {
match error {
v2::Error::ConnectionFailed(e) => v1::MysqlError::ConnectionFailed(e),
v2::Error::BadParameter(e) => v1::MysqlError::BadParameter(e),
v2::Error::QueryFailed(e) => v1::MysqlError::QueryFailed(e),
v2::Error::ValueConversionFailed(e) => v1::MysqlError::ValueConversionFailed(e),
v2::Error::Other(e) => v1::MysqlError::OtherError(e),
params: Vec<v1::ParameterValue>,
) -> Result<Result<v1::RowSet, v1::MysqlError>> {
delegate!(self.query(
address,
statement,
params.into_iter().map(Into::into).collect()
))
.map(|r| r.map(Into::into))
}
}

Expand Down
40 changes: 19 additions & 21 deletions crates/outbound-pg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use anyhow::{anyhow, Result};
use native_tls::TlsConnector;
use postgres_native_tls::MakeTlsConnector;
use spin_core::{async_trait, wasmtime::component::Resource, HostComponent};
use spin_world::v1::{
postgres as v1,
rdbms_types::{Column, DbDataType, DbValue, ParameterValue, RowSet},
};
use spin_world::v1::postgres as v1;
use spin_world::v1::rdbms_types as v1_types;
use spin_world::v2::postgres::{self as v2, Connection};
use spin_world::v2::rdbms_types::{Column, DbDataType, DbValue, ParameterValue, RowSet};
use tokio_postgres::{
config::SslMode,
types::{ToSql, Type},
Expand Down Expand Up @@ -350,11 +349,11 @@ macro_rules! delegate {
($self:ident.$name:ident($address:expr, $($arg:expr),*)) => {{
let connection = match <Self as v2::HostConnection>::open($self, $address).await? {
Ok(c) => c,
Err(e) => return Ok(Err(to_legacy_error(e))),
Err(e) => return Ok(Err(e.into())),
};
Ok(<Self as v2::HostConnection>::$name($self, connection, $($arg),*)
.await?
.map_err(|e| to_legacy_error(e)))
.map_err(|e| e.into()))
}};
}

Expand All @@ -364,27 +363,26 @@ impl v1::Host for OutboundPg {
&mut self,
address: String,
statement: String,
params: Vec<ParameterValue>,
params: Vec<v1_types::ParameterValue>,
) -> Result<Result<u64, v1::PgError>> {
delegate!(self.execute(address, statement, params))
delegate!(self.execute(
address,
statement,
params.into_iter().map(Into::into).collect()
))
}

async fn query(
&mut self,
address: String,
statement: String,
params: Vec<ParameterValue>,
) -> Result<Result<RowSet, v1::PgError>> {
delegate!(self.query(address, statement, params))
}
}

fn to_legacy_error(error: v2::Error) -> v1::PgError {
match error {
v2::Error::ConnectionFailed(e) => v1::PgError::ConnectionFailed(e),
v2::Error::BadParameter(e) => v1::PgError::BadParameter(e),
v2::Error::QueryFailed(e) => v1::PgError::QueryFailed(e),
v2::Error::ValueConversionFailed(e) => v1::PgError::ValueConversionFailed(e),
v2::Error::Other(e) => v1::PgError::OtherError(e),
params: Vec<v1_types::ParameterValue>,
) -> Result<Result<v1_types::RowSet, v1::PgError>> {
delegate!(self.query(
address,
statement,
params.into_iter().map(Into::into).collect()
))
.map(|r| r.map(Into::into))
}
}
17 changes: 12 additions & 5 deletions crates/outbound-redis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use anyhow::Result;
use redis::{aio::Connection, AsyncCommands, FromRedisValue, Value};
use spin_core::{async_trait, wasmtime::component::Resource};
use spin_locked_app::MetadataKey;
use spin_world::v1::redis::{self as v1, RedisParameter, RedisResult};
use spin_world::v2::redis::{self as v2, Connection as RedisConnection, Error};
use spin_world::v1::redis as v1;
use spin_world::v2::redis::{
self as v2, Connection as RedisConnection, Error, RedisParameter, RedisResult,
};

pub const ALLOWED_HOSTS_KEY: MetadataKey<Option<Vec<String>>> =
MetadataKey::new("allowed_outbound_hosts");
Expand Down Expand Up @@ -332,9 +334,14 @@ impl v1::Host for OutboundRedis {
&mut self,
address: String,
command: String,
arguments: Vec<RedisParameter>,
) -> Result<Result<Vec<RedisResult>, v1::Error>> {
delegate!(self.execute(address, command, arguments))
arguments: Vec<v1::RedisParameter>,
) -> Result<Result<Vec<v1::RedisResult>, v1::Error>> {
delegate!(self.execute(
address,
command,
arguments.into_iter().map(Into::into).collect()
))
.map(|r| r.map(|v| v.into_iter().map(Into::into).collect()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/redis/tests/rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::str::{from_utf8, Utf8Error};

wit_bindgen::generate!({
world: "redis-trigger",
path: "../../../../wit/preview2",
path: "../../../../wit/preview2/deps/spin@unversioned",
exports: {
"fermyon:spin/inbound-redis": SpinRedis,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/trigger-http/benches/spin-http-benchmark/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
wit_bindgen::generate!({
world: "http-trigger",
path: "../../../../wit/preview2",
path: "../../../../wit/preview2/deps/spin@unversioned",
exports: {
"fermyon:spin/inbound-http": SpinHttp,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/trigger-http/tests/rust-http-test/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
wit_bindgen::generate!({
world: "http-trigger",
path: "../../../../wit/preview2",
path: "../../../../wit/preview2/deps/spin@unversioned",
exports: {
"fermyon:spin/inbound-http": SpinHttp,
}
Expand Down
170 changes: 170 additions & 0 deletions crates/world/src/conversions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
use super::*;

mod rdbms_types {
use super::*;

impl From<v2::rdbms_types::Column> for v1::rdbms_types::Column {
fn from(value: v2::rdbms_types::Column) -> Self {
v1::rdbms_types::Column {
name: value.name,
data_type: value.data_type.into(),
}
}
}

impl From<v2::rdbms_types::DbValue> for v1::rdbms_types::DbValue {
fn from(value: v2::rdbms_types::DbValue) -> v1::rdbms_types::DbValue {
match value {
v2::rdbms_types::DbValue::Boolean(b) => v1::rdbms_types::DbValue::Boolean(b),
v2::rdbms_types::DbValue::Int8(i) => v1::rdbms_types::DbValue::Int8(i),
v2::rdbms_types::DbValue::Int16(i) => v1::rdbms_types::DbValue::Int16(i),
v2::rdbms_types::DbValue::Int32(i) => v1::rdbms_types::DbValue::Int32(i),
v2::rdbms_types::DbValue::Int64(i) => v1::rdbms_types::DbValue::Int64(i),
v2::rdbms_types::DbValue::Uint8(j) => v1::rdbms_types::DbValue::Uint8(j),
v2::rdbms_types::DbValue::Uint16(u) => v1::rdbms_types::DbValue::Uint16(u),
v2::rdbms_types::DbValue::Uint32(u) => v1::rdbms_types::DbValue::Uint32(u),
v2::rdbms_types::DbValue::Uint64(u) => v1::rdbms_types::DbValue::Uint64(u),
v2::rdbms_types::DbValue::Floating32(r) => v1::rdbms_types::DbValue::Floating32(r),
v2::rdbms_types::DbValue::Floating64(r) => v1::rdbms_types::DbValue::Floating64(r),
v2::rdbms_types::DbValue::Str(s) => v1::rdbms_types::DbValue::Str(s),
v2::rdbms_types::DbValue::Binary(b) => v1::rdbms_types::DbValue::Binary(b),
v2::rdbms_types::DbValue::DbNull => v1::rdbms_types::DbValue::DbNull,
v2::rdbms_types::DbValue::Unsupported => v1::rdbms_types::DbValue::Unsupported,
}
}
}

impl From<v2::rdbms_types::DbDataType> for v1::rdbms_types::DbDataType {
fn from(value: v2::rdbms_types::DbDataType) -> v1::rdbms_types::DbDataType {
match value {
v2::rdbms_types::DbDataType::Boolean => v1::rdbms_types::DbDataType::Boolean,
v2::rdbms_types::DbDataType::Int8 => v1::rdbms_types::DbDataType::Int8,
v2::rdbms_types::DbDataType::Int16 => v1::rdbms_types::DbDataType::Int16,
v2::rdbms_types::DbDataType::Int32 => v1::rdbms_types::DbDataType::Int32,
v2::rdbms_types::DbDataType::Int64 => v1::rdbms_types::DbDataType::Int64,
v2::rdbms_types::DbDataType::Uint8 => v1::rdbms_types::DbDataType::Uint8,
v2::rdbms_types::DbDataType::Uint16 => v1::rdbms_types::DbDataType::Uint16,
v2::rdbms_types::DbDataType::Uint32 => v1::rdbms_types::DbDataType::Uint32,
v2::rdbms_types::DbDataType::Uint64 => v1::rdbms_types::DbDataType::Uint64,
v2::rdbms_types::DbDataType::Floating32 => v1::rdbms_types::DbDataType::Floating32,
v2::rdbms_types::DbDataType::Floating64 => v1::rdbms_types::DbDataType::Floating64,
v2::rdbms_types::DbDataType::Str => v1::rdbms_types::DbDataType::Str,
v2::rdbms_types::DbDataType::Binary => v1::rdbms_types::DbDataType::Binary,
v2::rdbms_types::DbDataType::Other => v1::rdbms_types::DbDataType::Other,
}
}
}

impl From<v1::rdbms_types::ParameterValue> for v2::rdbms_types::ParameterValue {
fn from(value: v1::rdbms_types::ParameterValue) -> v2::rdbms_types::ParameterValue {
match value {
v1::rdbms_types::ParameterValue::Boolean(b) => {
v2::rdbms_types::ParameterValue::Boolean(b)
}
v1::rdbms_types::ParameterValue::Int8(i) => {
v2::rdbms_types::ParameterValue::Int8(i)
}
v1::rdbms_types::ParameterValue::Int16(i) => {
v2::rdbms_types::ParameterValue::Int16(i)
}
v1::rdbms_types::ParameterValue::Int32(i) => {
v2::rdbms_types::ParameterValue::Int32(i)
}
v1::rdbms_types::ParameterValue::Int64(i) => {
v2::rdbms_types::ParameterValue::Int64(i)
}
v1::rdbms_types::ParameterValue::Uint8(u) => {
v2::rdbms_types::ParameterValue::Uint8(u)
}
v1::rdbms_types::ParameterValue::Uint16(u) => {
v2::rdbms_types::ParameterValue::Uint16(u)
}
v1::rdbms_types::ParameterValue::Uint32(u) => {
v2::rdbms_types::ParameterValue::Uint32(u)
}
v1::rdbms_types::ParameterValue::Uint64(u) => {
v2::rdbms_types::ParameterValue::Uint64(u)
}
v1::rdbms_types::ParameterValue::Floating32(r) => {
v2::rdbms_types::ParameterValue::Floating32(r)
}
v1::rdbms_types::ParameterValue::Floating64(r) => {
v2::rdbms_types::ParameterValue::Floating64(r)
}
v1::rdbms_types::ParameterValue::Str(s) => v2::rdbms_types::ParameterValue::Str(s),
v1::rdbms_types::ParameterValue::Binary(b) => {
v2::rdbms_types::ParameterValue::Binary(b)
}
v1::rdbms_types::ParameterValue::DbNull => v2::rdbms_types::ParameterValue::DbNull,
}
}
}

impl From<v2::rdbms_types::Error> for v1::mysql::MysqlError {
fn from(error: v2::rdbms_types::Error) -> v1::mysql::MysqlError {
match error {
v2::mysql::Error::ConnectionFailed(e) => v1::mysql::MysqlError::ConnectionFailed(e),
v2::mysql::Error::BadParameter(e) => v1::mysql::MysqlError::BadParameter(e),
v2::mysql::Error::QueryFailed(e) => v1::mysql::MysqlError::QueryFailed(e),
v2::mysql::Error::ValueConversionFailed(e) => {
v1::mysql::MysqlError::ValueConversionFailed(e)
}
v2::mysql::Error::Other(e) => v1::mysql::MysqlError::OtherError(e),
}
}
}

impl From<v2::rdbms_types::Error> for v1::postgres::PgError {
fn from(error: v2::rdbms_types::Error) -> v1::postgres::PgError {
match error {
v2::mysql::Error::ConnectionFailed(e) => v1::postgres::PgError::ConnectionFailed(e),
v2::mysql::Error::BadParameter(e) => v1::postgres::PgError::BadParameter(e),
v2::mysql::Error::QueryFailed(e) => v1::postgres::PgError::QueryFailed(e),
v2::mysql::Error::ValueConversionFailed(e) => {
v1::postgres::PgError::ValueConversionFailed(e)
}
v2::mysql::Error::Other(e) => v1::postgres::PgError::OtherError(e),
}
}
}
}

mod mysql {
use super::*;
impl From<v2::mysql::RowSet> for v1::mysql::RowSet {
fn from(value: v2::mysql::RowSet) -> v1::mysql::RowSet {
v1::mysql::RowSet {
columns: value.columns.into_iter().map(Into::into).collect(),
rows: value
.rows
.into_iter()
.map(|r| r.into_iter().map(Into::into).collect())
.collect(),
}
}
}
}

mod redis {
use super::*;

impl From<v1::redis::RedisParameter> for v2::redis::RedisParameter {
fn from(value: v1::redis::RedisParameter) -> Self {
match value {
v1::redis::RedisParameter::Int64(i) => v2::redis::RedisParameter::Int64(i),
v1::redis::RedisParameter::Binary(b) => v2::redis::RedisParameter::Binary(b),
}
}
}

impl From<v2::redis::RedisResult> for v1::redis::RedisResult {
fn from(value: v2::redis::RedisResult) -> Self {
match value {
v2::redis::RedisResult::Nil => v1::redis::RedisResult::Nil,
v2::redis::RedisResult::Status(s) => v1::redis::RedisResult::Status(s),
v2::redis::RedisResult::Int64(i) => v1::redis::RedisResult::Int64(i),
v2::redis::RedisResult::Binary(b) => v1::redis::RedisResult::Binary(b),
}
}
}
}
2 changes: 2 additions & 0 deletions crates/world/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ wasmtime::component::bindgen!({

pub use fermyon::spin as v1;
pub use fermyon::spin2_0_0 as v2;

mod conversions;
Loading

0 comments on commit 7396a78

Please sign in to comment.