github.com/sacloud/libsacloud/v2@v2.32.3/internal/tools/gen-api-models/main.go (about)

     1  // Copyright 2016-2022 The Libsacloud 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/libsacloud/v2/internal/define"
    22  	"github.com/sacloud/libsacloud/v2/internal/tools"
    23  )
    24  
    25  const destination = "sacloud/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/libsacloud/internal/tools/gen-api-models'; DO NOT EDIT
    43  
    44  package sacloud
    45  
    46  import (
    47  {{- range .ImportStatementsForModelDef "github.com/sacloud/libsacloud/v2/helper/validate" "github.com/sacloud/libsacloud/v2/sacloud/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  // Validate validates by field tags
    66  func (o *{{ .Name}}) Validate() error {
    67  	return validate.Struct(o)
    68  }
    69  
    70  // setDefaults implements sacloud.argumentDefaulter 
    71  func (o *{{.Name}}) setDefaults() interface{} {
    72  	return &struct {
    73  	{{- range .Fields }}
    74  	{{.Name}} {{.TypeName}} {{if .HasTag }}` + "`" + `{{.TagString}}` + "`" + `{{end}}
    75  	{{- end }}
    76  	{{- range .ConstFields }}
    77  	{{.Name}} {{.TypeName}} {{if .HasTag }}` + "`" + `{{.TagString}}` + "`" + `{{end}}
    78  	{{- end }}
    79  	} {
    80  	{{- range .Fields }}
    81  	{{.Name}}: o.Get{{.Name}}(),
    82  	{{- end }}
    83  	{{- range .ConstFields }}
    84  	{{.Name}}: {{.Value}},
    85  	{{- end }}
    86  	}
    87  }
    88  
    89  {{- $struct := .Name -}}
    90  {{- range .Methods }}
    91  // {{.Name}} {{if .Description}}{{.Description}}{{else}}.{{end}}
    92  func (o *{{ $struct }}) {{ .Name }}({{ range .Arguments }}{{ .ArgName }} {{ .TypeName }},{{ end }}) ({{ range .ResultTypes }}{{.GoTypeSourceCode}},{{end}}) {
    93  	{{ if .ResultTypes }}return {{ end }}accessor.{{if eq .AccessorFuncName ""}}{{.Name}}{{else}}{{.AccessorFuncName}}{{end}}(o,{{ range .Arguments }}{{ .ArgName }},{{ end }})
    94  }
    95  {{- end }}
    96  
    97  {{- range .Fields }} {{ $name := .Name }}{{ $typeName := .TypeName }}
    98  // Get{{$name}} returns value of {{$name}} 
    99  func (o *{{ $struct }}) Get{{$name}}() {{$typeName}} {
   100  	{{ if .DefaultValue -}}
   101  	{{ if eq .Type.GoType "time.Time" -}}
   102  	if o.{{$name}}.IsZero() {
   103  		return {{.DefaultValue}}
   104  	}
   105  	{{ else -}}
   106  	if o.{{$name}} == {{.Type.ZeroInitializeSourceCode}}{
   107  		return {{.DefaultValue}}
   108  	}
   109  	{{ end -}}
   110  	{{ end -}}
   111  	return o.{{$name}}
   112  }
   113  
   114  // Set{{$name}} sets value to {{$name}} 
   115  func (o *{{ $struct }}) Set{{$name}}(v {{$typeName}}) {
   116  	o.{{$name}} = v
   117  }
   118  
   119  {{- range .Methods }}
   120  // {{.Name}} {{if .Description}}{{.Description}}{{else}}.{{end}}
   121  func (o *{{ $struct }}) {{ .Name }}({{ range .Arguments }}{{ .ArgName }} {{ .TypeName }},{{ end }}) ({{ range .ResultTypes }}{{.GoTypeSourceCode}},{{end}}) {
   122  	{{ if .ResultTypes }}return {{ end }}accessor.{{if eq .AccessorFuncName ""}}{{.Name}}{{else}}{{.AccessorFuncName}}{{end}}(o,{{ range .Arguments }}{{ .ArgName }},{{ end }})
   123  }
   124  {{- end }}
   125  {{- end }} {{/* end of range .Fields */}}
   126  
   127  {{- end }} {{/* end of range .Models */}}
   128  `