Skip to content

Commit

Permalink
feat: truncated generator
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 3a9edea8b087b3f5c8f34b953bce747974a5e54b
  • Loading branch information
christos-h authored and oq-bot committed Jun 8, 2021
1 parent 346532f commit 65b6772
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "synth-core"
version = "0.4.5"
version = "0.4.6"
authors = [
"Damien Broka <[email protected]>",
"Christos Hadjiaslanis <[email protected]>"
Expand Down
13 changes: 10 additions & 3 deletions core/src/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub mod null;
pub use null::NullNode;

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

pub mod number;
pub use number::{Incrementing, NumberNode, RandomF64, RandomI64, RandomU64, UniformRangeStep};
Expand Down Expand Up @@ -355,8 +355,14 @@ pub mod tests {
"frequency": 0.2
},
"username": {
"type": "string",
"pattern": "[a-z0-9]{5,15}"
"type": "string",
"truncated": {
"content": {
"type": "string",
"pattern": "[a-zA-Z0-9]{0, 255}"
},
"length": 5
}
},
"bank_country": {
"type": "string",
Expand Down Expand Up @@ -530,6 +536,7 @@ pub mod tests {
println!("bank_country={}", user.bank_country);
assert!(&user.bank_country == "GB" || &user.bank_country == "ES");
assert!(user.id >= 100);
assert!(user.username.len() <= 5);
all_users.insert(user.username.clone());
currencies.insert(user.username, user.currency);
/*
Expand Down
12 changes: 11 additions & 1 deletion core/src/graph/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ pub use date_time::RandomDateTime;

pub mod faker;
pub mod serialized;
pub mod truncated;
pub mod uuid;

pub use self::uuid::UuidGen;
pub use faker::RandFaker;
pub use serialized::Serialized;
pub use truncated::Truncated;

derive_generator! {
yield String,
Expand All @@ -22,6 +24,7 @@ derive_generator! {
Serialized(TryOnce<Serialized>)
Categorical(OnceInfallible<Random<String, Categorical<String>>>)
Uuid(OnceInfallible<UuidGen>)
Truncated(Truncated)
}
}

Expand Down Expand Up @@ -55,10 +58,17 @@ impl From<UuidGen> for RandomString {
}
}

impl From<Truncated> for RandomString {
fn from(trunc: Truncated) -> Self {
Self::Truncated(trunc)
}
}

derive_generator! {
yield Token,
return Result<Value, Error>,
pub enum StringNode {
pub enum
StringNode {
String(Valuize<Tokenizer<RandomString>, String>),
DateTime(Valuize<Tokenizer<RandomDateTime>, ChronoValue>)
}
Expand Down
46 changes: 46 additions & 0 deletions core/src/graph/string/truncated.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::graph::prelude::*;
use crate::graph::{RandomString, StringNode};
use anyhow::Result;

pub struct Truncated {
len: usize,
inner: Box<RandomString>,
}

impl Truncated {
pub(crate) fn new(len: usize, graph: Graph) -> Result<Self> {
match graph {
Graph::String(StringNode::String(random_string)) => {
let unwrapped = random_string.into_inner().into_inner();
Ok(Self {
inner: Box::new(unwrapped),
len,
})
}
_ => Err(anyhow!(
"Truncated generators can only have content of type 'string'."
)),
}
}
}

impl Generator for Truncated {
type Yield = String;
type Return = Result<String, Error>;

fn next<R: Rng>(&mut self, rng: &mut R) -> GeneratorState<Self::Yield, Self::Return> {
match self.inner.next(rng) {
GeneratorState::Yielded(mut s) => {
s.truncate(self.len);
GeneratorState::Yielded(s)
}
GeneratorState::Complete(r) => match r {
Ok(mut s) => {
s.truncate(self.len);
GeneratorState::Complete(Ok(s))
}
Err(e) => GeneratorState::Complete(Err(e)),
},
}
}
}
13 changes: 13 additions & 0 deletions core/src/schema/content/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub enum StringContent {
Categorical(Categorical<String>),
Serialized(SerializedContent),
Uuid(Uuid),
Truncated(TruncatedContent),
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
Expand All @@ -25,6 +26,7 @@ impl StringContent {
Self::Categorical(_) => "categorical",
Self::Serialized(_) => "serialized",
Self::Uuid(_) => "uuid",
Self::Truncated(_) => "truncated",
}
}
}
Expand Down Expand Up @@ -189,6 +191,13 @@ pub enum SerializedContent {
JSON(JsonContent),
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "lowercase")]
pub struct TruncatedContent {
content: Box<Content>,
length: usize,
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct JsonContent {
content: Box<Content>,
Expand Down Expand Up @@ -550,6 +559,10 @@ impl Compile for StringContent {
RandomString::from(Serialized::new_json(inner)).into()
}
},
StringContent::Truncated(trunc) => {
let inner = trunc.content.compile(compiler)?;
RandomString::from(Truncated::new(trunc.length, inner)?).into()
}
StringContent::Uuid(_uuid) => RandomString::from(UuidGen {}).into(),
};
Ok(Graph::String(string_node))
Expand Down
1 change: 1 addition & 0 deletions core/src/schema/inference/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl MergeStrategy<StringContent, String> for OptionalMergeStrategy {
StringContent::Faker(_) => Ok(()),
StringContent::Serialized(_) => Ok(()), // we can probably do better here
StringContent::Uuid(_) => Ok(()),
StringContent::Truncated(_) => Ok(()),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
, release ? true
}:
let
version = "0.4.6";
version = "0.4.7";
darwinBuildInputs =
stdenv.lib.optionals stdenv.hostPlatform.isDarwin (with darwin.apple_sdk.frameworks; [
libiconv
Expand Down
25 changes: 25 additions & 0 deletions docs/docs/content/string.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,31 @@ Accepted values for the `"date_time"` key are objects with the following keys:
}
```

## truncated

The `truncated` generator truncates the output of it's inner generator to a fixed length.

If the output of its inner generator is less than or equal to the length, it is left untouched.

`truncated` has 2 fields,
- `length`: The number of characters to truncate to.
- `content`: The content to be truncated. This can be any Synth generator that yields a String.

#### Example

```json synth
{
"type": "string",
"truncated": {
"content": {
"type": "string",
"pattern": "[a-zA-Z0-9]{0, 255}"
},
"length": 5
}
}
```



## categorical
Expand Down
6 changes: 6 additions & 0 deletions gen/src/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,12 @@ where
}
}

impl<G, F, O> MapComplete<G, F, O> {
pub fn into_inner(self) -> G {
self.inner
}
}

/// This `struct` is constructed by the
/// [`map_yielded`](crate::GeneratorExt::map_yielded) method on
/// [`Generator`](crate::Generator).
Expand Down
10 changes: 10 additions & 0 deletions gen/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,16 @@ where
inner: G,
}

impl<G> Tokenizer<G>
where
G: Generator,
G::Yield: IntoToken,
{
pub fn into_inner(self) -> G {
self.inner
}
}

impl<G> Generator for Tokenizer<G>
where
G: Generator,
Expand Down
2 changes: 1 addition & 1 deletion synth/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "synth"
version = "0.4.6"
version = "0.4.7"
authors = [
"Damien Broka <[email protected]>",
"Christos Hadjiaslanis <[email protected]>"
Expand Down

0 comments on commit 65b6772

Please sign in to comment.