func()

in generate/generate.go [1900:2075]


func (s *service) generateResponseType(a *API) {
	pn := s.pn
	tn := capitalize(strings.TrimPrefix(a.Name, "configure") + "Response")

	// add custom response types for some specific API calls
	if a.Name == "quotaBalance" {
		pn("type QuotaBalanceResponse struct {")
		pn("    Statement QuotaBalanceResponseType `json:\"balance\"`")
		pn("}")
		pn("")
		pn("type QuotaBalanceResponseType struct {")
		pn("    StartQuota float64  `json:\"startquota\"`")
		pn("    Credits    []string `json:\"credits\"`")
		pn("    StartDate  string   `json:\"startdate\"`")
		pn("    Currency   string   `json:\"currency\"`")
		pn("}")
		pn("")
		return
	}
	if a.Name == "quotaStatement" {
		pn("type QuotaStatementResponse struct {")
		pn("    Statement QuotaStatementResponseType `json:\"statement\"`")
		pn("}")
		pn("")
		pn("type QuotaStatementResponseType struct {")
		pn("    QuotaUsage []QuotaUsage `json:\"quotausage\"`")
		pn("    TotalQuota float64      `json:\"totalquota\"`")
		pn("    StartDate  string       `json:\"startdate\"`")
		pn("    EndDate    string       `json:\"enddate\"`")
		pn("    Currency   string       `json:\"currency\"`")
		pn("}")
		pn("")
		pn("type QuotaUsage struct {")
		pn("    Type      int     `json:\"type\"`")
		pn("    Accountid int     `json:\"accountid\"`")
		pn("    Domain    int     `json:\"domain\"`")
		pn("    Name      string  `json:\"name\"`")
		pn("    Unit      string  `json:\"unit\"`")
		pn("    Quota     float64 `json:\"quota\"`")
		pn("}")
		pn("")
		return
	}

	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" || a.Name == "registerUserData" ||
		a.Name == "quotaBalance" || a.Name == "quotaSummary" || a.Name == "quotaTariffList" {
		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 "listManagementServersMetrics":
			pn("	Count int `json:\"count\"`")
			pn("	%s []*%s `json:\"%s\"`", ln, parseSingular(ln), "managementserver")
		case "listDbMetrics":
			pn("	%s %s `json:\"%s\"`", ln, parseSingular(ln), "dbMetrics")
		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")
		case "listVmwareDcVms":
			pn("	Count int `json:\"count\"`")
			pn("	%s []*%s `json:\"%s\"`", ln, parseSingular(ln), "unmanagedinstance")
		case "registerUserData":
			pn("    Account string `json:\"account\"`")
			pn("    Accountid string `json:\"accountid\"`")
			pn("    Domain string `json:\"domain\"`")
			pn("    Domainid string `json:\"domainid\"`")
			pn("    Hasannotations bool `json:\"hasannotations\"`")
			pn("    Id string `json:\"id\"`")
			pn("    JobID string `json:\"jobid\"`")
			pn("    Jobstatus int `json:\"jobstatus\"`")
			pn("    Name string `json:\"name\"`")
			pn("    Params string `json:\"params\"`")
			pn("    Userdata string `json:\"userdata\"`")
		case "listObjectStoragePools":
			pn("	Count int `json:\"count\"`")
			pn("	%s []*%s `json:\"%s\"`", ln, parseSingular(ln), "objectstore")
		case "listStoragePoolObjects":
			pn("	Count int `json:\"count\"`")
			pn("	%s []*%s `json:\"%s\"`", ln, parseSingular(ln), "datastoreobject")
		case "listImageStoreObjects":
			pn("	Count int `json:\"count\"`")
			pn("	%s []*%s `json:\"%s\"`", ln, parseSingular(ln), "datastoreobject")
		case "listVolumesUsageHistory":
			pn("	Count int `json:\"count\"`")
			pn("	%s []*%s `json:\"%s\"`", ln, parseSingular(ln), "volume")
		case "listHostHAProviders":
			pn("	Count int `json:\"count\"`")
			pn("	%s []*%s `json:\"%s\"`", ln, parseSingular(ln), "haprovider")
		case "listSecondaryStorageSelectors":
			pn("	Count int `json:\"count\"`")
			pn("	%s []*%s `json:\"%s\"`", ln, parseSingular(ln), "heuristics")
		case "listHostHAResources":
			pn("	Count int `json:\"count\"`")
			pn("	%s []*%s `json:\"%s\"`", ln, parseSingular(ln), "hostha")
		case "listInfrastructure":
			pn("	Count int `json:\"count\"`")
			pn("	%s *%s `json:\"%s\"`", ln, parseSingular(ln), "infrastructure")
		case "listStoragePoolsMetrics":
			pn("	Count int `json:\"count\"`")
			pn("	%s []*%s `json:\"%s\"`", ln, parseSingular(ln), "storagepool")
		case "quotaTariffList":
			pn("	Count int `json:\"count\"`")
			pn("	%s []*%s `json:\"%s\"`", ln, parseSingular(ln), "quotatariff")
		case "quotaBalance":
			pn("	%s []*%s `json:\"%s\"`", ln, parseSingular(ln), "balance")
		case "quotaSummary":
			pn("	Count int `json:\"count\"`")
			pn("	%s []*%s `json:\"%s\"`", ln, parseSingular(ln), "summary")
		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("")
	}
}