in generate/generate.go [1835:1913]
func (s *service) generateResponseType(a *API) {
pn := s.pn
tn := capitalize(strings.TrimPrefix(a.Name, "configure") + "Response")
ln := capitalize(strings.TrimPrefix(a.Name, "list"))
// If this is a 'list' response, we need an separate list struct. There seem to be other
// types of responses that also need a separate list struct, so checking on exact matches
// for those once.
if strings.HasPrefix(a.Name, "list") || a.Name == "registerTemplate" || a.Name == "findHostsForMigration" {
pn("type %s struct {", tn)
// This nasty check is for some specific response that do not behave consistent
switch a.Name {
case "listAsyncJobs":
pn(" Count int `json:\"count\"`")
pn(" %s []*%s `json:\"%s\"`", ln, parseSingular(ln), "asyncjobs")
case "listCapabilities":
pn(" %s *%s `json:\"%s\"`", ln, parseSingular(ln), "capability")
case "listEgressFirewallRules":
pn(" Count int `json:\"count\"`")
pn(" %s []*%s `json:\"%s\"`", ln, parseSingular(ln), "firewallrule")
case "listLoadBalancerRuleInstances":
pn(" Count int `json:\"count\"`")
pn(" LBRuleVMIDIPs []*%s `json:\"%s\"`", parseSingular(ln), "lbrulevmidip")
pn(" LoadBalancerRuleInstances []*VirtualMachine `json:\"%s\"`", strings.ToLower(parseSingular(ln)))
case "listVirtualMachinesMetrics":
pn(" Count int `json:\"count\"`")
pn(" %s []*%s `json:\"%s\"`", ln, parseSingular(ln), "virtualmachine")
case "registerTemplate":
pn(" Count int `json:\"count\"`")
pn(" %s []*%s `json:\"%s\"`", ln, parseSingular(ln), "template")
case "listDomainChildren":
pn(" Count int `json:\"count\"`")
pn(" %s []*%s `json:\"%s\"`", ln, parseSingular(ln), "domain")
case "findHostsForMigration":
pn(" Count int `json:\"count\"`")
pn(" Host []*%s `json:\"%s\"`", customResponseStructTypes[a.Name], "host")
default:
pn(" Count int `json:\"count\"`")
pn(" %s []*%s `json:\"%s\"`", ln, parseSingular(ln), strings.ToLower(parseSingular(ln)))
}
pn("}")
pn("")
tn = parseSingular(ln)
}
sort.Sort(a.Response)
customMarshal := s.recusiveGenerateResponseType(a.Name, tn, a.Response, a.Isasync)
if customMarshal {
pn("func (r *%s) UnmarshalJSON(b []byte) error {", tn)
pn(" var m map[string]interface{}")
pn(" err := json.Unmarshal(b, &m)")
pn(" if err != nil {")
pn(" return err")
pn(" }")
pn("")
pn(" if success, ok := m[\"success\"].(string); ok {")
pn(" m[\"success\"] = success == \"true\"")
pn(" b, err = json.Marshal(m)")
pn(" if err != nil {")
pn(" return err")
pn(" }")
pn(" }")
pn("")
pn(" if ostypeid, ok := m[\"ostypeid\"].(float64); ok {")
pn(" m[\"ostypeid\"] = strconv.Itoa(int(ostypeid))")
pn(" b, err = json.Marshal(m)")
pn(" if err != nil {")
pn(" return err")
pn(" }")
pn(" }")
pn("")
pn(" type alias %s", tn)
pn(" return json.Unmarshal(b, (*alias)(r))")
pn("}")
pn("")
}
}