Skip to content

Commit

Permalink
Merge pull request #14 from bavix/yaml
Browse files Browse the repository at this point in the history
[2.0] Getting started with yaml
  • Loading branch information
rez1dent3 authored Aug 31, 2023
2 parents b4fecd7 + 598562c commit f599ac4
Show file tree
Hide file tree
Showing 20 changed files with 588 additions and 757 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ gripmock
.DS_Store
protogen/*
!protogen/go.mod
!protogen/empty.go
!protogen/example/
temp
26 changes: 26 additions & 0 deletions example/simple/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,38 @@ func main() {
if err != nil {
log.Fatalf("error from grpc: %v", err)
}
if r.ReturnCode != 1 {
log.Fatalf("grpc server returned code: %d, expected code: %d", r.ReturnCode, 1)
}
log.Printf("Greeting: %s (return code %d)", r.Message, r.ReturnCode)

name = "world"
r, err = c.SayHello(context.Background(), &pb.Request{Name: name})
if err != nil {
log.Fatalf("error from grpc: %v", err)
}
if r.ReturnCode != 1 {
log.Fatalf("grpc server returned code: %d, expected code: %d", r.ReturnCode, 1)
}
log.Printf("Greeting: %s (return code %d)", r.Message, r.ReturnCode)

name = "simple2"
r, err = c.SayHello(context.Background(), &pb.Request{Name: name})
if err != nil {
log.Fatalf("error from grpc: %v", err)
}
if r.ReturnCode != 2 {
log.Fatalf("grpc server returned code: %d, expected code: %d", r.ReturnCode, 2)
}
log.Printf("Greeting: %s (return code %d)", r.Message, r.ReturnCode)

name = "simple3"
r, err = c.SayHello(context.Background(), &pb.Request{Name: name})
if err != nil {
log.Fatalf("error from grpc: %v", err)
}
if r.ReturnCode != 3 {
log.Fatalf("grpc server returned code: %d, expected code: %d", r.ReturnCode, 3)
}
log.Printf("Greeting: %s (return code %d)", r.Message, r.ReturnCode)
}
9 changes: 9 additions & 0 deletions example/simple/stub/simple2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- service: Gripmock
method: SayHello
input:
equals:
name: simple2
output:
data:
message: Hello Simple2
return_code: 2
9 changes: 9 additions & 0 deletions example/simple/stub/simple3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
- service: Gripmock
method: SayHello
input:
equals:
name: simple3
output:
data:
message: Hello Simple3
return_code: 3
16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ module github.com/tokopedia/gripmock
go 1.21

require (
github.com/go-chi/chi v4.1.2+incompatible
github.com/goccy/go-yaml v1.11.0
github.com/google/uuid v1.3.0
github.com/gorilla/mux v1.8.0
github.com/lithammer/fuzzysearch v1.1.8
github.com/stretchr/testify v1.8.4
github.com/tokopedia/gripmock/protogen v0.0.0
github.com/tokopedia/gripmock/protogen/example v0.0.0
golang.org/x/text v0.12.0
google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.31.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.10.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tokopedia/gripmock/protogen v0.0.0 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mattn/go-isatty v0.0.12 // indirect
golang.org/x/net v0.14.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

// this is for generated server to be able to run
Expand Down
44 changes: 26 additions & 18 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-chi/chi v4.1.2+incompatible h1:fGFk2Gmi/YKXk0OmGfBh0WgmN3XB8lVnEyNz34tQRec=
github.com/go-chi/chi v4.1.2+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ=
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
github.com/go-playground/locales v0.13.0/go.mod h1:taPMhCMXrRLJO55olJkUXHZBHCxTMfnGwq/HNwmWNS8=
github.com/go-playground/universal-translator v0.17.0 h1:icxd5fm+REJzpZx7ZfpaD876Lmtgy7VtROAbHHXk8no=
github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+Scu5vgOQjsIJAF8j9muTVoKLVtA=
github.com/go-playground/validator/v10 v10.4.1 h1:pH2c5ADXtd66mxoE0Zm9SUhxE20r7aM3F26W0hOn+GE=
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
github.com/goccy/go-yaml v1.11.0 h1:n7Z+zx8S9f9KgzG6KtQKf+kwqXZlLNR2F6018Dgau54=
github.com/goccy/go-yaml v1.11.0/go.mod h1:H+mJrWtjPTJAHvRbV09MCK9xYwODM+wRTVFFTWckfng=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/leodido/go-urn v1.2.0 h1:hpXL4XnriNwQ/ABnpepYM/1vCLWNDfUNts8dX3xTG6Y=
github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgxpxOKII=
github.com/lithammer/fuzzysearch v1.1.8 h1:/HIuJnjHuXS8bKaiTMeeDlW2/AyIWk2brx1V8LFgLN4=
github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ5ajtkr5xPLts4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.12.0 h1:tFM/ta59kqch6LlvYnPa0yx5a83cL2nHflFhYKvv9Yk=
golang.org/x/crypto v0.12.0/go.mod h1:NF0Gs7EO5K4qLn+Ylc+fih8BSTeIjAP05siRnAh98yw=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand All @@ -34,6 +43,8 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -57,6 +68,8 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d h1:uvYuEyMHKNt+lT4K3bN6fGswmK8qSvcreM3BwjDh+y4=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
google.golang.org/grpc v1.57.0 h1:kfzNeI/klCGD2YPMUlaGNT3pxvYfga7smW3Vth8Zsiw=
Expand All @@ -65,8 +78,3 @@ google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp0
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1 change: 1 addition & 0 deletions gripmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"strings"
"syscall"

_ "github.com/tokopedia/gripmock/protogen"
"github.com/tokopedia/gripmock/stub"
)

Expand Down
63 changes: 63 additions & 0 deletions pkg/sdk/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package sdk

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)

type StubApiClient struct {
url string
httpClient *http.Client
}

func NewStubApiClient(url string, client *http.Client) *StubApiClient {
return &StubApiClient{url: url, httpClient: client}
}

type Payload struct {
Service string `json:"service"`
Method string `json:"method"`
Data interface{} `json:"data"`
}

type Response struct {
Data interface{} `json:"data"`
Error string `json:"error"`
}

func (c *StubApiClient) Search(payload Payload) (any, error) {
postBody, err := json.Marshal(payload)
if err != nil {
return nil, err
}

resp, err := c.httpClient.Post(c.url+"/api/stubs/search", "application/json", bytes.NewReader(postBody))
if err != nil {
return nil, err
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)

return nil, fmt.Errorf(string(body))
}

result := new(Response)
decoder := json.NewDecoder(resp.Body)
decoder.UseNumber()

if err := decoder.Decode(result); err != nil {
return nil, err
}

if result.Error != "" {
return nil, fmt.Errorf(result.Error)
}

return result.Data, nil
}
131 changes: 131 additions & 0 deletions pkg/storage/stubs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package storage

import (
"errors"
"github.com/google/uuid"
"sync"
)

var ErrServiceNotFound = errors.New("service not found")
var ErrMethodNotFound = errors.New("method not found")

type Stub struct {
ID *uuid.UUID `json:"id,omitempty"`
Service string `json:"service"`
Method string `json:"method"`
Input Input `json:"input"`
Output Output `json:"output"`
}

type Input struct {
Equals map[string]interface{} `json:"equals"`
Contains map[string]interface{} `json:"contains"`
Matches map[string]interface{} `json:"matches"`
}

type Output struct {
Data map[string]interface{} `json:"data"`
Error string `json:"error"`
}

type storage struct {
ID uuid.UUID
Input Input
Output Output
}

type StubStorage struct {
mu sync.RWMutex
items map[string]map[string][]storage
total uint64
}

func New() *StubStorage {
return &StubStorage{
items: make(map[string]map[string][]storage),
}
}

func (r *StubStorage) Add(stubs ...*Stub) []uuid.UUID {
r.mu.Lock()
defer r.mu.Unlock()

result := make([]uuid.UUID, 0, len(stubs))

for _, stub := range stubs {
if _, ok := r.items[stub.Service]; !ok {
r.items[stub.Service] = make(map[string][]storage, 1)
}

r.items[stub.Service][stub.Method] = append(r.items[stub.Service][stub.Method], storage{
ID: stub.GetID(),
Input: stub.Input,
Output: stub.Output,
})

result = append(result, stub.GetID())

r.total++
}

return result
}

func (r *StubStorage) Delete(_ ...uuid.UUID) {
r.total-- // fixme
}

func (r *StubStorage) Purge() {
r.mu.Lock()
defer r.mu.Unlock()

r.items = map[string]map[string][]storage{}
r.total = 0
}

func (r *StubStorage) ItemsBy(service, method string) ([]storage, error) {
r.mu.RLock()
defer r.mu.RUnlock()

if _, ok := r.items[service]; !ok {
return nil, ErrServiceNotFound
}

if _, ok := r.items[service][method]; !ok {
return nil, ErrMethodNotFound
}

return r.items[service][method], nil
}

func (r *StubStorage) Stubs() []Stub {
r.mu.RLock()
defer r.mu.RUnlock()

results := make([]Stub, 0, r.total)

for service, methods := range r.items {
for method, storages := range methods {
for _, datum := range storages {
results = append(results, Stub{
ID: &datum.ID,
Service: service,
Method: method,
Input: datum.Input,
Output: datum.Output,
})
}
}
}

return results
}

func (s *Stub) GetID() uuid.UUID {
if s.ID == nil {
id := uuid.New()
s.ID = &id
}

return *s.ID
}
Loading

0 comments on commit f599ac4

Please sign in to comment.