gofakeitを用いたダミーデータ生成

go でダミーデータを生成する機会があり、gofakeitgithub.com を使ったので備忘メモ。

基本的な使い方

READMEの通り。
いろいろなダミーデータが用意されているので、用意されているデータであればメソッドを呼び出すだけで生成できる。

gofakeit.Name()             // Markus Moen
gofakeit.Email()            // alaynawuckert@kozey.biz
gofakeit.Phone()            // (570)245-7485
gofakeit.BS()               // front-end
gofakeit.BeerName()         // Duvel
gofakeit.Color()            // MediumOrchid
gofakeit.Company()          // Moen, Pagac and Wuckert
gofakeit.CreditCardNumber() // 4287271570245748
gofakeit.HackerPhrase()     // Connecting the array won't do anything, we need to generate the haptic COM driver!
gofakeit.JobTitle()         // Director
gofakeit.CurrencyShort()    // USD

データを変更する場合

gofakeitでは基本的に、ライブラリ側で英語で用意されたデータセットの中からランダムで生成される。
このデータは、gofakeit/data パッケージの Data という構造体の中に格納されているので、そこを上書きしてあげることで、データセットを変更できる。

package main

import (
    "fmt"

    "github.com/brianvoe/gofakeit/v6"
    "github.com/brianvoe/gofakeit/v6/data"
)

func main() {
    gofakeit.Seed(1)
    fmt.Println(gofakeit.Name()) // Bart Beatty
    fmt.Println(gofakeit.Name()) // Cordia Jacobi

    data.SetSub("person", "first", []string{"太郎", "二郎", "三郎", "四郎", "五郎"})
    data.SetSub("person", "last", []string{"佐藤", "鈴木", "高橋"})

    fmt.Println(gofakeit.Name()) // 二郎 佐藤
    fmt.Println(gofakeit.Name()) // 太郎 高橋
}

カスタムのデータを生成する場合

AddFuncLookup で、カスタムのデータ生成関数を追加できる。
追加した生成関数を使う際には、Generate という関数を利用できる。

package main

import (
    "fmt"
    "math/rand"

    "github.com/brianvoe/gofakeit/v6"
)

func main() {
    gofakeit.Seed(1)

    customIndustries := []string{
        "IT",
        "製造業",
        "医療",
        "金融",
        "教育",
        "エネルギー",
        "不動産",
    }

    gofakeit.AddFuncLookup("industry", gofakeit.Info{
        Category:    "company",
        Description: "Random industry",
        Example:     "IT",
        Output:      "string",
        Generate: func(r *rand.Rand, m *gofakeit.MapParams, info *gofakeit.Info) (interface{}, error) {
            return customIndustries[r.Intn(len(customIndustries))], nil
        },
    })

    fmt.Println(gofakeit.Generate("{industry}")) // 不動産
}

先の例では Generate を利用して生成したが、gofakeit では Struct という関数で構造体ごとデータを生成できる機能があり、これと組み合わせることでより便利に使うことができる。

Struct を使う際には、あらかじめ構造体にfake というタグで生成したいデータを指定する。 参考
カスタムの生成関数を利用したい場合は、 AddFuncLookup の第一引数で渡していたfunctionNameをタグにつければ良い。

package main

import (
    "fmt"
    "math/rand"

    "github.com/brianvoe/gofakeit/v6"
    "github.com/brianvoe/gofakeit/v6/data"
)

type Company struct {
    Name     string `fake:"{company}"`
    Industry string `fake:"{industry}"`
}

func main() {
    gofakeit.Seed(1)

    companies := []string{"トヨタ自動車", "ソニー", "パナソニック"}
    data.SetSub("company", "name", companies)

    customIndustries := []string{
        "IT",
        "製造業",
        "医療",
        "金融",
        "教育",
        "エネルギー",
        "不動産",
    }

    gofakeit.AddFuncLookup("industry", gofakeit.Info{
        Category:    "company",
        Description: "Random industry",
        Example:     "IT",
        Output:      "string",
        Generate: func(r *rand.Rand, m *gofakeit.MapParams, info *gofakeit.Info) (interface{}, error) {
            return customIndustries[r.Intn(len(customIndustries))], nil
        },
    })

    var company Company
    gofakeit.Struct(&company)
    fmt.Printf("%+v", company)
}

構造体の一部でダミーデータ生成が不要な場合

ゼロ値のままにしておきたい場合は、タグとして `fake:"skip"` をつければ良い。