github.com/sacloud/iaas-api-go@v1.12.0/internal/tools/gen-api-transformer/main.go (about) 1 // Copyright 2022-2023 The sacloud/iaas-api-go Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "log" 19 "path/filepath" 20 21 "github.com/sacloud/iaas-api-go/internal/define" 22 "github.com/sacloud/iaas-api-go/internal/tools" 23 ) 24 25 const destination = "zz_api_transformers.go" 26 27 func init() { 28 log.SetFlags(0) 29 log.SetPrefix("gen-api-transformer: ") 30 } 31 32 func main() { 33 outputPath := destination 34 tools.WriteFileWithTemplate(&tools.TemplateConfig{ 35 OutputPath: filepath.Join(tools.ProjectRootPath(), outputPath), 36 Template: tmpl, 37 Parameter: define.APIs, 38 }) 39 log.Printf("generated: %s\n", outputPath) 40 } 41 42 const tmpl = `// generated by 'github.com/sacloud/iaas-api-go/internal/tools/gen-api-transformer'; DO NOT EDIT 43 44 package iaas 45 46 import ( 47 "encoding/json" 48 "github.com/sacloud/iaas-api-go/mapconv" 49 "github.com/sacloud/iaas-api-go/types" 50 ) 51 52 {{ range . }}{{ $typeName := .TypeName }}{{$resource := .}} 53 {{ range .Operations }}{{$returnErrStatement := .ReturnErrorStatement}}{{ $operationName := .MethodName }} 54 {{ if .HasRequestEnvelope }} 55 func (o *{{ $typeName }}Op) transform{{.MethodName}}Args({{ range .Arguments }}{{ .ArgName }} {{ .TypeName }},{{ end }}) (*{{.RequestEnvelopeStructName}}, error) { 56 {{- range $i, $v := .Arguments }} 57 if {{.ArgName}} == {{.ZeroValueOnSource}} { 58 {{.ArgName}} = {{.ZeroInitializer}} 59 } 60 var arg{{$i}} interface{} = {{.ArgName}} 61 if v , ok := arg{{$i}}.(argumentDefaulter); ok { 62 arg{{$i}} = v.setDefaults() 63 } 64 {{- end }} 65 args := &struct { 66 {{- range $i, $v := .Arguments }} 67 Arg{{ $i }} interface{} {{.MapConvTagSrc}} 68 {{- end }} 69 }{ 70 {{- range $i, $v := .Arguments }} 71 Arg{{ $i }}: arg{{ $i }}, 72 {{- end }} 73 } 74 75 v := &{{.RequestEnvelopeStructName}}{} 76 if err := mapconv.ConvertTo(args, v); err != nil { 77 return nil, err 78 } 79 return v, nil 80 } 81 {{ end -}} 82 83 {{ if .HasResponseEnvelope }} 84 func (o *{{ $typeName }}Op) transform{{.MethodName}}Results(data []byte) (*{{.ResultTypeName}}, error) { 85 nakedResponse := &{{.ResponseEnvelopeStructName}}{} 86 if err := json.Unmarshal(data, nakedResponse); err != nil { 87 return nil, err 88 } 89 90 results := &{{.ResultTypeName}}{} 91 if err := mapconv.ConvertFrom(nakedResponse, results); err != nil { 92 return nil, err 93 } 94 return results, nil 95 } 96 {{ end -}} 97 {{ end -}} 98 {{ end -}} 99 `