datagen

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 16, 2023 License: MIT Imports: 19 Imported by: 5

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")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Address

func Address(local ...string) string

Address 生成一个详细地址

func Boolean

func Boolean(v ...bool) bool

func CallFunction

func CallFunction(fn string) any

func City

func City(local ...string) string

City 生成一个城市

func Color

func Color(typ string) string

Color 生成web css色值

func Date

func Date(format ...string) string

func DateTime

func DateTime(format ...string) string

func Domain

func Domain() string

func Email

func Email() string

func Float

func Float(v ...float64) float64

Float v [min,max,fixed] n = len(v) n == 0 :random n == 1 :max == min

func GetLocale

func GetLocale(lang ...string) *locale

func HTTPCode

func HTTPCode() int64

func HTTPMethod

func HTTPMethod() string

func IPv4

func IPv4() string

func IPv6

func IPv6() string

func ImageData

func ImageData(n ...int) string

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]

func ImageURL

func ImageURL(n ...int) string

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]

func Integer

func Integer(v ...int64) int64

func JSONSchemaGen

func JSONSchemaGen(data []byte, opt *GenOption) (any, error)

func Latitude

func Latitude() float64

Latitude 生成维度

func Longitude

func Longitude() float64

Longitude 生成经度

func LongitudeAndLatitude

func LongitudeAndLatitude() string

LongitudeAndLatitude 生成经纬度坐标

func Now

func Now(format ...string) string

func NumberPattern

func NumberPattern(p string) string

NumberPattern 数字字符串模式 使用#替代数字 example: `(###)##-###` -> `(219)10-231`

func OneOf

func OneOf(v ...any) any

OneOf 从数组中随机取一个值

func ProvinceorState

func ProvinceorState(local ...string) string

ProvinceorState 生成一个省/邦/州

func ProvinceorStateCity added in v0.0.4

func ProvinceorStateCity(local ...string) string

ProvinceorStateCity 省市

func Regexp

func Regexp(regstr string) string

Regexp 通过正则表达式生成数据

func RegisterFunction

func RegisterFunction(name string, handler FuncHandler)

func Seek

func Seek(seed int64)

func Street

func Street(local ...string) string

Street 生成一个街道

func String

func String(model string, n ...int) string

String 生成字符串 mode

func StructGen

func StructGen(v any, opt *GenOption) (any, error)

func Time

func Time(format ...string) string

func Timestamp

func Timestamp() int64

func URL

func URL() string

func UUID

func UUID() string

func ZipCode

func ZipCode(local ...string) string

ZipCode 生成邮编

Types

type Args

type Args []string

func (Args) At

func (a Args) At(i int) string

func (Args) Bool

func (a Args) Bool(i int) bool

func (Args) Float

func (a Args) Float(i int) float64

func (Args) Int

func (a Args) Int(i int) int

func (Args) SliceAny

func (a Args) SliceAny(i ...int) []any

func (Args) SliceBool

func (a Args) SliceBool(i ...int) []bool

func (Args) SliceFloat

func (a Args) SliceFloat(i ...int) []float64

func (Args) SliceInt

func (a Args) SliceInt(i ...int) []int

func (Args) SliceInt64

func (a Args) SliceInt64(i ...int) []int64

func (Args) Sub

func (a Args) Sub(i ...int) Args

type FuncHandler

type FuncHandler func(Param) any

type Function

type Function struct {
	Name string
	Param
}

func ParseFunction

func ParseFunction(function string) Function

ParseFunction 解析function eg:funcname(locales...)|args...

type GenOption

type GenOption struct {
	DatagenKey string
	SkipError  bool
}

type JSchema

type JSchema map[string]json.RawMessage

type Param

type Param struct {
	Loc  []string // locales
	Args Args
}

type PeopleObject

type PeopleObject struct {
	// contains filtered or unexported fields
}

func People

func People(local ...string) *PeopleObject

func (*PeopleObject) FirstName

func (p *PeopleObject) FirstName() string

func (*PeopleObject) Gender

func (p *PeopleObject) Gender() string

func (*PeopleObject) IDCard

func (p *PeopleObject) IDCard() string

IDCard 公民唯一号码 中国:身份证号 美国:社会统一保险号

func (*PeopleObject) LastName

func (p *PeopleObject) LastName() string

func (*PeopleObject) Name

func (p *PeopleObject) Name() string

func (*PeopleObject) Phone

func (p *PeopleObject) Phone() string

Phone 生成电话号码 现在基本都是手机了各国基本也都是手机和座机号码基本规则一致

type TypographyObject

type TypographyObject struct {
	// contains filtered or unexported fields
}

func Typography

func Typography(local ...string) *TypographyObject

func (*TypographyObject) Markdown

func (t *TypographyObject) Markdown() string

func (*TypographyObject) Paragraph

func (t *TypographyObject) Paragraph(n ...int) string

func (*TypographyObject) Phrase

func (t *TypographyObject) Phrase(n ...int) string

func (*TypographyObject) Sentence

func (t *TypographyObject) Sentence(n ...int) string

func (*TypographyObject) Title

func (t *TypographyObject) Title(n ...int) string

func (*TypographyObject) Word

func (t *TypographyObject) Word(n ...int) string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL