github.com/sacloud/iaas-api-go@v1.12.0/internal/tools/gen-api-tracer/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 = "trace/zz_api_tracer.go"
    27  
    28  func init() {
    29  	log.SetFlags(0)
    30  	log.SetPrefix("gen-api-tracer: ")
    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-tracer'; DO NOT EDIT
    45  
    46  package trace
    47  
    48  import (
    49  {{- range .ImportStatements "context" "encoding/json" "log" "sync"}}
    50  	{{ . }}
    51  {{- end }}
    52  )
    53  
    54  var initOnce sync.Once
    55  
    56  // AddClientFactoryHooks add client factory hooks
    57  func AddClientFactoryHooks() {
    58  	initOnce.Do(func(){
    59  		addClientFactoryHooks()
    60  	})
    61  }
    62  
    63  func addClientFactoryHooks() {
    64  {{ range . -}} 
    65  	iaas.AddClientFacotyHookFunc("{{.TypeName}}", func(in interface{}) interface{} {
    66  		return New{{.TypeName}}Tracer(in.(iaas.{{.TypeName}}API))
    67  	})
    68  {{ end -}}
    69  }
    70  
    71  {{ range . }} {{$typeName := .TypeName}} {{ $resource := . }}
    72  /************************************************* 
    73  * {{ $typeName }}Tracer
    74  *************************************************/
    75  
    76  // {{ $typeName }}Tracer is for trace {{ $typeName }}Op operations
    77  type {{ $typeName }}Tracer struct {
    78  	Internal iaas.{{$typeName}}API
    79  }
    80  
    81  // New{{ $typeName}}Tracer creates new {{ $typeName}}Tracer instance
    82  func New{{ $typeName}}Tracer(in iaas.{{$typeName}}API) iaas.{{$typeName}}API {
    83  	return &{{ $typeName}}Tracer {
    84  		Internal: in,
    85  	}
    86  }
    87  
    88  {{ range .Operations }}{{$returnErrStatement := .ReturnErrorStatement}}{{ $operationName := .MethodName }}
    89  // {{ .MethodName }} is API call with trace log
    90  func (t *{{ $typeName }}Tracer) {{ .MethodName }}(ctx context.Context{{if not $resource.IsGlobal}}, zone string{{end}}{{ range .Arguments }}, {{ .ArgName }} {{ .TypeName }}{{ end }}) {{.ResultsStatement}} {
    91  	log.Println("[TRACE] {{ $typeName }}API.{{ .MethodName }} start")
    92  	targetArguments := struct {
    93  {{ if not $resource.IsGlobal }}
    94  		Argzone string
    95  {{ end -}}
    96  {{ range .Arguments -}}
    97  		Arg{{.ArgName}} {{.TypeName}} ` + "`json:\"{{.ArgName}}\"`" + `
    98  {{ end -}}
    99  	} {
   100  {{if not $resource.IsGlobal -}}
   101  		Argzone: zone,
   102  {{ end -}}
   103  {{ range .Arguments -}}
   104  		Arg{{.ArgName}}: {{.ArgName}},
   105  {{ end -}}
   106  	}
   107  	if d, err := json.Marshal(targetArguments); err == nil {
   108  		log.Printf("[TRACE] \targs: %s\n", string(d))
   109  	}
   110  
   111  	defer func() {
   112  		log.Println("[TRACE] {{ $typeName }}API.{{ .MethodName }} end")
   113  	}()
   114  
   115  	{{range .ResultsTypeInfo}}{{.VarName}}, {{end}}err := t.Internal.{{ .MethodName }}(ctx{{if not $resource.IsGlobal}}, zone{{end}}{{ range .Arguments }}, {{ .ArgName }}{{ end }})
   116  	targetResults := struct {
   117  {{ range .ResultsTypeInfo -}}
   118  		{{.FieldName}} {{.Type.GoTypeSourceCode}} 
   119  {{ end -}}
   120  		Error error
   121  	} {
   122  {{ range .ResultsTypeInfo -}}
   123  		{{.FieldName}}: {{.VarName}},
   124  {{ end -}}
   125  		Error: err,
   126  	}
   127  	if d, err := json.Marshal(targetResults); err == nil {
   128  		log.Printf("[TRACE] \tresults: %s\n", string(d))
   129  	}
   130  
   131  	return {{range .ResultsTypeInfo}}{{.VarName}}, {{end}}err
   132  }
   133  {{- end -}}
   134  
   135  {{ end }}
   136  `