Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

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

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

a mock tool is based on struct tag

install

go get github.com/pigfu/gomock

recommend

  1. if the struct generated by the proto file, you can useprotoc-go-inject-tag library.
  2. for testers,you can do API fuzzy testing by this library.
  3. 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.
  4. for back-end developers, you can quickly build parameters for API debugging.

mock function

function key value must match the regular expression '[a-z_]+' .

FunctionDescription
stringmock string data, only english string
integermock integer data
decimalmock decimal data
mobile_phonemock mobile phone
emailmock email
addrmock addr, only china
timemock time

mock tag

tag key value must match the regular expression '[a-z_]+' .

TagDescription
keyappoint mock function
eqfor integer, decimal, string, eq will ensure that the value is equal to the parameter given. for slice, it will ensure the length
ltfor integer, decimal, lt will ensure the maximum value. for string, slice, it will ensure the maximum length
ltefor integer, decimal, lt will ensure the maximum value. for string, slice, it will ensure the maximum length
gtfor integer, decimal, lt will ensure the minimum value. for string, slice, it will ensure the minimum length
gtefor integer, decimal, lt will ensure the minimum value. for string, slice, it will ensure the minimum length
optionsspecify optional data, like options=2 5 8
weightsspecify the weight of optional data, default weights=1 1 1
intoappoint to mock struct or slice field,previous modifications to slice, subsequent modifications to internal fields of slice,eg: into=1
skipskip the field,eg: skip=1
addronly support addr mock function, any one or any combination of optional province city county
timeonly 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)
regfor integer, decimal, string, generate content based on regular expression

example

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

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp