autoCode.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package autoCodeModel
  2. import (
  3. "fmt"
  4. "html/template"
  5. "os"
  6. )
  7. //开发中功能,若您发现这块代码可以研究,可以无视
  8. type AutoCodeStruct struct {
  9. StructName string `json:"structName"`
  10. PackageName string `json:"packageName"`
  11. Abbreviation string `json:"abbreviation"`
  12. Components []Component `json:"components"`
  13. }
  14. type Component struct {
  15. ComponentName string `json:"componentName"`
  16. ComponentType string `json:"componentType"`
  17. ComponentJson string `json:"componentJson"`
  18. Ismultiple bool `json:"isMultiple"`
  19. ComponentShowType string `json:"componentShowType"`
  20. NideDictionary bool `json:"nideDictionary"`
  21. DictionaryName string `json:"dictionaryName"`
  22. ComponentDictionary []Dictionary `json:"dictionary"`
  23. }
  24. type Dictionary struct {
  25. Label string `json:"label"`
  26. Value string `json:"value"`
  27. }
  28. func Temp() {
  29. tmpl, err := template.ParseFiles("../../tpl/te/struct.go.tpl")
  30. fmt.Println(tmpl, err)
  31. a1 := Component{
  32. ComponentName: "TestComponent",
  33. ComponentType: "string",
  34. ComponentJson: "testComponent",
  35. Ismultiple: false,
  36. ComponentShowType: "",
  37. NideDictionary: false,
  38. DictionaryName: "",
  39. ComponentDictionary: nil,
  40. }
  41. a2 := Component{
  42. ComponentName: "TestBigComponent",
  43. ComponentType: "int",
  44. ComponentJson: "testBigComponent",
  45. Ismultiple: false,
  46. ComponentShowType: "",
  47. NideDictionary: false,
  48. DictionaryName: "",
  49. ComponentDictionary: nil,
  50. }
  51. a := AutoCodeStruct{
  52. StructName: "Test",
  53. PackageName: "autocode",
  54. Abbreviation: "t",
  55. Components: []Component{a1, a2},
  56. }
  57. _dir := "../" + a.PackageName
  58. exist, err := pathExists(_dir)
  59. if err != nil {
  60. //log.L.Info(fmt.Sprintf("get dir error![%v]\n", err))
  61. return
  62. }
  63. if exist {
  64. //log.L.Info(fmt.Sprintf("has dir![%v]\n"+_dir))
  65. } else {
  66. //log.L.Info(fmt.Sprintf("no dir![%v]\n"+_dir))
  67. // 创建文件夹
  68. err := os.Mkdir(_dir, os.ModePerm)
  69. if err != nil {
  70. //log.L.Error(fmt.Sprintf("mkdir error![%v]\n",err))
  71. } else {
  72. //log.L.Info("mkdir success!\n")
  73. }
  74. }
  75. file, err := os.OpenFile("../"+a.PackageName+"/struct.go", os.O_CREATE|os.O_WRONLY, 0755)
  76. err = tmpl.Execute(file, a)
  77. }
  78. // 判断文件夹是否存在
  79. func pathExists(path string) (bool, error) {
  80. _, err := os.Stat(path)
  81. if err == nil {
  82. return true, nil
  83. }
  84. if os.IsNotExist(err) {
  85. return false, nil
  86. }
  87. return false, err
  88. }