mirror of
https://github.com/opbnq-q/nto-cli.git
synced 2025-12-06 18:20:34 +07:00
feat: field gen
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -2,3 +2,4 @@ main
|
|||||||
main.exe
|
main.exe
|
||||||
frontend
|
frontend
|
||||||
task.py
|
task.py
|
||||||
|
example
|
||||||
@@ -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}"
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
8
main.go
8
main.go
@@ -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)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
func FindFrontendPath() string {
|
func FindFrontendPath() string {
|
||||||
@@ -11,10 +11,20 @@ func FindFrontendPath() string {
|
|||||||
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
|
||||||
|
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"))
|
panic(errors.New("You're not in frontend/src"))
|
||||||
}
|
}
|
||||||
|
|
||||||
var path string
|
var path string
|
||||||
for i, dir := range dirs {
|
for i, dir := range dirs {
|
||||||
if dir == "frontend" {
|
if dir == "frontend" {
|
||||||
@@ -25,5 +35,9 @@ func FindFrontendPath() string {
|
|||||||
}
|
}
|
||||||
path += dir
|
path += dir
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if dirs[0] == "home" {
|
||||||
|
path = "/" + path
|
||||||
|
}
|
||||||
return path
|
return path
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user