Categorygithub.com/apicat/datagen
modulepackage
0.1.1
Repository: https://github.com/apicat/datagen.git
Documentation: pkg.go.dev

# README

datagen

模拟数据生成 可以用作虚假数据生成,使用场景如mock,或者生成数据库测试数据等场景

特性

  1. 国际化 目前仅支持英文和中文
  2. 支持通过正则表达式生成数据
  3. 支持jsonschema生成数据

常用functions

已支持函数

从字符串表达生成数据

字符串格式

funcName(loc...)|args...

  • funcName 函数名
  • loc 语言信息(非必须) 比如en,zh 默认en
  • args 参数(非必须) args默认使用,隔开支持多参数,如果参数包含,请使用双引号或单引号包裹,强制指定未整体字符串
eg:

regexp|\d{4}
numberpattern|###-####
float|10,20
word(zh)

所有字符串规则

jsonschema 生成数据

默认使用jsonschema内不的format,maxitems等属性生成,如果存在x-datagen字段 则会使用字符串表达式生成

x-datagen字段 支持自定义如 x-apicat-mock


{
    "type":"object",
    "properties":{
        "children":{
            "type":"array",
            "items":{
                "type":"string",
                "x-datagen":"uuid"
            }
        }
    }
}


JSONSchemaGen(jsonschemaString, &GenOption{DatagenKey: "x-datagen"})

struct 生成数据

type T struct {
    values map[string]string
    uid    string `datagen:"uuid"`
    info   struct {
        name    string `datagen:"name"`
        age     int    `datagen:"integer|10,40"`
        address string `datagen:"address"`
    }
}
var testt T
StructGen(testt, &GenOption{DatagenKey: "datagen"})

添加自己专用的函数

RegisterFunction("customgen", func(p datagen.Param)any{
    return "nihao"+p.Args.At(0)
})


CallFunction("customgen|a")

# Functions

Address 生成一个详细地址.
No description provided by the author
No description provided by the author
City 生成一个城市.
Color 生成web css色值.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Float v [min,max,fixed] n = len(v) n == 0 :random n == 1 :max == min.
No description provided by the author
No description provided by the author
No description provided by the author
ImageData image base64 data len(n) == 0 default width/heght = imageDefaultSize len(n) == 1 widht == height == n[0] len(n) >= 2 width = n[0] height = n[1].
ImageURL url image len(n) == 0 default width/heght = imageDefaultSize len(n) == 1 widht == height == n[0] len(n) >= 2 width = n[0] height = n[1].
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
Latitude 生成维度.
Longitude 生成经度.
LongitudeAndLatitude 生成经纬度坐标.
No description provided by the author
NumberPattern 数字字符串模式 使用#替代数字 example: `(###)##-###` -> `(219)10-231`.
OneOf 从数组中随机取一个值.
ParseFunction 解析function eg:funcname(locales...)|args...
No description provided by the author
ProvinceorState 生成一个省/邦/州.
ProvinceorStateCity 省市.
Regexp 通过正则表达式生成数据.
No description provided by the author
No description provided by the author
Street 生成一个街道.
String 生成字符串 mode.
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
ZipCode 生成邮编.

# Structs

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Type aliases

No description provided by the author
No description provided by the author
No description provided by the author