github.com/sacloud/iaas-api-go@v1.12.0/internal/tools/gen-api-models/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_models.go"
    26  
    27  func init() {
    28  	log.SetFlags(0)
    29  	log.SetPrefix("gen-api-models: ")
    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-models'; DO NOT EDIT
    43  
    44  package iaas
    45  
    46  import (
    47  {{- range .ImportStatementsForModelDef "github.com/sacloud/iaas-api-go/accessor" }}
    48  	{{ . }}
    49  {{- end }}
    50  )
    51  
    52  {{ range .Models }}
    53  
    54  /************************************************* 
    55  * {{.Name}}
    56  *************************************************/
    57  
    58  // {{ .Name }} represents API parameter/response structure
    59  type {{ .Name }} struct {
    60  	{{- range .Fields }}
    61  	{{.Name}} {{.TypeName}} {{if .HasTag }}` + "`" + `{{.TagString}}` + "`" + `{{end}}
    62  	{{- end }}
    63  }
    64  
    65  // setDefaults implements iaas.argumentDefaulter 
    66  func (o *{{.Name}}) setDefaults() interface{} {
    67  	return &struct {
    68  	{{- range .Fields }}
    69  	{{.Name}} {{.TypeName}} {{if .HasTag }}` + "`" + `{{.TagString}}` + "`" + `{{end}}
    70  	{{- end }}
    71  	{{- range .ConstFields }}
    72  	{{.Name}} {{.TypeName}} {{if .HasTag }}` + "`" + `{{.TagString}}` + "`" + `{{end}}
    73  	{{- end }}
    74  	} {
    75  	{{- range .Fields }}
    76  	{{.Name}}: o.Get{{.Name}}(),
    77  	{{- end }}
    78  	{{- range .ConstFields }}
    79  	{{.Name}}: {{.Value}},
    80  	{{- end }}
    81  	}
    82  }
    83  
    84  {{- $struct := .Name -}}
    85  {{- range .Methods }}
    86  // {{.Name}} {{if .Description}}{{.Description}}{{else}}.{{end}}
    87  func (o *{{ $struct }}) {{ .Name }}({{ range .Arguments }}{{ .ArgName }} {{ .TypeName }},{{ end }}) ({{ range .ResultTypes }}{{.GoTypeSourceCode}},{{end}}) {
    88  	{{ if .ResultTypes }}return {{ end }}accessor.{{if eq .AccessorFuncName ""}}{{.Name}}{{else}}{{.AccessorFuncName}}{{end}}(o,{{ range .Arguments }}{{ .ArgName }},{{ end }})
    89  }
    90  {{- end }}
    91  
    92  {{- range .Fields }} {{ $name := .Name }}{{ $typeName := .TypeName }}
    93  // Get{{$name}} returns value of {{$name}} 
    94  func (o *{{ $struct }}) Get{{$name}}() {{$typeName}} {
    95  	{{ if .DefaultValue -}}
    96  	{{ if eq .Type.GoType "time.Time" -}}
    97  	if o.{{$name}}.IsZero() {
    98  		return {{.DefaultValue}}
    99  	}
   100  	{{ else -}}
   101  	if o.{{$name}} == {{.Type.ZeroInitializeSourceCode}}{
   102  		return {{.DefaultValue}}
   103  	}
   104  	{{ end -}}
   105  	{{ end -}}
   106  	return o.{{$name}}
   107  }
   108  
   109  // Set{{$name}} sets value to {{$name}} 
   110  func (o *{{ $struct }}) Set{{$name}}(v {{$typeName}}) {
   111  	o.{{$name}} = v
   112  }
   113  
   114  {{- range .Methods }}
   115  // {{.Name}} {{if .Description}}{{.Description}}{{else}}.{{end}}
   116  func (o *{{ $struct }}) {{ .Name }}({{ range .Arguments }}{{ .ArgName }} {{ .TypeName }},{{ end }}) ({{ range .ResultTypes }}{{.GoTypeSourceCode}},{{end}}) {
   117  	{{ if .ResultTypes }}return {{ end }}accessor.{{if eq .AccessorFuncName ""}}{{.Name}}{{else}}{{.AccessorFuncName}}{{end}}(o,{{ range .Arguments }}{{ .ArgName }},{{ end }})
   118  }
   119  {{- end }}
   120  {{- end }} {{/* end of range .Fields */}}
   121  
   122  {{- end }} {{/* end of range .Models */}}
   123  `