github.com/fumiama/NanoBot@v0.0.0-20231122134259-c22d8183efca/codegen/putopenapiof/main.go (about)

     1  package main
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  )
     7  
     8  const head = `// Code generated by codegen/putopenapiof. DO NOT EDIT.
     9  
    10  package nano
    11  
    12  import (
    13  	"io"
    14  
    15  	"github.com/pkg/errors"
    16  )
    17  `
    18  
    19  const template = `
    20  func (bot *Bot) putOpenAPIof[T any](ep string, body io.Reader) (*[T any], error) {
    21  	resp := &struct {
    22  		CodeMessageBase
    23  		[T any]
    24  	}{}
    25  	err := bot.PutOpenAPI(ep, "", resp, body)
    26  	if err != nil {
    27  		err = errors.Wrap(err, getCallerFuncName())
    28  	}
    29  	return &resp.[T any], err
    30  }
    31  `
    32  
    33  func main() {
    34  	f, err := os.Create("openapi_codegen_putopenapiof.go")
    35  	if err != nil {
    36  		panic(err)
    37  	}
    38  	defer f.Close()
    39  	_, err = f.WriteString(head)
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  	for _, name := range os.Args[1:] {
    44  		_, err = f.WriteString(strings.ReplaceAll(template, "[T any]", name))
    45  		if err != nil {
    46  			panic(err)
    47  		}
    48  	}
    49  }