-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathpaths.go
66 lines (58 loc) Β· 2.16 KB
/
paths.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
package codegen
import (
"fmt"
"path/filepath"
"goa.design/goa/codegen"
"goa.design/goa/expr"
)
// PathFiles returns the service path files.
func PathFiles(root *expr.RootExpr) []*codegen.File {
fw := make([]*codegen.File, 2*len(root.API.HTTP.Services))
for i := 0; i < len(root.API.HTTP.Services); i++ {
fw[i*2] = serverPath(root.API.HTTP.Services[i])
fw[i*2+1] = clientPath(root.API.HTTP.Services[i])
}
return fw
}
// serverPath returns the server file containing the request path constructors
// for the given service.
func serverPath(svc *expr.HTTPServiceExpr) *codegen.File {
sd := HTTPServices.Get(svc.Name())
path := filepath.Join(codegen.Gendir, "http", codegen.SnakeCase(sd.Service.VarName), "server", "paths.go")
return &codegen.File{Path: path, SectionTemplates: pathSections(svc, "server")}
}
// clientPath returns the client file containing the request path constructors
// for the given service.
func clientPath(svc *expr.HTTPServiceExpr) *codegen.File {
sd := HTTPServices.Get(svc.Name())
path := filepath.Join(codegen.Gendir, "http", codegen.SnakeCase(sd.Service.VarName), "client", "paths.go")
return &codegen.File{Path: path, SectionTemplates: pathSections(svc, "client")}
}
// pathSections returns the sections of the file of the pkg package that
// contains the request path constructors for the given service.
func pathSections(svc *expr.HTTPServiceExpr, pkg string) []*codegen.SectionTemplate {
title := fmt.Sprintf("HTTP request path constructors for the %s service.", svc.Name())
sections := []*codegen.SectionTemplate{
codegen.Header(title, pkg, []*codegen.ImportSpec{
{Path: "fmt"},
{Path: "net/url"},
{Path: "strconv"},
{Path: "strings"},
}),
}
sdata := HTTPServices.Get(svc.Name())
for _, e := range svc.HTTPEndpoints {
sections = append(sections, &codegen.SectionTemplate{
Name: "path",
Source: pathT,
Data: sdata.Endpoint(e.Name()),
})
}
return sections
}
// input: EndpointData
const pathT = `{{ range .Routes }}// {{ .PathInit.Description }}
func {{ .PathInit.Name }}({{ range .PathInit.ServerArgs }}{{ .Name }} {{ .TypeRef }}, {{ end }}) {{ .PathInit.ReturnTypeRef }} {
{{- .PathInit.ServerCode }}
}
{{ end }}`