Skip to content

Commit

Permalink
feat: make date/times their own type rather than a subtype of string
Browse files Browse the repository at this point in the history
  • Loading branch information
bmoxb authored Oct 18, 2021
1 parent e7d53cf commit 87cef82
Show file tree
Hide file tree
Showing 30 changed files with 686 additions and 695 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
use super::super::prelude::*;
use super::prelude::*;

use std::{ops::Range as StdRange, sync::Arc};

derive_generator! {
yield Token,
return Result<Value, Error>,
pub struct DateTimeNode(Valuize<Tokenizer<RandomDateTime>, ChronoValueAndFormat>);
}

impl From<RandomDateTime> for DateTimeNode {
fn from(value: RandomDateTime) -> Self {
Self(value.into_token().map_complete(value_from_ok::<ChronoValueAndFormat>))
}
}

pub struct RandomDateTime {
inner: OnceInfallible<Random<ChronoValue, Uniform<ChronoValue>>>,
format: Arc<str>,
Expand Down
36 changes: 16 additions & 20 deletions core/src/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ pub use null::NullNode;

pub mod string;
pub use string::{
Format, FormatArgs, RandFaker, RandomDateTime, RandomString, StringNode, Truncated, UuidGen,
Format, FormatArgs, RandFaker, RandomString, StringNode, Truncated, UuidGen,
};

pub mod date_time;
pub use date_time::{RandomDateTime, DateTimeNode};

pub mod number;
pub use number::{Incrementing, NumberNode, RandomF64, RandomI64, RandomU64, StandardIntRangeStep, StandardFloatRangeStep};

Expand Down Expand Up @@ -328,7 +331,7 @@ impl Encode<'_, MySql> for Value {
Value::Object(_) => return None, //TODO: Use JSON here?
Value::Array(elems) => if elems.is_empty() {
return None
} else if let Value::Number(Number::U8(_) | Number::I8(_)) = elems[0] {
} else if let Value::Number(Number::U8(_) | Number::I8(_)) = elems[0] {
<Vec<u8> as Type<MySql>>::type_info()
} else {
return None //TODO: other variants that would make sense?
Expand Down Expand Up @@ -511,6 +514,7 @@ derive_generator!(
Bool(BoolNode),
Number(NumberNode),
String(StringNode),
DateTime(DateTimeNode),
Object(ObjectNode),
Array(ArrayNode),
OneOf(OneOfNode),
Expand Down Expand Up @@ -701,23 +705,17 @@ pub mod tests {
}
},
"created_at_date": {
"type": "string",
"date_time": {
"format": "%Y/%m/%d"
}
"type": "date_time",
"format": "%Y/%m/%d"
},
"created_at_time": {
"type": "string",
"date_time": {
"format": "%H:%M:%S"
}
"type": "date_time",
"format": "%H:%M:%S"
},
"last_login_at": {
"type": "string",
"date_time": {
"format": "%Y-%m-%dT%H:%M:%S%z",
"begin": "2020-01-01T00:00:00+0000"
}
"type": "date_time",
"format": "%Y-%m-%dT%H:%M:%S%z",
"begin": "2020-01-01T00:00:00+0000"
},
"maybe_an_email": {
"optional": true,
Expand Down Expand Up @@ -748,11 +746,9 @@ pub mod tests {
"ref": "users.content.currency"
},
"timestamp": {
"type": "string",
"date_time": {
"format": "%Y-%m-%dT%H:%M:%S%z",
"begin": "2020-01-01T00:00:00+0000"
}
"type": "date_time",
"format": "%Y-%m-%dT%H:%M:%S%z",
"begin": "2020-01-01T00:00:00+0000"
},
"amount": {
"type": "number",
Expand Down
7 changes: 2 additions & 5 deletions core/src/graph/string/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ impl dynfmt::FormatArgs for FormatArgs<String> {
#[cfg(test)]
pub mod tests {
use super::*;
use crate::graph::{
ChronoValue, Graph, NumberNode, RandFaker, RandomDateTime, RandomI64, RandomString,
StringNode,
};
use crate::graph::{ChronoValue, DateTimeNode, Graph, NumberNode, RandFaker, RandomDateTime, RandomI64, RandomString, StringNode};
use chrono::naive::NaiveDate;

fn faker_graph(name: &str) -> Graph {
Expand Down Expand Up @@ -171,7 +168,7 @@ pub mod tests {
let args = FormatArgs {
named: vec![(
"date".to_string(),
Graph::String(StringNode::from(RandomDateTime::new(
Graph::DateTime(DateTimeNode::from(RandomDateTime::new(
ChronoValue::NaiveDate(NaiveDate::from_ymd(2021, 10, 4))
..ChronoValue::NaiveDate(NaiveDate::from_ymd(2021, 10, 4)),
"%Y-%m-%d",
Expand Down
20 changes: 2 additions & 18 deletions core/src/graph/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ use super::prelude::*;

use rand_regex::Regex as RandRegex;

pub mod date_time;
pub use date_time::RandomDateTime;

pub mod faker;
pub mod format;
pub mod serialized;
Expand Down Expand Up @@ -76,24 +73,11 @@ impl From<Format> for RandomString {
derive_generator! {
yield Token,
return Result<Value, Error>,
pub enum StringNode {
String(Valuize<Tokenizer<RandomString>, String>),
DateTime(Valuize<Tokenizer<RandomDateTime>, ChronoValueAndFormat>)
}
pub struct StringNode(Valuize<Tokenizer<RandomString>, String>);
}

impl From<RandomString> for StringNode {
fn from(value: RandomString) -> Self {
Self::String(value.into_token().map_complete(value_from_ok::<String>))
}
}

impl From<RandomDateTime> for StringNode {
fn from(value: RandomDateTime) -> Self {
Self::DateTime(
value
.into_token()
.map_complete(value_from_ok::<ChronoValueAndFormat>),
)
Self(value.into_token().map_complete(value_from_ok::<String>))
}
}
Loading

0 comments on commit 87cef82

Please sign in to comment.