From b927981d1a6f0384161983fa3474063878e075da Mon Sep 17 00:00:00 2001 From: Virgiel <> Date: Sat, 1 Apr 2023 02:00:30 +0200 Subject: [PATCH] Use std SystemTime for timestamps --- codegen_test/src/cornucopia.rs | 128 +++++++++++++++---------------- codegen_test/src/main.rs | 28 +++---- cornucopia/src/codegen.rs | 4 +- cornucopia/src/type_registrar.rs | 3 +- 4 files changed, 77 insertions(+), 86 deletions(-) diff --git a/codegen_test/src/cornucopia.rs b/codegen_test/src/cornucopia.rs index 38df419d..73056a65 100644 --- a/codegen_test/src/cornucopia.rs +++ b/codegen_test/src/cornucopia.rs @@ -3886,10 +3886,10 @@ pub mod queries { pub text_: T1, pub varchar_: T2, pub bytea_: T3, - pub timestamp_: time::PrimitiveDateTime, - pub timestamp_without_time_zone_: time::PrimitiveDateTime, - pub timestamptz_: time::OffsetDateTime, - pub timestamp_with_time_zone_: time::OffsetDateTime, + pub timestamp_: std::time::SystemTime, + pub timestamp_without_time_zone_: std::time::SystemTime, + pub timestamptz_: std::time::SystemTime, + pub timestamp_with_time_zone_: std::time::SystemTime, pub date_: time::Date, pub time_: time::Time, pub json_: T4, @@ -3920,10 +3920,10 @@ pub mod queries { T17: cornucopia_async::ArraySql, T18: cornucopia_async::BytesSql, T19: cornucopia_async::ArraySql, - T20: cornucopia_async::ArraySql, - T21: cornucopia_async::ArraySql, - T22: cornucopia_async::ArraySql, - T23: cornucopia_async::ArraySql, + T20: cornucopia_async::ArraySql, + T21: cornucopia_async::ArraySql, + T22: cornucopia_async::ArraySql, + T23: cornucopia_async::ArraySql, T24: cornucopia_async::ArraySql, T25: cornucopia_async::ArraySql, T26: cornucopia_async::JsonSql, @@ -3988,10 +3988,10 @@ pub mod queries { pub text_: String, pub varchar_: String, pub bytea_: Vec, - pub timestamp_: time::PrimitiveDateTime, - pub timestamp_without_time_zone_: time::PrimitiveDateTime, - pub timestamptz_: time::OffsetDateTime, - pub timestamp_with_time_zone_: time::OffsetDateTime, + pub timestamp_: std::time::SystemTime, + pub timestamp_without_time_zone_: std::time::SystemTime, + pub timestamptz_: std::time::SystemTime, + pub timestamp_with_time_zone_: std::time::SystemTime, pub date_: time::Date, pub time_: time::Time, pub json_: serde_json::Value, @@ -4024,10 +4024,10 @@ pub mod queries { pub text_: &'a str, pub varchar_: &'a str, pub bytea_: &'a [u8], - pub timestamp_: time::PrimitiveDateTime, - pub timestamp_without_time_zone_: time::PrimitiveDateTime, - pub timestamptz_: time::OffsetDateTime, - pub timestamp_with_time_zone_: time::OffsetDateTime, + pub timestamp_: std::time::SystemTime, + pub timestamp_without_time_zone_: std::time::SystemTime, + pub timestamptz_: std::time::SystemTime, + pub timestamp_with_time_zone_: std::time::SystemTime, pub date_: time::Date, pub time_: time::Time, pub json_: postgres_types::Json<&'a serde_json::value::RawValue>, @@ -4138,10 +4138,10 @@ pub mod queries { pub text_: Option, pub varchar_: Option, pub bytea_: Option>, - pub timestamp_: Option, - pub timestamp_without_time_zone_: Option, - pub timestamptz_: Option, - pub timestamp_with_time_zone_: Option, + pub timestamp_: Option, + pub timestamp_without_time_zone_: Option, + pub timestamptz_: Option, + pub timestamp_with_time_zone_: Option, pub date_: Option, pub time_: Option, pub json_: Option, @@ -4174,10 +4174,10 @@ pub mod queries { pub text_: Option<&'a str>, pub varchar_: Option<&'a str>, pub bytea_: Option<&'a [u8]>, - pub timestamp_: Option, - pub timestamp_without_time_zone_: Option, - pub timestamptz_: Option, - pub timestamp_with_time_zone_: Option, + pub timestamp_: Option, + pub timestamp_without_time_zone_: Option, + pub timestamptz_: Option, + pub timestamp_with_time_zone_: Option, pub date_: Option, pub time_: Option, pub json_: Option>, @@ -4282,10 +4282,10 @@ pub mod queries { pub text_: Vec, pub varchar_: Vec, pub bytea_: Vec>, - pub timestamp_: Vec, - pub timestamp_without_time_zone_: Vec, - pub timestamptz_: Vec, - pub timestamp_with_time_zone_: Vec, + pub timestamp_: Vec, + pub timestamp_without_time_zone_: Vec, + pub timestamptz_: Vec, + pub timestamp_with_time_zone_: Vec, pub date_: Vec, pub time_: Vec, pub json_: Vec, @@ -4312,12 +4312,12 @@ pub mod queries { pub text_: cornucopia_async::ArrayIterator<'a, &'a str>, pub varchar_: cornucopia_async::ArrayIterator<'a, &'a str>, pub bytea_: cornucopia_async::ArrayIterator<'a, &'a [u8]>, - pub timestamp_: cornucopia_async::ArrayIterator<'a, time::PrimitiveDateTime>, + pub timestamp_: cornucopia_async::ArrayIterator<'a, std::time::SystemTime>, pub timestamp_without_time_zone_: - cornucopia_async::ArrayIterator<'a, time::PrimitiveDateTime>, - pub timestamptz_: cornucopia_async::ArrayIterator<'a, time::OffsetDateTime>, + cornucopia_async::ArrayIterator<'a, std::time::SystemTime>, + pub timestamptz_: cornucopia_async::ArrayIterator<'a, std::time::SystemTime>, pub timestamp_with_time_zone_: - cornucopia_async::ArrayIterator<'a, time::OffsetDateTime>, + cornucopia_async::ArrayIterator<'a, std::time::SystemTime>, pub date_: cornucopia_async::ArrayIterator<'a, time::Date>, pub time_: cornucopia_async::ArrayIterator<'a, time::Time>, pub json_: cornucopia_async::ArrayIterator< @@ -4420,10 +4420,10 @@ pub mod queries { pub text_: Option>, pub varchar_: Option>, pub bytea_: Option>>, - pub timestamp_: Option>, - pub timestamp_without_time_zone_: Option>, - pub timestamptz_: Option>, - pub timestamp_with_time_zone_: Option>, + pub timestamp_: Option>, + pub timestamp_without_time_zone_: Option>, + pub timestamptz_: Option>, + pub timestamp_with_time_zone_: Option>, pub date_: Option>, pub time_: Option>, pub json_: Option>, @@ -4450,12 +4450,12 @@ pub mod queries { pub text_: Option>, pub varchar_: Option>, pub bytea_: Option>, - pub timestamp_: Option>, + pub timestamp_: Option>, pub timestamp_without_time_zone_: - Option>, - pub timestamptz_: Option>, + Option>, + pub timestamptz_: Option>, pub timestamp_with_time_zone_: - Option>, + Option>, pub date_: Option>, pub time_: Option>, pub json_: Option< @@ -4963,10 +4963,10 @@ FROM text_: &'a T1, varchar_: &'a T2, bytea_: &'a T3, - timestamp_: &'a time::PrimitiveDateTime, - timestamp_without_time_zone_: &'a time::PrimitiveDateTime, - timestamptz_: &'a time::OffsetDateTime, - timestamp_with_time_zone_: &'a time::OffsetDateTime, + timestamp_: &'a std::time::SystemTime, + timestamp_without_time_zone_: &'a std::time::SystemTime, + timestamptz_: &'a std::time::SystemTime, + timestamp_with_time_zone_: &'a std::time::SystemTime, date_: &'a time::Date, time_: &'a time::Time, json_: &'a T4, @@ -5211,10 +5211,10 @@ FROM T17: cornucopia_sync::ArraySql, T18: cornucopia_sync::BytesSql, T19: cornucopia_sync::ArraySql, - T20: cornucopia_sync::ArraySql, - T21: cornucopia_sync::ArraySql, - T22: cornucopia_sync::ArraySql, - T23: cornucopia_sync::ArraySql, + T20: cornucopia_sync::ArraySql, + T21: cornucopia_sync::ArraySql, + T22: cornucopia_sync::ArraySql, + T23: cornucopia_sync::ArraySql, T24: cornucopia_sync::ArraySql, T25: cornucopia_sync::ArraySql, T26: cornucopia_sync::JsonSql, @@ -5315,10 +5315,10 @@ FROM T17: cornucopia_sync::ArraySql, T18: cornucopia_sync::BytesSql, T19: cornucopia_sync::ArraySql, - T20: cornucopia_sync::ArraySql, - T21: cornucopia_sync::ArraySql, - T22: cornucopia_sync::ArraySql, - T23: cornucopia_sync::ArraySql, + T20: cornucopia_sync::ArraySql, + T21: cornucopia_sync::ArraySql, + T22: cornucopia_sync::ArraySql, + T23: cornucopia_sync::ArraySql, T24: cornucopia_sync::ArraySql, T25: cornucopia_sync::ArraySql, T26: cornucopia_sync::JsonSql, @@ -5926,10 +5926,10 @@ FROM text_: &'a T1, varchar_: &'a T2, bytea_: &'a T3, - timestamp_: &'a time::PrimitiveDateTime, - timestamp_without_time_zone_: &'a time::PrimitiveDateTime, - timestamptz_: &'a time::OffsetDateTime, - timestamp_with_time_zone_: &'a time::OffsetDateTime, + timestamp_: &'a std::time::SystemTime, + timestamp_without_time_zone_: &'a std::time::SystemTime, + timestamptz_: &'a std::time::SystemTime, + timestamp_with_time_zone_: &'a std::time::SystemTime, date_: &'a time::Date, time_: &'a time::Time, json_: &'a T4, @@ -6188,10 +6188,10 @@ FROM T17: cornucopia_async::ArraySql, T18: cornucopia_async::BytesSql, T19: cornucopia_async::ArraySql, - T20: cornucopia_async::ArraySql, - T21: cornucopia_async::ArraySql, - T22: cornucopia_async::ArraySql, - T23: cornucopia_async::ArraySql, + T20: cornucopia_async::ArraySql, + T21: cornucopia_async::ArraySql, + T22: cornucopia_async::ArraySql, + T23: cornucopia_async::ArraySql, T24: cornucopia_async::ArraySql, T25: cornucopia_async::ArraySql, T26: cornucopia_async::JsonSql, @@ -6294,10 +6294,10 @@ FROM T17: cornucopia_async::ArraySql, T18: cornucopia_async::BytesSql, T19: cornucopia_async::ArraySql, - T20: cornucopia_async::ArraySql, - T21: cornucopia_async::ArraySql, - T22: cornucopia_async::ArraySql, - T23: cornucopia_async::ArraySql, + T20: cornucopia_async::ArraySql, + T21: cornucopia_async::ArraySql, + T22: cornucopia_async::ArraySql, + T23: cornucopia_async::ArraySql, T24: cornucopia_async::ArraySql, T25: cornucopia_async::ArraySql, T26: cornucopia_async::JsonSql, diff --git a/codegen_test/src/main.rs b/codegen_test/src/main.rs index 8e5d3e5f..4641923b 100644 --- a/codegen_test/src/main.rs +++ b/codegen_test/src/main.rs @@ -9,8 +9,8 @@ use std::{ borrow::Cow, collections::HashMap, net::{IpAddr, Ipv4Addr}, + time::SystemTime, }; -use time::{OffsetDateTime, PrimitiveDateTime}; use uuid::Uuid; use crate::cornucopia::{ @@ -369,15 +369,7 @@ pub fn test_domain(client: &mut Client) { // Test hard cases pub fn test_stress(client: &mut Client) { - let primitive_datetime_format = - time::format_description::parse("[year]-[month]-[day] [hour]:[minute]:[second]").unwrap(); - let primitive_datetime = - PrimitiveDateTime::parse("2020-01-02 03:04:05", &primitive_datetime_format).unwrap(); - let offset_datetime = OffsetDateTime::parse( - "1985-04-12T23:20:50.52Z", - &time::format_description::well_known::Rfc3339, - ) - .unwrap(); + let now = SystemTime::now(); let json: Value = serde_json::from_str("{}").unwrap(); // Every supported type @@ -404,10 +396,10 @@ pub fn test_stress(client: &mut Client) { text_: String::from("hello"), varchar_: String::from("hello"), bytea_: vec![222u8, 173u8, 190u8, 239u8], - timestamp_: primitive_datetime, - timestamp_without_time_zone_: primitive_datetime, - timestamptz_: offset_datetime, - timestamp_with_time_zone_: offset_datetime, + timestamp_: now, + timestamp_without_time_zone_: now, + timestamptz_: now, + timestamp_with_time_zone_: now, date_: time::Date::from_calendar_date(1999, time::Month::January, 8).unwrap(), time_: time::Time::from_hms_milli(4, 5, 6, 789).unwrap(), json_: json.clone(), @@ -475,10 +467,10 @@ pub fn test_stress(client: &mut Client) { text_: vec![String::from("hello")], varchar_: vec![String::from("hello")], bytea_: vec![vec![222u8, 173u8, 190u8, 239u8]], - timestamp_: vec![primitive_datetime], - timestamp_without_time_zone_: vec![primitive_datetime], - timestamptz_: vec![offset_datetime], - timestamp_with_time_zone_: vec![offset_datetime], + timestamp_: vec![now], + timestamp_without_time_zone_: vec![now], + timestamptz_: vec![now], + timestamp_with_time_zone_: vec![now], date_: vec![time::Date::from_calendar_date(1999, time::Month::January, 8).unwrap()], time_: vec![time::Time::from_hms_milli(4, 5, 6, 789).unwrap()], json_: vec![json.clone()], diff --git a/cornucopia/src/codegen.rs b/cornucopia/src/codegen.rs index af6c4b8a..d4e2165f 100644 --- a/cornucopia/src/codegen.rs +++ b/cornucopia/src/codegen.rs @@ -323,7 +323,7 @@ fn gen_params_struct(w: &mut impl Write, params: &PreparedItem, ctx: &GenCtx) { .map(|p| p.param_ergo_ty(traits, ctx)) .collect::>(); let fields_name = fields.iter().map(|p| &p.ident.rs); - let traits_idx = (1..=traits.len()).into_iter().map(idx_char); + let traits_idx = (1..=traits.len()).map(idx_char); code!(w => #[derive($copy Debug)] pub struct $name<$lifetime $($traits_idx: $traits,)> { @@ -507,7 +507,7 @@ fn gen_query_fn(w: &mut W, module: &PreparedModule, query: &PreparedQu .map(|idx| param_field[*idx].param_ergo_ty(traits, ctx)) .collect(); let params_name = order.iter().map(|idx| ¶m_field[*idx].ident.rs); - let traits_idx = (1..=traits.len()).into_iter().map(idx_char); + let traits_idx = (1..=traits.len()).map(idx_char); let lazy_impl = |w: &mut W| { if let Some((idx, index)) = row { let item = module.rows.get_index(*idx).unwrap().1; diff --git a/cornucopia/src/type_registrar.rs b/cornucopia/src/type_registrar.rs index c26b538a..1d8da320 100644 --- a/cornucopia/src/type_registrar.rs +++ b/cornucopia/src/type_registrar.rs @@ -385,8 +385,7 @@ impl TypeRegistrar { Type::FLOAT8 => ("f64", true), Type::TEXT | Type::VARCHAR => ("String", false), Type::BYTEA => ("Vec", false), - Type::TIMESTAMP => ("time::PrimitiveDateTime", true), - Type::TIMESTAMPTZ => ("time::OffsetDateTime", true), + Type::TIMESTAMP | Type::TIMESTAMPTZ => ("std::time::SystemTime", true), Type::DATE => ("time::Date", true), Type::TIME => ("time::Time", true), Type::JSON | Type::JSONB => ("serde_json::Value", false),