params.go 764 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package got
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. . "git.eugeniocarvalho.dev/eugeniucarvalho/apicodegen/common"
  6. G "github.com/dave/jennifer/jen"
  7. )
  8. func createParamsFile(p *Project) (err error) {
  9. var (
  10. file = G.NewFile(p.Package)
  11. out []byte
  12. )
  13. if out, err = json.Marshal(p.Resource.CommonParams); err != nil {
  14. return
  15. }
  16. input := fmt.Sprintf("`%s`", string(out))
  17. file.Id(`
  18. var (
  19. UserRequestParams = map[string]*`).Qual(CODE_GEN_V2_COMMON, "Parameter").Id(fmt.Sprintf(`{}
  20. )
  21. func init(){
  22. input := %s
  23. `, input)).Id(`if err :=`).Qual("encoding/json", "Unmarshal").Id(`([]byte(input),&UserRequestParams); err != nil {
  24. panic(err)
  25. }
  26. }`)
  27. // `).Id(string(out)).Id(`
  28. return Write(fmt.Sprintf("%s/%s/params.go", p.OutPath, p.Package), file)
  29. }