/
utility.go
42 lines (34 loc) · 947 Bytes
/
utility.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package jshapi
import (
"log"
"github.com/derekdowling/go-json-spec-handler"
)
const testType = "test"
var testObjAttrs = map[string]string{
"foo": "bar",
}
// NewMockResource builds a mock API endpoint that can perform basic CRUD actions:
//
// GET /types
// POST /types
// GET /types/:id
// DELETE /types/:id
// PATCH /types/:id
//
// Will return objects and lists based upon the sampleObject that is specified here
// in the constructor.
func NewMockResource(resourceType string, listCount int, sampleObject interface{}) *Resource {
mock := &MockStorage{
ResourceType: resourceType,
ResourceAttributes: sampleObject,
ListCount: listCount,
}
return NewCRUDResource(resourceType, mock)
}
func sampleObject(id string, resourceType string, sampleObject interface{}) *jsh.Object {
object, err := jsh.NewObject(id, resourceType, sampleObject)
if err != nil {
log.Fatal(err.Error())
}
return object
}