Skip to content

Commit

Permalink
feat: uuid generator
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 09cbc75723f4d2081528df7475bf63ec95f8190e
  • Loading branch information
christos-h authored and oq-bot committed Apr 22, 2021
1 parent 70e9a25 commit 5c9ec6f
Show file tree
Hide file tree
Showing 12 changed files with 55 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.0"
version = "0.4.1"
authors = [
"Damien Broka <[email protected]>",
"Christos Hadjiaslanis <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion core/src/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub mod null;
pub use null::NullNode;

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

pub mod number;
pub use number::{Incrementing, NumberNode, RandomF64, RandomI64, RandomU64, UniformRangeStep};
Expand Down
9 changes: 9 additions & 0 deletions core/src/graph/string/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ pub use date_time::RandomDateTime;

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

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

Expand All @@ -19,6 +21,7 @@ derive_generator! {
Faker(TryOnce<RandFaker>),
Serialized(TryOnce<Serialized>)
Categorical(OnceInfallible<Random<String, Categorical<String>>>)
Uuid(OnceInfallible<UuidGen>)
}
}

Expand Down Expand Up @@ -46,6 +49,12 @@ impl From<Categorical<String>> for RandomString {
}
}

impl From<UuidGen> for RandomString {
fn from(uuid: UuidGen) -> Self {
Self::Uuid(uuid.infallible().try_once())
}
}

derive_generator! {
yield Token,
return Result<Value, Error>,
Expand Down
15 changes: 15 additions & 0 deletions core/src/graph/string/uuid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use crate::graph::prelude::{Generator, GeneratorState, Rng};
use synth_gen::Never;
use uuid::Uuid;

pub struct UuidGen {}

impl Generator for UuidGen {
type Yield = String;
type Return = Never;

fn next<R: Rng>(&mut self, rng: &mut R) -> GeneratorState<Self::Yield, Self::Return> {
let uuid = Uuid::from_u128(rng.gen());
GeneratorState::Yielded(uuid.to_hyphenated().to_string())
}
}
2 changes: 1 addition & 1 deletion core/src/schema/content/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub use number::{number_content, NumberContent, NumberContentKind, NumberKindExt
mod string;
pub use string::{
ChronoValue, ChronoValueFormatter, ChronoValueType, DateTimeContent, FakerContent,
FakerContentArgument, RegexContent, StringContent,
FakerContentArgument, RegexContent, StringContent, Uuid,
};

mod array;
Expand Down
7 changes: 6 additions & 1 deletion core/src/schema/content/string.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::prelude::*;

use super::Categorical;

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
Expand All @@ -11,8 +10,12 @@ pub enum StringContent {
DateTime(DateTimeContent),
Categorical(Categorical<String>),
Serialized(SerializedContent),
Uuid(Uuid),
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct Uuid;

impl StringContent {
pub fn kind(&self) -> &str {
match self {
Expand All @@ -21,6 +24,7 @@ impl StringContent {
Self::DateTime(date_time) => date_time.kind(),
Self::Categorical(_) => "categorical",
Self::Serialized(_) => "serialized",
Self::Uuid(_) => "uuid",
}
}
}
Expand Down Expand Up @@ -552,6 +556,7 @@ impl Compile for StringContent {
RandomString::from(Serialized::new_json(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 @@ -111,6 +111,7 @@ impl MergeStrategy<StringContent, String> for OptionalMergeStrategy {
StringContent::DateTime(date_time_content) => self.try_merge(date_time_content, value),
StringContent::Faker(_) => Ok(()),
StringContent::Serialized(_) => Ok(()), // we can probably do better here
StringContent::Uuid(_) => 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.3.4";
version = "0.4.1";
darwinBuildInputs =
stdenv.lib.optionals stdenv.hostPlatform.isDarwin (with darwin.apple_sdk.frameworks; [
libiconv
Expand Down
15 changes: 15 additions & 0 deletions docs/docs/content/string.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ String values generated by a specified regular expression in the `pattern` key.
}
```

## uuid

`uuid` generates hyphenated [UUIDs](https://en.wikipedia.org/wiki/Universally_unique_identifier).

This generator has no parameters.

#### Example

```json synth
{
"type": "string",
"uuid": {}
}
```

## faker

Synth integrates with the Python [Faker library][faker]. To generate a string using `Faker`, use the `"faker": {...}`
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.3.4"
version = "0.4.1"
authors = [
"Damien Broka <[email protected]>",
"Christos Hadjiaslanis <[email protected]>"
Expand Down
3 changes: 2 additions & 1 deletion synth/src/cli/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::convert::TryFrom;

use crate::sampler::Sampler;
use std::str::FromStr;
use synth_core::graph::prelude::VariantContent;
use synth_core::graph::prelude::{Uuid, VariantContent};
use synth_core::schema::number_content::*;
use synth_core::schema::{
ArrayContent, BoolContent, ChronoValueType, DateTimeContent, FieldContent, FieldRef, Id,
Expand Down Expand Up @@ -484,6 +484,7 @@ impl From<ColumnInfo> for FieldContent {
begin: None,
end: None,
})),
"uuid" => Content::String(StringContent::Uuid(Uuid)),
_ => unimplemented!("We haven't implemented a converter for {}", column.udt_name),
};

Expand Down

0 comments on commit 5c9ec6f

Please sign in to comment.