Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ratio for Option<T> WIP #54

Closed
wants to merge 1 commit into from
Closed

feat: ratio for Option<T> WIP #54

wants to merge 1 commit into from

Conversation

tujh2
Copy link

@tujh2 tujh2 commented Apr 10, 2021

Option<T> is something like Boolean (it has Some(T) value or None). So I'd like to use ratio for this. But now we have a 50:50 ratio (look at percentage on 1 screenshot).
I have a code something like this:

    let mut rng = rand::thread_rng();
    let file = File::create("data.csv")?;
    let mut wtr = csv::Writer::from_writer(file);
    let mut i = 0;
    let rows = rng.gen_range(10000..100000);
    loop {
        wtr.serialize(Faker.fake::<Foo>())?;
        if i == rows {
            break;
        }
        i += 1;
    }
    wtr.flush()?;

Current behavior - we have 50:50 ration:
pic1

After this commit:
pic2

P.S. Of course to start it working we should add #[dummy(faker = 'Opt((), 98)') to our struct's members like this

    #[dummy(faker = "Opt(0..2000, 93)")]
    order_id: Option<u64>,
    #[dummy(faker = "Opt(0..2000, 81)")]
    make_id: Option<u64>,
    #[dummy(faker = "Opt(0..2000, 98)")]
    time_id: Option<u64>,
    #[dummy(faker = "Opt(0..2000, 90)")]
    this_id: Option<u64>,
    #[dummy(faker = "Opt(0.0..2000.0, 78)")]
    my_float: Option<f64>,

@tujh2 tujh2 changed the title feat: ratio for Option<T> feat: ratio for Option<T> WIP Apr 10, 2021
@cksac
Copy link
Owner

cksac commented May 3, 2023

this is a breaking change, as now there has impl for Option, which also disallow other faker struct to impl Dummy for Option

impl<T, U> Dummy<U> for Option<T>
where
    T: Dummy<U>,
{
    fn dummy_with_rng<R: Rng + ?Sized>(config: &U, rng: &mut R) -> Self {
        if Faker.fake_with_rng::<bool, _>(rng) {
            Some(T::dummy_with_rng(config, rng))
        } else {
            None
        }
    }
}

@cksac
Copy link
Owner

cksac commented May 3, 2023

released 2.6.0, now you can use below

use fake::{Dummy, Fake, Faker, Opt, Optional};

#[derive(Debug, Dummy)]
pub struct Order {
    #[dummy(faker = "0..200")]
    pub a: Option<u64>,

    #[dummy(faker = "Opt(0..200, 100)", from = "Optional<u64>")]
    pub always_some: Option<u64>,

    #[dummy(expr = "Some((0..200).fake())")]
    pub always_some_v2: Option<u64>,

    #[dummy(faker = "Opt(0..200, 0)", from = "Optional<u64>")]
    pub always_none: Option<u64>,

    #[dummy(expr="None")]
    pub always_none_v2: Option<u64>,

    #[dummy(faker = "0..200")]
    pub c: Option<Option<u64>>,

    #[dummy(expr = "Opt(Opt(0..200, 50), 50).fake::<Optional<Optional<u64>>>().0.map(|v| v.0)")]
    pub d: Option<Option<u64>>,
}

fn main() {
    let opt: Optional<usize> = Opt(0..100, 100).fake();
    println!("{:?}", opt);

    let opt: Optional<Optional<usize>> = Opt(Opt(0..200, 50), 20).fake();
    println!("{:?}", opt.0.map(|v| v.0));

    let opt: Option<usize> = Opt(0..100, 100).fake::<Optional<usize>>().into();
    println!("{:?}", opt);

    let o: Order = Faker.fake();
    println!("{:?}", o);
}

@cksac cksac closed this May 6, 2023
@tujh2 tujh2 deleted the patch-1 branch May 7, 2023 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants