-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi_tmpl.go
174 lines (150 loc) · 5.79 KB
/
api_tmpl.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
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package main
var headerTmpl = `
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Generated by https://github.com/cloudescape/govsphere
// Do not manually edit it.
`
var importsTmpl = `
import (
gowsdl "github.com/cloudescape/gowsdl/generator"
)
`
var moTmpl = doTmpl
var doTmpl = `
{{$comment := comment .Description}}
{{$type := makePublic .Name true}}
{{$namespace := .Namespace}}
{{if $comment}} {{$comment}} {{end}}
type {{$type}} struct {
{{$extends := toGoType .Extends}}
{{if ne $extends "*ManagedEntity"}}
{{if ne $extends "*ExtensibleManagedObject"}}
{{if eq $namespace "mo"}}*ManagedObject{{end}}
{{end}}{{end}}{{lookUpNamespace $extends $namespace}}
{{range .Properties}}
{{$fieldComment := comment .Description}}
{{$privileges := comment .RequiredPrivileges}}
{{$fieldType := toGoType .Type}}
{{if ne $namespace "mo"}}{{if $fieldComment}} {{$fieldComment}} {{end}}{{if $privileges}} {{$privileges}} {{end}}{{end}}
{{if ne $namespace "mo"}}{{makePublic .Name true}}{{else}}{{replaceReservedWords .Name}}{{end}} {{lookUpNamespace $fieldType $namespace}}{{if eq $type "ManagedObjectReference"}}{{if eq .Name "type"}}` + "`" + `xml:"{{.Name}},attr"` + "`" + `{{else if eq .Name "value"}}` + "`" + `xml:",innerxml"` + "`" + `{{end}}{{else}}` + "`" + `xml:"{{.Name}},omitempty"` + "`" + `{{end}}
{{end}}
}
{{if eq $namespace "mo"}}
{{range .Properties}}
{{$fieldComment := comment .Description}}
{{$privileges := comment .RequiredPrivileges}}
{{$fieldType := toGoType .Type}}
{{$nullValue := toNullType $fieldType}}
{{$getterName := genGetterName .Name $extends}}
{{if $fieldComment}} {{$fieldComment}} {{end}}{{if $privileges}} {{$privileges}} {{end}}
func (mo *{{$type}}) {{$getterName}}() ({{$fieldType}}, error) {
p, err := mo.currentProperty("{{.Name}}")
if err != nil {
return {{$nullValue}}, err
}
{{if eq $fieldType "[]*ManagedEntity"}}
refs := p.(*ArrayOfManagedObjectReference).Things
return ToManagedEntities(refs), nil
{{else if eq $fieldType "[]string"}}
return p.(*ArrayOfString).Things, nil
{{else if eq $fieldType "[]int32"}}
return p.(*ArrayOfInt).Things, nil
{{else if eq $fieldType "[]int8"}}
return p.(*ArrayOfByte).Things, nil
{{else if $fieldType}}
if p != nil {
return p.({{$fieldType}}), nil
}
return {{$nullValue}}, nil
{{else}}
return nil
{{end}}
}
{{end}}
{{end}}
{{range .Methods}}
{{$mcomment := comment .Description}}
{{$returnType := toGoType .ReturnValue.Type}}
{{if $mcomment}} {{$mcomment}} {{end}}
func ({{$namespace}} *{{$type}}) {{makePublic .Name true}}(
{{range .Parameters}} {{if ne .Name "_this"}}{{$ptype := toGoType .Type}} {{replaceReservedWords .Name}} {{lookUpNamespace $ptype $namespace}}, {{end}}{{end}}
) ({{if $returnType}}{{lookUpNamespace $returnType $namespace}},{{end}} error) {
{{$requestType := makePublic .Name true}}
request := struct {
XMLName xml.Name ` + "`" + `xml:"{{$requestType}}"` + "`" + `
{{range .Parameters}}{{$ptype := toGoType .Type}}{{if eq .Name "_this"}}This{{else}}{{makePublic .Name true}}{{end}} {{lookUpNamespace $ptype $namespace}} ` + "`" + `xml:"{{.Name}},omitempty"` + "`" + `
{{end}}
}{
{{range .Parameters}}{{if eq .Name "_this"}}This{{else}}{{makePublic .Name true}}{{end}}:{{if eq .Name "_this"}}&ManagedObjectReference{Type: mo.ManagedObject.Type, Value:mo.ManagedObject.Value},{{else}}{{replaceReservedWords .Name}},{{end}}
{{end}}
}
{{if $returnType}}
response := struct {
Returnval {{lookUpNamespace $returnType $namespace}} ` + "`" + `xml:"returnval"` + "`" + `
}{}
{{end}}
if session == nil {
panic("You need to create a vSphereSession first")
}
err := session.invoke(request, {{if $returnType}}&response{{else}}nil{{end}})
if err != nil {
{{if $returnType}}return {{toNullType $returnType}}, err{{else}}return err{{end}}
}
{{if $returnType}}
return response.Returnval, nil
{{else}}
return nil
{{end}}
}
{{end}}
`
var enumTmpl = `
{{$type := .Name}}
{{$comment := comment .Description}}
{{if $comment}} {{$comment}} {{end}}
type {{$type}} string
const (
{{range .Constants}}
{{$constComment := comment .Description}}
{{if $constComment}} {{$constComment}} {{end}}
{{makePublic .Name true}}_{{$type}} {{$type}} = "{{.Name}}"
{{end}}
)
`
var faultTmpl = doTmpl
var registryTmpl = `
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This is an auto-generated type registry map needed in order to convert
// anyType SOAP types to Go types in runtime, without using reflection.
type ArrayOfManagedObjectReference struct {
Things []*ManagedObjectReference ` + "`xml:\"ManagedObjectReference\"`" + `
}
type ArrayOfString struct {
Things []string ` + "`xml:\"string\"`" + `
}
type ArrayOfByte struct {
Things []int8 ` + "`xml:\"byte\"`" + `
}
type ArrayOfInt struct {
Things []int32 ` + "`xml:\"int\"`" + `
}
var registry map[string]func() interface{}
func init() {
registry = map[string]func() interface{}{
"ArrayOfManagedObjectReference": func() interface{} { return &ArrayOfManagedObjectReference{} },
"ArrayOfString": func() interface{} { return &ArrayOfString{} },
"ArrayOfByte": func() interface{} { return &ArrayOfByte{} },
"ArrayOfInt": func() interface{} { return &ArrayOfInt{} },
{{range .}}"{{.Name}}": func() interface{} { return &{{.Name}}{} },
{{end}}
}
}
`