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