From eaeae88f043fe1319e684eadcf48e9c0ff78a4bf Mon Sep 17 00:00:00 2001 From: "2944321442@qq.com" <1320751758@qq.com> Date: Tue, 4 Mar 2025 15:19:27 +0800 Subject: [PATCH 1/5] feat: add service model mapping configuration --- .vscode/launch.json | 16 +++ go.mod | 6 +- go.sum | 6 +- module/service/dto/input.go | 2 + module/service/dto/output.go | 22 ++-- module/service/iml.go | 51 +++++--- service/service/iml.go | 6 +- service/service_model_mapping/iml.go | 88 +++++++++++++ service/service_model_mapping/model.go | 38 ++++++ service/service_model_mapping/model_test.go | 138 ++++++++++++++++++++ service/service_model_mapping/service.go | 25 ++++ stores/service/model_mapping.go | 42 ++++++ stores/service/store.go | 11 ++ 13 files changed, 416 insertions(+), 35 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 service/service_model_mapping/iml.go create mode 100644 service/service_model_mapping/model.go create mode 100644 service/service_model_mapping/model_test.go create mode 100644 service/service_model_mapping/service.go create mode 100644 stores/service/model_mapping.go diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..6de470d3 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // 使用 IntelliSense 了解相关属性。 + // 悬停以查看现有属性的描述。 + // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Launch Package", + "type": "go", + "request": "launch", + "mode": "auto", + "program": "${workspaceFolder}", + "args": ["-c", "${workspaceFolder}/config.yml"] + } + ] +} diff --git a/go.mod b/go.mod index fb4396bf..0c475f61 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ go 1.21 require ( github.com/eolinker/ap-account v1.0.15 github.com/eolinker/eosc v0.18.3 - github.com/eolinker/go-common v1.1.4 + github.com/eolinker/go-common v1.1.5 github.com/gabriel-vasile/mimetype v1.4.4 github.com/getkin/kin-openapi v0.127.0 github.com/gin-gonic/gin v1.10.0 @@ -15,6 +15,7 @@ require ( github.com/google/uuid v1.6.0 github.com/influxdata/influxdb-client-go/v2 v2.14.0 github.com/nsqio/go-nsq v1.1.0 + github.com/stretchr/testify v1.9.0 github.com/urfave/cli v1.22.16 golang.org/x/crypto v0.24.0 gopkg.in/yaml.v3 v3.0.1 @@ -29,10 +30,10 @@ require ( github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect github.com/dustin/go-humanize v1.0.0 // indirect github.com/ghodss/yaml v1.0.0 // indirect - github.com/gin-contrib/gzip v1.0.1 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-openapi/jsonpointer v0.21.0 // indirect github.com/go-openapi/swag v0.23.0 // indirect @@ -60,6 +61,7 @@ require ( github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/perimeterx/marshmallow v1.1.5 // indirect github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/redis/go-redis/v9 v9.5.3 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect diff --git a/go.sum b/go.sum index 01c9b855..bfec4ac8 100644 --- a/go.sum +++ b/go.sum @@ -32,16 +32,14 @@ github.com/eolinker/ap-account v1.0.15 h1:n6DJeL6RHZ8eLlZUcY2U3H4d/GPaA5oelAx3R0 github.com/eolinker/ap-account v1.0.15/go.mod h1:zm/Ivs6waJ/M/nEszhpPmM6g50y/MKO+5eABFAdeD0g= github.com/eolinker/eosc v0.18.3 h1:3IK5HkAPnJRfLbQ0FR7kWsZr6Y/OiqqGazvN1q2BL5A= github.com/eolinker/eosc v0.18.3/go.mod h1:O9PQQXFCpB6fjHf+oFt/LN6EOAv779ItbMixMKCfTfk= -github.com/eolinker/go-common v1.1.4 h1:U5AtQMr3RCudgeV6jcX5TemUGrTz8WqLu//KrZm3BzA= -github.com/eolinker/go-common v1.1.4/go.mod h1:Kb/jENMN1mApnodvRgV4YwO9FJby1Jkt2EUjrBjvSX4= +github.com/eolinker/go-common v1.1.5 h1:unYPcpptqL2zk+BNuzc4cJVJBDEpjs918nTkGUw+q7U= +github.com/eolinker/go-common v1.1.5/go.mod h1:Kb/jENMN1mApnodvRgV4YwO9FJby1Jkt2EUjrBjvSX4= github.com/gabriel-vasile/mimetype v1.4.4 h1:QjV6pZ7/XZ7ryI2KuyeEDE8wnh7fHP9YnQy+R0LnH8I= github.com/gabriel-vasile/mimetype v1.4.4/go.mod h1:JwLei5XPtWdGiMFB5Pjle1oEeoSeEuJfJE+TtfvdB/s= github.com/getkin/kin-openapi v0.127.0 h1:Mghqi3Dhryf3F8vR370nN67pAERW+3a95vomb3MAREY= github.com/getkin/kin-openapi v0.127.0/go.mod h1:OZrfXzUfGrNbsKj+xmFBx6E5c6yH3At/tAKSc2UszXM= github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= -github.com/gin-contrib/gzip v1.0.1 h1:HQ8ENHODeLY7a4g1Au/46Z92bdGFl74OhxcZble9WJE= -github.com/gin-contrib/gzip v1.0.1/go.mod h1:njt428fdUNRvjuJf16tZMYZ2Yl+WQB53X5wmhDwXvC4= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= diff --git a/module/service/dto/input.go b/module/service/dto/input.go index 333c79d8..979efa46 100644 --- a/module/service/dto/input.go +++ b/module/service/dto/input.go @@ -14,6 +14,7 @@ type CreateService struct { Provider *string `json:"provider" aocheck:"ai_provider"` AsApp *bool `json:"as_app"` AsServer *bool `json:"as_server"` + ModelMapping string `json:"model_mapping"` } type EditService struct { @@ -25,6 +26,7 @@ type EditService struct { Tags *[]string `json:"tags"` Provider *string `json:"provider" aocheck:"ai_provider"` ApprovalType *string `json:"approval_type"` + ModelMapping string `json:"model_mapping"` } type CreateApp struct { diff --git a/module/service/dto/output.go b/module/service/dto/output.go index d4d6e4c5..b2c073c0 100644 --- a/module/service/dto/output.go +++ b/module/service/dto/output.go @@ -62,6 +62,7 @@ type Service struct { AsServer bool `json:"as_server"` AsApp bool `json:"as_app"` ServiceKind string `json:"service_kind"` + ModelMapping string `json:"model_mapping"` } type App struct { @@ -133,16 +134,17 @@ type ServiceDoc struct { } type ExportService struct { - Id string `json:"id"` - Name string `json:"name"` - Prefix string `json:"prefix,omitempty"` - Description string `json:"description"` - Team string `json:"team"` - ServiceType string `json:"service_type"` - Catalogue string `json:"catalogue"` - Tags []string `json:"tags"` - Logo string `json:"logo"` - Doc string `json:"doc"` + Id string `json:"id"` + Name string `json:"name"` + Prefix string `json:"prefix,omitempty"` + Description string `json:"description"` + Team string `json:"team"` + ServiceType string `json:"service_type"` + Catalogue string `json:"catalogue"` + Tags []string `json:"tags"` + Logo string `json:"logo"` + Doc string `json:"doc"` + ModelMapping string `json:"model_mapping"` } type ExportApp struct { diff --git a/module/service/iml.go b/module/service/iml.go index 8edf26d3..cc63add4 100644 --- a/module/service/iml.go +++ b/module/service/iml.go @@ -15,6 +15,7 @@ import ( "github.com/eolinker/ap-account/service/role" application_authorization "github.com/APIParkLab/APIPark/service/application-authorization" + "github.com/APIParkLab/APIPark/service/service_model_mapping" api_doc "github.com/APIParkLab/APIPark/service/api-doc" @@ -63,6 +64,8 @@ type imlServiceModule struct { apiService api.IAPIService `autowired:""` apiDocService api_doc.IAPIDocService `autowired:""` transaction store.ITransaction `autowired:""` + + serviceModelMappingService service_model_mapping.IServiceModelMappingService `autowired:""` } func (i *imlServiceModule) ExportAll(ctx context.Context) ([]*service_dto.ExportService, error) { @@ -111,11 +114,9 @@ func (i *imlServiceModule) ExportAll(ctx context.Context) ([]*service_dto.Export items = append(items, info) } return items, nil - } func (i *imlServiceModule) searchMyServices(ctx context.Context, teamId string, keyword string) ([]*service.Service, error) { - userID := utils.UserId(ctx) condition := make(map[string]interface{}) condition["as_server"] = true @@ -135,7 +136,6 @@ func (i *imlServiceModule) searchMyServices(ctx context.Context, teamId string, condition["team"] = teamIds return i.serviceService.Search(ctx, keyword, condition, "update_at desc") } - } func (i *imlServiceModule) SearchMyServices(ctx context.Context, teamId string, keyword string) ([]*service_dto.ServiceItem, error) { @@ -177,7 +177,6 @@ func (i *imlServiceModule) Simple(ctx context.Context) ([]*service_dto.SimpleSer items := make([]*service_dto.SimpleServiceItem, 0, len(services)) for _, p := range services { - items = append(items, &service_dto.SimpleServiceItem{ Id: p.Id, Name: p.Name, @@ -190,14 +189,12 @@ func (i *imlServiceModule) Simple(ctx context.Context) ([]*service_dto.SimpleSer func (i *imlServiceModule) MySimple(ctx context.Context) ([]*service_dto.SimpleServiceItem, error) { services, err := i.searchMyServices(ctx, "", "") - if err != nil { return nil, err } items := make([]*service_dto.SimpleServiceItem, 0, len(services)) for _, p := range services { - items = append(items, &service_dto.SimpleServiceItem{ Id: p.Id, Name: p.Name, @@ -223,6 +220,13 @@ func (i *imlServiceModule) Get(ctx context.Context, id string) (*service_dto.Ser s.Tags = auto.List(utils.SliceToSlice(tags, func(p *service_tag.Tag) string { return p.Tid })) + + serviceModelMapping, err := i.serviceModelMappingService.GetByService(ctx, id) + if err != nil { + return nil, err + } + s.ModelMapping = serviceModelMapping.Content + log.Infof("get service cost %d ms", time.Since(now).Milliseconds()) return s, nil } @@ -286,7 +290,6 @@ func toServiceItem(model *service.Service) *service_dto.ServiceItem { } func (i *imlServiceModule) Create(ctx context.Context, teamID string, input *service_dto.CreateService) (*service_dto.Service, error) { - if input.Id == "" { input.Id = uuid.New().String() } @@ -343,6 +346,15 @@ func (i *imlServiceModule) Create(ctx context.Context, teamID string, input *ser } } } + if input.ModelMapping != "" { + err := i.serviceModelMappingService.Create(ctx, &service_model_mapping.Create{ + Service: input.Id, + Content: input.ModelMapping, + }) + if err != nil { + return err + } + } return i.serviceService.Create(ctx, mo) }) if err != nil { @@ -362,7 +374,6 @@ func (i *imlServiceModule) Edit(ctx context.Context, id string, input *service_d if input.Provider != nil { info.AdditionalConfig["provider"] = *input.Provider } - } err = i.transaction.Transaction(ctx, func(ctx context.Context) error { serviceType := (*service.ServiceType)(input.ServiceType) @@ -404,9 +415,23 @@ func (i *imlServiceModule) Edit(ctx context.Context, id string, input *service_d } } } + if input.ModelMapping != "" { + // 先删除旧的映射 + err := i.serviceModelMappingService.Delete(ctx, id) + if err != nil { + return err + } + // 创建新的映射 + err = i.serviceModelMappingService.Create(ctx, &service_model_mapping.Create{ + Service: id, + Content: input.ModelMapping, + }) + if err != nil { + return err + } + } return nil }) - if err != nil { return nil, err } @@ -470,7 +495,6 @@ type imlServiceDocModule struct { func (i *imlServiceDocModule) ServiceDoc(ctx context.Context, pid string) (*serviceDto.ServiceDoc, error) { _, err := i.serviceService.Check(ctx, pid, map[string]bool{"as_server": true}) - if err != nil { return nil, err } @@ -502,7 +526,6 @@ func (i *imlServiceDocModule) ServiceDoc(ctx context.Context, pid string) (*serv func (i *imlServiceDocModule) SaveServiceDoc(ctx context.Context, pid string, input *serviceDto.SaveServiceDoc) error { _, err := i.serviceService.Check(ctx, pid, map[string]bool{"as_server": true}) - if err != nil { return err } @@ -684,7 +707,6 @@ func (i *imlAppModule) Search(ctx context.Context, teamId string, keyword string } func (i *imlAppModule) CreateApp(ctx context.Context, teamID string, input *service_dto.CreateApp) (*service_dto.App, error) { - if input.Id == "" { input.Id = uuid.New().String() } @@ -706,9 +728,7 @@ func (i *imlAppModule) CreateApp(ctx context.Context, teamID string, input *serv } err = i.transaction.Transaction(ctx, func(ctx context.Context) error { - return i.serviceService.Create(ctx, mo) - }) if err != nil { return nil, err @@ -717,7 +737,7 @@ func (i *imlAppModule) CreateApp(ctx context.Context, teamID string, input *serv } func (i *imlAppModule) UpdateApp(ctx context.Context, appId string, input *service_dto.UpdateApp) (*service_dto.App, error) { - //userId := utils.UserId(ctx) + // userId := utils.UserId(ctx) info, err := i.serviceService.Get(ctx, appId) if err != nil { return nil, err @@ -854,7 +874,6 @@ func (i *imlAppModule) MySimpleApps(ctx context.Context, keyword string) ([]*ser } items := make([]*service_dto.SimpleAppItem, 0, len(services)) for _, p := range services { - items = append(items, &service_dto.SimpleAppItem{ Id: p.Id, Name: p.Name, diff --git a/service/service/iml.go b/service/service/iml.go index 719e55e7..be5ea44f 100644 --- a/service/service/iml.go +++ b/service/service/iml.go @@ -14,9 +14,7 @@ import ( "github.com/APIParkLab/APIPark/stores/service" ) -var ( - _ IServiceService = (*imlServiceService)(nil) -) +var _ IServiceService = (*imlServiceService)(nil) type imlServiceService struct { store service.IServiceStore `autowired:""` @@ -148,9 +146,11 @@ func (i *imlServiceService) OnComplete() { func labelHandler(e *service.Service) []string { return []string{e.Name, e.UUID, e.Description} } + func uniquestHandler(i *Create) []map[string]interface{} { return []map[string]interface{}{{"uuid": i.Id}} } + func createEntityHandler(i *Create) *service.Service { cfg, _ := json.Marshal(i.AdditionalConfig) now := time.Now() diff --git a/service/service_model_mapping/iml.go b/service/service_model_mapping/iml.go new file mode 100644 index 00000000..e7c26793 --- /dev/null +++ b/service/service_model_mapping/iml.go @@ -0,0 +1,88 @@ +package service_model_mapping + +import ( + "context" + "errors" + "time" + + "github.com/APIParkLab/APIPark/service/universally" + "github.com/APIParkLab/APIPark/stores/service" + "github.com/eolinker/go-common/auto" + "github.com/eolinker/go-common/utils" + "github.com/google/uuid" + "gorm.io/gorm" +) + +var _ IServiceModelMappingService = (*imlServiceModelMappingService)(nil) + +type imlServiceModelMappingService struct { + store service.IModelMappingStore `autowired:""` + universally.IServiceGet[ModelMapping] + universally.IServiceDelete + universally.IServiceCreate[Create] + universally.IServiceEdit[Edit] +} + +func (i *imlServiceModelMappingService) GetByService(ctx context.Context, serviceId string) (*ModelMapping, error) { + w := map[string]interface{}{ + "service": serviceId, + } + entity, err := i.store.First(ctx, w) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return &ModelMapping{ + Service: serviceId, + Content: "", + }, nil + } + return nil, err + } + return FromEntity(entity), nil +} + +func (i *imlServiceModelMappingService) GetLabels(ctx context.Context, ids ...string) map[string]string { + if len(ids) == 0 { + return nil + } + list, err := i.store.ListQuery(ctx, "`uuid` in (?)", []interface{}{ids}, "id") + if err != nil { + return nil + } + return utils.SliceToMapO(list, func(i *service.ModelMapping) (string, string) { + return i.UUID, i.Service + }) +} + +func (i *imlServiceModelMappingService) OnComplete() { + i.IServiceGet = universally.NewGetSoftDelete[ModelMapping, service.ModelMapping](i.store, FromEntity) + i.IServiceDelete = universally.NewSoftDelete[service.ModelMapping](i.store) + i.IServiceCreate = universally.NewCreatorSoftDelete[Create, service.ModelMapping](i.store, "service_model_mapping", createEntityHandler, uniquestHandler, labelHandler) + i.IServiceEdit = universally.NewEdit[Edit, service.ModelMapping](i.store, updateHandler, labelHandler) + auto.RegisterService("service_model_mapping", i) +} + +func labelHandler(e *service.ModelMapping) []string { + return []string{e.Service, e.UUID} +} + +func uniquestHandler(i *Create) []map[string]interface{} { + return []map[string]interface{}{{"service": i.Service}} +} + +func createEntityHandler(i *Create) *service.ModelMapping { + now := time.Now() + return &service.ModelMapping{ + UUID: uuid.New().String(), + Service: i.Service, + Content: i.Content, + CreateAt: now, + UpdateAt: now, + } +} + +func updateHandler(e *service.ModelMapping, i *Edit) { + if i.Content != nil { + e.Content = *i.Content + e.UpdateAt = time.Now() + } +} diff --git a/service/service_model_mapping/model.go b/service/service_model_mapping/model.go new file mode 100644 index 00000000..ae504a45 --- /dev/null +++ b/service/service_model_mapping/model.go @@ -0,0 +1,38 @@ +package service_model_mapping + +import ( + "time" + + "github.com/APIParkLab/APIPark/stores/service" +) + +type ModelMapping struct { + UUID string `json:"uuid"` + Service string `json:"service"` + Content string `json:"content"` + CreateAt time.Time `json:"create_at"` + UpdateAt time.Time `json:"update_at"` +} + +type Create struct { + Service string `json:"service" validate:"required"` + Content string `json:"content" validate:"required"` +} + +type Edit struct { + Content *string `json:"content,omitempty"` +} + +func FromEntity(e *service.ModelMapping) *ModelMapping { + content := "" + if e.Content != "" { + content = e.Content + } + return &ModelMapping{ + UUID: e.UUID, + Service: e.Service, + Content: content, + CreateAt: e.CreateAt, + UpdateAt: e.UpdateAt, + } +} \ No newline at end of file diff --git a/service/service_model_mapping/model_test.go b/service/service_model_mapping/model_test.go new file mode 100644 index 00000000..6be588e9 --- /dev/null +++ b/service/service_model_mapping/model_test.go @@ -0,0 +1,138 @@ +package service_model_mapping + +import ( + "testing" + "time" + + "github.com/APIParkLab/APIPark/stores/service" + "github.com/stretchr/testify/assert" +) + +func TestFromEntity(t *testing.T) { + tests := []struct { + name string + entity *service.ModelMapping + expected *ModelMapping + }{ + { + name: "正常转换", + entity: &service.ModelMapping{ + UUID: "test-uuid", + Service: "test-service", + Content: "{\"key\":\"value\"}", + CreateAt: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), + UpdateAt: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), + }, + expected: &ModelMapping{ + UUID: "test-uuid", + Service: "test-service", + Content: "{\"key\":\"value\"}", + CreateAt: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), + UpdateAt: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), + }, + }, + { + name: "空内容转换", + entity: &service.ModelMapping{ + UUID: "test-uuid", + Service: "test-service", + Content: "", + CreateAt: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), + UpdateAt: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), + }, + expected: &ModelMapping{ + UUID: "test-uuid", + Service: "test-service", + Content: "", + CreateAt: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), + UpdateAt: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := FromEntity(tt.entity) + assert.Equal(t, tt.expected, result) + }) + } +} + +func TestCreate_Validation(t *testing.T) { + tests := []struct { + name string + create Create + isValid bool + }{ + { + name: "有效的创建参数", + create: Create{ + Service: "test-service", + Content: "{\"key\":\"value\"}", + }, + isValid: true, + }, + { + name: "缺少Service", + create: Create{ + Content: "{\"key\":\"value\"}", + }, + isValid: false, + }, + { + name: "缺少Content", + create: Create{ + Service: "test-service", + }, + isValid: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // 这里需要实现验证逻辑的测试 + // 由于验证逻辑可能依赖于具体的验证框架,这里只是示例 + // 实际使用时需要根据项目使用的验证框架来实现 + if tt.isValid { + assert.NotEmpty(t, tt.create.Service) + assert.NotEmpty(t, tt.create.Content) + } else { + assert.True(t, tt.create.Service == "" || tt.create.Content == "") + } + }) + } +} + +func TestEdit_Validation(t *testing.T) { + content := "new content" + tests := []struct { + name string + edit Edit + isValid bool + }{ + { + name: "有效的编辑参数", + edit: Edit{ + Content: &content, + }, + isValid: true, + }, + { + name: "空编辑参数", + edit: Edit{ + Content: nil, + }, + isValid: true, // 允许空编辑 + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if tt.isValid { + if tt.edit.Content != nil { + assert.NotEmpty(t, *tt.edit.Content) + } + } + }) + } +} diff --git a/service/service_model_mapping/service.go b/service/service_model_mapping/service.go new file mode 100644 index 00000000..e2c636db --- /dev/null +++ b/service/service_model_mapping/service.go @@ -0,0 +1,25 @@ +package service_model_mapping + +import ( + "context" + "reflect" + + "github.com/APIParkLab/APIPark/service/universally" + "github.com/eolinker/go-common/autowire" +) + +type IServiceModelMappingService interface { + universally.IServiceGet[ModelMapping] + universally.IServiceCreate[Create] + universally.IServiceEdit[Edit] + universally.IServiceDelete + + // GetByService 根据服务ID获取模型映射 + GetByService(ctx context.Context, serviceId string) (*ModelMapping, error) +} + +func init() { + autowire.Auto[IServiceModelMappingService](func() reflect.Value { + return reflect.ValueOf(new(imlServiceModelMappingService)) + }) +} diff --git a/stores/service/model_mapping.go b/stores/service/model_mapping.go new file mode 100644 index 00000000..4a3c50ab --- /dev/null +++ b/stores/service/model_mapping.go @@ -0,0 +1,42 @@ +package service + +import ( + "reflect" + "time" + + "github.com/eolinker/go-common/store" + "github.com/eolinker/go-common/autowire" +) + +type ModelMapping struct { + Id int64 `gorm:"column:id;type:BIGINT(20);AUTO_INCREMENT;NOT NULL;comment:PRIMARY ID;primary_key"` + UUID string `gorm:"type:varchar(36);not null;column:uuid;uniqueIndex:uuid;comment:uuid"` + Service string `gorm:"type:varchar(36);not null;column:service;index:service;comment:service:uuid"` + Content string `gorm:"type:text;not null;column:content;comment:mapping json"` + Creator string `gorm:"type:varchar(36);not null;column:creator;comment:creator" aovalue:"creator"` + Updater string `gorm:"type:varchar(36);not null;column:updater;comment:updater" aovalue:"updater"` + CreateAt time.Time `gorm:"type:timestamp;NOT NULL;DEFAULT:CURRENT_TIMESTAMP;column:create_at;comment:create_at"` + UpdateAt time.Time `gorm:"type:timestamp;NOT NULL;DEFAULT:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;column:update_at;comment:update_at"` +} + +func (m *ModelMapping) TableName() string { + return "service_model_mapping" +} + +func (m *ModelMapping) IdValue() int64 { + return m.Id +} + +type IModelMappingStore interface { + store.ISearchStore[ModelMapping] +} + +type imlModelMappingStore struct { + store.SearchStore[ModelMapping] +} + +func init() { + autowire.Auto[IModelMappingStore](func() reflect.Value { + return reflect.ValueOf(new(imlModelMappingStore)) + }) +} diff --git a/stores/service/store.go b/stores/service/store.go index 8c24fc15..eb6cd1b0 100644 --- a/stores/service/store.go +++ b/stores/service/store.go @@ -8,9 +8,20 @@ import "reflect" type IServiceStore interface { store.ISearchStore[Service] + GetModelMapping(service string) (*ModelMapping, error) + SaveModelMapping(mapping *ModelMapping) error } type imlServiceStore struct { store.SearchStore[Service] + mappingStore IModelMappingStore `autowired:""` +} + +func (i *imlServiceStore) GetModelMapping(service string) (*ModelMapping, error) { + return i.mappingStore.First(nil, map[string]interface{}{"service": service}) +} + +func (i *imlServiceStore) SaveModelMapping(mapping *ModelMapping) error { + return i.mappingStore.Save(nil, mapping) } type IServiceTagStore interface { From 860874ef5c3bba637d2422e54a26dba20c15b289 Mon Sep 17 00:00:00 2001 From: "2944321442@qq.com" <1320751758@qq.com> Date: Wed, 5 Mar 2025 10:15:05 +0800 Subject: [PATCH 2/5] =?UTF-8?q?feat:=E6=B7=BB=E5=8A=A0=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E6=98=A0=E5=B0=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .air.toml | 44 +++++++ go.mod | 1 - go.sum | 2 + module/service/iml.go | 42 +++--- service/service-doc/iml.go | 3 +- service/service-doc/service.go | 3 +- service/service-model-mapping/iml.go | 67 ++++++++++ service/service-model-mapping/model.go | 20 +++ service/service-model-mapping/service.go | 19 +++ service/service_model_mapping/iml.go | 88 ------------- service/service_model_mapping/model.go | 38 ------ service/service_model_mapping/model_test.go | 138 -------------------- service/service_model_mapping/service.go | 25 ---- service/universally/delete.go | 10 +- stores/service/model.go | 23 +++- stores/service/model_mapping.go | 42 ------ stores/service/store.go | 25 ++-- 17 files changed, 211 insertions(+), 379 deletions(-) create mode 100644 .air.toml create mode 100644 service/service-model-mapping/iml.go create mode 100644 service/service-model-mapping/model.go create mode 100644 service/service-model-mapping/service.go delete mode 100644 service/service_model_mapping/iml.go delete mode 100644 service/service_model_mapping/model.go delete mode 100644 service/service_model_mapping/model_test.go delete mode 100644 service/service_model_mapping/service.go delete mode 100644 stores/service/model_mapping.go diff --git a/.air.toml b/.air.toml new file mode 100644 index 00000000..ab291006 --- /dev/null +++ b/.air.toml @@ -0,0 +1,44 @@ +root = "." +testdata_dir = "testdata" +tmp_dir = "tmp" + +[build] + args_bin = [] + bin = "./tmp/main" + cmd = "go build -o ./tmp/main ." + delay = 1000 + exclude_dir = ["assets", "tmp", "vendor", "testdata", "frontend"] + exclude_file = [] + exclude_regex = ["_test.go"] + exclude_unchanged = false + follow_symlink = false + full_bin = "" + include_dir = [] + include_ext = ["go", "tpl", "tmpl", "html"] + include_file = [] + kill_delay = "0s" + log = "build-errors.log" + poll = false + poll_interval = 0 + rerun = false + rerun_delay = 500 + send_interrupt = false + stop_on_error = true + +[color] + app = "" + build = "yellow" + main = "magenta" + runner = "green" + watcher = "cyan" + +[log] + main_only = false + time = false + +[misc] + clean_on_exit = false + +[screen] + clear_on_rebuild = false + keep_scroll = true \ No newline at end of file diff --git a/go.mod b/go.mod index ff13f2bb..a347fe38 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,6 @@ require ( github.com/eolinker/ap-account v1.0.15 github.com/eolinker/eosc v0.18.3 github.com/eolinker/go-common v1.1.5 - github.com/eolinker/go-common v1.1.5 github.com/gabriel-vasile/mimetype v1.4.4 github.com/getkin/kin-openapi v0.127.0 github.com/gin-contrib/gzip v1.0.1 diff --git a/go.sum b/go.sum index 922827d0..0322d23f 100644 --- a/go.sum +++ b/go.sum @@ -40,6 +40,8 @@ github.com/getkin/kin-openapi v0.127.0 h1:Mghqi3Dhryf3F8vR370nN67pAERW+3a95vomb3 github.com/getkin/kin-openapi v0.127.0/go.mod h1:OZrfXzUfGrNbsKj+xmFBx6E5c6yH3At/tAKSc2UszXM= github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= +github.com/gin-contrib/gzip v1.0.1 h1:HQ8ENHODeLY7a4g1Au/46Z92bdGFl74OhxcZble9WJE= +github.com/gin-contrib/gzip v1.0.1/go.mod h1:njt428fdUNRvjuJf16tZMYZ2Yl+WQB53X5wmhDwXvC4= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI= github.com/gin-gonic/gin v1.10.0 h1:nTuyha1TYqgedzytsKYqna+DfLos46nTv2ygFy86HFU= diff --git a/module/service/iml.go b/module/service/iml.go index d98c322b..f081fc86 100644 --- a/module/service/iml.go +++ b/module/service/iml.go @@ -19,7 +19,7 @@ import ( "github.com/eolinker/ap-account/service/role" application_authorization "github.com/APIParkLab/APIPark/service/application-authorization" - "github.com/APIParkLab/APIPark/service/service_model_mapping" + service_model_mapping "github.com/APIParkLab/APIPark/service/service-model-mapping" api_doc "github.com/APIParkLab/APIPark/service/api-doc" @@ -244,8 +244,8 @@ func (i *imlServiceModule) Get(ctx context.Context, id string) (*service_dto.Ser } } - - serviceModelMapping, err := i.serviceModelMappingService.GetByService(ctx, id) + + serviceModelMapping, err := i.serviceModelMappingService.Get(ctx, id) if err != nil { return nil, err } @@ -359,7 +359,6 @@ func (i *imlServiceModule) Create(ctx context.Context, teamID string, input *ser return nil, fmt.Errorf("ai service: model can not be empty") } mo.AdditionalConfig["model"] = *input.Model - } if input.AsApp == nil { // 默认值为false @@ -391,14 +390,13 @@ func (i *imlServiceModule) Create(ctx context.Context, teamID string, input *ser } } } - if input.ModelMapping != "" { - err := i.serviceModelMappingService.Create(ctx, &service_model_mapping.Create{ - Service: input.Id, - Content: input.ModelMapping, - }) - if err != nil { - return err - } + + err := i.serviceModelMappingService.Save(ctx, &service_model_mapping.Save{ + Sid: input.Id, + Content: input.ModelMapping, + }) + if err != nil { + return err } return i.serviceService.Create(ctx, mo) }) @@ -468,20 +466,12 @@ func (i *imlServiceModule) Edit(ctx context.Context, id string, input *service_d } } } - if input.ModelMapping != "" { - // 先删除旧的映射 - err := i.serviceModelMappingService.Delete(ctx, id) - if err != nil { - return err - } - // 创建新的映射 - err = i.serviceModelMappingService.Create(ctx, &service_model_mapping.Create{ - Service: id, - Content: input.ModelMapping, - }) - if err != nil { - return err - } + err = i.serviceModelMappingService.Save(ctx, &service_model_mapping.Save{ + Sid: id, + Content: input.ModelMapping, + }) + if err != nil { + return err } return nil }) diff --git a/service/service-doc/iml.go b/service/service-doc/iml.go index ddfcf67a..4546af02 100644 --- a/service/service-doc/iml.go +++ b/service/service-doc/iml.go @@ -3,9 +3,10 @@ package service_doc import ( "context" "errors" - "github.com/APIParkLab/APIPark/service/universally/commit" "time" + "github.com/APIParkLab/APIPark/service/universally/commit" + "github.com/eolinker/go-common/utils" "gorm.io/gorm" diff --git a/service/service-doc/service.go b/service/service-doc/service.go index 109bae1f..71ff1cc0 100644 --- a/service/service-doc/service.go +++ b/service/service-doc/service.go @@ -2,9 +2,10 @@ package service_doc import ( "context" - "github.com/APIParkLab/APIPark/service/universally/commit" "reflect" + "github.com/APIParkLab/APIPark/service/universally/commit" + "github.com/eolinker/go-common/autowire" ) diff --git a/service/service-model-mapping/iml.go b/service/service-model-mapping/iml.go new file mode 100644 index 00000000..69b7ed5e --- /dev/null +++ b/service/service-model-mapping/iml.go @@ -0,0 +1,67 @@ +package service_model_mapping + +import ( + "context" + "errors" + "time" + + "github.com/APIParkLab/APIPark/stores/service" + "github.com/eolinker/go-common/utils" + "gorm.io/gorm" +) + +var _ IServiceModelMappingService = (*imlServiceModelMappingService)(nil) + +type imlServiceModelMappingService struct { + store service.IServiceModelMappingStore `autowired:""` +} + +func (i *imlServiceModelMappingService) Get(ctx context.Context, sid string) (*ModelMapping, error) { + entity, err := i.store.First(ctx, map[string]interface{}{"sid": sid}) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return &ModelMapping{ + Sid: sid, + Content: "", + }, nil + } + return nil, err + } + return FromEntity(entity), nil +} + +func FromEntity(e *service.ModelMapping) *ModelMapping { + content := "" + if e.Content != "" { + content = e.Content + } + return &ModelMapping{ + ID: e.Id, + Sid: e.Sid, + Content: content, + CreateAt: e.CreateAt, + UpdateAt: e.UpdateAt, + } +} + +func (i *imlServiceModelMappingService) Save(ctx context.Context, input *Save) error { + info, err := i.store.First(ctx, map[string]interface{}{"sid": input.Sid}) + if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { + return err + } + userID := utils.UserId(ctx) + if info != nil { + info.Content = input.Content + info.Updater = userID + info.UpdateAt = time.Now() + return i.store.Save(ctx, info) + } + return i.store.Insert(ctx, &service.ModelMapping{ + Sid: input.Sid, + Content: input.Content, + CreateAt: time.Now(), + UpdateAt: time.Now(), + Creator: userID, + Updater: userID, + }) +} diff --git a/service/service-model-mapping/model.go b/service/service-model-mapping/model.go new file mode 100644 index 00000000..66dec9e4 --- /dev/null +++ b/service/service-model-mapping/model.go @@ -0,0 +1,20 @@ +package service_model_mapping + +import ( + "time" +) + +type ModelMapping struct { + ID int64 `json:"id"` + Sid string `json:"sid"` + Content string `json:"content"` + Creator string `json:"creator"` + Updater string `json:"updater"` + CreateAt time.Time `json:"create_at"` + UpdateAt time.Time `json:"update_at"` +} + +type Save struct { + Sid string `json:"sid" validate:"required"` + Content string `json:"content" validate:"required"` +} diff --git a/service/service-model-mapping/service.go b/service/service-model-mapping/service.go new file mode 100644 index 00000000..533614ce --- /dev/null +++ b/service/service-model-mapping/service.go @@ -0,0 +1,19 @@ +package service_model_mapping + +import ( + "context" + "reflect" + + "github.com/eolinker/go-common/autowire" +) + +type IServiceModelMappingService interface { + Get(ctx context.Context, sid string) (*ModelMapping, error) + Save(ctx context.Context, input *Save) error +} + +func init() { + autowire.Auto[IServiceModelMappingService](func() reflect.Value { + return reflect.ValueOf(new(imlServiceModelMappingService)) + }) +} diff --git a/service/service_model_mapping/iml.go b/service/service_model_mapping/iml.go deleted file mode 100644 index e7c26793..00000000 --- a/service/service_model_mapping/iml.go +++ /dev/null @@ -1,88 +0,0 @@ -package service_model_mapping - -import ( - "context" - "errors" - "time" - - "github.com/APIParkLab/APIPark/service/universally" - "github.com/APIParkLab/APIPark/stores/service" - "github.com/eolinker/go-common/auto" - "github.com/eolinker/go-common/utils" - "github.com/google/uuid" - "gorm.io/gorm" -) - -var _ IServiceModelMappingService = (*imlServiceModelMappingService)(nil) - -type imlServiceModelMappingService struct { - store service.IModelMappingStore `autowired:""` - universally.IServiceGet[ModelMapping] - universally.IServiceDelete - universally.IServiceCreate[Create] - universally.IServiceEdit[Edit] -} - -func (i *imlServiceModelMappingService) GetByService(ctx context.Context, serviceId string) (*ModelMapping, error) { - w := map[string]interface{}{ - "service": serviceId, - } - entity, err := i.store.First(ctx, w) - if err != nil { - if errors.Is(err, gorm.ErrRecordNotFound) { - return &ModelMapping{ - Service: serviceId, - Content: "", - }, nil - } - return nil, err - } - return FromEntity(entity), nil -} - -func (i *imlServiceModelMappingService) GetLabels(ctx context.Context, ids ...string) map[string]string { - if len(ids) == 0 { - return nil - } - list, err := i.store.ListQuery(ctx, "`uuid` in (?)", []interface{}{ids}, "id") - if err != nil { - return nil - } - return utils.SliceToMapO(list, func(i *service.ModelMapping) (string, string) { - return i.UUID, i.Service - }) -} - -func (i *imlServiceModelMappingService) OnComplete() { - i.IServiceGet = universally.NewGetSoftDelete[ModelMapping, service.ModelMapping](i.store, FromEntity) - i.IServiceDelete = universally.NewSoftDelete[service.ModelMapping](i.store) - i.IServiceCreate = universally.NewCreatorSoftDelete[Create, service.ModelMapping](i.store, "service_model_mapping", createEntityHandler, uniquestHandler, labelHandler) - i.IServiceEdit = universally.NewEdit[Edit, service.ModelMapping](i.store, updateHandler, labelHandler) - auto.RegisterService("service_model_mapping", i) -} - -func labelHandler(e *service.ModelMapping) []string { - return []string{e.Service, e.UUID} -} - -func uniquestHandler(i *Create) []map[string]interface{} { - return []map[string]interface{}{{"service": i.Service}} -} - -func createEntityHandler(i *Create) *service.ModelMapping { - now := time.Now() - return &service.ModelMapping{ - UUID: uuid.New().String(), - Service: i.Service, - Content: i.Content, - CreateAt: now, - UpdateAt: now, - } -} - -func updateHandler(e *service.ModelMapping, i *Edit) { - if i.Content != nil { - e.Content = *i.Content - e.UpdateAt = time.Now() - } -} diff --git a/service/service_model_mapping/model.go b/service/service_model_mapping/model.go deleted file mode 100644 index ae504a45..00000000 --- a/service/service_model_mapping/model.go +++ /dev/null @@ -1,38 +0,0 @@ -package service_model_mapping - -import ( - "time" - - "github.com/APIParkLab/APIPark/stores/service" -) - -type ModelMapping struct { - UUID string `json:"uuid"` - Service string `json:"service"` - Content string `json:"content"` - CreateAt time.Time `json:"create_at"` - UpdateAt time.Time `json:"update_at"` -} - -type Create struct { - Service string `json:"service" validate:"required"` - Content string `json:"content" validate:"required"` -} - -type Edit struct { - Content *string `json:"content,omitempty"` -} - -func FromEntity(e *service.ModelMapping) *ModelMapping { - content := "" - if e.Content != "" { - content = e.Content - } - return &ModelMapping{ - UUID: e.UUID, - Service: e.Service, - Content: content, - CreateAt: e.CreateAt, - UpdateAt: e.UpdateAt, - } -} \ No newline at end of file diff --git a/service/service_model_mapping/model_test.go b/service/service_model_mapping/model_test.go deleted file mode 100644 index 6be588e9..00000000 --- a/service/service_model_mapping/model_test.go +++ /dev/null @@ -1,138 +0,0 @@ -package service_model_mapping - -import ( - "testing" - "time" - - "github.com/APIParkLab/APIPark/stores/service" - "github.com/stretchr/testify/assert" -) - -func TestFromEntity(t *testing.T) { - tests := []struct { - name string - entity *service.ModelMapping - expected *ModelMapping - }{ - { - name: "正常转换", - entity: &service.ModelMapping{ - UUID: "test-uuid", - Service: "test-service", - Content: "{\"key\":\"value\"}", - CreateAt: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), - UpdateAt: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), - }, - expected: &ModelMapping{ - UUID: "test-uuid", - Service: "test-service", - Content: "{\"key\":\"value\"}", - CreateAt: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), - UpdateAt: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), - }, - }, - { - name: "空内容转换", - entity: &service.ModelMapping{ - UUID: "test-uuid", - Service: "test-service", - Content: "", - CreateAt: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), - UpdateAt: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), - }, - expected: &ModelMapping{ - UUID: "test-uuid", - Service: "test-service", - Content: "", - CreateAt: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), - UpdateAt: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), - }, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - result := FromEntity(tt.entity) - assert.Equal(t, tt.expected, result) - }) - } -} - -func TestCreate_Validation(t *testing.T) { - tests := []struct { - name string - create Create - isValid bool - }{ - { - name: "有效的创建参数", - create: Create{ - Service: "test-service", - Content: "{\"key\":\"value\"}", - }, - isValid: true, - }, - { - name: "缺少Service", - create: Create{ - Content: "{\"key\":\"value\"}", - }, - isValid: false, - }, - { - name: "缺少Content", - create: Create{ - Service: "test-service", - }, - isValid: false, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - // 这里需要实现验证逻辑的测试 - // 由于验证逻辑可能依赖于具体的验证框架,这里只是示例 - // 实际使用时需要根据项目使用的验证框架来实现 - if tt.isValid { - assert.NotEmpty(t, tt.create.Service) - assert.NotEmpty(t, tt.create.Content) - } else { - assert.True(t, tt.create.Service == "" || tt.create.Content == "") - } - }) - } -} - -func TestEdit_Validation(t *testing.T) { - content := "new content" - tests := []struct { - name string - edit Edit - isValid bool - }{ - { - name: "有效的编辑参数", - edit: Edit{ - Content: &content, - }, - isValid: true, - }, - { - name: "空编辑参数", - edit: Edit{ - Content: nil, - }, - isValid: true, // 允许空编辑 - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - if tt.isValid { - if tt.edit.Content != nil { - assert.NotEmpty(t, *tt.edit.Content) - } - } - }) - } -} diff --git a/service/service_model_mapping/service.go b/service/service_model_mapping/service.go deleted file mode 100644 index e2c636db..00000000 --- a/service/service_model_mapping/service.go +++ /dev/null @@ -1,25 +0,0 @@ -package service_model_mapping - -import ( - "context" - "reflect" - - "github.com/APIParkLab/APIPark/service/universally" - "github.com/eolinker/go-common/autowire" -) - -type IServiceModelMappingService interface { - universally.IServiceGet[ModelMapping] - universally.IServiceCreate[Create] - universally.IServiceEdit[Edit] - universally.IServiceDelete - - // GetByService 根据服务ID获取模型映射 - GetByService(ctx context.Context, serviceId string) (*ModelMapping, error) -} - -func init() { - autowire.Auto[IServiceModelMappingService](func() reflect.Value { - return reflect.ValueOf(new(imlServiceModelMappingService)) - }) -} diff --git a/service/universally/delete.go b/service/universally/delete.go index 565237a9..2677278e 100644 --- a/service/universally/delete.go +++ b/service/universally/delete.go @@ -4,15 +4,14 @@ import ( "context" "errors" "fmt" + "github.com/eolinker/go-common/auto" "github.com/eolinker/go-common/store" "github.com/eolinker/go-common/utils" "gorm.io/gorm" ) -var ( - _ IServiceDelete = (*imlServiceDelete[any])(nil) -) +var _ IServiceDelete = (*imlServiceDelete[any])(nil) type IServiceDelete interface { Delete(ctx context.Context, uuid string) error @@ -26,10 +25,12 @@ func NewDelete[E any](store store.ISearchStore[E]) IServiceDelete { assert(new(E)) return &imlServiceDelete[E]{store: store} } + func NewSoftDelete[E any](s store.ISearchStore[E]) IServiceDelete { assert(new(E)) return &imlServiceSoftDelete[E]{store: s} } + func (p *imlServiceDelete[E]) Delete(ctx context.Context, uuid string) error { return p.store.Transaction(ctx, func(ctx context.Context) error { o, err := p.store.First(ctx, map[string]interface{}{"uuid": uuid}) @@ -46,7 +47,6 @@ func (p *imlServiceDelete[E]) Delete(ctx context.Context, uuid string) error { } return p.store.SetLabels(ctx, idValue(o)) }) - } type imlServiceSoftDelete[E any] struct { @@ -66,7 +66,5 @@ func (p *imlServiceSoftDelete[E]) Delete(ctx context.Context, uuid string) error auto.Auto("operator", operator, o) return p.store.SoftDelete(ctx, map[string]interface{}{"uuid": uuid}) - }) - } diff --git a/stores/service/model.go b/stores/service/model.go index d0d42d30..3e205ace 100644 --- a/stores/service/model.go +++ b/stores/service/model.go @@ -1,6 +1,8 @@ package service -import "time" +import ( + "time" +) type Service struct { Id int64 `gorm:"column:id;type:BIGINT(20);AUTO_INCREMENT;NOT NULL;comment:id;primary_key;comment:主键ID;"` @@ -27,6 +29,7 @@ type Service struct { func (p *Service) IdValue() int64 { return p.Id } + func (p *Service) TableName() string { return "service" } @@ -87,3 +90,21 @@ func (d *Doc) IdValue() int64 { func (d *Doc) TableName() string { return "server_doc" } + +type ModelMapping struct { + Id int64 `gorm:"column:id;type:BIGINT(20);AUTO_INCREMENT;NOT NULL;comment:PRIMARY ID;primary_key"` + Sid string `gorm:"size:36;not null;column:sid;comment:service uuid;uniqueIndex:unique_sid;"` + Content string `gorm:"type:text;not null;column:content;comment:mapping json"` + Creator string `gorm:"type:varchar(36);not null;column:creator;comment:creator" aovalue:"creator"` + Updater string `gorm:"type:varchar(36);not null;column:updater;comment:updater" aovalue:"updater"` + CreateAt time.Time `gorm:"type:timestamp;NOT NULL;DEFAULT:CURRENT_TIMESTAMP;column:create_at;comment:create_at"` + UpdateAt time.Time `gorm:"type:timestamp;NOT NULL;DEFAULT:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;column:update_at;comment:update_at"` +} + +func (m *ModelMapping) TableName() string { + return "service_model_mapping" +} + +func (m *ModelMapping) IdValue() int64 { + return m.Id +} diff --git a/stores/service/model_mapping.go b/stores/service/model_mapping.go deleted file mode 100644 index 4a3c50ab..00000000 --- a/stores/service/model_mapping.go +++ /dev/null @@ -1,42 +0,0 @@ -package service - -import ( - "reflect" - "time" - - "github.com/eolinker/go-common/store" - "github.com/eolinker/go-common/autowire" -) - -type ModelMapping struct { - Id int64 `gorm:"column:id;type:BIGINT(20);AUTO_INCREMENT;NOT NULL;comment:PRIMARY ID;primary_key"` - UUID string `gorm:"type:varchar(36);not null;column:uuid;uniqueIndex:uuid;comment:uuid"` - Service string `gorm:"type:varchar(36);not null;column:service;index:service;comment:service:uuid"` - Content string `gorm:"type:text;not null;column:content;comment:mapping json"` - Creator string `gorm:"type:varchar(36);not null;column:creator;comment:creator" aovalue:"creator"` - Updater string `gorm:"type:varchar(36);not null;column:updater;comment:updater" aovalue:"updater"` - CreateAt time.Time `gorm:"type:timestamp;NOT NULL;DEFAULT:CURRENT_TIMESTAMP;column:create_at;comment:create_at"` - UpdateAt time.Time `gorm:"type:timestamp;NOT NULL;DEFAULT:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;column:update_at;comment:update_at"` -} - -func (m *ModelMapping) TableName() string { - return "service_model_mapping" -} - -func (m *ModelMapping) IdValue() int64 { - return m.Id -} - -type IModelMappingStore interface { - store.ISearchStore[ModelMapping] -} - -type imlModelMappingStore struct { - store.SearchStore[ModelMapping] -} - -func init() { - autowire.Auto[IModelMappingStore](func() reflect.Value { - return reflect.ValueOf(new(imlModelMappingStore)) - }) -} diff --git a/stores/service/store.go b/stores/service/store.go index eb6cd1b0..702a358c 100644 --- a/stores/service/store.go +++ b/stores/service/store.go @@ -1,27 +1,17 @@ package service import ( + "reflect" + "github.com/eolinker/go-common/autowire" "github.com/eolinker/go-common/store" ) -import "reflect" type IServiceStore interface { store.ISearchStore[Service] - GetModelMapping(service string) (*ModelMapping, error) - SaveModelMapping(mapping *ModelMapping) error } type imlServiceStore struct { store.SearchStore[Service] - mappingStore IModelMappingStore `autowired:""` -} - -func (i *imlServiceStore) GetModelMapping(service string) (*ModelMapping, error) { - return i.mappingStore.First(nil, map[string]interface{}{"service": service}) -} - -func (i *imlServiceStore) SaveModelMapping(mapping *ModelMapping) error { - return i.mappingStore.Save(nil, mapping) } type IServiceTagStore interface { @@ -48,6 +38,14 @@ type imlAuthorizationStore struct { store.SearchStore[Authorization] } +type IServiceModelMappingStore interface { + store.ISearchStore[ModelMapping] +} + +type imlServiceModelMappingStore struct { + store.SearchStore[ModelMapping] +} + func init() { autowire.Auto[IServiceStore](func() reflect.Value { return reflect.ValueOf(new(imlServiceStore)) @@ -63,4 +61,7 @@ func init() { return reflect.ValueOf(new(imlServiceDocStore)) }) + autowire.Auto[IServiceModelMappingStore](func() reflect.Value { + return reflect.ValueOf(new(imlServiceModelMappingStore)) + }) } From 8e854adaa3b8695ae1a0827ad2216e1acc1422ee Mon Sep 17 00:00:00 2001 From: "2944321442@qq.com" <1320751758@qq.com> Date: Wed, 5 Mar 2025 10:43:20 +0800 Subject: [PATCH 3/5] feat:update .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index cb2bf660..4b6d8fc4 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ /apipark .gitlab-ci.yml /.vscode/ +.vscode/ \ No newline at end of file From afb9cc2e445dfd668107d2aaf9e40294aa9d4329 Mon Sep 17 00:00:00 2001 From: "2944321442@qq.com" <1320751758@qq.com> Date: Wed, 5 Mar 2025 10:44:19 +0800 Subject: [PATCH 4/5] feat:update .gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 4b6d8fc4..6ab23ba1 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ /apipark .gitlab-ci.yml /.vscode/ -.vscode/ \ No newline at end of file +.vscode/ +.air.toml \ No newline at end of file From 8c40e1cf054bb1422cd83247c90218c96dca6a43 Mon Sep 17 00:00:00 2001 From: "2944321442@qq.com" <1320751758@qq.com> Date: Wed, 5 Mar 2025 10:49:22 +0800 Subject: [PATCH 5/5] .gitignore --- .air.toml | 44 -------------------------------------------- .vscode/launch.json | 16 ---------------- 2 files changed, 60 deletions(-) delete mode 100644 .air.toml delete mode 100644 .vscode/launch.json diff --git a/.air.toml b/.air.toml deleted file mode 100644 index ab291006..00000000 --- a/.air.toml +++ /dev/null @@ -1,44 +0,0 @@ -root = "." -testdata_dir = "testdata" -tmp_dir = "tmp" - -[build] - args_bin = [] - bin = "./tmp/main" - cmd = "go build -o ./tmp/main ." - delay = 1000 - exclude_dir = ["assets", "tmp", "vendor", "testdata", "frontend"] - exclude_file = [] - exclude_regex = ["_test.go"] - exclude_unchanged = false - follow_symlink = false - full_bin = "" - include_dir = [] - include_ext = ["go", "tpl", "tmpl", "html"] - include_file = [] - kill_delay = "0s" - log = "build-errors.log" - poll = false - poll_interval = 0 - rerun = false - rerun_delay = 500 - send_interrupt = false - stop_on_error = true - -[color] - app = "" - build = "yellow" - main = "magenta" - runner = "green" - watcher = "cyan" - -[log] - main_only = false - time = false - -[misc] - clean_on_exit = false - -[screen] - clear_on_rebuild = false - keep_scroll = true \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 6de470d3..00000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - // 使用 IntelliSense 了解相关属性。 - // 悬停以查看现有属性的描述。 - // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "Launch Package", - "type": "go", - "request": "launch", - "mode": "auto", - "program": "${workspaceFolder}", - "args": ["-c", "${workspaceFolder}/config.yml"] - } - ] -}