From 08166e75d341927d3708c28485b13c8c01ea24c9 Mon Sep 17 00:00:00 2001 From: opbnq-q Date: Sat, 15 Feb 2025 22:10:46 +0700 Subject: [PATCH] feat: field gen --- .gitignore | 3 ++- entities/field.go | 50 ++++++++++++++++++++++++++++++++++ generation/gen.go | 2 +- generation/service.go | 8 +++--- main.go | 8 ++++-- utils/find_frontend_path.go | 54 +++++++++++++++++++++++-------------- 6 files changed, 97 insertions(+), 28 deletions(-) diff --git a/.gitignore b/.gitignore index 08b230c..4098685 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ main main.exe frontend -task.py \ No newline at end of file +task.py +example \ No newline at end of file diff --git a/entities/field.go b/entities/field.go index d4699b7..034500a 100644 --- a/entities/field.go +++ b/entities/field.go @@ -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}" +} \ No newline at end of file diff --git a/generation/gen.go b/generation/gen.go index 6036360..bd25ffe 100644 --- a/generation/gen.go +++ b/generation/gen.go @@ -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) } diff --git a/generation/service.go b/generation/service.go index a5feca3..cfee4c6 100644 --- a/generation/service.go +++ b/generation/service.go @@ -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) } diff --git a/main.go b/main.go index a8b776a..4928e70 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/utils/find_frontend_path.go b/utils/find_frontend_path.go index fc4401e..069b4d8 100644 --- a/utils/find_frontend_path.go +++ b/utils/find_frontend_path.go @@ -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 } \ No newline at end of file