-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from bavix/yaml
[2.0] Getting started with yaml
- Loading branch information
Showing
20 changed files
with
588 additions
and
757 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ gripmock | |
.DS_Store | ||
protogen/* | ||
!protogen/go.mod | ||
!protogen/empty.go | ||
!protogen/example/ | ||
temp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.