-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathviews.go
130 lines (120 loc) Β· 3.17 KB
/
views.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package service
import (
"path/filepath"
"goa.design/goa/codegen"
"goa.design/goa/expr"
)
type viewedType struct {
// Name is the type name.
Name string
// Views is the view data for all views defined in the type.
Views []*ViewData
}
// ViewsFile returns the views file for the given service which contains
// logic to render result types using the defined views.
func ViewsFile(genpkg string, service *expr.ServiceExpr) *codegen.File {
svc := Services.Get(service.Name)
if len(svc.projectedTypes) == 0 {
return nil
}
path := filepath.Join(codegen.Gendir, codegen.SnakeCase(svc.VarName), "views", "view.go")
var (
sections []*codegen.SectionTemplate
)
{
header := codegen.Header(service.Name+" views", "views",
[]*codegen.ImportSpec{
{Path: "goa.design/goa", Name: "goa"},
{Path: "unicode/utf8"},
})
sections = []*codegen.SectionTemplate{header}
// type definitions
for _, t := range svc.viewedResultTypes {
sections = append(sections, &codegen.SectionTemplate{
Name: "viewed-result-type",
Source: userTypeT,
Data: t.UserTypeData,
})
}
for _, t := range svc.projectedTypes {
sections = append(sections, &codegen.SectionTemplate{
Name: "projected-type",
Source: userTypeT,
Data: t.UserTypeData,
})
}
// generate a map for result types with view name as key and the fields
// rendered in the view as value.
var (
rtdata []*viewedType
seen = make(map[string]struct{})
)
{
for _, t := range svc.viewedResultTypes {
name := t.Views[0].TypeVarName
if _, ok := seen[name]; !ok {
rtdata = append(rtdata, &viewedType{Name: name, Views: t.Views})
seen[name] = struct{}{}
}
}
for _, t := range svc.projectedTypes {
if len(t.Views) == 0 {
continue
}
name := t.Views[0].TypeVarName
if _, ok := seen[name]; !ok {
rtdata = append(rtdata, &viewedType{Name: name, Views: t.Views})
seen[name] = struct{}{}
}
}
}
sections = append(sections, &codegen.SectionTemplate{
Name: "viewed-type-map",
Source: viewedMapT,
Data: map[string]interface{}{
"ViewedTypes": rtdata,
},
})
// validations
for _, t := range svc.viewedResultTypes {
sections = append(sections, &codegen.SectionTemplate{
Name: "validate-viewed-result-type",
Source: validateT,
Data: t.Validate,
})
}
for _, t := range svc.projectedTypes {
for _, v := range t.Validations {
sections = append(sections, &codegen.SectionTemplate{
Name: "validate-projected-type",
Source: validateT,
Data: v,
})
}
}
}
return &codegen.File{Path: path, SectionTemplates: sections}
}
// input: ValidateData
const validateT = `{{ comment .Description }}
func {{ .Name }}(result {{ .Ref }}) (err error) {
{{ .Validate }}
return
}
`
// input: map[string]interface{}{"ViewedTypes": []*viewedType}
const viewedMapT = `var (
{{- range .ViewedTypes }}
{{ printf "%sMap is a map of attribute names in result type %s indexed by view name." .Name .Name | comment }}
{{ .Name }}Map = map[string][]string{
{{- range .Views }}
"{{ .Name }}": []string{
{{- range $n := .Attributes }}
"{{ $n }}",
{{- end }}
},
{{- end }}
}
{{- end }}
)
`