github.com/sacloud/iaas-api-go@v1.12.0/internal/tools/gen-api-stub/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/dsl" 23 "github.com/sacloud/iaas-api-go/internal/tools" 24 ) 25 26 const destination = "stub/zz_api_stubs.go" 27 28 func init() { 29 log.SetFlags(0) 30 log.SetPrefix("gen-api-stub: ") 31 } 32 33 func main() { 34 dsl.IsOutOfSacloudPackage = true 35 36 tools.WriteFileWithTemplate(&tools.TemplateConfig{ 37 OutputPath: filepath.Join(tools.ProjectRootPath(), destination), 38 Template: tmpl, 39 Parameter: define.APIs, 40 }) 41 log.Printf("generated: %s\n", filepath.Join(tools.ProjectRootPath(), destination)) 42 } 43 44 const tmpl = `// generated by 'github.com/sacloud/iaas-api-go/internal/tools/gen-api-stub'; DO NOT EDIT 45 46 package stub 47 48 import ( 49 {{- range .ImportStatements "context" "log" }} 50 {{ . }} 51 {{- end }} 52 ) 53 54 {{ range . }} {{ $typeName := .TypeName }}{{ $resource := . }} 55 56 /************************************************* 57 * {{ $typeName }}Stub 58 *************************************************/ 59 60 {{ range .Operations }} 61 // {{ $typeName }}{{.MethodName}}StubResult is expected values of the {{ .MethodName }} operation 62 type {{ $typeName }}{{.MethodName}}StubResult struct { 63 {{ range .StubFieldDefines -}} 64 {{ . }} 65 {{ end -}} 66 Err error 67 } 68 {{ end -}} 69 70 // {{ $typeName }}Stub is for trace {{ $typeName }}Op operations 71 type {{ $typeName }}Stub struct { 72 {{ range .Operations -}} 73 {{.MethodName}}StubResult *{{ $typeName }}{{.MethodName}}StubResult 74 {{ end -}} 75 } 76 77 // New{{ $typeName}}Stub creates new {{ $typeName}}Stub instance 78 func New{{ $typeName}}Stub(caller iaas.APICaller) iaas.{{$typeName}}API { 79 return &{{ $typeName}}Stub{} 80 } 81 82 {{ range .Operations }}{{$returnErrStatement := .ReturnErrorStatement}}{{ $operationName := .MethodName }} 83 // {{ .MethodName }} is API call with trace log 84 func (s *{{ $typeName }}Stub) {{ .MethodName }}(ctx context.Context{{if not $resource.IsGlobal}}, zone string{{end}}{{ range .Arguments }}, {{ .ArgName }} {{ .TypeName }}{{ end }}) {{.ResultsStatement}} { 85 if s.{{$operationName}}StubResult == nil { 86 log.Fatal("{{$typeName}}Stub.{{$operationName}}StubResult is not set") 87 } 88 {{.StubReturnStatement "s"}} 89 } 90 {{- end -}} 91 92 {{ end }} 93 `