in generate/generate.go [896:1103]
func (s *service) GenerateCode() ([]byte, error) {
// Buffer the output in memory, for gofmt'ing later in the defer.
var buf bytes.Buffer
s.p = func(format string, args ...interface{}) {
_, err := fmt.Fprintf(&buf, format, args...)
if err != nil {
panic(err)
}
}
s.pn = func(format string, args ...interface{}) {
s.p(format+"\n", args...)
}
pn := s.pn
pn("//")
pn("// Licensed to the Apache Software Foundation (ASF) under one")
pn("// or more contributor license agreements. See the NOTICE file")
pn("// distributed with this work for additional information")
pn("// regarding copyright ownership. The ASF licenses this file")
pn("// to you under the Apache License, Version 2.0 (the")
pn("// \"License\"); you may not use this file except in compliance")
pn("// with the License. You may obtain a copy of the License at")
pn("//")
pn("// http://www.apache.org/licenses/LICENSE-2.0")
pn("//")
pn("// Unless required by applicable law or agreed to in writing,")
pn("// software distributed under the License is distributed on an")
pn("// \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY")
pn("// KIND, either express or implied. See the License for the")
pn("// specific language governing permissions and limitations")
pn("// under the License.")
pn("//")
pn("")
pn("package %s", pkg)
pn("")
if s.name == "FirewallService" {
pn("// Helper function for maintaining backwards compatibility")
pn("func convertFirewallServiceResponse(b []byte) ([]byte, error) {")
pn(" var raw map[string]interface{}")
pn(" if err := json.Unmarshal(b, &raw); err != nil {")
pn(" return nil, err")
pn(" }")
pn("")
pn(" if _, ok := raw[\"firewallrule\"]; ok {")
pn(" return convertFirewallServiceListResponse(b)")
pn(" }")
pn("")
pn(" for _, k := range []string{\"endport\", \"startport\"} {")
pn(" if sVal, ok := raw[k].(string); ok {")
pn(" iVal, err := strconv.Atoi(sVal)")
pn(" if err != nil {")
pn(" return nil, err")
pn(" }")
pn(" raw[k] = iVal")
pn(" }")
pn(" }")
pn("")
pn(" return json.Marshal(raw)")
pn("}")
pn("")
pn("// Helper function for maintaining backwards compatibility")
pn("func convertFirewallServiceListResponse(b []byte) ([]byte, error) {")
pn(" var rawList struct {")
pn(" Count int `json:\"count\"`")
pn(" FirewallRules []map[string]interface{} `json:\"firewallrule\"`")
pn(" }")
pn("")
pn(" if err := json.Unmarshal(b, &rawList); err != nil {")
pn(" return nil, err")
pn(" }")
pn("")
pn(" for _, r := range rawList.FirewallRules {")
pn(" for _, k := range []string{\"endport\", \"startport\"} {")
pn(" if sVal, ok := r[k].(string); ok {")
pn(" iVal, err := strconv.Atoi(sVal)")
pn(" if err != nil {")
pn(" return nil, err")
pn(" }")
pn(" r[k] = iVal")
pn(" }")
pn(" }")
pn(" }")
pn("")
pn(" return json.Marshal(rawList)")
pn("}")
pn("")
}
if s.name == "SecurityGroupService" {
pn("// Helper function for maintaining backwards compatibility")
pn("func convertAuthorizeSecurityGroupIngressResponse(b []byte) ([]byte, error) {")
pn(" var raw struct {")
pn(" Ingressrule []interface{} `json:\"ingressrule\"`")
pn(" }")
pn(" if err := json.Unmarshal(b, &raw); err != nil {")
pn(" return nil, err")
pn(" }")
pn("")
pn(" if len(raw.Ingressrule) != 1 {")
pn(" return b, nil")
pn(" }")
pn("")
pn(" return json.Marshal(raw.Ingressrule[0])")
pn("}")
pn("")
pn("// Helper function for maintaining backwards compatibility")
pn("func convertAuthorizeSecurityGroupEgressResponse(b []byte) ([]byte, error) {")
pn(" var raw struct {")
pn(" Egressrule []interface{} `json:\"egressrule\"`")
pn(" }")
pn(" if err := json.Unmarshal(b, &raw); err != nil {")
pn(" return nil, err")
pn(" }")
pn("")
pn(" if len(raw.Egressrule) != 1 {")
pn(" return b, nil")
pn(" }")
pn("")
pn(" return json.Marshal(raw.Egressrule[0])")
pn("}")
pn("")
}
if s.name == "CustomService" {
pn("type CustomServiceParams struct {")
pn(" p map[string]interface{}")
pn("}")
pn("")
pn("func (p *CustomServiceParams) toURLValues() url.Values {")
pn(" u := url.Values{}")
pn(" if p.p == nil {")
pn(" return u")
pn(" }")
pn("")
pn(" for k, v := range p.p {")
pn(" switch t := v.(type) {")
pn(" case bool:")
pn(" u.Set(k, strconv.FormatBool(t))")
pn(" case int:")
pn(" u.Set(k, strconv.Itoa(t))")
pn(" case int64:")
pn(" vv := strconv.FormatInt(t, 10)")
pn(" u.Set(k, vv)")
pn(" case string:")
pn(" u.Set(k, t)")
pn(" case []string:")
pn(" u.Set(k, strings.Join(t, \", \"))")
pn(" case map[string]string:")
pn(" i := 0")
pn(" for kk, vv := range t {")
pn(" u.Set(fmt.Sprintf(\"%%s[%%d].%%s\", k, i, kk), vv)")
pn(" i++")
pn(" }")
pn(" }")
pn(" }")
pn("")
pn(" return u")
pn("}")
pn("")
pn("func (p *CustomServiceParams) SetParam(param string, v interface{}) {")
pn(" if p.p == nil {")
pn(" p.p = make(map[string]interface{})")
pn(" }")
pn(" p.p[param] = v")
pn("}")
pn("func (p *CustomServiceParams) GetParam(param string) (interface{}, bool) {")
pn(" if p.p == nil {")
pn(" p.p = make(map[string]interface{})")
pn(" }")
pn(" value, ok := p.p[param].(interface{})")
pn(" return value, ok")
pn("}")
pn("")
pn("func (s *CustomService) CustomRequest(api string, p *CustomServiceParams, result interface{}) error {")
pn(" resp, err := s.cs.newRequest(api, p.toURLValues())")
pn(" if err != nil {")
pn(" return err")
pn(" }")
pn("")
pn(" return json.Unmarshal(resp, result)")
pn("}")
pn("func (s *CustomService) CustomPostRequest(api string, p *CustomServiceParams, result interface{}) error {")
pn(" resp, err := s.cs.newPostRequest(api, p.toURLValues())")
pn(" if err != nil {")
pn(" return err")
pn(" }")
pn("")
pn(" return json.Unmarshal(resp, result)")
pn("}")
}
s.generateInterfaceType()
for _, a := range s.apis {
s.generateParamType(a)
s.generateToURLValuesFunc(a)
s.generateParamGettersAndSettersFunc(a)
s.generateNewParamTypeFunc(a)
s.generateHelperFuncs(a)
s.generateNewAPICallFunc(a)
s.generateResponseType(a)
}
clean, err := format.Source(buf.Bytes())
if err != nil {
buf.WriteTo(os.Stdout)
return buf.Bytes(), err
}
return clean, nil
}