- Notifications
You must be signed in to change notification settings - Fork0
a mock tool base on go struct tag, support regexp(regular expression)
License
NotificationsYou must be signed in to change notification settings
pigfu/gomock
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
a mock tool is based on struct tag
go get github.com/pigfu/gomock
- if the struct generated by the proto file, you can useprotoc-go-inject-tag library.
- for testers,you can do API fuzzy testing by this library.
- for front-end developers,you can quickly build mock services for front-end API debugging, interrupting the development mode of front-end waiting for back-end API.
- for back-end developers, you can quickly build parameters for API debugging.
function key value must match the regular expression '[a-z_]+' .
Function | Description |
---|---|
string | mock string data, only english string |
integer | mock integer data |
decimal | mock decimal data |
mobile_phone | mock mobile phone |
mock email | |
addr | mock addr, only china |
time | mock time |
tag key value must match the regular expression '[a-z_]+' .
Tag | Description |
---|---|
key | appoint mock function |
eq | for integer, decimal, string, eq will ensure that the value is equal to the parameter given. for slice, it will ensure the length |
lt | for integer, decimal, lt will ensure the maximum value. for string, slice, it will ensure the maximum length |
lte | for integer, decimal, lt will ensure the maximum value. for string, slice, it will ensure the maximum length |
gt | for integer, decimal, lt will ensure the minimum value. for string, slice, it will ensure the minimum length |
gte | for integer, decimal, lt will ensure the minimum value. for string, slice, it will ensure the minimum length |
options | specify optional data, like options=2 5 8 |
weights | specify the weight of optional data, default weights=1 1 1 |
into | appoint to mock struct or slice field,previous modifications to slice, subsequent modifications to internal fields of slice,eg: into=1 |
skip | skip the field,eg: skip=1 |
addr | only support addr mock function, any one or any combination of optional province city county |
time | only support time mock function, supports timestamps at the second (ts_s) and millisecond (ts_ms) level or any time format (eg:2006/01/02 15:04:05) |
reg | for integer, decimal, string, generate content based on regular expression |
package mainimport (."github.com/pigfu/gomock""encoding/json""fmt")typeHobbyTypeint32typeHobbystruct {Idint64`json:"id" mock:"key=integer,eq=5"`HTHobbyType`json:"ht" mock:"key=integer,options=1 2 3"`Namestring`json:"name" mock:"key=string,gte=4,lte=23"`Pros []string`json:"pros" mock:"gte=1,lte=5,into=1,key=string,gte=3,lte=6"`}typeManstruct {Idint64`json:"id" mock:"key=integer,eq=5"`Ids []int64`json:"ids" mock:"eq=2,into=1,key=integer,gte=23,lte=55"`Namestring`json:"name" mock:"key=string,gte=6,lte=10"`Age*int8`json:"age" mock:"key=integer,gte=23"`Hobby*Hobby`json:"hobby,omitempty" mock:"into=1"`Hobbies []*Hobby`json:"hobbies,omitempty" mock:"eq=1,into=1"`Optionint32`json:"option,omitempty" mock:"key=integer,options=2 3 4 5,weights=10 5 2 2"`Decimalfloat64`json:"decimal,omitempty" mock:"key=decimal,gte=-23.235,lte=5.580"`MobilePhonestring`json:"mobile_phone,omitempty" mock:"key=mobile_phone"`Emailstring`json:"email,omitempty" mock:"key=email"`Addressstring`json:"address,omitempty" mock:"key=addr,addr=city county"`CreateTimeint64`json:"create_time,omitempty" mock:"key=time,time=ts_ms"`UpdateTimestring`json:"update_time,omitempty" mock:"key=time,time=2006-01-02 15:04:05"`RegDecimalfloat64`json:"reg_decimal,omitempty" mock:"key=decimal,reg=[1-9]{3}\\.\\d{1,5}"`RegNamestring`json:"reg_name,omitempty" mock:"key=string,reg=[\u4e00-\u9fa5]{6,}"`}funcmain() {mock:=New()man:=&Man{}err:=mock.Struct(man)iferr!=nil {fmt.Println(err)return}b,_:=json.Marshal(man)fmt.Println("success: ",string(b))}
You will get the following:
{"id":5,"ids":[33,25],"name":"X5K8LD3F","age":49,"hobby":{"id":5,"ht":1,"name":"8fzH9yJQ","pros":["5XDTkS","mQLJ","G9T0"]},"hobbies":[{"id":5,"ht":2,"name":"q97NuPswO0I6VZ","pros":["MGbx","ZEi7L4","xOwM67","zpl","LYzBo0"]}],"decimal":0.159,"mobile_phone":"18909537318","email":"08A7z7PMZS@hotmail.com","address":"天津市 西青区","create_time":1699695881544,"update_time":"2023-11-11 17:44:41","reg_decimal":727.5378,"reg_name":"泗傻貕贵耎鎦鶓櫫"}
Of course, you can customize the mock method what you need
package mainimport ("context""encoding/json""errors""fmt"."github.com/pigfu/gomock""reflect")typeHobbystruct {Namestring`json:"name" mock:"key=chinese,chinese_tag=李明"`Nicknamestring`json:"nickname" mock:"key=chinese"`Ids []int64`json:"ids" mock:"key=ids"`}funcmain() {mock:=New()mock.RegisterMock("chinese",func(_ context.Context,flFieldLevel) (reflect.Value,error) {iffl.GetKind()!=reflect.String {returnreflect.New(fl.GetType()),errors.New("only support the type string")}value:=fl.GetTags().Key("chinese_tag").GetStr()ifvalue!="" {returnreflect.ValueOf(value),nil}returnreflect.ValueOf("你好世界!!!"),nil})mock.RegisterTag("chinese_tag",func(_ reflect.Type,key,valuestring) (TagLevel,error) {return&MockTag{Key:key,Value:value,StrVal:value,},nil})mock.RegisterMock("ids",func(ctx context.Context,flFieldLevel) (reflect.Value,error) {iffl.GetKind()!=reflect.Slice {return reflect.Value{},errors.New("only support the type slice")}ids,_:=ctx.Value("ids").([]int64)iffl.IsPtr() {returnreflect.ValueOf(&ids),nil}returnreflect.ValueOf(ids),nil})hobby:=&Hobby{}mockCtx:=context.WithValue(context.Background(),"ids", []int64{101,201,301,999})err:=mock.StructCtx(mockCtx,hobby)iferr!=nil {fmt.Println(err)return}b,_:=json.Marshal(hobby)fmt.Println("success: ",string(b))}
You will get the following:
{"name":"李明","nickname":"你好世界!!!","ids":[101,201,301,999]}
About
a mock tool base on go struct tag, support regexp(regular expression)
Topics
Resources
License
Stars
Watchers
Forks
Packages0
No packages published