Skip to content
This repository has been archived by the owner on Jul 26, 2021. It is now read-only.

Commit

Permalink
Tweak the solution just a little
Browse files Browse the repository at this point in the history
  • Loading branch information
svanharmelen committed Sep 8, 2019
1 parent b570342 commit ea9e2fe
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ import (

const pkg = "cloudstack"

// detailsRequireKeyValue is a prefilled map with a list of details
// that need to be encoded using an explicit key and a value entry.
var detailsRequireKeyValue = map[string]bool{
"addGuestOs": true,
"addImageStore": true,
"addResourceDetail": true,
"createSecondaryStagingStore": true,
"updateCloudToUseObjectStore": true,
"updateGuestOs": true,
"updateZone": true,
}

// We prefill this one value to make sure it is not
// created twice, as this is also a top level type.
var typeNames = map[string]bool{"Nic": true}
Expand Down Expand Up @@ -830,7 +842,7 @@ func (s *service) generateToURLValuesFunc(a *API) {
pn(" }")
for _, ap := range a.Params {
pn(" if v, found := p.p[\"%s\"]; found {", ap.Name)
s.generateConvertCode(ap.Name, a.Name, mapType(ap.Type))
s.generateConvertCode(a.Name, ap.Name, mapType(ap.Type))
pn(" }")
}
pn(" return u")
Expand All @@ -839,7 +851,7 @@ func (s *service) generateToURLValuesFunc(a *API) {
return
}

func (s *service) generateConvertCode(name, cmdName, typ string) {
func (s *service) generateConvertCode(cmd, name, typ string) {
pn := s.pn

switch typ {
Expand All @@ -862,7 +874,7 @@ func (s *service) generateConvertCode(name, cmdName, typ string) {
pn("for k, vv := range v.(map[string]string) {")
switch name {
case "details":
if detailsRequiresKeyValue(cmdName) {
if detailsRequireKeyValue[cmd] {
pn(" u.Set(fmt.Sprintf(\"%s[%%d].key\", i), k)", name)
pn(" u.Set(fmt.Sprintf(\"%s[%%d].value\", i), vv)", name)
} else {
Expand All @@ -884,24 +896,6 @@ func (s *service) generateConvertCode(name, cmdName, typ string) {
return
}

func detailsRequiresKeyValue(cmd string) bool {
var requiredCmds = []string{
"addGuestOs",
"updateGuestOs",
"addImageStore",
"createSecondaryStagingStore",
"updateCloudToUseObjectStore",
"addResourceDetail",
"updateZone",
}
for _, req := range requiredCmds {
if req == cmd {
return true
}
}
return false
}

func (s *service) parseParamName(name string) string {
if name != "type" {
return name
Expand Down

0 comments on commit ea9e2fe

Please sign in to comment.