2023-07-26 16:19:28 +03:00
|
|
|
|
package templ
|
2023-06-23 14:54:47 +03:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"bytes"
|
|
|
|
|
"embed"
|
2023-08-02 16:04:17 +03:00
|
|
|
|
"errors"
|
2023-06-26 15:35:24 +03:00
|
|
|
|
"fmt"
|
2023-06-23 14:54:47 +03:00
|
|
|
|
"log"
|
|
|
|
|
"os"
|
2023-08-02 16:04:17 +03:00
|
|
|
|
"path/filepath"
|
2023-07-18 14:33:58 +03:00
|
|
|
|
"regexp"
|
2023-06-23 14:54:47 +03:00
|
|
|
|
"strings"
|
|
|
|
|
"text/template"
|
2023-06-26 15:35:24 +03:00
|
|
|
|
|
|
|
|
|
"git.ymnuktech.ru/ymnuk/yt-gen-app/structs"
|
2023-08-17 22:49:02 +03:00
|
|
|
|
uuid "github.com/satori/go.uuid"
|
2023-06-23 14:54:47 +03:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
//go:embed tmpl/*
|
2023-07-26 16:19:28 +03:00
|
|
|
|
var Content embed.FS
|
2023-06-23 14:54:47 +03:00
|
|
|
|
|
|
|
|
|
var funcMap = template.FuncMap{
|
2023-11-07 16:35:47 +03:00
|
|
|
|
|
|
|
|
|
//"includeTemplPart": IncludeTemplPart,
|
|
|
|
|
|
2023-07-26 16:19:28 +03:00
|
|
|
|
"fieldName": FieldName,
|
2023-08-14 16:30:06 +03:00
|
|
|
|
"fieldChildName": FieldChildName,
|
2023-07-26 16:19:28 +03:00
|
|
|
|
"fieldNamePrepare": FieldNamePrepare,
|
|
|
|
|
"fieldNameLowerPrepare": FieldNameLowerPrepare,
|
2023-08-17 22:49:02 +03:00
|
|
|
|
"fieldJsonNameStr": FieldJsonNameStr,
|
2023-07-26 16:19:28 +03:00
|
|
|
|
"fieldType": FieldType,
|
2023-11-07 16:35:47 +03:00
|
|
|
|
"fieldTypeStr": FieldTypeStr,
|
2023-09-01 10:39:12 +03:00
|
|
|
|
"fieldStringToType": FieldStringToType,
|
2023-08-14 16:30:06 +03:00
|
|
|
|
//"fieldDbTableType": FieldDbTableType,
|
2023-11-07 16:35:47 +03:00
|
|
|
|
"fieldTypeDB": FieldTypeDB,
|
|
|
|
|
"fieldTypeDBStr": FieldTypeDBStr,
|
2023-08-14 16:30:06 +03:00
|
|
|
|
"fieldTypeParentTable": FieldTypeParentTable,
|
|
|
|
|
"hasFieldType": HasFieldType,
|
|
|
|
|
"fieldDescript": FieldDescript,
|
|
|
|
|
"configParamName": ConfigParamName,
|
|
|
|
|
"configParamType": ConfigParamType,
|
|
|
|
|
"configParamTag": ConfigParamTag,
|
|
|
|
|
"packageName": PackageName,
|
2023-08-02 16:04:17 +03:00
|
|
|
|
|
|
|
|
|
"methodNameGetId": MethodNameGetId,
|
|
|
|
|
|
|
|
|
|
"methodSummary": MethodSummary,
|
|
|
|
|
"methodComment": MethodComment,
|
|
|
|
|
"getModelName": GetModelName,
|
2023-08-17 22:49:02 +03:00
|
|
|
|
"getModelNameAngular": GetModelNameAngular,
|
2023-08-02 16:04:17 +03:00
|
|
|
|
"displayMethodNamePost": DisplayMethodNamePost,
|
|
|
|
|
"methodNamePost": MethodNamePost,
|
|
|
|
|
"displayMethodNameGet": DisplayMethodNameGet,
|
|
|
|
|
"methodNameGet": MethodNameGet,
|
2023-08-09 14:45:36 +03:00
|
|
|
|
|
|
|
|
|
"isMethod": IsMethod,
|
|
|
|
|
"roles": Roles,
|
2023-08-11 12:28:30 +03:00
|
|
|
|
|
|
|
|
|
"removeFirstChar": RemoveFirstChar,
|
|
|
|
|
|
|
|
|
|
"angularRestName": AngularRestName,
|
|
|
|
|
"angularFilename": AngularFilename,
|
2023-08-17 22:49:02 +03:00
|
|
|
|
|
|
|
|
|
"getFieldsByModelID": GetFieldsByModelID,
|
|
|
|
|
"getFksByModelID": GetFksByModelID,
|
2023-08-18 12:30:22 +03:00
|
|
|
|
"getFksByModelID2": GetFksByModelID2,
|
|
|
|
|
|
|
|
|
|
"fieldToVisField": FieldToVisField,
|
|
|
|
|
"fieldToVisField2": FieldToVisField2,
|
|
|
|
|
"fieldJsonDescriptionStrClear": FieldJsonDescriptionStrClear,
|
2023-08-25 15:05:28 +03:00
|
|
|
|
|
|
|
|
|
"backendFsPath": BackendFsPath,
|
|
|
|
|
"parseRestPath": ParseRestPath,
|
|
|
|
|
"parseRestPathForName": ParseRestPathForName,
|
|
|
|
|
"parseRestPathParams": ParseRestPathParams,
|
2023-09-01 10:39:12 +03:00
|
|
|
|
"fieldInTableByName": FieldInTableByName,
|
2023-11-12 14:33:05 +03:00
|
|
|
|
|
|
|
|
|
"escapeChars": EscapeChars,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func EscapeChars(value string) string {
|
|
|
|
|
value = strings.ReplaceAll(value, "/", "\\/")
|
|
|
|
|
return value
|
2023-08-09 14:45:36 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-07 16:35:47 +03:00
|
|
|
|
func IncludeTemplPart(templName string, data interface{}) string {
|
|
|
|
|
var err error
|
|
|
|
|
var tmpl *template.Template
|
|
|
|
|
if tmpl, err = ReadTmplFile(templName); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
var out []byte
|
|
|
|
|
if out, err = ExecuteTmplFile(tmpl, data); err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
/*arr:=strings.Split(string(out))
|
|
|
|
|
var newArr []string
|
|
|
|
|
for*/
|
|
|
|
|
return string(out)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-09 14:45:36 +03:00
|
|
|
|
func IsMethod(arr map[string][]string, method string) bool {
|
|
|
|
|
if arr == nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
_, ok := arr[method]
|
|
|
|
|
return ok
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Roles(arr map[string][]string, method string) []string {
|
|
|
|
|
if arr == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return arr[method]
|
2023-06-23 14:54:47 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 16:19:28 +03:00
|
|
|
|
func FieldNamePrepare(value string) string {
|
2023-07-18 14:33:58 +03:00
|
|
|
|
value = strings.Trim(value, " ")
|
|
|
|
|
if value == "" {
|
2023-08-14 16:30:06 +03:00
|
|
|
|
//log.SetFlags(log.Llongfile)
|
2023-07-18 14:33:58 +03:00
|
|
|
|
log.Fatal("Should be set name for field")
|
2023-06-23 14:54:47 +03:00
|
|
|
|
}
|
2023-07-18 14:33:58 +03:00
|
|
|
|
asRunes := []rune(value)
|
2023-06-23 14:54:47 +03:00
|
|
|
|
|
2023-07-18 14:33:58 +03:00
|
|
|
|
asRunes[0] = []rune(strings.ToUpper(string([]rune(value)[0])))[0]
|
|
|
|
|
value = string(asRunes)
|
|
|
|
|
i := strings.Index(value, "_")
|
2023-06-23 14:54:47 +03:00
|
|
|
|
for i != -1 {
|
2023-07-18 14:33:58 +03:00
|
|
|
|
asRunes := []rune(value)
|
|
|
|
|
if i >= len(value) {
|
2023-06-23 14:54:47 +03:00
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
asRunes[i+1] = []rune(strings.ToUpper(string(asRunes[i+1])))[0]
|
2023-07-18 14:33:58 +03:00
|
|
|
|
asRunes = append(asRunes[:i], asRunes[i+1:]...)
|
|
|
|
|
value = string(asRunes)
|
|
|
|
|
i = strings.Index(value, "_")
|
2023-06-26 15:35:24 +03:00
|
|
|
|
}
|
2023-11-02 13:53:45 +03:00
|
|
|
|
var regexpForReplace = regexp.MustCompile(`([Ii][Dd]$)`)
|
|
|
|
|
value = regexpForReplace.ReplaceAllString(value, "ID")
|
2023-07-18 14:33:58 +03:00
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 16:19:28 +03:00
|
|
|
|
func FieldNameLowerPrepare(value string) string {
|
2023-07-18 14:33:58 +03:00
|
|
|
|
var regexpForReplace = regexp.MustCompile(`([A-Z])`)
|
|
|
|
|
value = strings.ToLower(regexpForReplace.ReplaceAllString(value, "_$1"))
|
|
|
|
|
if []rune(value)[0] == '_' {
|
|
|
|
|
value = string([]rune(value)[1:])
|
|
|
|
|
}
|
|
|
|
|
value = strings.ReplaceAll(value, "i_d", "id")
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 16:19:28 +03:00
|
|
|
|
func FieldName(field *structs.Field) string {
|
2023-07-18 14:33:58 +03:00
|
|
|
|
if field == nil {
|
|
|
|
|
log.Fatal("Field is null")
|
|
|
|
|
}
|
|
|
|
|
field.Name = strings.Trim(field.Name, " ")
|
|
|
|
|
if field.Name == "" {
|
|
|
|
|
log.Fatal("Should be set name for field")
|
|
|
|
|
}
|
2023-07-26 16:19:28 +03:00
|
|
|
|
field.Name = FieldNamePrepare(field.Name)
|
2023-06-26 15:35:24 +03:00
|
|
|
|
return field.Name
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-14 16:30:06 +03:00
|
|
|
|
func FieldChildName(field *structs.Field) string {
|
|
|
|
|
return FieldName(field)
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-26 15:35:24 +03:00
|
|
|
|
// Возвращает имя в БД
|
2023-07-26 16:19:28 +03:00
|
|
|
|
func FieldDBName(field *structs.Field) (res string) {
|
2023-06-26 15:35:24 +03:00
|
|
|
|
res = strings.Trim(field.Name, " ")
|
|
|
|
|
if res == "" {
|
|
|
|
|
log.Fatal("Shoudl be set name for field")
|
|
|
|
|
}
|
2023-08-10 15:37:50 +03:00
|
|
|
|
//res = strings.ToLower(res)
|
|
|
|
|
res = FieldNameLowerPrepare(res)
|
2023-06-26 15:35:24 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-14 16:30:06 +03:00
|
|
|
|
func FieldTypeParentTable(field *structs.Field) string {
|
|
|
|
|
return FieldType(&structs.Field{Type: field.TypeParentTable})
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-07 16:35:47 +03:00
|
|
|
|
func FieldTypeStr(value string) string {
|
|
|
|
|
value = strings.ToLower(strings.Trim(value, " "))
|
|
|
|
|
if value == "" {
|
|
|
|
|
return value
|
2023-06-26 15:35:24 +03:00
|
|
|
|
}
|
2023-11-07 16:35:47 +03:00
|
|
|
|
switch value {
|
2023-06-26 15:35:24 +03:00
|
|
|
|
case "text":
|
|
|
|
|
return "*string"
|
|
|
|
|
case "string":
|
|
|
|
|
return "*string"
|
|
|
|
|
case "int":
|
|
|
|
|
return "*int"
|
|
|
|
|
case "bigint":
|
|
|
|
|
return "*int64"
|
2023-08-10 09:36:16 +03:00
|
|
|
|
case "double":
|
|
|
|
|
return "*float64"
|
2023-08-10 09:42:00 +03:00
|
|
|
|
case "float":
|
|
|
|
|
return "*float32"
|
2023-07-19 12:05:56 +03:00
|
|
|
|
case "uuid":
|
|
|
|
|
return "*uuid.UUID"
|
2023-08-10 09:56:00 +03:00
|
|
|
|
case "date":
|
|
|
|
|
return "*time.Time"
|
|
|
|
|
case "time":
|
|
|
|
|
return "*time.Time"
|
2023-08-14 16:30:06 +03:00
|
|
|
|
case "bool":
|
|
|
|
|
return "*bool"
|
2023-08-25 15:05:28 +03:00
|
|
|
|
case "any":
|
|
|
|
|
return "[]byte"
|
2023-06-26 15:35:24 +03:00
|
|
|
|
default:
|
2023-08-14 16:30:06 +03:00
|
|
|
|
fkTmp := &structs.Field{
|
2023-11-07 16:35:47 +03:00
|
|
|
|
Name: value,
|
2023-08-14 16:30:06 +03:00
|
|
|
|
}
|
|
|
|
|
return FieldName(fkTmp)
|
|
|
|
|
//log.Fatalf("Unknow format %s", field.Type)
|
2023-06-23 14:54:47 +03:00
|
|
|
|
}
|
2023-11-07 16:35:47 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func FieldType(field *structs.Field) string {
|
|
|
|
|
if field == nil {
|
|
|
|
|
log.Fatal("field is empty")
|
|
|
|
|
}
|
|
|
|
|
field.Type = strings.ToLower(strings.Trim(field.Type, " "))
|
|
|
|
|
value := FieldTypeStr(field.Type)
|
|
|
|
|
if value == "" {
|
|
|
|
|
field.Type = "text"
|
|
|
|
|
value = "text"
|
|
|
|
|
}
|
|
|
|
|
return value
|
2023-08-14 16:30:06 +03:00
|
|
|
|
//return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func FieldDbTableType(field *structs.Field) string {
|
|
|
|
|
return FieldNamePrepare(FieldDBName(&structs.Field{
|
|
|
|
|
Name: field.Type,
|
|
|
|
|
}))
|
2023-06-26 15:35:24 +03:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 16:19:28 +03:00
|
|
|
|
func HasFieldType(fields []structs.Field, typeName string) (has bool) {
|
2023-07-19 16:02:07 +03:00
|
|
|
|
for _, field := range fields {
|
|
|
|
|
if (field.Type == "date" || field.Type == "time") && (typeName == "date" || typeName == "time") {
|
|
|
|
|
return true
|
|
|
|
|
}
|
2023-08-14 16:30:06 +03:00
|
|
|
|
if field.Type == typeName || field.TypeParentTable == typeName {
|
2023-07-19 16:02:07 +03:00
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-07 16:35:47 +03:00
|
|
|
|
func FieldTypeDBStr(value string, length *int) string {
|
|
|
|
|
switch value {
|
2023-06-26 15:35:24 +03:00
|
|
|
|
case "text":
|
|
|
|
|
return "TEXT"
|
|
|
|
|
case "string":
|
|
|
|
|
tmp := "VARCHAR"
|
2023-11-07 16:35:47 +03:00
|
|
|
|
if length != nil && *length == 0 {
|
|
|
|
|
tmp += fmt.Sprintf("(%d)", *length)
|
2023-06-26 15:35:24 +03:00
|
|
|
|
} else {
|
|
|
|
|
tmp += "(255)"
|
|
|
|
|
}
|
|
|
|
|
//return "TEXT"
|
|
|
|
|
return tmp
|
|
|
|
|
case "int":
|
|
|
|
|
return "INT"
|
|
|
|
|
case "bigint":
|
|
|
|
|
return "BIGINT"
|
2023-07-19 12:05:56 +03:00
|
|
|
|
case "uuid":
|
|
|
|
|
return "UUID"
|
2023-08-10 09:42:00 +03:00
|
|
|
|
case "float":
|
|
|
|
|
return "FLOAT"
|
2023-08-10 09:36:16 +03:00
|
|
|
|
case "double":
|
|
|
|
|
return "DOUBLE"
|
2023-08-10 09:56:00 +03:00
|
|
|
|
case "date":
|
|
|
|
|
return "DATE"
|
|
|
|
|
case "time":
|
|
|
|
|
return "timestamp"
|
2023-08-14 16:30:06 +03:00
|
|
|
|
case "bool":
|
|
|
|
|
return "int"
|
2023-06-26 15:35:24 +03:00
|
|
|
|
default:
|
2023-11-07 16:35:47 +03:00
|
|
|
|
log.Fatalf("Unknow format %s", value)
|
2023-06-26 15:35:24 +03:00
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-07 16:35:47 +03:00
|
|
|
|
func FieldTypeDB(field *structs.Field) string {
|
|
|
|
|
field.Type = strings.ToLower(strings.Trim(field.Type, " "))
|
|
|
|
|
return FieldTypeDBStr(field.Type, field.Length)
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-26 15:35:24 +03:00
|
|
|
|
// Генерирование описания таблиц в БД
|
2023-08-10 16:01:02 +03:00
|
|
|
|
func FieldDescript(field *structs.Field, fieldId bool) (str string) {
|
|
|
|
|
str = "`"
|
|
|
|
|
if fieldId {
|
|
|
|
|
str = fmt.Sprintf("`gorm:\"column:%s;type:%s", FieldDBName(field), FieldTypeDB(field))
|
|
|
|
|
field.Description = strings.Trim(field.Description, " ")
|
|
|
|
|
if field.Description != "" {
|
|
|
|
|
str += fmt.Sprintf(";comment:%s", field.Description)
|
|
|
|
|
}
|
|
|
|
|
str += "\""
|
|
|
|
|
}
|
|
|
|
|
if fieldId {
|
|
|
|
|
str = fmt.Sprintf("%s json:\"%sId,omitempty\"`", str, FieldJsonName(field))
|
|
|
|
|
} else {
|
|
|
|
|
str = fmt.Sprintf("%s json:\"%s,omitempty\"`", str, FieldJsonName(field))
|
|
|
|
|
}
|
2023-06-26 15:35:24 +03:00
|
|
|
|
|
2023-06-23 14:54:47 +03:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-18 14:33:58 +03:00
|
|
|
|
// Вернуть название параметра
|
2023-07-26 16:19:28 +03:00
|
|
|
|
func ConfigParamName(field *structs.ParamConfig) string {
|
2023-07-18 14:33:58 +03:00
|
|
|
|
if field == nil {
|
|
|
|
|
log.Fatal("Field is null")
|
|
|
|
|
}
|
|
|
|
|
field.Name = strings.Trim(field.Name, " ")
|
|
|
|
|
if field.Name == "" {
|
|
|
|
|
log.Fatal("Should be set name for field")
|
|
|
|
|
}
|
2023-07-26 16:19:28 +03:00
|
|
|
|
return FieldNamePrepare(field.Name)
|
2023-07-18 14:33:58 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Вернуть тип параметра
|
2023-07-26 16:19:28 +03:00
|
|
|
|
func ConfigParamType(field *structs.ParamConfig) string {
|
2023-07-18 14:33:58 +03:00
|
|
|
|
if field == nil {
|
|
|
|
|
log.Fatal("Param should be set")
|
|
|
|
|
}
|
|
|
|
|
field.Type = strings.Trim(field.Type, " ")
|
|
|
|
|
if field.Type == "" {
|
|
|
|
|
log.Fatal("Param type should be set")
|
|
|
|
|
}
|
|
|
|
|
switch field.Type {
|
|
|
|
|
case "string":
|
|
|
|
|
return "string"
|
|
|
|
|
default:
|
|
|
|
|
log.Fatal("Unknow type")
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Вернуть строку для тэгирования параметров
|
2023-07-26 16:19:28 +03:00
|
|
|
|
func ConfigParamTag(field *structs.ParamConfig) string {
|
2023-07-18 14:33:58 +03:00
|
|
|
|
if field == nil {
|
|
|
|
|
log.Fatal("Param should be set")
|
|
|
|
|
}
|
|
|
|
|
if field == nil {
|
|
|
|
|
log.Fatal("Field is null")
|
|
|
|
|
}
|
|
|
|
|
field.Name = strings.Trim(field.Name, " ")
|
|
|
|
|
if field.Name == "" {
|
|
|
|
|
log.Fatal("Shoudl be set name for field")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tag := fmt.Sprintf("`arg:\"--%s,env:%s\"", strings.ReplaceAll(strings.ToLower(field.Name), "_", "-"), strings.ReplaceAll(strings.ToUpper(field.Name), "-", "_"))
|
|
|
|
|
field.Help = strings.Trim(field.Help, " ")
|
|
|
|
|
if field.Help != "" {
|
|
|
|
|
tag += fmt.Sprintf(" help:\"%s\"", field.Help)
|
|
|
|
|
}
|
|
|
|
|
tag += "`"
|
|
|
|
|
return tag
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-23 14:54:47 +03:00
|
|
|
|
func WriteTmplFile(filename string, outname string) error {
|
2023-07-21 15:42:28 +03:00
|
|
|
|
fmt.Printf("Generate: %s\n", outname)
|
2023-07-26 16:19:28 +03:00
|
|
|
|
if buff, err := Content.ReadFile(filename); err != nil {
|
2023-06-23 14:54:47 +03:00
|
|
|
|
return err
|
|
|
|
|
} else {
|
|
|
|
|
if err = os.WriteFile(outname, buff, 0755); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-07 16:35:47 +03:00
|
|
|
|
// Чтение шаблона и его подготовка
|
|
|
|
|
func ReadTmplFile(filename string) (tmpl *template.Template, err error) {
|
2023-06-23 14:54:47 +03:00
|
|
|
|
var (
|
|
|
|
|
buff []byte
|
|
|
|
|
)
|
2023-07-26 16:19:28 +03:00
|
|
|
|
if buff, err = Content.ReadFile(filename); err != nil {
|
2023-11-07 16:35:47 +03:00
|
|
|
|
return
|
2023-06-23 14:54:47 +03:00
|
|
|
|
}
|
|
|
|
|
var (
|
2023-11-07 16:35:47 +03:00
|
|
|
|
tmp string
|
|
|
|
|
//tmpl *template.Template
|
2023-06-23 14:54:47 +03:00
|
|
|
|
)
|
|
|
|
|
if tmp, err = RandomHex(4); err != nil {
|
2023-11-07 16:35:47 +03:00
|
|
|
|
return
|
2023-06-23 14:54:47 +03:00
|
|
|
|
}
|
2023-11-07 16:35:47 +03:00
|
|
|
|
if _, ok := funcMap["includeTemplPart"]; !ok {
|
|
|
|
|
funcMap["includeTemplPart"] = IncludeTemplPart
|
2023-06-23 14:54:47 +03:00
|
|
|
|
}
|
2023-11-07 16:35:47 +03:00
|
|
|
|
tmpl, err = template.New(tmp).Funcs(funcMap).Parse(string(buff))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Запись файла на диск
|
|
|
|
|
func WriteFile(outname string, data []byte) (err error) {
|
|
|
|
|
err = os.WriteFile(outname, data, 0755)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Выполнить шаблон
|
|
|
|
|
func ExecuteTmplFile(tmpl *template.Template, data interface{}) (out []byte, err error) {
|
2023-06-23 14:54:47 +03:00
|
|
|
|
var b bytes.Buffer
|
|
|
|
|
w := bufio.NewWriter(&b)
|
|
|
|
|
if err = tmpl.Execute(w, data); err != nil {
|
2023-11-07 16:35:47 +03:00
|
|
|
|
return
|
2023-06-23 14:54:47 +03:00
|
|
|
|
}
|
|
|
|
|
w.Flush()
|
2023-11-07 16:35:47 +03:00
|
|
|
|
out = b.Bytes()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Использование шаблона
|
|
|
|
|
func PrepareTmplFile(filename string, data interface{}, outname string) (err error) {
|
|
|
|
|
dir := filepath.Dir(outname)
|
|
|
|
|
if err = MkdirIsNotExists(dir); err != nil {
|
2023-08-09 14:45:36 +03:00
|
|
|
|
return err
|
|
|
|
|
}
|
2023-11-07 16:35:47 +03:00
|
|
|
|
|
|
|
|
|
fmt.Printf("Generate: %s\n", outname)
|
|
|
|
|
|
|
|
|
|
/*var b bytes.Buffer
|
|
|
|
|
w := bufio.NewWriter(&b)*/
|
|
|
|
|
var tmpl *template.Template
|
|
|
|
|
if tmpl, err = ReadTmplFile(filename); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
/*if err = tmpl.Execute(w, data); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
w.Flush()*/
|
|
|
|
|
var out []byte
|
|
|
|
|
if out, err = ExecuteTmplFile(tmpl, data); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
return WriteFile(outname, out)
|
2023-06-23 14:54:47 +03:00
|
|
|
|
}
|
2023-08-02 16:04:17 +03:00
|
|
|
|
|
2023-11-07 16:35:47 +03:00
|
|
|
|
// Подготовка файла, если его нет на диске
|
2023-08-02 16:04:17 +03:00
|
|
|
|
func PrepareTmplIsNotExists(filename string, data interface{}, outname string) (err error) {
|
|
|
|
|
|
|
|
|
|
if err = MkdirIsNotExists(filepath.Dir(outname)); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := os.Stat(outname); errors.Is(err, os.ErrNotExist) {
|
|
|
|
|
return PrepareTmplFile(filename, data, outname)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func MkdirIsNotExists(pathdir string) (err error) {
|
|
|
|
|
if _, err = os.Stat(pathdir); os.IsNotExist(err) {
|
|
|
|
|
if err = os.MkdirAll(pathdir, 0775); err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
err = nil
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-08-11 12:28:30 +03:00
|
|
|
|
|
|
|
|
|
func AngularRestName(tmp string) string {
|
|
|
|
|
value := tmp
|
|
|
|
|
value = strings.Trim(value, " ")
|
|
|
|
|
if value == "" {
|
|
|
|
|
return "Unknow"
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-25 15:05:28 +03:00
|
|
|
|
value = strings.ReplaceAll(ParseRestPathForName(value), "/", "_")
|
2023-08-11 12:28:30 +03:00
|
|
|
|
|
|
|
|
|
value = FieldNamePrepare(value)
|
|
|
|
|
|
|
|
|
|
/*var regexpForReplace = regexp.MustCompile(`_([a-z])`)
|
|
|
|
|
value = strings.ToUpper(regexpForReplace.ReplaceAllString(value, `$1`))
|
|
|
|
|
asRunes := []rune(value)
|
|
|
|
|
|
|
|
|
|
asRunes[0] = []rune(strings.ToUpper(string([]rune(value)[0])))[0]
|
|
|
|
|
value = string(asRunes)*/
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func AngularFilename(value string) string {
|
|
|
|
|
value = strings.Trim(value, " ")
|
|
|
|
|
if value == "" {
|
|
|
|
|
return "unknow"
|
|
|
|
|
}
|
2023-08-25 15:05:28 +03:00
|
|
|
|
value = strings.ReplaceAll(ParseRestPathForName(value), "/", "-")
|
2023-08-11 12:28:30 +03:00
|
|
|
|
if []rune(value)[0] == '-' {
|
|
|
|
|
value = string([]rune(value)[1:])
|
|
|
|
|
}
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func RemoveFirstChar(value string) string {
|
|
|
|
|
value = strings.Trim(value, " ")
|
|
|
|
|
if value == "" {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-08-17 22:49:02 +03:00
|
|
|
|
runes := []rune(value)
|
2023-08-25 15:05:28 +03:00
|
|
|
|
if runes[0] == '-' || runes[0] == '/' || runes[0] == '\\' || runes[0] == '_' || runes[0] == '*' {
|
2023-08-17 22:49:02 +03:00
|
|
|
|
value = string(runes[1:])
|
|
|
|
|
} else {
|
|
|
|
|
value = string(runes)
|
|
|
|
|
}
|
2023-08-11 12:28:30 +03:00
|
|
|
|
return value
|
|
|
|
|
}
|
2023-08-17 22:49:02 +03:00
|
|
|
|
|
|
|
|
|
func GetFieldsByModelID(project *structs.Project, id uuid.UUID) []structs.Field {
|
|
|
|
|
tmpSrc := FindSourceDataInDBByID(project, id)
|
|
|
|
|
if tmpSrc != nil {
|
|
|
|
|
return tmpSrc.Fields
|
|
|
|
|
}
|
|
|
|
|
tmpInterface := FindSourceDataInInterfaceByID(project, id)
|
|
|
|
|
if tmpInterface != nil {
|
|
|
|
|
return tmpInterface.Fields
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetFksByModelID(project *structs.Project, id uuid.UUID) []structs.Field {
|
|
|
|
|
tmpSrc := FindSourceDataInDBByID(project, id)
|
|
|
|
|
if tmpSrc != nil {
|
|
|
|
|
return tmpSrc.FkFields
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 12:30:22 +03:00
|
|
|
|
func GetFksByModelID2(project *structs.Project, id uuid.UUID) []structs.FK {
|
|
|
|
|
tmpSrc := FindSourceDataInDBByID(project, id)
|
|
|
|
|
if tmpSrc != nil {
|
|
|
|
|
return tmpSrc.FKs
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-17 22:49:02 +03:00
|
|
|
|
func GetFieldsByModelName(project *structs.Project, name string) []structs.Field {
|
|
|
|
|
tmpSrc := FindSourceDataInDBByName(project, name)
|
|
|
|
|
if tmpSrc != nil {
|
|
|
|
|
return tmpSrc.Fields
|
|
|
|
|
}
|
|
|
|
|
tmpInterface := FindSourceDataInInterfaceByName(project, name)
|
|
|
|
|
if tmpInterface != nil {
|
|
|
|
|
return tmpInterface.Fields
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2023-08-18 12:30:22 +03:00
|
|
|
|
|
|
|
|
|
func FieldToVisField(field structs.Field) (res structs.VisField) {
|
|
|
|
|
res.Name = field.Name
|
|
|
|
|
res.ID = field.ID
|
|
|
|
|
// fmt.Println(res)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func FieldToVisField2(field structs.FK) (res structs.VisField) {
|
|
|
|
|
res.Name = field.Name
|
|
|
|
|
res.ID = field.TableID
|
|
|
|
|
// fmt.Println(res)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func FieldJsonDescriptionStrClear(value string) string {
|
|
|
|
|
return strings.ReplaceAll(strings.ReplaceAll(value, "\"", ""), "\\", "")
|
|
|
|
|
}
|