forked from goadesign/goa
/
publicizer.go
138 lines (127 loc) · 5.07 KB
/
publicizer.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
131
132
133
134
135
136
137
138
package codegen
import (
"fmt"
"strings"
"text/template"
"github.com/goadesign/goa/design"
)
var (
simplePublicizeT *template.Template
recursivePublicizeT *template.Template
objectPublicizeT *template.Template
arrayPublicizeT *template.Template
hashPublicizeT *template.Template
)
func init() {
var err error
fm := template.FuncMap{
"tabs": Tabs,
"goify": Goify,
"gotyperef": GoTypeRef,
"gotypedef": GoTypeDef,
"add": Add,
"publicizer": Publicizer,
"recursivePublicizer": RecursivePublicizer,
}
if simplePublicizeT, err = template.New("simplePublicize").Funcs(fm).Parse(simplePublicizeTmpl); err != nil {
panic(err)
}
if recursivePublicizeT, err = template.New("recursivePublicize").Funcs(fm).Parse(recursivePublicizeTmpl); err != nil {
panic(err)
}
if objectPublicizeT, err = template.New("objectPublicize").Funcs(fm).Parse(objectPublicizeTmpl); err != nil {
panic(err)
}
if arrayPublicizeT, err = template.New("arrPublicize").Funcs(fm).Parse(arrayPublicizeTmpl); err != nil {
panic(err)
}
if hashPublicizeT, err = template.New("hashPublicize").Funcs(fm).Parse(hashPublicizeTmpl); err != nil {
panic(err)
}
}
// RecursivePublicizer produces code that copies fields from the private struct to the
// public struct
func RecursivePublicizer(att *design.AttributeDefinition, source, target string, depth int) string {
var publications []string
if o := att.Type.ToObject(); o != nil {
if ds, ok := att.Type.(design.DataStructure); ok {
att = ds.Definition()
}
o.IterateAttributes(func(n string, catt *design.AttributeDefinition) error {
publication := Publicizer(
catt,
fmt.Sprintf("%s.%s", source, Goify(n, true)),
fmt.Sprintf("%s.%s", target, Goify(n, true)),
catt.Type.IsPrimitive() && !att.IsPrimitivePointer(n),
depth+1,
false,
)
publication = fmt.Sprintf("%sif %s.%s != nil {\n%s\n%s}",
Tabs(depth), source, Goify(n, true), publication, Tabs(depth))
publications = append(publications, publication)
return nil
})
}
return strings.Join(publications, "\n")
}
// Publicizer publicizes a single attribute based on the type.
func Publicizer(att *design.AttributeDefinition, sourceField, targetField string, dereference bool, depth int, init bool) string {
var publication string
data := map[string]interface{}{
"sourceField": sourceField,
"targetField": targetField,
"depth": depth,
"att": att,
"dereference": dereference,
"init": init,
}
switch {
case att.Type.IsPrimitive():
publication = RunTemplate(simplePublicizeT, data)
case att.Type.IsObject():
if _, ok := att.Type.(*design.MediaTypeDefinition); ok {
publication = RunTemplate(recursivePublicizeT, data)
} else if _, ok := att.Type.(*design.UserTypeDefinition); ok {
publication = RunTemplate(recursivePublicizeT, data)
} else {
publication = RunTemplate(objectPublicizeT, data)
}
case att.Type.IsArray():
// If the array element is primitive type, we can simply copy the elements over (i.e) []string
if att.Type.HasAttributes() {
data["elemType"] = att.Type.ToArray().ElemType
publication = RunTemplate(arrayPublicizeT, data)
} else {
publication = RunTemplate(simplePublicizeT, data)
}
case att.Type.IsHash():
if att.Type.HasAttributes() {
h := att.Type.ToHash()
data["keyType"] = h.KeyType
data["elemType"] = h.ElemType
publication = RunTemplate(hashPublicizeT, data)
} else {
publication = RunTemplate(simplePublicizeT, data)
}
}
return publication
}
const (
simplePublicizeTmpl = `{{ tabs .depth }}{{ .targetField }} {{ if .init }}:{{ end }}= {{ if .dereference }}*{{ end }}{{ .sourceField }}`
recursivePublicizeTmpl = `{{ tabs .depth }}{{ .targetField }} {{ if .init }}:{{ end }}= {{ .sourceField }}.Publicize()`
objectPublicizeTmpl = `{{ tabs .depth }}{{ .targetField }} = &{{ gotypedef .att .depth true false }}{}
{{ recursivePublicizer .att .sourceField .targetField .depth }}`
arrayPublicizeTmpl = `{{ tabs .depth }}{{ .targetField }} {{ if .init }}:{{ end }}= make({{ gotyperef .att.Type .att.AllRequired .depth false }}, len({{ .sourceField }})){{/*
*/}}{{ $i := printf "%s%d" "i" .depth }}{{ $elem := printf "%s%d" "elem" .depth }}
{{ tabs .depth }}for {{ $i }}, {{ $elem }} := range {{ .sourceField }} {
{{ tabs .depth }}{{ publicizer .elemType $elem (printf "%s[%s]" .targetField $i) .dereference (add .depth 1) false }}
{{ tabs .depth }}}`
hashPublicizeTmpl = `{{ tabs .depth }}{{ .targetField }} {{ if .init }}:{{ end }}= make({{ gotyperef .att.Type .att.AllRequired .depth false }}, len({{ .sourceField }})){{/*
*/}}{{ $k := printf "%s%d" "k" .depth }}{{ $v := printf "%s%d" "v" .depth }}
{{ tabs .depth }}for {{ $k }}, {{ $v }} := range {{ .sourceField }} {
{{ $pubk := printf "%s%s" "pub" $k }}{{ $pubv := printf "%s%s" "pub" $v }}{{/*
*/}}{{ tabs .depth }}{{ publicizer .keyType $k $pubk .dereference (add .depth 1) true }}
{{ tabs .depth }}{{ publicizer .elemType $v $pubv .dereference (add .depth 1) true }}
{{ tabs .depth }} {{ printf "%s[%s]" .targetField $pubk }} = {{ $pubv }}
{{ tabs .depth }}}`
)