mirror of
https://github.com/ManyakRus/crud_generator.git
synced 2024-12-25 00:46:20 +02:00
сделал grpc
This commit is contained in:
parent
9bfbf269f5
commit
e2ab024db7
282
bin/templates/grpc_client/grpc_client.go_
Normal file
282
bin/templates/grpc_client/grpc_client.go_
Normal file
@ -0,0 +1,282 @@
|
||||
package grpc_lawsuit_status_types
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
model "gitlab.aescorp.ru/dsp_dev/claim/common/object_model"
|
||||
"gitlab.aescorp.ru/dsp_dev/claim/common/object_model/pkg/grpc/grpc_client"
|
||||
"gitlab.aescorp.ru/dsp_dev/claim/common/object_model/pkg/grpc/grpc_client/constants"
|
||||
"gitlab.aescorp.ru/dsp_dev/claim/common/object_model/pkg/grpc/grpc_proto"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
// VersionModel - хранит версию структуры модели
|
||||
var VersionModel uint32
|
||||
|
||||
// TableName - имя таблицы в БД Postgres
|
||||
const TableName string = "lawsuit_status_types"
|
||||
|
||||
// объект для CRUD операций через GRPC
|
||||
type Crud_GRPC struct {
|
||||
}
|
||||
|
||||
// GetVersionModel - возвращает хэш версии структуры модели
|
||||
func (crud Crud_GRPC) GetVersionModel() uint32 {
|
||||
if VersionModel == 0 {
|
||||
VersionModel = model.LawsuitStatusType{}.GetStructVersion()
|
||||
}
|
||||
return VersionModel
|
||||
}
|
||||
|
||||
// Read - возвращает модель из БД
|
||||
func (crud Crud_GRPC) Read(m *model.LawsuitStatusType) error {
|
||||
//var Otvet model.LawsuitStatusType
|
||||
|
||||
//подключение
|
||||
if grpc_client.Client == nil {
|
||||
grpc_client.Connect()
|
||||
}
|
||||
|
||||
//подготовка запроса
|
||||
var VersionModel = crud.GetVersionModel()
|
||||
|
||||
Request := &grpc_proto.RequestId{}
|
||||
Request.Id = l.ID
|
||||
Request.VersionModel = VersionModel
|
||||
|
||||
ctxMain := context.Background()
|
||||
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_SECONDS))
|
||||
defer ctxCancelFunc()
|
||||
|
||||
//запрос
|
||||
Response, err := grpc_client.Client.LawsuitStatusTypes_Read(ctx, Request)
|
||||
if err != nil {
|
||||
sError := err.Error()
|
||||
if sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION {
|
||||
log.Panic("table: ", TableName, " error: ", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
//ответ
|
||||
sModel := Response.ModelString
|
||||
err = json.Unmarshal([]byte(sModel), l)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Create - записывает новую модель в БД
|
||||
func (crud Crud_GRPC) Create(m *model.LawsuitStatusType) error {
|
||||
//var Otvet model.LawsuitStatusType
|
||||
|
||||
//подключение
|
||||
if grpc_client.Client == nil {
|
||||
grpc_client.Connect()
|
||||
}
|
||||
|
||||
//подготовка запроса
|
||||
var VersionModel = crud.GetVersionModel()
|
||||
|
||||
ModelString, err := l.GetJSON()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
Request := &grpc_proto.RequestModel{}
|
||||
Request.ModelString = ModelString
|
||||
Request.VersionModel = VersionModel
|
||||
|
||||
ctxMain := context.Background()
|
||||
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_SECONDS))
|
||||
defer ctxCancelFunc()
|
||||
|
||||
//запрос
|
||||
Response, err := grpc_client.Client.LawsuitStatusTypes_Create(ctx, Request)
|
||||
if err != nil {
|
||||
sError := err.Error()
|
||||
if sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION {
|
||||
log.Panic("table: ", TableName, " error: ", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
//ответ
|
||||
sModel := Response.ModelString
|
||||
err = json.Unmarshal([]byte(sModel), l)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Update - обновляет модель в БД
|
||||
func (crud Crud_GRPC) Update(m *model.LawsuitStatusType) error {
|
||||
//var Otvet model.LawsuitStatusType
|
||||
|
||||
//подключение
|
||||
if grpc_client.Client == nil {
|
||||
grpc_client.Connect()
|
||||
}
|
||||
|
||||
//подготовка запроса
|
||||
var VersionModel = crud.GetVersionModel()
|
||||
|
||||
ModelString, err := l.GetJSON()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
Request := &grpc_proto.RequestModel{}
|
||||
Request.ModelString = ModelString
|
||||
Request.VersionModel = VersionModel
|
||||
|
||||
ctxMain := context.Background()
|
||||
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_SECONDS))
|
||||
defer ctxCancelFunc()
|
||||
|
||||
//запрос
|
||||
Response, err := grpc_client.Client.LawsuitStatusTypes_Update(ctx, Request)
|
||||
if err != nil {
|
||||
sError := err.Error()
|
||||
if sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION {
|
||||
log.Panic("table: ", TableName, " error: ", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
//ответ
|
||||
sModel := Response.ModelString
|
||||
err = json.Unmarshal([]byte(sModel), l)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Save - обновляет (или создаёт) модель в БД
|
||||
func (crud Crud_GRPC) Save(m *model.LawsuitStatusType) error {
|
||||
//var Otvet model.LawsuitStatusType
|
||||
|
||||
//подключение
|
||||
if grpc_client.Client == nil {
|
||||
grpc_client.Connect()
|
||||
}
|
||||
|
||||
//подготовка запроса
|
||||
var VersionModel = crud.GetVersionModel()
|
||||
|
||||
ModelString, err := l.GetJSON()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
Request := &grpc_proto.RequestModel{}
|
||||
Request.ModelString = ModelString
|
||||
Request.VersionModel = VersionModel
|
||||
|
||||
ctxMain := context.Background()
|
||||
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_SECONDS))
|
||||
defer ctxCancelFunc()
|
||||
|
||||
//запрос
|
||||
Response, err := grpc_client.Client.LawsuitStatusTypes_Save(ctx, Request)
|
||||
if err != nil {
|
||||
sError := err.Error()
|
||||
if sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION {
|
||||
log.Panic("table: ", TableName, " error: ", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
//ответ
|
||||
sModel := Response.ModelString
|
||||
err = json.Unmarshal([]byte(sModel), l)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Delete - устанавливает is_deleted = true в БД
|
||||
func (crud Crud_GRPC) Delete(m *model.LawsuitStatusType) error {
|
||||
//var Otvet model.LawsuitStatusType
|
||||
|
||||
//подключение
|
||||
if grpc_client.Client == nil {
|
||||
grpc_client.Connect()
|
||||
}
|
||||
|
||||
//подготовка запроса
|
||||
var VersionModel = crud.GetVersionModel()
|
||||
|
||||
Request := &grpc_proto.RequestId{}
|
||||
Request.Id = l.ID
|
||||
Request.VersionModel = VersionModel
|
||||
|
||||
ctxMain := context.Background()
|
||||
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_SECONDS))
|
||||
defer ctxCancelFunc()
|
||||
|
||||
//запрос
|
||||
Response, err := grpc_client.Client.LawsuitStatusTypes_Delete(ctx, Request)
|
||||
if err != nil {
|
||||
sError := err.Error()
|
||||
if sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION {
|
||||
log.Panic("table: ", TableName, " error: ", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
//ответ
|
||||
sModel := Response.ModelString
|
||||
err = json.Unmarshal([]byte(sModel), l)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// Restore - устанавливает is_deleted = false в БД
|
||||
func (crud Crud_GRPC) Restore(m *model.LawsuitStatusType) error {
|
||||
//var Otvet model.LawsuitStatusType
|
||||
|
||||
//подключение
|
||||
if grpc_client.Client == nil {
|
||||
grpc_client.Connect()
|
||||
}
|
||||
|
||||
//подготовка запроса
|
||||
var VersionModel = crud.GetVersionModel()
|
||||
|
||||
Request := &grpc_proto.RequestId{}
|
||||
Request.Id = l.ID
|
||||
Request.VersionModel = VersionModel
|
||||
|
||||
ctxMain := context.Background()
|
||||
ctx, ctxCancelFunc := context.WithTimeout(ctxMain, time.Second*time.Duration(constants.TIMEOUT_SECONDS))
|
||||
defer ctxCancelFunc()
|
||||
|
||||
//запрос
|
||||
Response, err := grpc_client.Client.LawsuitStatusTypes_Restore(ctx, Request)
|
||||
if err != nil {
|
||||
sError := err.Error()
|
||||
if sError[0:len(constants.TEXT_ERROR_MODEL_VERSION)] == constants.TEXT_ERROR_MODEL_VERSION {
|
||||
log.Panic("table: ", TableName, " error: ", err)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
//ответ
|
||||
sModel := Response.ModelString
|
||||
err = json.Unmarshal([]byte(sModel), l)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
152
bin/templates/grpc_client/grpc_client_test.go_
Normal file
152
bin/templates/grpc_client/grpc_client_test.go_
Normal file
@ -0,0 +1,152 @@
|
||||
package grpc_lawsuit_status_types
|
||||
|
||||
import (
|
||||
model "gitlab.aescorp.ru/dsp_dev/claim/common/object_model"
|
||||
"gitlab.aescorp.ru/dsp_dev/claim/common/object_model/pkg/grpc/grpc_client"
|
||||
config "gitlab.aescorp.ru/dsp_dev/claim/nikitin/config"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Postgres_ID_Test - ID таблицы для тестирования
|
||||
const Postgres_ID_Test = 1
|
||||
|
||||
func TestGetVersionModel(t *testing.T) {
|
||||
|
||||
crud := Crud_GRPC{}
|
||||
Otvet := crud.GetVersionModel()
|
||||
if Otvet == 0 {
|
||||
t.Error("TestGetVersionModel() error: Otvet =0")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRead(t *testing.T) {
|
||||
config.LoadEnv()
|
||||
grpc_client.Connect()
|
||||
defer grpc_client.CloseConnection()
|
||||
|
||||
crud := Crud_GRPC{}
|
||||
Otvet := model.LawsuitStatusType{}
|
||||
Otvet.ID = Postgres_ID_Test
|
||||
err := crud.Read(&Otvet)
|
||||
|
||||
if err != nil {
|
||||
t.Error("TestRead() error: ", err)
|
||||
}
|
||||
|
||||
if Otvet.ID == 0 {
|
||||
t.Error("TestRead() error: ID =0")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreate(t *testing.T) {
|
||||
config.LoadEnv()
|
||||
grpc_client.Connect()
|
||||
defer grpc_client.CloseConnection()
|
||||
|
||||
Model := model.LawsuitStatusType{}
|
||||
Model.ID = -1
|
||||
|
||||
crud := Crud_GRPC{}
|
||||
err := crud.Create(&Model)
|
||||
|
||||
if err == nil {
|
||||
t.Error("TestCreate() error: ", err)
|
||||
}
|
||||
|
||||
if Model.ID > 0 {
|
||||
t.Error("TestCreate() error: ID >0")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
config.LoadEnv()
|
||||
grpc_client.Connect()
|
||||
defer grpc_client.CloseConnection()
|
||||
|
||||
Model := model.LawsuitStatusType{}
|
||||
Model.ID = 0
|
||||
|
||||
crud := Crud_GRPC{}
|
||||
err := crud.Update(&Model)
|
||||
|
||||
if err == nil {
|
||||
t.Error("TestUpdate() error: ", err)
|
||||
}
|
||||
|
||||
if Model.ID != 0 {
|
||||
t.Error("TestUpdate() error: ID =0")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSave(t *testing.T) {
|
||||
config.LoadEnv()
|
||||
grpc_client.Connect()
|
||||
defer grpc_client.CloseConnection()
|
||||
|
||||
crud := Crud_GRPC{}
|
||||
Otvet := model.LawsuitStatusType{}
|
||||
Otvet.ID = Postgres_ID_Test
|
||||
err := crud.Read(&Otvet)
|
||||
if err != nil {
|
||||
t.Error("TestSave() error: ", err)
|
||||
return
|
||||
}
|
||||
|
||||
err = crud.Save(&Otvet)
|
||||
|
||||
if err != nil {
|
||||
t.Error("TestSave() error: ", err)
|
||||
}
|
||||
|
||||
if Otvet.ID == 0 {
|
||||
t.Error("TestSave() error: ID =0")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDelete(t *testing.T) {
|
||||
config.LoadEnv()
|
||||
grpc_client.Connect()
|
||||
defer grpc_client.CloseConnection()
|
||||
|
||||
crud := Crud_GRPC{}
|
||||
Otvet := model.LawsuitStatusType{}
|
||||
Otvet.ID = Postgres_ID_Test
|
||||
err := crud.Read(&Otvet)
|
||||
if err != nil {
|
||||
t.Error("TestRead() error: ", err)
|
||||
}
|
||||
|
||||
if Otvet.IsDeleted == false {
|
||||
err = crud.Delete(&Otvet)
|
||||
if err != nil {
|
||||
t.Error("TestDelete() error: ", err)
|
||||
}
|
||||
if Otvet.ID == 0 {
|
||||
t.Error("TestDelete() error: ID =0")
|
||||
}
|
||||
|
||||
err = crud.Restore(&Otvet)
|
||||
if err != nil {
|
||||
t.Error("TestDelete() error: ", err)
|
||||
}
|
||||
if Otvet.ID == 0 {
|
||||
t.Error("TestDelete() error: ID =0")
|
||||
}
|
||||
} else {
|
||||
err = crud.Restore(&Otvet)
|
||||
if err != nil {
|
||||
t.Error("TestDelete() error: ", err)
|
||||
}
|
||||
if Otvet.ID == 0 {
|
||||
t.Error("TestDelete() error: ID =0")
|
||||
}
|
||||
|
||||
err = crud.Delete(&Otvet)
|
||||
if err != nil {
|
||||
t.Error("TestDelete() error: ", err)
|
||||
}
|
||||
if Otvet.ID == 0 {
|
||||
t.Error("TestDelete() error: ID =0")
|
||||
}
|
||||
}
|
||||
}
|
208
bin/templates/grpc_server/grpc_server.go_
Normal file
208
bin/templates/grpc_server/grpc_server.go_
Normal file
@ -0,0 +1,208 @@
|
||||
package server_grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
model "gitlab.aescorp.ru/dsp_dev/claim/common/object_model"
|
||||
"gitlab.aescorp.ru/dsp_dev/claim/common/object_model/pkg/grpc/grpc_proto"
|
||||
)
|
||||
|
||||
// LawsuitStatusTypes_Read - читает и возвращает модель из БД
|
||||
func (s *ServerGRPC) LawsuitStatusTypes_Read(ctx context.Context, Request *grpc_proto.RequestId) (*grpc_proto.Response, error) {
|
||||
var Otvet grpc_proto.Response
|
||||
var err error
|
||||
|
||||
//проверим совпадения версии модели
|
||||
VersionServer := model.LawsuitStatusType{}.GetStructVersion()
|
||||
VersionClient := Request.VersionModel
|
||||
if VersionServer != VersionClient {
|
||||
err = ErrorModelVersion(model.LawsuitStatusType{})
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
//запрос в БД
|
||||
Model := &model.LawsuitStatusType{}
|
||||
Model.ID = Request.Id
|
||||
err = Model.Read()
|
||||
if err != nil {
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
//заполяем ответ
|
||||
ModelString, err := Model.GetJSON()
|
||||
if err != nil {
|
||||
return &Otvet, err
|
||||
}
|
||||
Otvet.ModelString = ModelString
|
||||
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
// LawsuitStatusTypes_Delete - записывает в БД is_deleted = true и возвращает модель из БД
|
||||
func (s *ServerGRPC) LawsuitStatusTypes_Delete(ctx context.Context, Request *grpc_proto.RequestId) (*grpc_proto.Response, error) {
|
||||
var Otvet grpc_proto.Response
|
||||
var err error
|
||||
|
||||
//проверим совпадения версии модели
|
||||
VersionServer := model.LawsuitStatusType{}.GetStructVersion()
|
||||
VersionClient := Request.VersionModel
|
||||
if VersionServer != VersionClient {
|
||||
err = ErrorModelVersion(model.LawsuitStatusType{})
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
//запрос в БД
|
||||
Model := &model.LawsuitStatusType{}
|
||||
Model.ID = Request.Id
|
||||
err = Model.Delete()
|
||||
if err != nil {
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
//заполяем ответ
|
||||
ModelString, err := Model.GetJSON()
|
||||
if err != nil {
|
||||
return &Otvet, err
|
||||
}
|
||||
Otvet.ModelString = ModelString
|
||||
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
// LawsuitStatusTypes_Restore - записывает в БД is_deleted = false и возвращает модель из БД
|
||||
func (s *ServerGRPC) LawsuitStatusTypes_Restore(ctx context.Context, Request *grpc_proto.RequestId) (*grpc_proto.Response, error) {
|
||||
var Otvet grpc_proto.Response
|
||||
var err error
|
||||
|
||||
//проверим совпадения версии модели
|
||||
VersionServer := model.LawsuitStatusType{}.GetStructVersion()
|
||||
VersionClient := Request.VersionModel
|
||||
if VersionServer != VersionClient {
|
||||
err = ErrorModelVersion(model.LawsuitStatusType{})
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
//запрос в БД
|
||||
Model := &model.LawsuitStatusType{}
|
||||
Model.ID = Request.Id
|
||||
err = Model.Restore()
|
||||
if err != nil {
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
//заполяем ответ
|
||||
ModelString, err := Model.GetJSON()
|
||||
if err != nil {
|
||||
return &Otvet, err
|
||||
}
|
||||
Otvet.ModelString = ModelString
|
||||
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
// LawsuitStatusTypes_Create - создаёт новую запись в БД
|
||||
func (s *ServerGRPC) LawsuitStatusTypes_Create(ctx context.Context, Request *grpc_proto.RequestModel) (*grpc_proto.Response, error) {
|
||||
var Otvet grpc_proto.Response
|
||||
var err error
|
||||
|
||||
//проверим совпадения версии модели
|
||||
VersionServer := model.LawsuitStatusType{}.GetStructVersion()
|
||||
VersionClient := Request.VersionModel
|
||||
if VersionServer != VersionClient {
|
||||
err = ErrorModelVersion(model.LawsuitStatusType{})
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
//получим модель из строки JSON
|
||||
Model := &model.LawsuitStatusType{}
|
||||
err = Model.GetModelFromJSON(Request.ModelString)
|
||||
if err != nil {
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
//запрос в БД
|
||||
err = Model.Create()
|
||||
if err != nil {
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
//заполяем ответ
|
||||
ModelString, err := Model.GetJSON()
|
||||
if err != nil {
|
||||
return &Otvet, err
|
||||
}
|
||||
Otvet.ModelString = ModelString
|
||||
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
// LawsuitStatusTypes_Update - обновляет новую запись в БД
|
||||
func (s *ServerGRPC) LawsuitStatusTypes_Update(ctx context.Context, Request *grpc_proto.RequestModel) (*grpc_proto.Response, error) {
|
||||
var Otvet grpc_proto.Response
|
||||
var err error
|
||||
|
||||
//проверим совпадения версии модели
|
||||
VersionServer := model.LawsuitStatusType{}.GetStructVersion()
|
||||
VersionClient := Request.VersionModel
|
||||
if VersionServer != VersionClient {
|
||||
err = ErrorModelVersion(model.LawsuitStatusType{})
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
//получим модель из строки JSON
|
||||
Model := &model.LawsuitStatusType{}
|
||||
err = Model.GetModelFromJSON(Request.ModelString)
|
||||
if err != nil {
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
//запрос в БД
|
||||
err = Model.Update()
|
||||
if err != nil {
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
//заполяем ответ
|
||||
ModelString, err := Model.GetJSON()
|
||||
if err != nil {
|
||||
return &Otvet, err
|
||||
}
|
||||
Otvet.ModelString = ModelString
|
||||
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
// LawsuitStatusTypes_Save - записывает (создаёт или обновляет) запись в БД
|
||||
func (s *ServerGRPC) LawsuitStatusTypes_Save(ctx context.Context, Request *grpc_proto.RequestModel) (*grpc_proto.Response, error) {
|
||||
var Otvet grpc_proto.Response
|
||||
var err error
|
||||
|
||||
//проверим совпадения версии модели
|
||||
VersionServer := model.LawsuitStatusType{}.GetStructVersion()
|
||||
VersionClient := Request.VersionModel
|
||||
if VersionServer != VersionClient {
|
||||
err = ErrorModelVersion(model.LawsuitStatusType{})
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
//получим модель из строки JSON
|
||||
Model := model.LawsuitStatusType{}
|
||||
err = Model.GetModelFromJSON(Request.ModelString)
|
||||
if err != nil {
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
//запрос в БД
|
||||
err = Model.Save()
|
||||
if err != nil {
|
||||
return &Otvet, err
|
||||
}
|
||||
|
||||
//заполяем ответ
|
||||
ModelString, err := Model.GetJSON()
|
||||
if err != nil {
|
||||
return &Otvet, err
|
||||
}
|
||||
Otvet.ModelString = ModelString
|
||||
|
||||
return &Otvet, err
|
||||
}
|
153
bin/templates/grpc_server/grpc_server_test.go_
Normal file
153
bin/templates/grpc_server/grpc_server_test.go_
Normal file
@ -0,0 +1,153 @@
|
||||
package server_grpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
model "gitlab.aescorp.ru/dsp_dev/claim/common/object_model"
|
||||
"gitlab.aescorp.ru/dsp_dev/claim/common/object_model/pkg/crud_starter"
|
||||
"gitlab.aescorp.ru/dsp_dev/claim/common/object_model/pkg/grpc/grpc_proto"
|
||||
"gitlab.aescorp.ru/dsp_dev/claim/nikitin/config"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// LawsuitStatusType_ID_Test - ID таблицы для тестирования
|
||||
const LawsuitStatusType_ID_Test = 1
|
||||
|
||||
func Test_server_LawsuitStatusTypes_Read(t *testing.T) {
|
||||
//t.SkipNow()
|
||||
config.LoadEnv()
|
||||
crud_starter.InitCrudTransport_DB()
|
||||
|
||||
ctx := context.Background()
|
||||
Request := grpc_proto.RequestId{}
|
||||
Request.Id = LawsuitStatusType_ID_Test
|
||||
Request.VersionModel = model.LawsuitStatusType{}.GetStructVersion()
|
||||
|
||||
server1 := &ServerGRPC{}
|
||||
Otvet, err := server1.LawsuitStatusTypes_Read(ctx, &Request)
|
||||
if err != nil {
|
||||
t.Error("Test_server_LawsuitStatusType_Read() error: ", err)
|
||||
}
|
||||
if Otvet.ModelString == "" {
|
||||
t.Error("Test_server_LawsuitStatusType_Read() error: ModelString=''")
|
||||
}
|
||||
}
|
||||
|
||||
func Test_server_LawsuitStatusTypes_Delete(t *testing.T) {
|
||||
config.LoadEnv()
|
||||
crud_starter.InitCrudTransport_DB()
|
||||
|
||||
ctx := context.Background()
|
||||
Request := grpc_proto.RequestId{}
|
||||
Request.Id = LawsuitStatusType_ID_Test
|
||||
Request.VersionModel = model.LawsuitStatusType{}.GetStructVersion()
|
||||
|
||||
server1 := &ServerGRPC{}
|
||||
Otvet, err := server1.LawsuitStatusTypes_Delete(ctx, &Request)
|
||||
if err != nil {
|
||||
t.Error("Test_server_LawsuitStatusType_Delete() error: ", err)
|
||||
}
|
||||
if Otvet.ModelString == "" {
|
||||
t.Error("Test_server_LawsuitStatusType_Delete() error: ModelString=''")
|
||||
}
|
||||
Otvet, err = server1.LawsuitStatusTypes_Restore(ctx, &Request)
|
||||
}
|
||||
|
||||
func Test_server_LawsuitStatusTypes_Create(t *testing.T) {
|
||||
config.LoadEnv()
|
||||
crud_starter.InitCrudTransport_DB()
|
||||
|
||||
var ModelString string
|
||||
Model := model.LawsuitStatusType{}
|
||||
Model.ID = 1
|
||||
ModelString, err := Model.GetJSON()
|
||||
if err != nil {
|
||||
t.Error("Test_server_LawsuitStatusType_Create() error: ", err)
|
||||
return
|
||||
}
|
||||
|
||||
RequestModel := grpc_proto.RequestModel{}
|
||||
RequestModel.VersionModel = model.LawsuitStatusType{}.GetStructVersion()
|
||||
RequestModel.ModelString = ModelString
|
||||
|
||||
ctx := context.Background()
|
||||
server1 := &ServerGRPC{}
|
||||
Otvet, err := server1.LawsuitStatusTypes_Create(ctx, &RequestModel)
|
||||
if err == nil {
|
||||
t.Error("Test_server_LawsuitStatusType_Create() error: ", err)
|
||||
}
|
||||
if Otvet.ModelString != "" {
|
||||
t.Error("Test_server_LawsuitStatusType_Create() error: ModelString !=''")
|
||||
}
|
||||
}
|
||||
|
||||
func Test_server_LawsuitStatusTypes_Update(t *testing.T) {
|
||||
config.LoadEnv()
|
||||
crud_starter.InitCrudTransport_DB()
|
||||
|
||||
ctx := context.Background()
|
||||
Request := grpc_proto.RequestId{}
|
||||
Request.Id = LawsuitStatusType_ID_Test
|
||||
Request.VersionModel = model.LawsuitStatusType{}.GetStructVersion()
|
||||
|
||||
server1 := &ServerGRPC{}
|
||||
Response1, err := server1.LawsuitStatusTypes_Read(ctx, &Request)
|
||||
if err != nil {
|
||||
t.Error("Test_server_LawsuitStatusType_Update() error: ", err)
|
||||
return
|
||||
}
|
||||
if Response1.ModelString == "" {
|
||||
t.Error("Test_server_LawsuitStatusType_Update() error: ModelString=''")
|
||||
}
|
||||
|
||||
var ModelString string
|
||||
ModelString = Response1.ModelString
|
||||
|
||||
RequestModel := grpc_proto.RequestModel{}
|
||||
RequestModel.VersionModel = model.LawsuitStatusType{}.GetStructVersion()
|
||||
RequestModel.ModelString = ModelString
|
||||
|
||||
Otvet, err := server1.LawsuitStatusTypes_Update(ctx, &RequestModel)
|
||||
if err != nil {
|
||||
t.Error("Test_server_LawsuitStatusType_Update() error: ", err)
|
||||
}
|
||||
if Otvet.ModelString == "" {
|
||||
t.Error("Test_server_LawsuitStatusType_Update() error: ModelString=''")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func Test_server_LawsuitStatusTypes_Save(t *testing.T) {
|
||||
config.LoadEnv()
|
||||
crud_starter.InitCrudTransport_DB()
|
||||
|
||||
//Model := model.LawsuitStatusType{}
|
||||
|
||||
ctx := context.Background()
|
||||
Request := grpc_proto.RequestId{}
|
||||
Request.Id = LawsuitStatusType_ID_Test
|
||||
Request.VersionModel = model.LawsuitStatusType{}.GetStructVersion()
|
||||
|
||||
server1 := &ServerGRPC{}
|
||||
Response1, err := server1.LawsuitStatusTypes_Read(ctx, &Request)
|
||||
if err != nil {
|
||||
t.Error("Test_server_LawsuitStatusType_Save() error: ", err)
|
||||
return
|
||||
}
|
||||
|
||||
var ModelString string
|
||||
ModelString = Response1.ModelString
|
||||
|
||||
//sModel, _ := GetJSON(Otvet)
|
||||
RequestModel := grpc_proto.RequestModel{}
|
||||
RequestModel.VersionModel = model.LawsuitStatusType{}.GetStructVersion()
|
||||
RequestModel.ModelString = ModelString
|
||||
|
||||
Otvet, err := server1.LawsuitStatusTypes_Save(ctx, &RequestModel)
|
||||
if err != nil {
|
||||
t.Error("Test_server_LawsuitStatusType_Save() error: ", err)
|
||||
}
|
||||
if Otvet.ModelString == "" {
|
||||
t.Error("Test_server_LawsuitStatusType_Save() error: ModelString=''")
|
||||
}
|
||||
|
||||
}
|
121
bin/templates/nrpc_server/server_nrpc_starter.go
Normal file
121
bin/templates/nrpc_server/server_nrpc_starter.go
Normal file
@ -0,0 +1,121 @@
|
||||
package server_grpc
|
||||
|
||||
import (
|
||||
"github.com/nats-io/nats.go"
|
||||
"gitlab.aescorp.ru/dsp_dev/claim/common/object_model/pkg/grpc/grpc_proto"
|
||||
"gitlab.aescorp.ru/dsp_dev/claim/nikitin/contextmain"
|
||||
"gitlab.aescorp.ru/dsp_dev/claim/nikitin/log"
|
||||
"gitlab.aescorp.ru/dsp_dev/claim/nikitin/stopapp"
|
||||
"gitlab.aescorp.ru/dsp_dev/claim/sync_service/internal/app/grpc/server_grpc"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
//// server is used to implement UnimplementedSyncServiceServer.
|
||||
//type server struct {
|
||||
//}
|
||||
|
||||
type SettingsINI struct {
|
||||
NATS_HOST string
|
||||
NATS_PORT string
|
||||
}
|
||||
|
||||
var Settings SettingsINI
|
||||
|
||||
// Conn - подключение к NATS
|
||||
var Conn *nats.Conn
|
||||
|
||||
// NRPCSubscription - подписка на топик NATS
|
||||
var NRPCSubscription *nats.Subscription
|
||||
|
||||
type server struct{}
|
||||
|
||||
func Connect() {
|
||||
var err error
|
||||
|
||||
if Settings.NATS_HOST == "" {
|
||||
FillSettings()
|
||||
}
|
||||
|
||||
NatsURL := "nats://" + Settings.NATS_HOST + ":" + Settings.NATS_PORT
|
||||
// Connect to the NATS server.
|
||||
Conn, err := nats.Connect(NatsURL, nats.Timeout(60*time.Second))
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
//defer Conn.Close()
|
||||
|
||||
// Our server implementation.
|
||||
s := &server_grpc.ServerGRPC{}
|
||||
//s := &server{}
|
||||
|
||||
// The NATS handler from the helloworld.nrpc.proto file.
|
||||
h := grpc_proto.NewSync_serviceHandler(contextmain.GetContext(), Conn, s)
|
||||
|
||||
// Start a NATS subscription using the handler. You can also use the
|
||||
// QueueSubscribe() method for a load-balanced set of servers.
|
||||
NRPCSubscription2, err := Conn.Subscribe(h.Subject(), h.Handler)
|
||||
if err != nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
if NRPCSubscription2 == nil {
|
||||
log.Panic(err)
|
||||
}
|
||||
NRPCSubscription = NRPCSubscription2
|
||||
|
||||
log.Info("Server NRPC started. NATS: ", NatsURL)
|
||||
}
|
||||
|
||||
// FillSettings - заполняет настройки из переменных окружения
|
||||
func FillSettings() {
|
||||
Settings = SettingsINI{}
|
||||
Settings.NATS_HOST = os.Getenv("BUS_LOCAL_HOST")
|
||||
Settings.NATS_PORT = os.Getenv("BUS_LOCAL_PORT")
|
||||
|
||||
if Settings.NATS_HOST == "" {
|
||||
log.Panic("Need fill BUS_LOCAL_HOST ! in OS Environment ")
|
||||
}
|
||||
|
||||
if Settings.NATS_PORT == "" {
|
||||
log.Panic("Need fill BUS_LOCAL_PORT ! in OS Environment ")
|
||||
}
|
||||
}
|
||||
|
||||
// WaitStop - ожидает отмену глобального контекста
|
||||
func WaitStop() {
|
||||
|
||||
select {
|
||||
case <-contextmain.GetContext().Done():
|
||||
log.Warn("Context app is canceled. nrpc server connect")
|
||||
}
|
||||
|
||||
//ждём пока отправляемых сейчас сообщений будет =0
|
||||
stopapp.WaitTotalMessagesSendingNow("nrpc server connect")
|
||||
|
||||
//закрываем соединение
|
||||
CloseConnection()
|
||||
stopapp.GetWaitGroup_Main().Done()
|
||||
}
|
||||
|
||||
// Start - необходимые процедуры для запуска сервера GRPC
|
||||
func Start() {
|
||||
Connect()
|
||||
|
||||
stopapp.GetWaitGroup_Main().Add(1)
|
||||
go WaitStop()
|
||||
|
||||
}
|
||||
|
||||
func CloseConnection() {
|
||||
if NRPCSubscription != nil {
|
||||
NRPCSubscription.Unsubscribe()
|
||||
}
|
||||
Conn.Close()
|
||||
log.Info("Server NRPC connection closed")
|
||||
}
|
||||
|
||||
//func (s *server) Read(ctx context.Context, Reques *grpc_proto.RequestId) *grpc_proto.Response {
|
||||
// var Otvet *grpc_proto.Response
|
||||
//
|
||||
// return Otvet
|
||||
//}
|
14
bin/templates/nrpc_server/server_nrpc_starter_test.go
Normal file
14
bin/templates/nrpc_server/server_nrpc_starter_test.go
Normal file
@ -0,0 +1,14 @@
|
||||
package server_grpc
|
||||
|
||||
import (
|
||||
config_main "gitlab.aescorp.ru/dsp_dev/claim/nikitin/config"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConnect(t *testing.T) {
|
||||
config_main.LoadEnv()
|
||||
FillSettings()
|
||||
|
||||
Connect()
|
||||
CloseConnection()
|
||||
}
|
@ -11,20 +11,24 @@ var Settings SettingsINI
|
||||
|
||||
// SettingsINI - структура для хранения всех нужных переменных окружения
|
||||
type SettingsINI struct {
|
||||
INCLUDE_TABLES string
|
||||
EXCLUDE_TABLES string
|
||||
TEMPLATE_FOLDERNAME string
|
||||
TEMPLATE_FOLDERNAME_MODEL string
|
||||
TEMPLATE_FOLDERNAME_DB string
|
||||
TEMPLATE_FOLDERNAME_GRPC string
|
||||
TEMPLATE_FOLDERNAME_NRPC string
|
||||
NEED_CRUD bool
|
||||
NEED_GRPC bool
|
||||
NEED_NRPC bool
|
||||
SERVICE_NAME string
|
||||
TEXT_TEMPLATE_MODEL string
|
||||
TEXT_TEMPLATE_TABLENAME string
|
||||
HAS_IS_DELETED bool
|
||||
INCLUDE_TABLES string
|
||||
EXCLUDE_TABLES string
|
||||
TEMPLATE_FOLDERNAME string
|
||||
TEMPLATE_FOLDERNAME_MODEL string
|
||||
TEMPLATE_FOLDERNAME_DB string
|
||||
TEMPLATE_FOLDERNAME_GRPC string
|
||||
TEMPLATE_FOLDERNAME_GRPC_SERVER string
|
||||
TEMPLATE_FOLDERNAME_GRPC_CLIENT string
|
||||
TEMPLATE_FOLDERNAME_NRPC string
|
||||
TEMPLATE_FOLDERNAME_NRPC_SERVER string
|
||||
TEMPLATE_FOLDERNAME_NRPC_CLIENT string
|
||||
NEED_CRUD bool
|
||||
NEED_GRPC bool
|
||||
NEED_NRPC bool
|
||||
SERVICE_NAME string
|
||||
TEXT_TEMPLATE_MODEL string
|
||||
TEXT_TEMPLATE_TABLENAME string
|
||||
HAS_IS_DELETED bool
|
||||
}
|
||||
|
||||
// FillSettings загружает переменные окружения в структуру из переменных окружения
|
||||
@ -36,7 +40,11 @@ func FillSettings() {
|
||||
Settings.TEMPLATE_FOLDERNAME_MODEL = os.Getenv("TEMPLATE_FOLDERNAME_MODEL")
|
||||
Settings.TEMPLATE_FOLDERNAME_DB = os.Getenv("TEMPLATE_FOLDERNAME_DB")
|
||||
Settings.TEMPLATE_FOLDERNAME_GRPC = os.Getenv("TEMPLATE_FOLDERNAME_GRPC")
|
||||
Settings.TEMPLATE_FOLDERNAME_GRPC_SERVER = os.Getenv("TEMPLATE_FOLDERNAME_GRPC_SERVER")
|
||||
Settings.TEMPLATE_FOLDERNAME_GRPC_CLIENT = os.Getenv("TEMPLATE_FOLDERNAME_GRPC_CLIENT")
|
||||
Settings.TEMPLATE_FOLDERNAME_NRPC = os.Getenv("TEMPLATE_FOLDERNAME_NRPC")
|
||||
Settings.TEMPLATE_FOLDERNAME_NRPC_SERVER = os.Getenv("TEMPLATE_FOLDERNAME_NRPC_SERVER")
|
||||
Settings.TEMPLATE_FOLDERNAME_NRPC_CLIENT = os.Getenv("TEMPLATE_FOLDERNAME_NRPC_CLIENT")
|
||||
Settings.TEXT_TEMPLATE_MODEL = os.Getenv("TEXT_TEMPLATE_MODEL")
|
||||
Settings.TEXT_TEMPLATE_TABLENAME = os.Getenv("TEXT_TEMPLATE_TABLENAME")
|
||||
sHAS_IS_DELETED := os.Getenv("HAS_IS_DELETED")
|
||||
|
@ -32,3 +32,59 @@ func FormatName(Name string) string {
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
func IsNumberType(TypeGo string) bool {
|
||||
Otvet := false
|
||||
|
||||
switch TypeGo {
|
||||
case "int", "int8", "int16", "int32", "int64", "float32", "float64", "uint", "uint8", "uint16", "uint32", "uint64", "byte":
|
||||
{
|
||||
Otvet = true
|
||||
}
|
||||
}
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncFromFuncName - удаляет функцию из текста начиная с объявления функции
|
||||
func DeleteFuncFromFuncName(Text, FuncName string) string {
|
||||
Otvet := Text
|
||||
|
||||
TextFind := "\nfunc " + FuncName + "("
|
||||
pos1 := strings.Index(Otvet, TextFind)
|
||||
if pos1 < 0 {
|
||||
return Otvet
|
||||
}
|
||||
s2 := Otvet[pos1+1:]
|
||||
|
||||
posEnd := strings.Index(s2, "\n}")
|
||||
if posEnd < 0 {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = Otvet[:pos1-1] + Otvet[pos1+posEnd+3:]
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncFromComment - удаляет функцию из текста начиная с комментария
|
||||
func DeleteFuncFromComment(Text, Comment string) string {
|
||||
Otvet := Text
|
||||
|
||||
TextFind := Comment //"\n// Delete "
|
||||
pos1 := strings.Index(Otvet, TextFind)
|
||||
if pos1 < 0 {
|
||||
return Otvet
|
||||
}
|
||||
s2 := Otvet[pos1+1:]
|
||||
|
||||
posEnd := strings.Index(s2, "\n}")
|
||||
if posEnd < 0 {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = Otvet[:pos1-1] + Otvet[pos1+posEnd+3:]
|
||||
|
||||
return Otvet
|
||||
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package db
|
||||
import (
|
||||
"github.com/ManyakRus/crud_generator/internal/config"
|
||||
"github.com/ManyakRus/crud_generator/internal/constants"
|
||||
"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"
|
||||
@ -10,22 +11,22 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// CreateDBFiles - создаёт все файлы в папке db
|
||||
func CreateDBFiles(MapAll map[string]*types.Table) error {
|
||||
// CreateAllFiles - создаёт все файлы в папке db
|
||||
func CreateAllFiles(MapAll map[string]*types.Table) error {
|
||||
var err error
|
||||
|
||||
for _, table1 := range MapAll {
|
||||
//файлы db
|
||||
err = CreateDBFiles1(table1)
|
||||
err = CreateFiles(table1)
|
||||
if err != nil {
|
||||
log.Error("CreateDBFiles1() table: ", table1.Name, " error: ", err)
|
||||
log.Error("CreateFiles() table: ", table1.Name, " error: ", err)
|
||||
return err
|
||||
}
|
||||
|
||||
//тестовые файлы db
|
||||
err = CreateDBTestFiles1(table1)
|
||||
err = CreateTestFiles(table1)
|
||||
if err != nil {
|
||||
log.Error("CreateDBTestFiles1() table: ", table1.Name, " error: ", err)
|
||||
log.Error("CreateTestFiles() table: ", table1.Name, " error: ", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -33,8 +34,8 @@ func CreateDBFiles(MapAll map[string]*types.Table) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// CreateDBFiles1 - создаёт 1 файл в папке db
|
||||
func CreateDBFiles1(Table1 *types.Table) error {
|
||||
// CreateFiles - создаёт 1 файл в папке db
|
||||
func CreateFiles(Table1 *types.Table) error {
|
||||
var err error
|
||||
|
||||
//чтение файлов
|
||||
@ -85,8 +86,8 @@ func CreateDBFiles1(Table1 *types.Table) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// CreateDBTestFiles1 - создаёт 1 файл в папке db
|
||||
func CreateDBTestFiles1(Table1 *types.Table) error {
|
||||
// CreateTestFiles - создаёт 1 файл в папке db
|
||||
func CreateTestFiles(Table1 *types.Table) error {
|
||||
var err error
|
||||
|
||||
//чтение файлов
|
||||
@ -137,112 +138,64 @@ func CreateDBTestFiles1(Table1 *types.Table) error {
|
||||
}
|
||||
|
||||
// DeleteFuncDelete - удаляет функцию Delete()
|
||||
func DeleteFuncDelete(TextDB string, Table1 *types.Table) string {
|
||||
Otvet := TextDB
|
||||
func DeleteFuncDelete(Text string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
_, ok := Table1.MapColumns["is_deleted"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
TextFind := "\n// Delete "
|
||||
pos1 := strings.Index(Otvet, TextFind)
|
||||
if pos1 < 0 {
|
||||
return Otvet
|
||||
}
|
||||
s2 := Otvet[pos1+1:]
|
||||
|
||||
posEnd := strings.Index(s2, "\n}")
|
||||
if posEnd < 0 {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = Otvet[:pos1-1] + Otvet[pos1+posEnd+3:]
|
||||
Otvet = create_files.DeleteFuncFromComment(Text, "\n// Delete ")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncRestore - удаляет функцию Restore()
|
||||
func DeleteFuncRestore(TextDB string, Table1 *types.Table) string {
|
||||
Otvet := TextDB
|
||||
func DeleteFuncRestore(Text string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
_, ok := Table1.MapColumns["is_deleted"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
TextFind := "\n// Restore "
|
||||
pos1 := strings.Index(Otvet, TextFind)
|
||||
if pos1 < 0 {
|
||||
return Otvet
|
||||
}
|
||||
s2 := Otvet[pos1+1:]
|
||||
|
||||
posEnd := strings.Index(s2, "\n}")
|
||||
if posEnd < 0 {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = Otvet[:pos1-1] + Otvet[pos1+posEnd+3:]
|
||||
Otvet = create_files.DeleteFuncFromComment(Text, "\n// Restore ")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncDeleteCtx - удаляет функцию Delete_ctx()
|
||||
func DeleteFuncDeleteCtx(TextDB string, Table1 *types.Table) string {
|
||||
Otvet := TextDB
|
||||
func DeleteFuncDeleteCtx(Text string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
_, ok := Table1.MapColumns["is_deleted"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
TextFind := "\n// Delete_ctx "
|
||||
pos1 := strings.Index(Otvet, TextFind)
|
||||
if pos1 < 0 {
|
||||
return Otvet
|
||||
}
|
||||
s2 := Otvet[pos1+1:]
|
||||
|
||||
posEnd := strings.Index(s2, "\n}")
|
||||
if posEnd < 0 {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = Otvet[:pos1-1] + Otvet[pos1+posEnd+3:]
|
||||
Otvet = create_files.DeleteFuncFromComment(Text, "\n// Delete_ctx ")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncRestoreCtx - удаляет функцию Restore_ctx()
|
||||
func DeleteFuncRestoreCtx(TextDB string, Table1 *types.Table) string {
|
||||
Otvet := TextDB
|
||||
func DeleteFuncRestoreCtx(Text string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
_, ok := Table1.MapColumns["is_deleted"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
TextFind := "\n// Restore_ctx "
|
||||
pos1 := strings.Index(Otvet, TextFind)
|
||||
if pos1 < 0 {
|
||||
return Otvet
|
||||
}
|
||||
s2 := Otvet[pos1+1:]
|
||||
|
||||
posEnd := strings.Index(s2, "\n}")
|
||||
if posEnd < 0 {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = Otvet[:pos1-1] + Otvet[pos1+posEnd+3:]
|
||||
Otvet = create_files.DeleteFuncFromComment(Text, "\n// Restore_ctx ")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncFind_byExtID - удаляет функцию Find_ByExtID()
|
||||
func DeleteFuncFind_byExtID(TextDB string, Table1 *types.Table) string {
|
||||
Otvet := TextDB
|
||||
func DeleteFuncFind_byExtID(Text string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
//
|
||||
_, ok := Table1.MapColumns["ext_id"]
|
||||
@ -256,78 +209,42 @@ func DeleteFuncFind_byExtID(TextDB string, Table1 *types.Table) string {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
TextFind := "\n// Find_ByExtID "
|
||||
pos1 := strings.Index(Otvet, TextFind)
|
||||
if pos1 < 0 {
|
||||
return Otvet
|
||||
}
|
||||
s2 := Otvet[pos1+1:]
|
||||
|
||||
posEnd := strings.Index(s2, "\n}")
|
||||
if posEnd < 0 {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = Otvet[:pos1-1] + Otvet[pos1+posEnd+3:]
|
||||
Otvet = create_files.DeleteFuncFromComment(Text, "\n// Find_ByExtID ")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncTestDelete - удаляет функцию Delete()
|
||||
func DeleteFuncTestDelete(TextDB string, Table1 *types.Table) string {
|
||||
Otvet := TextDB
|
||||
func DeleteFuncTestDelete(Text string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
_, ok := Table1.MapColumns["is_deleted"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
TextFind := "\nfunc TestDelete("
|
||||
pos1 := strings.Index(Otvet, TextFind)
|
||||
if pos1 < 0 {
|
||||
return Otvet
|
||||
}
|
||||
s2 := Otvet[pos1+1:]
|
||||
|
||||
posEnd := strings.Index(s2, "\n}")
|
||||
if posEnd < 0 {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = Otvet[:pos1-1] + Otvet[pos1+posEnd+3:]
|
||||
Otvet = create_files.DeleteFuncFromFuncName(Otvet, "TestDelete")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncTestRestore - удаляет функцию Restore()
|
||||
func DeleteFuncTestRestore(TextDB string, Table1 *types.Table) string {
|
||||
Otvet := TextDB
|
||||
func DeleteFuncTestRestore(Text string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
_, ok := Table1.MapColumns["is_deleted"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
TextFind := "\nfunc TestRestore("
|
||||
pos1 := strings.Index(Otvet, TextFind)
|
||||
if pos1 < 0 {
|
||||
return Otvet
|
||||
}
|
||||
s2 := Otvet[pos1+1:]
|
||||
|
||||
posEnd := strings.Index(s2, "\n}")
|
||||
if posEnd < 0 {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = Otvet[:pos1-1] + Otvet[pos1+posEnd+3:]
|
||||
Otvet = create_files.DeleteFuncFromFuncName(Otvet, "TestRestore")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncFind_byExtID - удаляет функцию Find_ByExtID()
|
||||
func DeleteFuncTestFind_byExtID(TextDB string, Table1 *types.Table) string {
|
||||
Otvet := TextDB
|
||||
func DeleteFuncTestFind_byExtID(Text string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
//
|
||||
_, ok := Table1.MapColumns["ext_id"]
|
||||
@ -341,19 +258,7 @@ func DeleteFuncTestFind_byExtID(TextDB string, Table1 *types.Table) string {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
TextFind := "func TestFind_ByExtID("
|
||||
pos1 := strings.Index(Otvet, TextFind)
|
||||
if pos1 < 0 {
|
||||
return Otvet
|
||||
}
|
||||
s2 := Otvet[pos1+1:]
|
||||
|
||||
posEnd := strings.Index(s2, "\n}")
|
||||
if posEnd < 0 {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = Otvet[:pos1-1] + Otvet[pos1+posEnd+3:]
|
||||
Otvet = create_files.DeleteFuncFromFuncName(Otvet, "TestFind_ByExtID")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
@ -390,7 +295,7 @@ func AddTextOmit(TextDB string, Table1 *types.Table) string {
|
||||
}
|
||||
|
||||
`
|
||||
} else if IsNumberType(TypeGo) == true && Column1.TableKey != "" {
|
||||
} else if create_files.IsNumberType(TypeGo) == true && Column1.TableKey != "" {
|
||||
TextFind := `if m.` + ColumnNameGo + ` == 0 {`
|
||||
pos1 := strings.Index(TextDB, TextFind)
|
||||
if pos1 >= 0 {
|
||||
@ -411,16 +316,3 @@ func AddTextOmit(TextDB string, Table1 *types.Table) string {
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
func IsNumberType(TypeGo string) bool {
|
||||
Otvet := false
|
||||
|
||||
switch TypeGo {
|
||||
case "int", "int8", "int16", "int32", "int64", "float32", "float64", "uint", "uint8", "uint16", "uint32", "uint64", "byte":
|
||||
{
|
||||
Otvet = true
|
||||
}
|
||||
}
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
263
internal/create_files/grpc_client/grpc_client.go
Normal file
263
internal/create_files/grpc_client/grpc_client.go
Normal file
@ -0,0 +1,263 @@
|
||||
package grpc_client
|
||||
|
||||
import (
|
||||
"github.com/ManyakRus/crud_generator/internal/config"
|
||||
"github.com/ManyakRus/crud_generator/internal/constants"
|
||||
"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"
|
||||
)
|
||||
|
||||
// CreateAllFiles - создаёт все файлы в папке grpc_client
|
||||
func CreateAllFiles(MapAll map[string]*types.Table) error {
|
||||
var err error
|
||||
|
||||
for _, table1 := range MapAll {
|
||||
//файлы grpc_client
|
||||
err = CreateFiles(table1)
|
||||
if err != nil {
|
||||
log.Error("CreateFiles() table: ", table1.Name, " error: ", err)
|
||||
return err
|
||||
}
|
||||
|
||||
//тестовые файлы grpc_client
|
||||
err = CreateTestFiles(table1)
|
||||
if err != nil {
|
||||
log.Error("CreateTestFiles() table: ", table1.Name, " error: ", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// CreateFiles - создаёт 1 файл в папке grpc_client
|
||||
func CreateFiles(Table1 *types.Table) error {
|
||||
var err error
|
||||
|
||||
//чтение файлов
|
||||
DirBin := micro.ProgramDir_bin()
|
||||
DirTemplates := DirBin + constants.FolderTemplates + micro.SeparatorFile()
|
||||
DirReady := DirBin + constants.FolderReady + micro.SeparatorFile()
|
||||
DirTemplatesGRPCClient := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_GRPC_CLIENT + micro.SeparatorFile()
|
||||
DirReadyGRPCClient := DirReady + "pkg" + micro.SeparatorFile() + "grpc" + micro.SeparatorFile() + "grpc_client" + micro.SeparatorFile()
|
||||
|
||||
FilenameTemplateGRPCClient := DirTemplatesGRPCClient + "grpc_client.go_"
|
||||
TableName := strings.ToLower(Table1.Name)
|
||||
DirTable := DirReadyGRPCClient + TableName + micro.SeparatorFile()
|
||||
FilenameReadyGRPCClient := DirTable + TableName + ".go"
|
||||
|
||||
//создадим каталог
|
||||
ok, err := micro.FileExists(DirTable)
|
||||
if ok == false {
|
||||
err = os.Mkdir(DirTable, 0777)
|
||||
if err != nil {
|
||||
log.Panic("Mkdir() ", DirTable, " error: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
bytes, err := os.ReadFile(FilenameTemplateGRPCClient)
|
||||
if err != nil {
|
||||
log.Panic("ReadFile() ", FilenameTemplateGRPCClient, " error: ", err)
|
||||
}
|
||||
TextDB := string(bytes)
|
||||
|
||||
//создание текста
|
||||
ModelName := Table1.NameGo
|
||||
TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
|
||||
TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
|
||||
TextDB = constants.TEXT_GENERATED + TextDB
|
||||
|
||||
if config.Settings.HAS_IS_DELETED == true {
|
||||
TextDB = DeleteFuncDelete(TextDB, ModelName, Table1)
|
||||
//TextDB = DeleteFuncDeleteCtx(TextDB, ModelName, Table1)
|
||||
TextDB = DeleteFuncRestore(TextDB, ModelName, Table1)
|
||||
//TextDB = DeleteFuncRestoreCtx(TextDB, ModelName, Table1)
|
||||
}
|
||||
TextDB = DeleteFuncFind_byExtID(TextDB, ModelName, Table1)
|
||||
|
||||
//запись файла
|
||||
err = os.WriteFile(FilenameReadyGRPCClient, []byte(TextDB), constants.FILE_PERMISSIONS)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// CreateTestFiles - создаёт 1 файл в папке grpc_client
|
||||
func CreateTestFiles(Table1 *types.Table) error {
|
||||
var err error
|
||||
|
||||
//чтение файлов
|
||||
DirBin := micro.ProgramDir_bin()
|
||||
DirTemplates := DirBin + constants.FolderTemplates + micro.SeparatorFile()
|
||||
DirReady := DirBin + constants.FolderReady + micro.SeparatorFile()
|
||||
DirTemplatesGRPCClient := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_GRPC_CLIENT + micro.SeparatorFile()
|
||||
DirReadyGRPCClient := DirReady + "pkg" + micro.SeparatorFile() + "grpc" + micro.SeparatorFile() + "grpc_client" + micro.SeparatorFile()
|
||||
|
||||
FilenameTemplateGRPCClient := DirTemplatesGRPCClient + "grpc_client_test.go_"
|
||||
TableName := strings.ToLower(Table1.Name)
|
||||
DirTable := DirReadyGRPCClient + TableName + micro.SeparatorFile()
|
||||
FilenameReadyGRPCClient := DirTable + TableName + "_test.go"
|
||||
|
||||
//создадим каталог
|
||||
ok, err := micro.FileExists(DirTable)
|
||||
if ok == false {
|
||||
err = os.Mkdir(DirTable, 0777)
|
||||
if err != nil {
|
||||
log.Panic("Mkdir() ", DirTable, " error: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
bytes, err := os.ReadFile(FilenameTemplateGRPCClient)
|
||||
if err != nil {
|
||||
log.Panic("ReadFile() ", FilenameTemplateGRPCClient, " error: ", err)
|
||||
}
|
||||
TextDB := string(bytes)
|
||||
|
||||
//создание текста
|
||||
ModelName := Table1.NameGo
|
||||
TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
|
||||
TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
|
||||
TextDB = constants.TEXT_GENERATED + TextDB
|
||||
|
||||
if config.Settings.HAS_IS_DELETED == true {
|
||||
TextDB = DeleteFuncDelete(TextDB, ModelName, Table1)
|
||||
//TextDB = DeleteFuncDeleteCtx(TextDB, ModelName, Table1)
|
||||
TextDB = DeleteFuncRestore(TextDB, ModelName, Table1)
|
||||
//TextDB = DeleteFuncRestoreCtx(TextDB, ModelName, Table1)
|
||||
}
|
||||
TextDB = DeleteFuncFind_byExtID(TextDB, ModelName, Table1)
|
||||
|
||||
//запись файла
|
||||
err = os.WriteFile(FilenameReadyGRPCClient, []byte(TextDB), constants.FILE_PERMISSIONS)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteFuncDelete - удаляет функцию Delete()
|
||||
func DeleteFuncDelete(Text, ModelName string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
_, ok := Table1.MapColumns["is_deleted"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = create_files.DeleteFuncFromComment(Text, "\n// Delete ")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncRestore - удаляет функцию Restore()
|
||||
func DeleteFuncRestore(Text, ModelName string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
_, ok := Table1.MapColumns["is_deleted"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = create_files.DeleteFuncFromComment(Text, "\n// Restore ")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncDeleteCtx - удаляет функцию Delete_ctx()
|
||||
func DeleteFuncDeleteCtx(Text, ModelName string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
_, ok := Table1.MapColumns["is_deleted"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = create_files.DeleteFuncFromComment(Text, "\n// Delete_ctx ")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncRestoreCtx - удаляет функцию Restore_ctx()
|
||||
func DeleteFuncRestoreCtx(Text, ModelName string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
_, ok := Table1.MapColumns["is_deleted"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = create_files.DeleteFuncFromComment(Text, "\n// Restore_ctx ")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncFind_byExtID - удаляет функцию Find_ByExtID()
|
||||
func DeleteFuncFind_byExtID(Text, ModelName string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
//
|
||||
_, ok := Table1.MapColumns["ext_id"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
//
|
||||
_, ok = Table1.MapColumns["connection_id"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = create_files.DeleteFuncFromComment(Text, "\n// Find_ByExtID ")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncTestDelete - удаляет функцию Delete()
|
||||
func DeleteFuncTestDelete(Text, ModelName string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
_, ok := Table1.MapColumns["is_deleted"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = create_files.DeleteFuncFromFuncName(Otvet, "TestDelete")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncTestRestore - удаляет функцию Restore()
|
||||
func DeleteFuncTestRestore(Text, ModelName string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
_, ok := Table1.MapColumns["is_deleted"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = create_files.DeleteFuncFromFuncName(Otvet, "TestRestore")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncFind_byExtID - удаляет функцию Find_ByExtID()
|
||||
func DeleteFuncTestFind_byExtID(Text, ModelName string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
//
|
||||
_, ok := Table1.MapColumns["ext_id"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
//
|
||||
_, ok = Table1.MapColumns["connection_id"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = create_files.DeleteFuncFromFuncName(Otvet, "TestFind_ByExtID")
|
||||
|
||||
return Otvet
|
||||
}
|
1
internal/create_files/grpc_client/grpc_client_test.go
Normal file
1
internal/create_files/grpc_client/grpc_client_test.go
Normal file
@ -0,0 +1 @@
|
||||
package grpc_client
|
245
internal/create_files/grpc_server/grpc_server.go
Normal file
245
internal/create_files/grpc_server/grpc_server.go
Normal file
@ -0,0 +1,245 @@
|
||||
package grpc_server
|
||||
|
||||
import (
|
||||
"github.com/ManyakRus/crud_generator/internal/config"
|
||||
"github.com/ManyakRus/crud_generator/internal/constants"
|
||||
"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"
|
||||
)
|
||||
|
||||
// CreateAllFiles - создаёт все файлы в папке grpc_server
|
||||
func CreateAllFiles(MapAll map[string]*types.Table) error {
|
||||
var err error
|
||||
|
||||
for _, table1 := range MapAll {
|
||||
//файлы grpc_server
|
||||
err = CreateFiles(table1)
|
||||
if err != nil {
|
||||
log.Error("CreateFiles() table: ", table1.Name, " error: ", err)
|
||||
return err
|
||||
}
|
||||
|
||||
//тестовые файлы grpc_server
|
||||
err = CreateTestFiles(table1)
|
||||
if err != nil {
|
||||
log.Error("CreateTestFiles() table: ", table1.Name, " error: ", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// CreateFiles - создаёт 1 файл в папке grpc_server
|
||||
func CreateFiles(Table1 *types.Table) error {
|
||||
var err error
|
||||
|
||||
//чтение файлов
|
||||
DirBin := micro.ProgramDir_bin()
|
||||
DirTemplates := DirBin + constants.FolderTemplates + micro.SeparatorFile()
|
||||
DirReady := DirBin + constants.FolderReady + micro.SeparatorFile()
|
||||
DirTemplatesGRPCServer := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_GRPC_SERVER + micro.SeparatorFile()
|
||||
DirReadyGRPCServer := DirReady + "internal" + micro.SeparatorFile() + "grpc_server" + micro.SeparatorFile()
|
||||
|
||||
FilenameTemplateGRPCServer := DirTemplatesGRPCServer + "grpc_server.go_"
|
||||
TableName := strings.ToLower(Table1.Name)
|
||||
DirTable := DirReadyGRPCServer
|
||||
FilenameReadyGRPCServer := DirTable + TableName + ".go"
|
||||
|
||||
bytes, err := os.ReadFile(FilenameTemplateGRPCServer)
|
||||
if err != nil {
|
||||
log.Panic("ReadFile() ", FilenameTemplateGRPCServer, " error: ", err)
|
||||
}
|
||||
TextDB := string(bytes)
|
||||
|
||||
//создание текста
|
||||
ModelName := Table1.NameGo
|
||||
TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
|
||||
TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
|
||||
TextDB = constants.TEXT_GENERATED + TextDB
|
||||
|
||||
if config.Settings.HAS_IS_DELETED == true {
|
||||
TextDB = DeleteFuncDelete(TextDB, ModelName, Table1)
|
||||
TextDB = DeleteFuncDeleteCtx(TextDB, ModelName, Table1)
|
||||
TextDB = DeleteFuncRestore(TextDB, ModelName, Table1)
|
||||
TextDB = DeleteFuncRestoreCtx(TextDB, ModelName, Table1)
|
||||
}
|
||||
TextDB = DeleteFuncFind_byExtID(TextDB, ModelName, Table1)
|
||||
|
||||
//запись файла
|
||||
err = os.WriteFile(FilenameReadyGRPCServer, []byte(TextDB), constants.FILE_PERMISSIONS)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// CreateTestFiles - создаёт 1 файл в папке grpc_server
|
||||
func CreateTestFiles(Table1 *types.Table) error {
|
||||
var err error
|
||||
|
||||
//чтение файлов
|
||||
DirBin := micro.ProgramDir_bin()
|
||||
DirTemplates := DirBin + constants.FolderTemplates + micro.SeparatorFile()
|
||||
DirReady := DirBin + constants.FolderReady + micro.SeparatorFile()
|
||||
DirTemplatesGRPCServer := DirTemplates + config.Settings.TEMPLATE_FOLDERNAME_GRPC_SERVER + micro.SeparatorFile()
|
||||
DirReadyGRPCServer := DirReady + "internal" + micro.SeparatorFile() + "grpc_server" + micro.SeparatorFile()
|
||||
|
||||
FilenameTemplateGRPCServer := DirTemplatesGRPCServer + "grpc_server_test.go_"
|
||||
TableName := strings.ToLower(Table1.Name)
|
||||
DirTable := DirReadyGRPCServer
|
||||
FilenameReadyGRPCServer := DirTable + TableName + "_test.go"
|
||||
|
||||
bytes, err := os.ReadFile(FilenameTemplateGRPCServer)
|
||||
if err != nil {
|
||||
log.Panic("ReadFile() ", FilenameTemplateGRPCServer, " error: ", err)
|
||||
}
|
||||
TextDB := string(bytes)
|
||||
|
||||
//создание текста
|
||||
ModelName := Table1.NameGo
|
||||
TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_MODEL, ModelName)
|
||||
TextDB = strings.ReplaceAll(TextDB, config.Settings.TEXT_TEMPLATE_TABLENAME, Table1.Name)
|
||||
TextDB = constants.TEXT_GENERATED + TextDB
|
||||
|
||||
if config.Settings.HAS_IS_DELETED == true {
|
||||
TextDB = DeleteFuncDelete(TextDB, ModelName, Table1)
|
||||
TextDB = DeleteFuncDeleteCtx(TextDB, ModelName, Table1)
|
||||
TextDB = DeleteFuncRestore(TextDB, ModelName, Table1)
|
||||
TextDB = DeleteFuncRestoreCtx(TextDB, ModelName, Table1)
|
||||
}
|
||||
TextDB = DeleteFuncFind_byExtID(TextDB, ModelName, Table1)
|
||||
|
||||
//запись файла
|
||||
err = os.WriteFile(FilenameReadyGRPCServer, []byte(TextDB), constants.FILE_PERMISSIONS)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// DeleteFuncDelete - удаляет функцию Delete()
|
||||
func DeleteFuncDelete(Text, ModelName string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
_, ok := Table1.MapColumns["is_deleted"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = create_files.DeleteFuncFromComment(Text, "\n// "+ModelName+"_Delete ")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncRestore - удаляет функцию Restore()
|
||||
func DeleteFuncRestore(Text, ModelName string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
_, ok := Table1.MapColumns["is_deleted"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = create_files.DeleteFuncFromComment(Text, "\n// "+ModelName+"_Restore ")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncDeleteCtx - удаляет функцию Delete_ctx()
|
||||
func DeleteFuncDeleteCtx(Text, ModelName string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
_, ok := Table1.MapColumns["is_deleted"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = create_files.DeleteFuncFromComment(Text, "\n// "+ModelName+"_Delete_ctx ")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncRestoreCtx - удаляет функцию Restore_ctx()
|
||||
func DeleteFuncRestoreCtx(Text, ModelName string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
_, ok := Table1.MapColumns["is_deleted"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = create_files.DeleteFuncFromComment(Text, "\n// "+ModelName+"_Restore_ctx ")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncFind_byExtID - удаляет функцию Find_ByExtID()
|
||||
func DeleteFuncFind_byExtID(Text, ModelName string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
//
|
||||
_, ok := Table1.MapColumns["ext_id"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
//
|
||||
_, ok = Table1.MapColumns["connection_id"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = create_files.DeleteFuncFromComment(Text, "\n// "+ModelName+"_Find_ByExtID ")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncTestDelete - удаляет функцию Delete()
|
||||
func DeleteFuncTestDelete(Text, ModelName string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
_, ok := Table1.MapColumns["is_deleted"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = create_files.DeleteFuncFromFuncName(Otvet, "Test_server_"+ModelName+"Delete")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncTestRestore - удаляет функцию Restore()
|
||||
func DeleteFuncTestRestore(Text, ModelName string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
_, ok := Table1.MapColumns["is_deleted"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = create_files.DeleteFuncFromFuncName(Otvet, "Test_server_"+ModelName+"Restore")
|
||||
|
||||
return Otvet
|
||||
}
|
||||
|
||||
// DeleteFuncFind_byExtID - удаляет функцию Find_ByExtID()
|
||||
func DeleteFuncTestFind_byExtID(Text, ModelName string, Table1 *types.Table) string {
|
||||
Otvet := Text
|
||||
|
||||
//
|
||||
_, ok := Table1.MapColumns["ext_id"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
//
|
||||
_, ok = Table1.MapColumns["connection_id"]
|
||||
if ok == true {
|
||||
return Otvet
|
||||
}
|
||||
|
||||
Otvet = create_files.DeleteFuncFromFuncName(Otvet, "Test_server_"+ModelName+"Find_ByExtID")
|
||||
|
||||
return Otvet
|
||||
}
|
1
internal/create_files/grpc_server/grpc_server_test.go
Normal file
1
internal/create_files/grpc_server/grpc_server_test.go
Normal file
@ -0,0 +1 @@
|
||||
package grpc_server
|
@ -14,13 +14,13 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func CreateModelFiles(MapAll map[string]*types.Table) error {
|
||||
func CreateAllFiles(MapAll map[string]*types.Table) error {
|
||||
var err error
|
||||
|
||||
for _, table1 := range MapAll {
|
||||
err = CreateModelFiles1(table1)
|
||||
err = CreateFiles(table1)
|
||||
if err != nil {
|
||||
log.Error("CreateModelFiles1() table: ", table1.Name, " error: ", err)
|
||||
log.Error("CreateFiles() table: ", table1.Name, " error: ", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -28,7 +28,7 @@ func CreateModelFiles(MapAll map[string]*types.Table) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func CreateModelFiles1(Table1 *types.Table) error {
|
||||
func CreateFiles(Table1 *types.Table) error {
|
||||
var err error
|
||||
|
||||
//чтение файлов
|
||||
|
@ -116,7 +116,7 @@ func CreateAllFolders() {
|
||||
}
|
||||
|
||||
//
|
||||
Filename = dir + config.Settings.SERVICE_NAME + micro.SeparatorFile() + "pkg" + micro.SeparatorFile() + "grpc" + micro.SeparatorFile() + "grpc_server"
|
||||
Filename = dir + config.Settings.SERVICE_NAME + micro.SeparatorFile() + "internal" + micro.SeparatorFile() + "grpc_server"
|
||||
ok, err = micro.FileExists(Filename)
|
||||
if ok == false || err != nil {
|
||||
err = CreateFolder(Filename, 0777)
|
||||
@ -162,7 +162,7 @@ func CreateAllFolders() {
|
||||
}
|
||||
|
||||
//
|
||||
Filename = dir + config.Settings.SERVICE_NAME + micro.SeparatorFile() + "pkg" + micro.SeparatorFile() + "grpc" + micro.SeparatorFile() + "nrpc_server"
|
||||
Filename = dir + config.Settings.SERVICE_NAME + micro.SeparatorFile() + "internal" + micro.SeparatorFile() + "nrpc_server"
|
||||
ok, err = micro.FileExists(Filename)
|
||||
if ok == false || err != nil {
|
||||
err = CreateFolder(Filename, 0777)
|
||||
@ -173,7 +173,7 @@ func CreateAllFolders() {
|
||||
}
|
||||
|
||||
//
|
||||
Filename = dir + config.Settings.SERVICE_NAME + micro.SeparatorFile() + "pkg" + micro.SeparatorFile() + "grpc" + micro.SeparatorFile() + "nrpc_client"
|
||||
Filename = dir + config.Settings.SERVICE_NAME + micro.SeparatorFile() + "pkg" + micro.SeparatorFile() + "nrpc" + micro.SeparatorFile() + "nrpc_client"
|
||||
ok, err = micro.FileExists(Filename)
|
||||
if ok == false || err != nil {
|
||||
err = CreateFolder(Filename, 0777)
|
||||
|
@ -2,6 +2,8 @@ package logic
|
||||
|
||||
import (
|
||||
"github.com/ManyakRus/crud_generator/internal/create_files/db"
|
||||
"github.com/ManyakRus/crud_generator/internal/create_files/grpc_client"
|
||||
"github.com/ManyakRus/crud_generator/internal/create_files/grpc_server"
|
||||
"github.com/ManyakRus/crud_generator/internal/create_files/model"
|
||||
"github.com/ManyakRus/crud_generator/internal/postgres"
|
||||
"github.com/ManyakRus/starter/log"
|
||||
@ -24,13 +26,25 @@ func StartFillAll() error {
|
||||
}
|
||||
|
||||
//модель
|
||||
err = model.CreateModelFiles(MapAll)
|
||||
err = model.CreateAllFiles(MapAll)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//db crud
|
||||
err = db.CreateDBFiles(MapAll)
|
||||
err = db.CreateAllFiles(MapAll)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//grpc_server
|
||||
err = grpc_server.CreateAllFiles(MapAll)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//grpc_client
|
||||
err = grpc_client.CreateAllFiles(MapAll)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user