/
types_builtins.go.template
249 lines (225 loc) 路 7.55 KB
/
types_builtins.go.template
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2016-2018, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Code generated by "generate.go"; DO NOT EDIT.
// nolint: lll, interfacer
package pulumi
import (
"context"
"reflect"
)
{{with $builtins := .Builtins}}
{{range $builtins}}
var {{.Name | Unexported}}Type = reflect.TypeOf((*{{.ElementType}})(nil)).Elem()
// {{.Name}}Input is an input type that accepts {{.Name}} and {{.Name}}Output values.
type {{.Name}}Input interface {
Input
To{{.Name}}Output() {{.Name}}Output
To{{.Name}}OutputWithContext(ctx context.Context) {{.Name}}Output
{{if .ImplementsPtrType}}
To{{.Name}}PtrOutput() {{.Name}}PtrOutput
To{{.Name}}PtrOutputWithContext(ctx context.Context) {{.Name}}PtrOutput
{{end}}
}
{{if .DefineInputType}}
// {{.Name}} is an input type for {{.Type}} values.
type {{.Name}} {{.Type}}
{{else if .DefinePtrType}}
type {{.PtrType}} {{.ElemElementType}}
// {{.Name}} is an input type for {{.Type}} values.
func {{.Name}}(v {{.ElemElementType}}) {{.Name}}Input {
return ({{.InputType}})(&v)
}
func {{.Name}}FromPtr(v *{{.ElemElementType}}) {{.Name}}Input {
if v == nil {
return nil
}
return ({{.InputType}})(v)
}
{{end}}
{{if .DefineInputMethods}}
// ElementType returns the element type of this Input ({{.ElementType}}).
func ({{.InputType}}) ElementType() reflect.Type {
return {{.Name | Unexported}}Type
}
func (in {{.InputType}}) To{{.Name}}Output() {{.Name}}Output {
return ToOutput(in).({{.Name}}Output)
}
func (in {{.InputType}}) To{{.Name}}OutputWithContext(ctx context.Context) {{.Name}}Output {
return ToOutputWithContext(ctx, in).({{.Name}}Output)
}
{{with $builtin := .}}
{{range $t := .Implements}}
func (in {{$builtin.InputType}}) To{{$t.Name}}Output() {{$t.Name}}Output {
return in.To{{$t.Name}}OutputWithContext(context.Background())
}
func (in {{$builtin.InputType}}) To{{$t.Name}}OutputWithContext(ctx context.Context) {{$t.Name}}Output {
return in.To{{$builtin.Name}}OutputWithContext(ctx).To{{$t.Name}}OutputWithContext(ctx)
}
{{end}}
{{end}}
{{if .ImplementsPtrType}}
func (in {{.InputType}}) To{{.Name}}PtrOutput() {{.Name}}PtrOutput {
return in.To{{.Name}}PtrOutputWithContext(context.Background())
}
func (in {{.InputType}}) To{{.Name}}PtrOutputWithContext(ctx context.Context) {{.Name}}PtrOutput {
return in.To{{.Name}}OutputWithContext(ctx).To{{.Name}}PtrOutputWithContext(ctx)
}
{{end}}
{{end}}
// {{.Name}}Output is an Output that returns {{.ElementType}} values.
type {{.Name}}Output struct { *OutputState }
// ElementType returns the element type of this Output ({{.ElementType}}).
func ({{.Name}}Output) ElementType() reflect.Type {
return {{.Name | Unexported}}Type
}
func (o {{.Name}}Output) To{{.Name}}Output() {{.Name}}Output {
return o
}
func (o {{.Name}}Output) To{{.Name}}OutputWithContext(ctx context.Context) {{.Name}}Output {
return o
}
{{with $builtin := .}}
{{range $t := .Implements}}
func (o {{$builtin.Name}}Output) To{{$t.Name}}Output() {{$t.Name}}Output {
return o.To{{$t.Name}}OutputWithContext(context.Background())
}
func (o {{$builtin.Name}}Output) To{{$t.Name}}OutputWithContext(ctx context.Context) {{$t.Name}}Output {
return o.ApplyTWithContext(ctx, func(_ context.Context, v {{$builtin.ElementType}}) {{$t.ElementType}} {
return ({{$t.ElementType}})(v)
}).({{$t.Name}}Output)
}
{{end}}
{{if .ImplementsPtrType}}
func (o {{.Name}}Output) To{{.Name}}PtrOutput() {{.Name}}PtrOutput {
return o.To{{.Name}}PtrOutputWithContext(context.Background())
}
func (o {{.Name}}Output) To{{.Name}}PtrOutputWithContext(ctx context.Context) {{.Name}}PtrOutput {
return o.ApplyTWithContext(ctx, func(_ context.Context, v {{.ElementType}}) *{{.ElementType}} {
return &v
}).({{.Name}}PtrOutput)
}
{{end}}
{{end}}
{{if .DefineElem}}
// Elem dereferences the pointer value or returns the zero value of the approporiate type if the pointer is nil.
func (o {{.Name}}Output) Elem() {{.ElemReturnType}}Output {
return o.ApplyT(func (v {{.ElementType}}) {{.ElemElementType}} {
var ret {{.ElemElementType}}
if v != nil {
ret = *v
}
return ret
}).({{.ElemReturnType}}Output)
}
{{end}}
{{if .DefineIndex}}
// Index looks up the i'th element of the array if it is in bounds or returns the zero value of the appropriate
// type if the index is out of bounds.
func (o {{.Name}}Output) Index(i IntInput) {{.IndexReturnType}}Output {
return All(o, i).ApplyT(func(vs []interface{}) {{.IndexElementType}} {
arr := vs[0].({{.ElementType}})
idx := vs[1].(int)
var ret {{.IndexElementType}}
if idx >= 0 && idx < len(arr) {
ret = arr[idx]
}
return ret
}).({{.IndexReturnType}}Output)
}
{{if .DefineToFunction}}
func To{{.Name}}(in {{.ElementType}}) {{.Name}} {
a := make({{.Name}}, len(in))
for i, v := range in {
a[i] = {{.IndexConversion}}(v)
}
return a
}
func To{{.Name}}Output(in []{{.IndexReturnType}}Output) {{.Name}}Output {
a := make({{.Name}}, len(in))
for i, v := range in {
a[i] = v
}
return a.To{{.Name}}Output()
}
{{end}}
{{end}}
{{if .DefineMapIndex}}
// MapIndex looks up the key k in the map.
func (o {{.Name}}Output) MapIndex(k StringInput) {{.MapIndexReturnType}}Output {
return All(o, k).ApplyT(func(vs []interface{}) {{.MapIndexElementType}} {
return vs[0].({{.ElementType}})[vs[1].(string)]
}).({{.MapIndexReturnType}}Output)
}
{{if .DefineToFunction}}
func To{{.Name}}(in {{.ElementType}}) {{.Name}} {
m := make({{.Name}})
for k, v := range in {
m[k] = {{.IndexConversion}}(v)
}
return m
}
func To{{.Name}}Output(in map[string]{{.MapIndexReturnType}}Output) {{.Name}}Output {
m := make({{.Name}})
for k, v := range in {
m[k] = v
}
return m.To{{.Name}}Output()
}
{{end}}
{{end}}
{{end}}
{{end}}
func getResolvedValue(input Input) (reflect.Value, bool) {
switch input := input.(type) {
case *asset, *archive:
return reflect.ValueOf(input), true
default:
return reflect.Value{}, false
}
}
{{range .Builtins}}
{{ if eq .Strategy "primitive" }}
// As{{.Name}}Output asserts that the type of the AnyOutput's underlying interface{} value is
// {{.ElementType}} and returns a `{{.Name}}Output` with that value. As{{.Name}}Output panics if the value
// was not the expected type.
func (a AnyOutput) As{{.Name}}Output() {{.Name}}Output {
return a.ApplyT(func(i interface{}) {{.ElementType}} {
return i.({{.ElementType}})
}).({{.Name}}Output)
}
{{else}}
// As{{.Name}}Output asserts that the type of the AnyOutput's underlying interface{} value is
// {{.ElementType}} or a compatible type and returns a `{{.Name}}Output` with that value.
// As{{.Name}}Output panics if the value was not the expected type or a compatible type.
func (a AnyOutput) As{{.Name}}Output() {{.Name}}Output {
return a.ApplyT(func(i interface{}) ({{.ElementType}}, error) {
v, err := coerceTypeConversion(i, reflect.TypeOf((*{{.ElementType}})(nil)).Elem())
if err != nil {
return nil, err
}
return v.({{.ElementType}}), nil
}).({{.Name}}Output)
}
{{end}}
{{end}}
func init() {
{{- range .Builtins}}
{{- if .RegisterInput}}
RegisterInputType(reflect.TypeOf((*{{.Name}}Input)(nil)).Elem(), {{.DefaultValue}})
{{- end}}
{{- end}}
{{- range .Builtins}}
RegisterOutputType({{.Name}}Output{})
{{- end}}
}