feat: field gen

This commit is contained in:
2025-02-15 22:10:46 +07:00
parent 011249d3e3
commit 08166e75d3
6 changed files with 97 additions and 28 deletions

1
.gitignore vendored
View File

@@ -2,3 +2,4 @@ main
main.exe main.exe
frontend frontend
task.py task.py
example

View File

@@ -1,7 +1,57 @@
package entities package entities
import (
"fmt"
"slices"
"strings"
)
type Field struct { type Field struct {
Name string Name string
Type string Type string
Medatada []Medatada Medatada []Medatada
} }
var PRIMITIVE_TYPES = []string{"date", "number", "string", "multiple", "boolean"}
func (f *Field) GenerateType() string {
result := " type: {\n"
if slices.Contains(PRIMITIVE_TYPES, strings.ToLower(f.Type)) {
result += fmt.Sprintf(` primitive: "%s",`, strings.ToLower(f.Type))
} else {
var field string
for _, meta := range f.Medatada {
if meta.Name == "field" {
field = "['" + strings.Join(meta.Values, "', '") + "']"
}
}
result += fmt.Sprintf(` nested: {
values: [],
field: %s
}, `, field)
}
result += "\n }"
for _, meta := range f.Medatada {
if meta.Name == "many" {
result += "\n many: true }"
}
}
return result
}
func (f *Field) Generate() string {
result := "{\n"
for _, meta := range f.Medatada {
if (meta.Name == "hidden") {
result += " hidden: true,\n"
} else if meta.Name == "label" {
result += fmt.Sprintf(` russian: "%s",` + "\n", meta.Values[0])
} else if (meta.Name == "readonly") {
result += " readonly: true,\n"
}
}
result += f.GenerateType()
return result + "\n}"
}

View File

@@ -9,7 +9,7 @@ import (
) )
func Generate(structName string, fields []entities.Field) { func Generate(structName string, fields []entities.Field) {
mkPath := strings.ToLower(fmt.Sprintf("%s/frontend/src/%s", utils.FindFrontendPath() , structName)) mkPath := fmt.Sprintf("%s/frontend/src/%s", utils.FindFrontendPath() , strings.ToLower(structName))
if err := os.Mkdir(mkPath, 0755); err != nil { if err := os.Mkdir(mkPath, 0755); err != nil {
panic(err) panic(err)
} }

View File

@@ -27,12 +27,12 @@ func GenerateService(structName, mkPath string) {
panic(err) panic(err)
} }
defer serviceFile.Close() defer serviceFile.Close()
_, err = serviceFile.WriteString( _, err = serviceFile.WriteString(fmt.Sprintf(
fmt.Sprintf(`import { GetAll, Create, Delete, ExportToExcel, GetById, Update, Count } from "%s" `import { GetAll, Create, Delete, ExportToExcel, GetById, Update, Count } from "%s"
import type { %s } from "%s" import type { %s } from "%s"
import type { Service } from "%s" import type { IService } from "%s"
export class %sService implements Service<%s> { export class %sService implements IService<%s> {
async read(id: number) { async read(id: number) {
return await GetById(id) return await GetById(id)
} }

View File

@@ -1,8 +1,9 @@
package main package main
import ( import (
"fmt"
input "nto_cli/cmd" input "nto_cli/cmd"
"nto_cli/generation" // "nto_cli/generation"
"nto_cli/utils" "nto_cli/utils"
"os" "os"
) )
@@ -16,6 +17,9 @@ func main() {
defer file.Close() defer file.Close()
structFields := utils.GetStructFields(file, structName) structFields := utils.GetStructFields(file, structName)
for _, field := range structFields {
fmt.Println(field.Generate())
}
generation.Generate(structName, structFields) // generation.Generate(structName, structFields)
} }

View File

@@ -3,27 +3,41 @@ package utils
import ( import (
"errors" "errors"
"os" "os"
"strings" "path/filepath"
) )
func FindFrontendPath() string { func FindFrontendPath() string {
currentPath, err := os.Getwd() currentPath, err := os.Getwd()
if err != nil { if err != nil {
panic(err) panic(err)
} }
dirs := strings.Split(currentPath, "\\")
if dirs[len(dirs) - 2] + "/" + dirs[len(dirs) - 1] != "frontend/src" { var dirs []string
panic(errors.New("You're not in frontend/src")) for currentPath != "/" {
} dir, file := filepath.Split(currentPath)
var path string if file != "" {
for i, dir := range dirs { dirs = append([]string{file}, dirs...)
if dir == "frontend" { }
break currentPath = filepath.Clean(dir)
} }
if i > 0 {
dir = "/" + dir if len(dirs) < 2 || dirs[len(dirs)-2]+"/"+dirs[len(dirs)-1] != "frontend/src" {
} panic(errors.New("You're not in frontend/src"))
path += dir }
}
return path var path string
for i, dir := range dirs {
if dir == "frontend" {
break
}
if i > 0 {
dir = "/" + dir
}
path += dir
}
if dirs[0] == "home" {
path = "/" + path
}
return path
} }