Skip to content

Commit

Permalink
Added some missing conversion for ToDatabaseValue trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
maoueh committed Jun 9, 2023
1 parent 24d6a2a commit eb6f397
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.2]

* Added some missing conversion for `ToDatabaseValue` trait.

## [1.1.1]

* Fixed encoding of `Vec<u8>` to be in `hex` format as expected by Postgres (and hopefully other database). This is technically a breaking change but it was probably never actually working, so we are changing. If you were relying on `base64` decoding, please let use know and we will find a solution.
Expand Down
45 changes: 24 additions & 21 deletions src/tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,30 +197,17 @@ impl_to_database_value_proxy_to_string!(u8);
impl_to_database_value_proxy_to_string!(u16);
impl_to_database_value_proxy_to_string!(u32);
impl_to_database_value_proxy_to_string!(u64);
impl_to_database_value_proxy_to_string!(bool);
impl_to_database_value_proxy_to_string!(::prost_types::Timestamp);
impl_to_database_value_proxy_to_string!(&::prost_types::Timestamp);
impl_to_database_value_proxy_to_string!(&str);
impl_to_database_value_proxy_to_string!(BigDecimal);
impl_to_database_value_proxy_to_string!(&BigDecimal);
impl_to_database_value_proxy_to_string!(BigInt);
impl_to_database_value_proxy_to_string!(&BigInt);

impl_to_database_value_proxy_to_ref!(bool);
impl_to_database_value_proxy_to_ref!(BigDecimal);
impl_to_database_value_proxy_to_ref!(BigInt);
impl_to_database_value_proxy_to_ref!(Vec<u8>);

impl ToDatabaseValue for &bool {
fn to_value(self) -> String {
(if *self == true { "true" } else { "false" }).to_string()
}
}

impl ToDatabaseValue for &BigDecimal {
fn to_value(self) -> String {
ToString::to_string(self)
}
}

impl ToDatabaseValue for &BigInt {
fn to_value(self) -> String {
ToString::to_string(self)
}
}

impl ToDatabaseValue for &String {
fn to_value(self) -> String {
self.clone()
Expand Down Expand Up @@ -250,3 +237,19 @@ impl<T: AsRef<[u8]>> ToDatabaseValue for &Hex<T> {
ToString::to_string(self)
}
}

#[cfg(test)]
mod test {
use crate::tables::ToDatabaseValue;

#[test]
fn to_database_value_proto_timestamp() {
assert_eq!(
ToDatabaseValue::to_value(::prost_types::Timestamp {
seconds: 60 * 60 + 60 + 1,
nanos: 1
}),
"1970-01-01T01:01:01.000000001Z"
);
}
}

0 comments on commit eb6f397

Please sign in to comment.