mock

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

README

mock

Go codecov

生成mock数据,第一个大版本使用反射填充数据。

一、install

go get github.com/antlabs/mock
二、快速开始
type MyType struct {
	Slice []int
	Map   map[string]string
}

type Person struct {
	Name    string
	Age     int
	Address Address
}

type Address struct {
	City    string
	Country string
}

type ReferenceType struct {
	Id          string
	MyType      MyType
	Person      Person
	MyTypeP     *MyType
	CreateTime  string
	PointerList []*int
	Email       string
	URL         string
	UserName    string
	NickName    string
	Country     string
}
var a ReferenceType
mock.MockData(&a)
all, err := json.Marshal(&a)
//输出如下

// {
//   "Id": "2db4f77a-ada2-47cd-9fa5-2d3d7d759d18",
//   "MyType": {
//     "Slice": [
//       1681352613,
//       20691731,
//       681305396,
//       1674447608,
//       266532997,
//       1292383940
//     ],
//     "Map": {
//       "2": "afcb",
//       "4": "f2",
//       "569": "fd2",
//       "a": "b59b48",
//       "a8792": "cee",
//       "bdaf35": "0d19dfe4",
//       "be2a50b4": "f19b56d8"
//     }
//   },
//   "Person": {
//     "Name": "ac0a61a4",
//     "Age": 337830828,
//     "Address": {
//       "City": "",
//       "Country": "U.S. Outlying Islands"
//     }
//   },
//   "MyTypeP": {
//     "Slice": [
//       1807852194,
//       2141777709,
//       92514607,
//       1286243933
//     ],
//     "Map": {
//       "34": "82ab6bf60",
//       "5fa6d": "b",
//       "636484814d": "",
//       "77": "da",
//       "82": "407e",
//       "85784638": "",
//       "a11b8a68": "00c9403dc",
//       "e28c": "b37424e"
//     }
//   },
//   "CreateTime": "2034-11-29T08:17:35+08:00",
//   "PointerList": [
//     895889511,
//     69415086,
//     952093756,
//     381246645,
//     775471733,
//     121055351,
//     2032148785,
//     697090480
//   ],
//   "Email": "c15a950@qq.com",
//   "URL": "http://github.com/antlabs/a0c/6d/4/d021/297/1bd/d1/b2dc/f25?3e=3",
//   "UserName": "寇舒舒",
//   "NickName": "谏静雯",
//   "Country": "Norway"
// }

三、WithXXX各种配置函数

type Test_MinMaxLenByField struct {
	S     string
	Slice []int
}

// 控制slice的生成长度范围
// 控制slice的生成长度范围
func TestMinMaxLenByField(t *testing.T) {
	e := Test_MinMaxLenByField{}
	mock.MockData(&e, mock.WithMinMaxLenByField("S", 10, 20), mock.WithMinMaxLenByField("Slice", 10, 20))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MockData

func MockData(x any, opts ...Option) error

通过反射的方式,对任意类型的数据进行mock

Types

type MinMax added in v0.0.3

type MinMax struct {
	// 生成的mock数据的最大长度
	MaxLen int
	// 生成的mock数据的最小长度
	MinLen int
}

minLen 和 maxLen 用于指定生成的mock数据的最大长度和最小长度

type Option

type Option func(*Options)

func WithCountryEn added in v0.0.4

func WithCountryEn() Option

设置country为中文

func WithMax

func WithMax(max int64) Option

func WithMaxLen

func WithMaxLen(maxLen int) Option

func WithMin

func WithMin(min int64) Option

func WithMinLen

func WithMinLen(minLen int) Option

func WithMinMaxLenByField added in v0.0.3

func WithMinMaxLenByField(field string, minLen int, maxLen int) Option

指定字段名,指定生成长度的范围, slice的长度和string的长度

type Options

type Options struct {
	// 生成的mock数据的最大长度
	MaxLen int
	// 生成的mock数据的最小长度
	MinLen int
	// 生成的mock数据的最大值
	Max int64
	// 生成的mock数据的最小值
	Min int64
	// 指定字段名,生成最大长度
	MinMaxLenByField map[string]MinMax
	// country 用中文还是英文
	CountryChina bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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