github.com/sacloud/iaas-api-go@v1.12.0/internal/tools/gen-api-envelope/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_envelopes.go"
    26  
    27  func init() {
    28  	log.SetFlags(0)
    29  	log.SetPrefix("gen-api-envelope: ")
    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-envelope'; DO NOT EDIT
    43  
    44  package iaas
    45  
    46  import (
    47  {{- range .ImportStatements "github.com/sacloud/iaas-api-go/types" "github.com/sacloud/iaas-api-go/naked" "github.com/sacloud/iaas-api-go/search" }}
    48  	{{ . }}
    49  {{- end }}
    50  )
    51  
    52  {{- range . }}
    53  {{- range .Operations -}}
    54  
    55  {{ if .HasRequestEnvelope }}
    56  // {{ .RequestEnvelopeStructName }} is envelop of API request
    57  type {{ .RequestEnvelopeStructName }} struct {
    58  {{ if .IsRequestSingular }}
    59  	{{- range .RequestPayloads}}
    60  	{{.Name}} {{.TypeName}} {{.TagString}}
    61  	{{- end }}
    62  {{- else if .IsRequestPlural -}}
    63  	{{- range .RequestPayloads}}
    64  	{{.Name}} []{{.TypeName}} {{.TagString}}
    65  	{{- end }}
    66  {{ end }}
    67  }
    68  {{ end }}
    69  
    70  {{ if .HasResponseEnvelope }}
    71  // {{ .ResponseEnvelopeStructName }} is envelop of API response
    72  type {{ .ResponseEnvelopeStructName }} struct {
    73  {{- if .IsResponsePlural -}}
    74  	Total       int        ` + "`" + `json:",omitempty"` + "`" + ` // トータル件数
    75  	From        int        ` + "`" + `json:",omitempty"` + "`" + ` // ページング開始ページ
    76  	Count       int        ` + "`" + `json:",omitempty"` + "`" + ` // 件数
    77  {{ else }}
    78  	IsOk    bool  ` + "`" + `json:"is_ok,omitempty"` + "`" + ` // is_ok項目
    79  	Success types.APIResult  ` + "`" + `json:",omitempty"` + "`" + `      // success項目
    80  {{ end }}
    81  {{ if .IsResponseSingular }}
    82  	{{- range .ResponsePayloads}}
    83  	{{.Name}} {{.TypeName}} {{.TagString}}
    84  	{{- end }}
    85  {{- else if .IsResponsePlural -}}
    86  	{{- range .ResponsePayloads}}
    87  	{{.Name}} []{{.TypeName}} {{.TagString}}
    88  	{{- end }}
    89  {{ end }}
    90  }
    91  {{ end }}
    92  
    93  {{- end -}}
    94  {{- end -}}
    95  `