faker

    六地区与结构化测试数据生成

    Download zip
    Version
    0.4.0
    License
    MIT
    Last updated
    7 days ago
    Downloads
    1

    #可执行 API 示例

    增加可复现的中文假数据 CSV 导出,符合字段引号和 CRLF 转义规则。这些例子调用公开 API,并随 moon test 执行。

    ///|
    test "CSV is deterministic has header and CRLF" {
    let text = @faker.csv(42U, 3)
    assert_eq(text, @faker.csv(42U, 3))
    assert_eq(text.split("\r\n").count(), 5)
    assert_true(text.has_prefix("name,email,address,birth_date,id\r\n\""))
    assert_true(text.contains("@example.test"))
    }

    0.3 新增可配置记录、25 类 provider 和 JSON/JSONL/CSV;词库与地区覆盖仍小,完整边界见 FEATURES.md。

    FakeError

    pub suberror FakeError {
    Invalid(String)
    } derive(
    Debug
    )

    Column

    pub(all) struct Column {
    name : String
    provider : Provider
    unique : Bool
    null_percent : Int
    } derive(Eq,
    Debug
    )

    Column::new

    fn Column::new(name : String, provider : Provider, unique? : Bool, null_percent? : Int) -> Column

    Generator

    pub struct Generator {
    state : UInt
    } derive(
    Debug
    )

    Generator::boolean

    fn Generator::boolean(self : Generator, percent? : Int) -> Bool raise FakeError

    Generator::bothify

    fn Generator::bothify(self : Generator, template : String) -> String raise FakeError

    Generator::choose

    fn[T] Generator::choose(self : Generator, items : Array[T]) -> T raise FakeError

    Generator::company

    fn Generator::company(self : Generator) -> String

    Generator::date

    fn Generator::date(self : Generator, first_year : Int, last_year : Int) -> String raise FakeError

    Generator::date_between

    fn Generator::date_between(self : Generator, first : String, last : String) -> String raise FakeError

    Samples Gregorian calendar days in the inclusive range.

    Generator::decimal

    fn Generator::decimal(self : Generator, min_scaled : Int, max_scaled : Int, places : Int) -> String raise FakeError

    Generator::ean13

    fn Generator::ean13(self : Generator) -> String raise FakeError

    Generator::email

    fn Generator::email(self : Generator) -> String raise FakeError

    Generator::hex_color

    fn Generator::hex_color(self : Generator) -> String raise FakeError

    Generator::integer

    fn Generator::integer(self : Generator, min : Int, max : Int) -> Int raise FakeError

    Generator::ipv4

    fn Generator::ipv4(self : Generator) -> String raise FakeError

    Generator::ipv6

    fn Generator::ipv6(self : Generator) -> String raise FakeError

    Generator::isbn13

    fn Generator::isbn13(self : Generator) -> String raise FakeError

    Checksum-correct 978-prefixed sample, without validating registry allocation.

    Generator::job

    fn Generator::job(self : Generator) -> String

    Generator::localized

    fn Generator::localized(self : Generator, locale : String, provider_name : String) -> String raise FakeError

    Locale-aware attributed provider data; the PRNG is local, not Python MT19937.

    Generator::mac_address

    fn Generator::mac_address(self : Generator) -> String raise FakeError

    Generator::name

    fn Generator::name(self : Generator) -> String

    Generator::new

    fn Generator::new(seed : UInt) -> Generator

    Generator::profile

    fn Generator::profile(self : Generator, index : Int) -> Profile raise FakeError

    Generator::restore

    fn Generator::restore(self : Generator, state : UInt) -> Unit raise FakeError

    Generator::sample

    fn[T] Generator::sample(self : Generator, items : Array[T], count : Int) -> Array[T] raise FakeError

    Sample distinct input positions without changing the source array.

    Generator::sentence

    fn Generator::sentence(self : Generator, words? : Int) -> String raise FakeError

    Generator::snapshot

    fn Generator::snapshot(self : Generator) -> UInt

    PRNG-only checkpoint, not a cryptographic seed or a uniqueness-state snapshot.

    Generator::url

    fn Generator::url(self : Generator) -> String raise FakeError

    Generator::username

    fn Generator::username(self : Generator) -> String raise FakeError

    Generator::uuid4

    fn Generator::uuid4(self : Generator) -> String raise FakeError

    RFC 9562 version/variant layout with deterministic, non-secret test bits.

    Generator::value

    fn Generator::value(self : Generator, provider : Provider, index? : Int) -> Json raise FakeError

    Generate one provider value. Sequence index is explicit and does not consume RNG.

    Generator::weighted_index

    fn Generator::weighted_index(self : Generator, weights : Array[Int]) -> Int raise FakeError

    Generator::word

    fn Generator::word(self : Generator) -> String

    Profile

    pub(all) struct Profile {
    name : String
    email : String
    address : String
    birth_date : String
    id : String
    } derive(Eq,
    Debug
    )

    Provider

    pub(all) enum Provider {
    Name
    Address
    Email
    Username
    Url
    Uuid4
    Ipv4
    Ipv6
    MacAddress
    HexColor
    Company
    Job
    Word
    Sentence(Int)
    Boolean(Int)
    Integer(Int, Int)
    Decimal(Int, Int, Int)
    DateBetween(String, String)
    Ean13
    Isbn13
    Template(String)
    Choice(Array[String])
    WeightedChoice(Array[String], Array[Int])
    Constant(Json)
    Sequence(Int, Int)
    Localized(String, String)
    } derive(Eq, ToJson,
    Debug
    )

    Session

    pub struct Session {
    generator : Generator
    spec : Spec
    unique_paths : Map[String, Bool]
    seen : Map[String, Map[String, Bool]]
    tables : Map[String, Array[Json]]
    index : Int
    unique_count : Int
    unique_text : Int
    }

    Session::bind_table

    fn Session::bind_table(self : Session, name : String, rows : Array[Json]) -> Unit raise FakeError

    Session::checkpoint

    fn Session::checkpoint(self : Session) -> Json

    Includes row index, RNG, unique sets, table data and the schema contract.

    Session::new

    fn Session::new(seed : UInt, spec : Spec) -> Session raise FakeError

    Session::next

    fn Session::next(self : Session) -> Json raise FakeError

    Each row commits atomically. On failure, RNG, position and uniqueness remain unchanged.

    Session::position

    fn Session::position(self : Session) -> Int

    Session::restore

    fn Session::restore(self : Session, checkpoint : Json) -> Unit raise FakeError

    Restore is atomic and requires the same schema. Checkpoints are not credentials.

    Spec

    pub(all) enum Spec {
    Value(Provider)
    Object(Array[(String, Spec)])
    List(Spec, Int, Int)
    Nullable(Spec, Int)
    Unique(Spec)
    Reference(String)
    Foreign(String, String)
    Join(Array[Spec], String)
    } derive(Eq, ToJson,
    Debug
    )

    csv

    fn csv(seed : UInt, count : Int) -> String raise FakeError

    RFC 4180 quoted CSV using the original synthetic dataset provider.

    dataset

    fn dataset(seed : UInt, count : Int) -> Array[Profile] raise FakeError

    ean13_from_body

    fn ean13_from_body(body : String) -> String raise FakeError

    Append the GS1 alternating 1/3 modulo-10 check digit to twelve digits.

    generate_request

    fn generate_request(request : String) -> String raise FakeError

    JSON request with seed/count/columns/format; output is json, jsonl, or CSV.

    locale_methods

    fn locale_methods(locale : String) -> Array[String] raise FakeError

    locale_values

    fn locale_values(locale : String, provider_name : String) -> Array[String] raise FakeError

    A copy of the attributed source values/templates, useful for data inspection.

    locales

    fn locales() -> Array[String]

    open_session

    fn open_session(request : String) -> Session raise FakeError

    Parse a schema/columns session definition, optionally with bound tables and checkpoint.

    parse_spec

    fn parse_spec(value : Json) -> Spec raise FakeError

    records

    fn records(seed : UInt, columns : Array[Column], count : Int) -> Array[Json] raise FakeError

    Flat scalar records. Per-column uniqueness includes null and has bounded retries.

    render_records

    fn render_records(columns : Array[Column], rows : Array[Json], format : String) -> String raise FakeError

    session_request

    fn session_request(input : String) -> String

    Thin stateful JSON bridge for JS hosts. Explicit close releases the session.