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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
|
// Copyright 2016 The go-qemu Authors.
//
// 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.
package gen
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
"strings"
"github.com/fatih/camelcase"
)
// a definition is the definition of one QMP API type and its docstring.
type event struct {
Name name `json:"event"`
Data fieldsOrRef `json:"data"`
BoxedData bool `json:"boxed"`
}
type command struct {
Name name
Inputs fieldsOrRef
Output field
BoxedInput bool
}
type alternate struct {
Name name `json:"alternate"`
Options map[name]name `json:"data"`
}
type flatUnion struct {
Name name
Base fieldsOrRef
Discriminator name
Options map[name]name
}
func (fu flatUnion) DiscriminatorField(api map[name]interface{}) field {
for _, f := range fu.Base.Fields(api) {
if f.Name == fu.Discriminator {
return f
}
}
panic("no discriminator field found")
}
type simpleUnion struct {
Name name
Options map[name]name
}
type structType struct {
Name name `json:"struct"`
Fields fields `json:"data"`
Base name `json:"base"`
}
func (s structType) AllFields(api map[name]interface{}) fields {
var ret fields
if s.Base != "" {
ret = append(ret, api[s.Base].(structType).Fields...)
}
ret = append(ret, s.Fields...)
return ret
}
func (s structType) HasInterfaceField(api map[name]interface{}) bool {
if s.Fields.HasInterfaceField(api) {
return true
}
if s.Base != "" && api[s.Base].(structType).Fields.HasInterfaceField(api) {
return true
}
return false
}
type enum struct {
Name name `json:"enum"`
Values []name `json:"data"`
}
type field struct {
Name name
List bool
Optional bool
Type name
}
type fields []field
func (fs fields) HasInterfaceField(api map[name]interface{}) bool {
for _, f := range fs {
if f.Type.InterfaceType(api) {
return true
}
}
return false
}
type fieldsOrRef struct {
AnonFields fields
Ref name
}
func (f fieldsOrRef) Fields(api map[name]interface{}) fields {
if f.AnonFields != nil {
return f.AnonFields
}
if f.Ref != "" {
return api[f.Ref].(structType).Fields
}
return nil
}
func (f *fieldsOrRef) UnmarshalJSON(bs []byte) error {
var s name
if err := json.Unmarshal(bs, &s); err == nil {
f.AnonFields = nil
f.Ref = s
return nil
}
return json.Unmarshal(bs, &f.AnonFields)
}
func (fs *fields) UnmarshalJSON(bs []byte) error {
d := json.NewDecoder(bytes.NewBuffer(bs))
// Dictionary starts with {
t, err := d.Token()
if err != nil {
return err
}
if delim, ok := t.(json.Delim); !ok || delim != '{' {
return fmt.Errorf("unexpected token %q (%T)", t, t)
}
// Then a series of key/value pairs until }.
for {
t, err := d.Token()
if err != nil {
return err
}
// End of the dict?
if delim, ok := t.(json.Delim); ok && delim == '}' {
break
}
var field field
// Dictionary key
s, ok := t.(string)
if !ok {
return fmt.Errorf("expected dictionary key (string), got %q (%T)", t, t)
}
if s[0] == '*' {
field.Name = name(s[1:])
field.Optional = true
} else {
field.Name = name(s)
}
// The value, either a string or a list of one string.
t, err = d.Token()
if err != nil {
return err
}
switch v := t.(type) {
case string:
field.Type = name(v)
case json.Delim:
if v != '[' {
return fmt.Errorf("unexpected delimiter %q, wanted a dict value", v)
}
// The actual type name
t, err = d.Token()
if err != nil {
return err
}
s, ok := t.(string)
if !ok {
return fmt.Errorf("unexpected token %q (%T), wanted string", t, t)
}
// Closing square bracket
t, err := d.Token()
if err != nil {
return err
}
if delim, ok := t.(json.Delim); !ok || delim != ']' {
return fmt.Errorf("unexpected token %q (%T), wanted ']'", t, t)
}
field.Type = name(s)
field.List = true
default:
return fmt.Errorf("unexpected token %q (%T), wanted string or '['", t, t)
}
*fs = append(*fs, field)
}
return nil
}
type name string
func (n name) Go() string {
if v, ok := builtinTypes[string(n)]; ok {
return v
}
return n.FieldName()
}
func (n name) FieldName() string {
out := []string{}
for _, p := range camelcase.Split(string(n)) {
if p == "-" || p == "_" {
continue
}
if upperWords[strings.ToLower(p)] {
p = strings.ToUpper(p)
} else {
p = strings.Title(strings.ToLower(p))
}
out = append(out, p)
}
return strings.Join(out, "")
}
func (n name) SimpleType() bool {
_, ok := builtinTypes[string(n)]
return ok
}
func (n name) NullType() bool {
if n.SimpleType() {
return false
}
return strings.EqualFold(string(n), "Null")
}
func (n name) InterfaceType(api map[name]interface{}) bool {
if n.SimpleType() {
return false
}
switch api[n].(type) {
case simpleUnion, flatUnion, alternate:
return true
}
return false
}
var builtinTypes = map[string]string{
"str": "string",
"number": "float64",
"int": "int64",
"int8": "int8",
"int16": "int16",
"int32": "int32",
"int64": "int64",
"uint8": "uint8",
"uint16": "uint16",
"uint32": "uint32",
"uint64": "uint64",
"size": "uint64",
"bool": "bool",
"any": "interface{}",
}
var upperWords = map[string]bool{
"acpi": true,
"acpiost": true,
"aio": true,
"cpu": true,
"fd": true,
"ftp": true,
"ftps": true,
"guid": true,
"http": true,
"https": true,
"id": true,
"io": true,
"ip": true,
"json": true,
"kvm": true,
"luks": true,
"mips": true,
"nbd": true,
"ok": true,
"pci": true,
"ppc": true,
"qmp": true,
"ram": true,
"sparc": true,
"ssh": true,
"tcp": true,
"tls": true,
"tpm": true,
"ttl": true,
"udp": true,
"uri": true,
"uuid": true,
"vm": true,
"vmdk": true,
"vnc": true,
}
func resolvePath(orig, new string) (string, error) {
u, err := url.Parse(orig)
if err != nil {
return "", err
}
u.Path = filepath.Join(filepath.Dir(u.Path), new)
return u.String(), nil
}
// getQAPI reads a QMP API spec file, from local disk or over HTTP(S).
func getQAPI(path string) ([]byte, error) {
u, err := url.Parse(path)
if err != nil {
return nil, err
}
if u.Scheme == "" {
return ioutil.ReadFile(path)
}
resp, err := http.Get(path)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return ioutil.ReadAll(resp.Body)
}
|