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
frontend
task.py
example

View File

@@ -1,7 +1,57 @@
package entities
import (
"fmt"
"slices"
"strings"
)
type Field struct {
Name string
Type string
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) {
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 {
panic(err)
}

View File

@@ -27,12 +27,12 @@ func GenerateService(structName, mkPath string) {
panic(err)
}
defer serviceFile.Close()
_, err = serviceFile.WriteString(
fmt.Sprintf(`import { GetAll, Create, Delete, ExportToExcel, GetById, Update, Count } from "%s"
_, err = serviceFile.WriteString(fmt.Sprintf(
`import { GetAll, Create, Delete, ExportToExcel, GetById, Update, Count } 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) {
return await GetById(id)
}

View File

@@ -1,8 +1,9 @@
package main
import (
"fmt"
input "nto_cli/cmd"
"nto_cli/generation"
// "nto_cli/generation"
"nto_cli/utils"
"os"
)
@@ -16,6 +17,9 @@ func main() {
defer file.Close()
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 (
"errors"
"os"
"strings"
"path/filepath"
)
func FindFrontendPath() string {
currentPath, err := os.Getwd()
if err != nil {
panic(err)
}
dirs := strings.Split(currentPath, "\\")
if dirs[len(dirs) - 2] + "/" + dirs[len(dirs) - 1] != "frontend/src" {
panic(errors.New("You're not in frontend/src"))
}
var path string
for i, dir := range dirs {
if dir == "frontend" {
break
}
if i > 0 {
dir = "/" + dir
}
path += dir
}
return path
currentPath, err := os.Getwd()
if err != nil {
panic(err)
}
var dirs []string
for currentPath != "/" {
dir, file := filepath.Split(currentPath)
if file != "" {
dirs = append([]string{file}, dirs...)
}
currentPath = filepath.Clean(dir)
}
if len(dirs) < 2 || dirs[len(dirs)-2]+"/"+dirs[len(dirs)-1] != "frontend/src" {
panic(errors.New("You're not in frontend/src"))
}
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
}