mock

package module
v0.0.8 Latest Latest
Warning

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

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

README

mock

Go codecov

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

支持的类型有

  • uint8/uint16/uint32/uint64
  • int8/int16/int32/int64
  • slice
  • map
  • time.Time
  • 人名
  • 国家名
  • mac地址
  • 省市区
  • ...更多

一、install

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

type Person struct {
	Name    string
	Age     int
}

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
	Ipv4        string
}
var a ReferenceType
mock.MockData(&a)
all, err := json.Marshal(&a)
//输出如下
// {
//   "Person": {
//     "Name": "f9da822c9d",
//     "Age": 48559177
//   },
//   "Id": "6f310b75-914b-4ff7-8b72-eb613100cba3",
//   "MyType": {
//     "Slice": [
//       1376038752,
//       1708184974,
//       1156005062,
//       1349914762,
//       54481841,
//       1714270649,
//       339267240,
//       566602164,
//       367706443,
//       203982404
//     ],
//     "Map": {
//       "55": "f69a1e57a",
//       "602d445c6": "50761",
//       "a94": "959",
//       "c80287f1": "df72f",
//       "d7c4": "60356c4b2"
//     }
//   },
//   "MyTypeP": {
//     "Slice": [
//       1051242817,
//       866106268,
//       1952326595,
//       884632111,
//       587070158,
//       1781740253,
//       844288137,
//       1888080123
//     ],
//     "Map": {
//       "0296d26f": "b",
//       "1d": "133bb8e30c",
//       "3867": "359557",
//       "97379af": "d9",
//       "a6fd02": "a415b9ab08",
//       "a7c9c": "829c45b6",
//       "c855": "59"
//     }
//   },
//   "CreateTime": "2029-05-07T06:28:15+08:00",
//   "PointerList": [
//     303312144,
//     1937065563
//   ],
//   "Email": "cb76@sina.com",
//   "URL": "http://github.com/antlabs/33b6/12/5024/69/b/da0/f8/65de/a6f6/24f/62/e7ec/64/57/54a7/3c/79d/d/16463/b185/101/f8132/d2b/dae/b5b/4/ae7/97ae/8/3/7a6/f92ec/9d/2392/84/9?3=5",
//   "UserName": "士孙玉珍",
//   "NickName": "卜凤玉",
//   "Country": "Cuba",
//   "HeadPic": "www.1.com",
//   "Ipv4": "4.148.97.94",
//   "Province": "湖北省",
//   "City": "恩施土家族苗族自治州",
//   "District": "利川市"
// }

三、WithXXX各种配置函数

3.1 配置指定字段的数据生成范围WithMinMaxLenByField
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))
}
3.2 配置指定字段的数据源WithContainsFieldSourceString

指定HeadPic字段的,数据源。

var a ReferenceType
image := []string{"image.xxx.com/1.headpic", "image.xxx.com/2.headpic", "image.xxx.com/3.headpic"}
err := mock.MockData(&a, mock.WithContainsFieldSourceString("HeadPic", image))
3.3 设置为英文
mock.WithCountryEn()

3.4 设置数据最大长度WithMaxLen
mock.WithMaxLen()
3.5 设置数据最大长度WithMinLen
mock.WithMaxLen()
3.6 设置数值的最大值WithMax
mock.WithMax()
3.7 设置数值的最大值WithMin
mock.WithMin()
3.8 设置忽略的字段名

字段有时候是由protobuf或者thrift生成,不能直接修改tag,可以使用mock.WithIgnoreFields接口忽略

// 设置忽略的字段名
mock.WithIgnoreFields([]string{"Country", "NickName"})

Documentation

Index

Constants

View Source
const (
	URL      = "url"
	UserName = "username"
	NickName = "nickname"
	ID       = "id"
	Time     = "time"
	Email    = "email"
	Country  = "country"
	Ipv4     = "ipv4"
	// 省
	Province = "province"
	// 市
	City = "city"
	// 区
	District = "district"
)

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 WithContainsFieldSourceString added in v0.0.5

func WithContainsFieldSourceString(field string, source []string) Option

包含field字符串,就会使用source中的数据, 字段比较是string类型

func WithCountryEn added in v0.0.4

func WithCountryEn() Option

设置country为英文

func WithIgnoreFields added in v0.0.6

func WithIgnoreFields(fields ...string) Option

设置忽略的字段名

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的长度

func WithTag added in v0.0.6

func WithTag(tag string) Option

设置tag名 TODO实现

type Options

type Options struct {
	// 指定字段名,指定生成的数据来源
	StringSource map[string][]string
	// 生成的mock数据的最大长度
	MaxLen int
	// 生成的mock数据的最小长度
	MinLen int
	// 生成的mock数据的最大值
	Max int64
	// 生成的mock数据的最小值
	Min int64
	// 指定字段名,生成最大长度
	MinMaxLenByField map[string]MinMax
	// country 用中文还是英文
	CountryChina bool
	// 默认是mock
	TagName string
	// 设置忽略的字段名
	IgnoreFields map[string]bool
	// 省份
	Province string
	// 城市
	City string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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