1
0
mirror of https://github.com/ManyakRus/crud_generator.git synced 2024-11-21 05:05:52 +02:00

сделал objects

This commit is contained in:
Nikitin Aleksandr 2024-11-06 11:38:10 +03:00
parent f09ac2a582
commit 85e267e65c
7 changed files with 256 additions and 78 deletions

View File

@ -169,6 +169,9 @@ PREFIX_SERVER_GRPC="server_grpc_"
#PREFIC_CRUD - filename prefix for crud database files
PREFIX_CRUD="crud_"
#SUFFIX_CRUD - filename suffix for crud files
SUFFIX_CRUD="_crud"
#PREFIX_TABLE - filename prefix for database table model files
PREFIX_TABLE="table_"
@ -556,6 +559,9 @@ TEMPLATES_READOBJECT_FOLDERNAME="pkg/object_model/objects"
#TEMPLATES_MODEL_READOBJECT_FILENAME - short filename of "objects_table.go_" file
TEMPLATES_MODEL_READOBJECT_FILENAME="objects_table.go_"
#TEMPLATES_MODEL_CRUD_READOBJECT_FILENAME - short filename of "objects_table_crud.go_" file
TEMPLATES_MODEL_CRUD_READOBJECT_FILENAME="objects_table_crud.go_"
#TEMPLATES_CRUD_READOBJECT_FOLDERNAME - folder name "pkg/db/crud_objects" for crud objects
TEMPLATES_CRUD_READOBJECT_FOLDERNAME="pkg/db/crud_objects"

View File

@ -1,4 +1,4 @@
//ObjectFieldNameTable
//FieldNameTable
ModelNameColumn:= TableNameForeign.ModelNameForeign{}
ModelNameColumn.PrimaryKeyNameF = m.FieldNameTable
err = crud_TableNameForeign.Read_ctx(ctx, db, &ModelNameColumn)

View File

@ -0,0 +1,66 @@
package object_lawsuit_status_types
import (
"encoding/json"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/calc_struct_version"
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/pkg/db/db_constants"
"reflect"
)
// versionLawsuitStatusType - версия структуры модели, с учётом имен и типов полей
var versionObjectLawsuitStatusType uint32
// Crud_LawsuitStatusType - объект контроллер crud операций
var Crud_ObjectLawsuitStatusType ICrud_ObjectLawsuitStatusType
// интерфейс стандартных CRUD операций, для использования в DB или GRPC или NRPC
type ICrud_ObjectLawsuitStatusType interface {
ReadObject(*ObjectLawsuitStatusType) error
}
// GetStructVersion - возвращает версию модели
func (m ObjectLawsuitStatusType) GetStructVersion() uint32 {
if versionObjectLawsuitStatusType == 0 {
versionObjectLawsuitStatusType = calc_struct_version.CalcStructVersion(reflect.TypeOf(m))
}
return versionObjectLawsuitStatusType
}
// GetModelFromJSON - создаёт модель из строки json
func (m *ObjectLawsuitStatusType) GetModelFromJSON(sModel string) error {
var err error
var bytes []byte
bytes = []byte(sModel)
err = json.Unmarshal(bytes, m)
return err
}
// GetJSON - возвращает строку json из модели
func (m ObjectLawsuitStatusType) GetJSON() (string, error) {
var Result string
var err error
bytes, err := json.Marshal(m)
if err != nil {
return Result, err
}
Result = string(bytes)
return Result, err
}
// ---------------------------- CRUD операции ------------------------------------------------------------
// ReadObject - находит запись в БД по ID, и заполняет в объект, а также заполняет все поля у которых есть foreign key
func (m *ObjectLawsuitStatusType) ReadObject() error {
if Crud_ObjectLawsuitStatusType == nil {
return db_constants.ErrorCrudIsNotInit
}
err := Crud_ObjectLawsuitStatusType.ReadObject(m)
return err
}

View File

@ -92,6 +92,7 @@ type SettingsINI struct {
PREFIX_SERVER_GRPC string
PREFIX_CLIENT_GRPC string
PREFIX_CRUD string
SUFFIX_CRUD string
PREFIX_MODEL string
PREFIX_TABLE string
COMMENT_MODEL_STRUCT string
@ -221,11 +222,12 @@ type SettingsINI struct {
TESTS_FOLDERNAME string
//Objects
NEED_CREATE_READOBJECT bool
PREFIX_READOBJECT string
PREFIX_CRUD_READOBJECT string
TEMPLATES_READOBJECT_FOLDERNAME string
TEMPLATES_MODEL_READOBJECT_FILENAME string
NEED_CREATE_READOBJECT bool
PREFIX_READOBJECT string
PREFIX_CRUD_READOBJECT string
TEMPLATES_READOBJECT_FOLDERNAME string
TEMPLATES_MODEL_READOBJECT_FILENAME string
TEMPLATES_MODEL_CRUD_READOBJECT_FILENAME string
TEMPLATES_CRUD_READOBJECT_FOLDERNAME string
@ -1287,6 +1289,16 @@ func FillSettings() {
s = Getenv(Name, true)
Settings.TESTS_FOLDERNAME = s
//
Name = "TEMPLATES_MODEL_CRUD_READOBJECT_FILENAME"
s = Getenv(Name, true)
Settings.TEMPLATES_MODEL_CRUD_READOBJECT_FILENAME = s
//
Name = "SUFFIX_CRUD"
s = Getenv(Name, true)
Settings.SUFFIX_CRUD = s
}
// CurrentDirectory - возвращает текущую директорию ОС

View File

@ -21,13 +21,20 @@ func CreateAllFiles(MapAll map[string]*types.Table) error {
continue
}
//создание файлов
//model
err = CreateFiles(MapAll, Table1)
if err != nil {
log.Error("CreateFiles() table: ", Table1.Name, ", error: ", err)
return err
}
//crud
err = CreateFiles_crud(MapAll, Table1)
if err != nil {
log.Error("CreateFiles() table: ", Table1.Name, ", error: ", err)
return err
}
}
return err

View File

@ -10,8 +10,8 @@ import (
"strings"
)
// CreateFiles - создаёт 1 файл в папке db
func CreateFiles(MapAll map[string]*types.Table, Table1 *types.Table) error {
// CreateFiles_crud - создаёт 1 файл в папке db
func CreateFiles_crud(MapAll map[string]*types.Table, Table1 *types.Table) error {
var err error
//чтение файлов
@ -21,20 +21,13 @@ func CreateFiles(MapAll map[string]*types.Table, Table1 *types.Table) error {
DirTemplatesObjects := DirTemplates + config.Settings.TEMPLATES_READOBJECT_FOLDERNAME + micro.SeparatorFile()
DirReadyDB := DirReady + config.Settings.TEMPLATES_READOBJECT_FOLDERNAME + micro.SeparatorFile()
FilenameTemplateObject := DirTemplatesObjects + config.Settings.TEMPLATES_MODEL_READOBJECT_FILENAME
FilenameTemplateObject := DirTemplatesObjects + config.Settings.TEMPLATES_MODEL_CRUD_READOBJECT_FILENAME
TableName := strings.ToLower(Table1.Name)
DirReadyTable := DirReadyDB + config.Settings.PREFIX_READOBJECT + TableName
FilenameReadyObjects := DirReadyTable + micro.SeparatorFile() + config.Settings.PREFIX_READOBJECT + TableName + ".go"
FilenameReadyObjects := DirReadyTable + micro.SeparatorFile() + config.Settings.PREFIX_READOBJECT + TableName + config.Settings.SUFFIX_CRUD + ".go"
//создадим каталог
create_files.CreateDirectory(DirReadyTable)
//ok, err := micro.FileExists(DirReadyTable)
//if ok == false {
// err = os.MkdirAll(DirReadyTable, 0777)
// if err != nil {
// log.Panic("Mkdir() ", DirReadyTable, " error: ", err)
// }
//}
//загрузим шаблон файла
bytes, err := os.ReadFile(FilenameTemplateObject)
@ -51,10 +44,18 @@ func CreateFiles(MapAll map[string]*types.Table, Table1 *types.Table) error {
if config.Settings.USE_DEFAULT_TEMPLATE == true {
TextGo = create_files.Delete_TemplateRepositoryImports(TextGo)
TextGo = CreateFiles1(MapAll, Table1, TextGo)
//TextGo = CreateFiles1(MapAll, Table1, TextGo)
ModelTableURL := create_files.Find_ModelTableURL(TableName)
TextGo = create_files.AddImport(TextGo, ModelTableURL)
//ModelTableURL := create_files.Find_ModelTableURL(TableName)
//TextGo = create_files.AddImport(TextGo, ModelTableURL)
//Find_DBConstants
ConstantsURL := create_files.Find_DBConstantsURL()
TextGo = create_files.AddImport(TextGo, ConstantsURL)
//calc_struct_version
CalcStructVersionURL := create_files.Find_CalcStructVersionURL()
TextGo = create_files.AddImport(TextGo, CalcStructVersionURL)
}
@ -84,60 +85,3 @@ func CreateFiles(MapAll map[string]*types.Table, Table1 *types.Table) error {
return err
}
// CreateFiles1 - возвращает текст заполненный
func CreateFiles1(MapAll map[string]*types.Table, Table1 *types.Table, TextGo string) string {
Otvet := TextGo
var err error
//
TextField := ""
for _, Column1 := range Table1.MapColumns {
//проверка имени колонки "DELETED_"
err = create_files.IsGood_Column(Column1)
if err != nil {
log.Warn("CreateFiles1() table: ", Table1.Name, " column: ", Column1.Name, " warning: ", err)
continue
}
//проверка есть внешний ключ
TableNameFK := Column1.TableKey
if TableNameFK == "" {
continue
}
//ColumnNameFK := Column1.ColumnKey
//if ColumnNameFK == "" {
// continue
//}
//
TableFK, ok := MapAll[TableNameFK]
if ok == false {
continue
}
//ColumnFK := TableFK.MapColumns[ColumnNameFK]
//if ColumnFK == nil {
// continue
//}
//добавим import
ModelTableURL := create_files.Find_ModelTableURL(TableNameFK)
Otvet = create_files.AddImport(Otvet, ModelTableURL)
//добавим поле
ModelNameColumn := create_files.Find_ObjectColumnModelName(Column1.NameGo)
TextField1 := "\t" + ModelNameColumn + " " + TableFK.Name + "." + TableFK.NameGo + "\n"
TextField = TextField + TextField1
}
//новые поля
Otvet = strings.ReplaceAll(Otvet, "//TextNewFields\n", TextField)
//описание таблицы
Otvet = strings.ReplaceAll(Otvet, "//TextDescription", Table1.Comment)
return Otvet
}

View File

@ -0,0 +1,143 @@
package object_tables
import (
"github.com/ManyakRus/crud_generator/internal/config"
"github.com/ManyakRus/crud_generator/internal/create_files"
"github.com/ManyakRus/crud_generator/internal/types"
"github.com/ManyakRus/starter/log"
"github.com/ManyakRus/starter/micro"
"os"
"strings"
)
// CreateFiles - создаёт 1 файл в папке db
func CreateFiles(MapAll map[string]*types.Table, Table1 *types.Table) error {
var err error
//чтение файлов
DirBin := micro.ProgramDir_bin()
DirTemplates := DirBin + config.Settings.TEMPLATE_FOLDERNAME + micro.SeparatorFile()
DirReady := DirBin + config.Settings.READY_FOLDERNAME + micro.SeparatorFile()
DirTemplatesObjects := DirTemplates + config.Settings.TEMPLATES_READOBJECT_FOLDERNAME + micro.SeparatorFile()
DirReadyDB := DirReady + config.Settings.TEMPLATES_READOBJECT_FOLDERNAME + micro.SeparatorFile()
FilenameTemplateObject := DirTemplatesObjects + config.Settings.TEMPLATES_MODEL_READOBJECT_FILENAME
TableName := strings.ToLower(Table1.Name)
DirReadyTable := DirReadyDB + config.Settings.PREFIX_READOBJECT + TableName
FilenameReadyObjects := DirReadyTable + micro.SeparatorFile() + config.Settings.PREFIX_READOBJECT + TableName + ".go"
//создадим каталог
create_files.CreateDirectory(DirReadyTable)
//ok, err := micro.FileExists(DirReadyTable)
//if ok == false {
// err = os.MkdirAll(DirReadyTable, 0777)
// if err != nil {
// log.Panic("Mkdir() ", DirReadyTable, " error: ", err)
// }
//}
//загрузим шаблон файла
bytes, err := os.ReadFile(FilenameTemplateObject)
if err != nil {
log.Panic("ReadFile() ", FilenameTemplateObject, " error: ", err)
}
TextGo := string(bytes)
//заменим имя пакета на новое
TextGo = create_files.Replace_PackageName(TextGo, DirReadyTable)
//ModelName := Table1.NameGo
//заменим импорты
if config.Settings.USE_DEFAULT_TEMPLATE == true {
TextGo = create_files.Delete_TemplateRepositoryImports(TextGo)
TextGo = CreateFiles1(MapAll, Table1, TextGo)
ModelTableURL := create_files.Find_ModelTableURL(TableName)
TextGo = create_files.AddImport(TextGo, ModelTableURL)
}
////замена импортов на новые URL
//TextGo = create_files.Replace_RepositoryImportsURL(TextGo)
//заменим имя модели
TextGo = create_files.Replace_ModelAndTableName(TextGo, Table1)
//uuid
TextGo = create_files.CheckAndAdd_ImportUUID_FromText(TextGo)
//alias
TextGo = create_files.CheckAndAdd_ImportAlias(TextGo)
//удаление пустого импорта
TextGo = create_files.Delete_EmptyImport(TextGo)
//импорт "fmt"
TextGo = create_files.CheckAndAdd_ImportFmt(TextGo)
//удаление пустых строк
TextGo = create_files.Delete_EmptyLines(TextGo)
//запись файла
err = os.WriteFile(FilenameReadyObjects, []byte(TextGo), config.Settings.FILE_PERMISSIONS)
return err
}
// CreateFiles1 - возвращает текст заполненный
func CreateFiles1(MapAll map[string]*types.Table, Table1 *types.Table, TextGo string) string {
Otvet := TextGo
var err error
//
TextField := ""
for _, Column1 := range Table1.MapColumns {
//проверка имени колонки "DELETED_"
err = create_files.IsGood_Column(Column1)
if err != nil {
log.Warn("CreateFiles1() table: ", Table1.Name, " column: ", Column1.Name, " warning: ", err)
continue
}
//проверка есть внешний ключ
TableNameFK := Column1.TableKey
if TableNameFK == "" {
continue
}
//ColumnNameFK := Column1.ColumnKey
//if ColumnNameFK == "" {
// continue
//}
//
TableFK, ok := MapAll[TableNameFK]
if ok == false {
continue
}
//ColumnFK := TableFK.MapColumns[ColumnNameFK]
//if ColumnFK == nil {
// continue
//}
//добавим import
ModelTableURL := create_files.Find_ModelTableURL(TableNameFK)
Otvet = create_files.AddImport(Otvet, ModelTableURL)
//добавим поле
ModelNameColumn := create_files.Find_ObjectColumnModelName(Column1.NameGo)
TextField1 := "\t" + ModelNameColumn + " " + TableFK.Name + "." + TableFK.NameGo + "\n"
TextField = TextField + TextField1
}
//новые поля
Otvet = strings.ReplaceAll(Otvet, "//TextNewFields\n", TextField)
//описание таблицы
Otvet = strings.ReplaceAll(Otvet, "//TextDescription", Table1.Comment)
return Otvet
}