generated from bool64/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
69 lines (52 loc) · 1.32 KB
/
handler.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package jsonform
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/swaggest/rest/web"
"github.com/swaggest/usecase"
"github.com/swaggest/usecase/status"
)
// Mount attaches handlers to web service.
func (r *Repository) Mount(s *web.Service, prefix string) {
r.baseURL = prefix
s.Get(prefix+"{name}-schema.json", r.GetSchema())
s.Mount(prefix, http.StripPrefix(prefix, staticServer))
}
type schemaReq struct {
Name schemaName `path:"name"`
}
type schemaName string
func (s schemaName) Enum() []interface{} {
ss := strings.Split(string(s), ",")
enum := make([]interface{}, 0, len(ss))
for _, v := range ss {
enum = append(enum, v)
}
return enum
}
// GetSchema returns JSONForm schema.
func (r *Repository) GetSchema() usecase.Interactor {
in := schemaReq{
Name: schemaName(strings.Join(r.Names(), ",")),
}
u := usecase.NewIOI(in, new(FormSchema), func(ctx context.Context, in, out interface{}) error {
input, ok := in.(schemaReq)
if !ok {
return fmt.Errorf("unexpected input: %T", in)
}
output, ok := out.(*FormSchema)
if !ok {
return fmt.Errorf("unexpected output: %T", out)
}
if fs, found := r.schemasByName[string(input.Name)]; found {
*output = fs
return nil
}
return status.NotFound
})
u.SetTitle("Get JSONForm Schema")
u.SetExpectedErrors(status.NotFound)
return u
}