Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ToOne, ToMany, Mutate resource handler helpers
- Loading branch information
Derek Dowling
committed
Dec 13, 2015
1 parent
a69748e
commit cbfed61
Showing
9 changed files
with
347 additions
and
190 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,70 @@ | ||
package jshapi | ||
|
||
import ( | ||
"log" | ||
"strconv" | ||
|
||
"github.com/derekdowling/go-json-spec-handler" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
// MockStorage allows you to mock out APIs really easily, and is also used internally | ||
// for testing the API layer. | ||
type MockStorage struct { | ||
// ResourceType is the name of the resource you are mocking i.e. "user", "comment" | ||
ResourceType string | ||
// ResourceAttributes a sample set of attributes a resource object should have | ||
// used by GET /resources and GET /resources/:id | ||
ResourceAttributes interface{} | ||
// ListCount is the number of sample objects to return in a GET /resources request | ||
ListCount int | ||
} | ||
|
||
// Save assigns a URL of 1 to the object | ||
func (m *MockStorage) Save(ctx context.Context, object *jsh.Object) (*jsh.Object, *jsh.Error) { | ||
object.ID = "1" | ||
return object, nil | ||
} | ||
|
||
// Get returns a resource with ID as specified by the request | ||
func (m *MockStorage) Get(ctx context.Context, id string) (*jsh.Object, *jsh.Error) { | ||
return m.SampleObject(id), nil | ||
} | ||
|
||
// List returns a sample list | ||
func (m *MockStorage) List(ctx context.Context) (jsh.List, *jsh.Error) { | ||
return m.SampleList(m.ListCount), nil | ||
} | ||
|
||
// Update does nothing | ||
func (m *MockStorage) Update(ctx context.Context, object *jsh.Object) (*jsh.Object, *jsh.Error) { | ||
return object, nil | ||
} | ||
|
||
// Delete does nothing | ||
func (m *MockStorage) Delete(ctx context.Context, id string) *jsh.Error { | ||
return nil | ||
} | ||
|
||
// SampleObject builds an object based on provided resource specifications | ||
func (m *MockStorage) SampleObject(id string) *jsh.Object { | ||
object, err := jsh.NewObject(id, m.ResourceType, m.ResourceAttributes) | ||
if err != nil { | ||
log.Fatal(err.Error()) | ||
} | ||
|
||
return object | ||
} | ||
|
||
// SampleList generates a sample list of resources that can be used for/against the | ||
// mock API | ||
func (m *MockStorage) SampleList(length int) jsh.List { | ||
|
||
list := jsh.List{} | ||
|
||
for id := 1; id <= length; id++ { | ||
list = append(list, m.SampleObject(strconv.Itoa(id))) | ||
} | ||
|
||
return list | ||
} |
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,11 @@ | ||
package jshapi | ||
|
||
// Relationship helps define the relationship between two resources | ||
type Relationship string | ||
|
||
const ( | ||
// ToOne signifies a one to one relationship | ||
ToOne Relationship = "One-To-One" | ||
// ToMany signifies a one to many relationship | ||
ToMany Relationship = "One-To-Many" | ||
) |
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
Oops, something went wrong.