yjp 1 år sedan
förälder
incheckning
a02507ac47

+ 9 - 2
binding/bind_item.go

@@ -41,10 +41,17 @@ func (item *BindItem[O]) bind(binder *Binder, middlewares ...api.Handler) {
 	// 给单个路由增加中间件
 	handlers := []api.Handler{
 		func(c *api.Context) {
-			params := item.RequestParams
+			var params request.Params
 
 			// 有请求数据
-			if params != nil {
+			if item.RequestParams != nil {
+				requestParamsType := reflect.TypeOf(item.RequestParams)
+				if requestParamsType.Kind() == reflect.Pointer {
+					params = reflect.New(requestParamsType.Elem()).Interface().(request.Params)
+				} else {
+					params = reflect.New(requestParamsType).Interface().(request.Params)
+				}
+
 				// 将请求数据解析到请求参数中
 				if item.RequestParamsBindFunc != nil {
 					err := item.RequestParamsBindFunc(c, params)

+ 2 - 0
binding/request/common.go

@@ -1,6 +1,7 @@
 package request
 
 type WithID interface {
+	Params
 	GetID() string
 }
 
@@ -29,6 +30,7 @@ func (id *IDQuery) GetID() string {
 }
 
 type TenantID interface {
+	Params
 	GetTenantID() string
 }
 

+ 24 - 0
convenient/data_containers/configuration.yaml

@@ -0,0 +1,24 @@
+kind: DataContainer
+spec:
+  namespace: # 替换
+  data_source: # 替换
+  name: # 替换.configurations
+  spec:
+    table_name: # 替换.configurations
+    columns:
+      - name: group
+        type: varchar(256)
+        comment: 范围
+        not_null: true
+        primary_key: true
+      - name: group
+        type: varchar(256)
+        comment: 组
+        not_null: true
+        primary_key: true
+      - name: value
+        type: varchar(256)
+        comment: 值
+        not_null: true
+        index: true
+

+ 2 - 1
convenient/domain/configuration/api.go

@@ -23,7 +23,7 @@ func BindConfiguration(binder *binding.Binder, opts ...Option) {
 
 	configurationTableName := tableName
 	if strutils.IsStringNotEmpty(options.schema) {
-		configurationTableName = tableName + "." + options.schema
+		configurationTableName = options.schema + "." + tableName
 	}
 
 	value_object.BindSimple(binder, &value_object.Simple[any]{
@@ -53,6 +53,7 @@ func BindConfiguration(binder *binding.Binder, opts ...Option) {
 				TableName:     configurationTableName,
 				SelectColumns: []string{ColumnValue},
 				Conditions: sql.NewConditions().
+					Equal(ColumnScope, e.Scope).
 					Equal(ColumnGroup, e.Group),
 			})
 			if err != nil {

+ 33 - 3
convenient/domain/configuration/entity.go

@@ -6,6 +6,7 @@ import (
 )
 
 const (
+	ColumnScope = "scope"
 	ColumnGroup = "group"
 	ColumnValue = "value"
 )
@@ -19,11 +20,13 @@ const (
 )
 
 const (
+	fieldScopeMaxLen = 256
 	fieldGroupMaxLen = 256
 	fieldValueMaxLen = 256
 )
 
 type Entity struct {
+	Scope string `sqlmapping:"column:scope;key;notUpdate;" sqlresult:"column:group;"`
 	Group string `sqlmapping:"column:group;key;notUpdate;" sqlresult:"column:group;"`
 	Value string `sqlmapping:"column:value;notUpdate;" sqlresult:"column:value;"`
 }
@@ -33,11 +36,26 @@ func (e *Entity) DomainCNName() string {
 }
 
 func (e *Entity) CheckKeyFields() error {
-	return e.checkFieldGroup()
+	err := e.checkFieldScope()
+	if err != nil {
+		return err
+	}
+
+	err = e.checkFieldGroup()
+	if err != nil {
+		return err
+	}
+
+	return nil
 }
 
 func (e *Entity) ForCreate() error {
-	err := e.checkFieldGroup()
+	err := e.checkFieldScope()
+	if err != nil {
+		return err
+	}
+
+	err = e.checkFieldGroup()
 	if err != nil {
 		return err
 	}
@@ -50,6 +68,18 @@ func (e *Entity) ForCreate() error {
 	return nil
 }
 
+func (e *Entity) checkFieldScope() error {
+	if strutils.IsStringEmpty(e.Scope) {
+		return fserr.New(domainCNName + "范围为空")
+	}
+
+	if len(e.Scope) > fieldScopeMaxLen {
+		return fserr.New(domainCNName + "范围超出限定长度")
+	}
+
+	return nil
+}
+
 func (e *Entity) checkFieldGroup() error {
 	if strutils.IsStringEmpty(e.Group) {
 		return fserr.New(domainCNName + "组为空")
@@ -67,7 +97,7 @@ func (e *Entity) checkFieldValue() error {
 		return fserr.New(domainCNName + "值为空")
 	}
 
-	if len([]byte(e.Value)) > fieldValueMaxLen {
+	if len(e.Value) > fieldValueMaxLen {
 		return fserr.New(domainCNName + "值超出限定长度")
 	}
 

+ 4 - 1
convenient/domain/configuration/request_params.go

@@ -2,15 +2,18 @@ package configuration
 
 type (
 	AddConfigurationJsonBody struct {
+		Scope string `json:"scope" binding:"required" assign:"toField:Scope"`
 		Group string `json:"group" binding:"required" assign:"toField:Group"`
 		Value string `json:"value" binding:"required" assign:"toField:Value"`
 	}
 
 	RemoveConfigurationJsonBody struct {
+		Scope string `json:"scope" binding:"required" assign:"toField:Scope"`
 		Group string `json:"group" binding:"required" assign:"toField:Group"`
 	}
 
 	GetConfigurationValuesQueryParams struct {
-		Group string `form:"group" binding:"required" assign:"toField:Group"`
+		Scope string `form:"scope" assign:"toField:Scope"`
+		Group string `form:"group" assign:"toField:Group"`
 	}
 )

+ 2 - 2
convenient/value_object/simple.go

@@ -57,7 +57,7 @@ func (simple *Simple[I]) bind(binder *binding.Binder) {
 		}
 	}
 
-	// 删除班级
+	// 删除
 	if !options.disableDelete {
 		if !options.deleteNeedTx {
 			binding.PostBind(binder, &binding.SimpleBindItem[any]{
@@ -80,7 +80,7 @@ func (simple *Simple[I]) bind(binder *binding.Binder) {
 		}
 	}
 
-	// 查询班级
+	// 查询
 	if !options.disableQuery {
 		binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[I]]{
 			Path:          simple.DomainPath + "/query",

+ 33 - 3
examples/example_domain/configuration/entity.go

@@ -10,6 +10,7 @@ const (
 )
 
 const (
+	ColumnScope = "scope"
 	ColumnGroup = "group"
 	ColumnValue = "value"
 )
@@ -19,11 +20,13 @@ const (
 )
 
 const (
+	fieldScopeMaxLen = 256
 	fieldGroupMaxLen = 256
 	fieldValueMaxLen = 256
 )
 
 type Entity struct {
+	Scope string `sqlmapping:"column:scope;key;notUpdate;" sqlresult:"column:group;"`
 	Group string `sqlmapping:"column:group;key;notUpdate;" sqlresult:"column:group;"`
 	Value string `sqlmapping:"column:value;notUpdate;" sqlresult:"column:value;"`
 }
@@ -33,11 +36,26 @@ func (e *Entity) DomainCNName() string {
 }
 
 func (e *Entity) CheckKeyFields() error {
-	return e.checkFieldGroup()
+	err := e.checkFieldScope()
+	if err != nil {
+		return err
+	}
+
+	err = e.checkFieldGroup()
+	if err != nil {
+		return err
+	}
+
+	return nil
 }
 
 func (e *Entity) ForCreate() error {
-	err := e.checkFieldGroup()
+	err := e.checkFieldScope()
+	if err != nil {
+		return err
+	}
+
+	err = e.checkFieldGroup()
 	if err != nil {
 		return err
 	}
@@ -50,6 +68,18 @@ func (e *Entity) ForCreate() error {
 	return nil
 }
 
+func (e *Entity) checkFieldScope() error {
+	if strutils.IsStringEmpty(e.Scope) {
+		return fserr.New(domainCNName + "范围为空")
+	}
+
+	if len(e.Scope) > fieldScopeMaxLen {
+		return fserr.New(domainCNName + "范围超出限定长度")
+	}
+
+	return nil
+}
+
 func (e *Entity) checkFieldGroup() error {
 	if strutils.IsStringEmpty(e.Group) {
 		return fserr.New(domainCNName + "组为空")
@@ -67,7 +97,7 @@ func (e *Entity) checkFieldValue() error {
 		return fserr.New(domainCNName + "值为空")
 	}
 
-	if len([]byte(e.Value)) > fieldValueMaxLen {
+	if len(e.Value) > fieldValueMaxLen {
 		return fserr.New(domainCNName + "值超出限定长度")
 	}
 

+ 4 - 1
examples/example_domain/configuration/request_params.go

@@ -4,16 +4,19 @@ import "git.sxidc.com/go-framework/baize/binding/request"
 
 type (
 	CreateConfigurationJsonBody struct {
+		Scope string `json:"scope" binding:"required" assign:"toField:Scope"`
 		Group string `json:"group" binding:"required" assign:"toField:Group"`
 		Value string `json:"value" binding:"required" assign:"toField:Value"`
 	}
 
 	DeleteConfigurationJsonBody struct {
+		Scope string `json:"scope" binding:"required" assign:"toField:Scope"`
 		Group string `json:"group" binding:"required" assign:"toField:Group"`
 	}
 
 	GetConfigurationValuesQueryParams struct {
-		Group string `form:"group" binding:"required" assign:"toField:Group"`
+		Scope string `form:"scope" assign:"toField:Scope"`
+		Group string `form:"group" assign:"toField:Group"`
 		request.BaseQuery
 	}
 )

+ 64 - 0
examples/examples/configuration/main.go

@@ -0,0 +1,64 @@
+package main
+
+import (
+	"git.sxidc.com/go-framework/baize"
+	"git.sxidc.com/go-framework/baize/application"
+	"git.sxidc.com/go-framework/baize/convenient/domain/configuration"
+	"git.sxidc.com/go-framework/baize/infrastructure"
+	"git.sxidc.com/go-framework/baize/infrastructure/database/operations"
+	DEATH "github.com/vrecan/death"
+	"syscall"
+)
+
+// curl -X POST -H "Content-Type: application/json" -d '{"scope": "global", "group":"test", "value":"test-value"}' "http://localhost:10100/test/v1/configuration/create"
+// curl -X GET "http://localhost:10100/test/v1/configuration/values?group=test&pageNo=1&pageSize=1"
+// curl -X POST -H "Content-Type: application/json" -d '{"scope": "global", "group":"test"}' "http://localhost:10100/test/v1/configuration/delete"
+
+func main() {
+	app := baize.NewApplication(application.Config{
+		ApiConfig: application.ApiConfig{
+			UrlPrefix: "test",
+			Port:      "10100",
+		},
+		InfrastructureConfig: application.InfrastructureConfig{
+			Database: infrastructure.DatabaseConfig{
+				Operations: &operations.Config{
+					UserName:           "test",
+					Password:           "123456",
+					Address:            "localhost",
+					Port:               "30432",
+					Database:           "test",
+					MaxConnections:     40,
+					MaxIdleConnections: 10,
+				},
+			},
+		},
+	})
+
+	defer func() {
+		baize.DestroyApplication(app)
+	}()
+
+	app.Api().
+		PrefixRouter().
+		RegisterVersionedRouter("v1")
+
+	configuration.BindConfiguration(app.Binder("v1"), configuration.WithSchema("test"))
+
+	go func() {
+		err := app.Start()
+		if err != nil {
+			panic(err)
+		}
+	}()
+
+	defer func() {
+		err := app.Finish()
+		if err != nil {
+			panic(err)
+		}
+	}()
+
+	death := DEATH.NewDeath(syscall.SIGINT, syscall.SIGTERM)
+	_ = death.WaitForDeath()
+}

+ 3 - 3
examples/examples/value_object/main.go

@@ -11,9 +11,9 @@ import (
 	"syscall"
 )
 
-// curl -X POST -H "Content-Type: application/json" -d '{"group":"test", "value":"test-value"}' "http://localhost:10100/test/v1/configuration/create"
-// curl -X GET "http://localhost:10100/test/v1/configuration/query?group=test&pageNo=1&pageSize=1"
-// curl -X POST -H "Content-Type: application/json" -d '{"group":"test"}' "http://localhost:10100/test/v1/configuration/delete"
+// curl -X POST -H "Content-Type: application/json" -d '{"scope": "global", "group":"test", "value":"test-value"}' "http://localhost:10100/test/v1/configuration/create"
+// curl -X GET "http://localhost:10100/test/v1/configuration/query?scope=global&pageNo=1&pageSize=1"
+// curl -X POST -H "Content-Type: application/json" -d '{"scope": "global", "group":"test"}' "http://localhost:10100/test/v1/configuration/delete"
 
 func main() {
 	app := baize.NewApplication(application.Config{

+ 6 - 1
examples/resources.yaml

@@ -62,11 +62,16 @@ spec:
   spec:
     table_name: test.configurations
     columns:
+      - name: scope
+        type: varchar(256)
+        comment: 范围
+        not_null: true
+        primary_key: true
       - name: group
         type: varchar(256)
         comment: 组
         not_null: true
-        index: true
+        primary_key: true
       - name: value
         type: varchar(256)
         comment: 值