瀏覽代碼

修改bug

yjp 1 年之前
父節點
當前提交
3714abb886

+ 1 - 8
convenient/binding/bind_item.go

@@ -42,12 +42,12 @@ func (item *BindItem[O]) bind(binder *Binder, middlewares ...api.Handler) {
 	handlers := []api.Handler{
 		func(c *api.Context) {
 			var params request.Params
+			outputZero := reflectutils.Zero[O]()
 
 			// 有请求数据
 			if item.RequestParams != nil {
 				requestParamsType := reflect.TypeOf(item.RequestParams)
 				if !reflectutils.IsTypeStructOrStructPointer(requestParamsType) {
-					var outputZero O
 					item.ResponseFunc(c, http.StatusOK, outputZero, fserr.New("请求参数不是结构或结构指针"))
 					return
 				}
@@ -62,7 +62,6 @@ func (item *BindItem[O]) bind(binder *Binder, middlewares ...api.Handler) {
 				if item.RequestParamsBindFunc != nil {
 					err := item.RequestParamsBindFunc(c, params)
 					if err != nil {
-						var outputZero O
 						item.ResponseFunc(c, http.StatusBadRequest, outputZero, err)
 						return
 					}
@@ -73,21 +72,18 @@ func (item *BindItem[O]) bind(binder *Binder, middlewares ...api.Handler) {
 					case http.MethodPut:
 						err := request.JsonBody(c, params)
 						if err != nil {
-							var outputZero O
 							item.ResponseFunc(c, http.StatusBadRequest, outputZero, err)
 							return
 						}
 					case http.MethodGet:
 						err := request.QueryParams(c, params)
 						if err != nil {
-							var outputZero O
 							item.ResponseFunc(c, http.StatusBadRequest, outputZero, err)
 							return
 						}
 					case http.MethodDelete:
 						err := request.PathParams(c, params)
 						if err != nil {
-							var outputZero O
 							item.ResponseFunc(c, http.StatusBadRequest, outputZero, err)
 							return
 						}
@@ -100,7 +96,6 @@ func (item *BindItem[O]) bind(binder *Binder, middlewares ...api.Handler) {
 			if item.FormDomainObjectsFunc != nil {
 				innerDomainObjects, err := item.FormDomainObjectsFunc(c, params)
 				if err != nil {
-					var outputZero O
 					item.ResponseFunc(c, http.StatusOK, outputZero, err)
 					return
 				}
@@ -115,7 +110,6 @@ func (item *BindItem[O]) bind(binder *Binder, middlewares ...api.Handler) {
 
 						objectType := reflect.TypeOf(object)
 						if !reflectutils.IsTypeStructOrStructPointer(objectType) {
-							var outputZero O
 							item.ResponseFunc(c, http.StatusOK, outputZero, fserr.New("领域对象不是结构或结构指针"))
 							return
 						}
@@ -125,7 +119,6 @@ func (item *BindItem[O]) bind(binder *Binder, middlewares ...api.Handler) {
 						if params != nil {
 							err := request.AssignRequestParamsToDomainObject(params, obj)
 							if err != nil {
-								var outputZero O
 								item.ResponseFunc(c, http.StatusOK, outputZero, err)
 								return
 							}

+ 2 - 2
convenient/binding/request/dto.go

@@ -15,7 +15,7 @@ func AssignRequestParamsToDomainObject(params Params, domainObject domain.Object
 }
 
 func Field[T any](params Params, fieldName string) (T, error) {
-	var zero T
+	zero := reflectutils.Zero[T]()
 
 	if params == nil {
 		return zero, fserr.New("请求参数为nil")
@@ -39,7 +39,7 @@ func Field[T any](params Params, fieldName string) (T, error) {
 }
 
 func ToConcrete[T Params](params Params) (T, error) {
-	var zero T
+	zero := reflectutils.Zero[T]()
 
 	if params == nil {
 		return zero, fserr.New("请求参数为nil")

+ 4 - 10
convenient/entity/service.go

@@ -10,6 +10,7 @@ import (
 	"git.sxidc.com/go-framework/baize/framwork/infrastructure/database"
 	"git.sxidc.com/go-framework/baize/framwork/infrastructure/database/sql"
 	"git.sxidc.com/go-framework/baize/framwork/tag/sql/sql_mapping"
+	"git.sxidc.com/go-tools/utils/reflectutils"
 	"git.sxidc.com/service-supports/fserr"
 	"reflect"
 )
@@ -238,14 +239,7 @@ func Query[O any](tableName string, callbacks *Callbacks[response.InfosData[O]],
 
 func GetByID[O any](tableName string, callbacks *Callbacks[O]) binding.ServiceFunc[O] {
 	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (O, error) {
-		var outputZero O
-
-		outputZeroValue := reflect.New(reflect.TypeOf(outputZero)).Elem()
-		if outputZeroValue.Kind() == reflect.Pointer {
-			outputZeroValue.Set(reflect.New(outputZeroValue.Type().Elem()))
-		}
-
-		outputZero = outputZeroValue.Interface().(O)
+		outputZero := reflectutils.Zero[O]()
 
 		object := objects[0]
 		if object == nil {
@@ -282,11 +276,11 @@ func GetByID[O any](tableName string, callbacks *Callbacks[O]) binding.ServiceFu
 			return callbackOnErrorReturn(callbacks, e, err, i, outputZero)
 		}
 
-		var info O
+		info := reflectutils.Zero[O]()
 		var infoPointer any
 
 		infoPointer = &info
-		if outputZeroValue.Kind() == reflect.Pointer {
+		if reflect.TypeOf(info).Kind() == reflect.Pointer {
 			infoPointer = info
 		}
 

+ 10 - 30
convenient/relation/one2one/service.go

@@ -8,6 +8,7 @@ import (
 	"git.sxidc.com/go-framework/baize/framwork/infrastructure"
 	"git.sxidc.com/go-framework/baize/framwork/infrastructure/database"
 	"git.sxidc.com/go-framework/baize/framwork/infrastructure/database/sql"
+	"git.sxidc.com/go-tools/utils/reflectutils"
 	"git.sxidc.com/go-tools/utils/strutils"
 	"git.sxidc.com/service-supports/fserr"
 	"reflect"
@@ -127,14 +128,7 @@ func Update(fromTableName string, fromRelationFieldName string, fromRelationColu
 
 func Query[TI any](fromTableName string, toTableName string, toRelationColumnName string) binding.ServiceFunc[TI] {
 	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (TI, error) {
-		var outputZero TI
-
-		outputZeroValue := reflect.New(reflect.TypeOf(outputZero)).Elem()
-		if outputZeroValue.Kind() == reflect.Pointer {
-			outputZeroValue.Set(reflect.New(outputZeroValue.Type().Elem()))
-		}
-
-		outputZero = outputZeroValue.Interface().(TI)
+		outputZero := reflectutils.Zero[TI]()
 
 		dbExecutor := i.DBExecutor()
 
@@ -177,11 +171,11 @@ func Query[TI any](fromTableName string, toTableName string, toRelationColumnNam
 			return outputZero, err
 		}
 
-		var info TI
+		info := reflectutils.Zero[TI]()
 		var infoPointer any
 
 		infoPointer = &info
-		if outputZeroValue.Kind() == reflect.Pointer {
+		if reflect.TypeOf(info).Kind() == reflect.Pointer {
 			infoPointer = info
 		}
 
@@ -196,22 +190,8 @@ func Query[TI any](fromTableName string, toTableName string, toRelationColumnNam
 
 func QueryWithOtherInfo[FI any, TI any](fromTableName string, toTableName string, toRelationColumnName string) binding.ServiceFunc[map[string]any] {
 	return func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
-		var outputFromZero FI
-		var outputToZero TI
-
-		outputFromZeroValue := reflect.New(reflect.TypeOf(outputFromZero)).Elem()
-		if outputFromZeroValue.Kind() == reflect.Pointer {
-			outputFromZeroValue.Set(reflect.New(outputFromZeroValue.Type().Elem()))
-		}
-
-		outputFromZero = outputFromZeroValue.Interface().(FI)
-
-		outputToZeroValue := reflect.New(reflect.TypeOf(outputToZero)).Elem()
-		if outputToZeroValue.Kind() == reflect.Pointer {
-			outputToZeroValue.Set(reflect.New(outputToZeroValue.Type().Elem()))
-		}
-
-		outputToZero = outputToZeroValue.Interface().(TI)
+		outputFromZero := reflectutils.Zero[FI]()
+		outputToZero := reflectutils.Zero[TI]()
 
 		zeroRetMap := map[string]any{
 			"self": outputFromZero,
@@ -259,11 +239,11 @@ func QueryWithOtherInfo[FI any, TI any](fromTableName string, toTableName string
 			return zeroRetMap, err
 		}
 
-		var fromInfo FI
+		fromInfo := reflectutils.Zero[FI]()
 		var fromInfoPointer any
 
 		fromInfoPointer = &fromInfo
-		if outputFromZeroValue.Kind() == reflect.Pointer {
+		if reflect.TypeOf(fromInfo).Kind() == reflect.Pointer {
 			fromInfoPointer = fromInfo
 		}
 
@@ -272,11 +252,11 @@ func QueryWithOtherInfo[FI any, TI any](fromTableName string, toTableName string
 			return zeroRetMap, err
 		}
 
-		var toInfo TI
+		toInfo := reflectutils.Zero[FI]()
 		var toInfoPointer any
 
 		toInfoPointer = &toInfo
-		if outputToZeroValue.Kind() == reflect.Pointer {
+		if reflect.TypeOf(toInfo).Kind() == reflect.Pointer {
 			toInfoPointer = toInfo
 		}
 

+ 1 - 1
examples/examples/project/application/service/student_and_family.go

@@ -21,7 +21,7 @@ func (app *StudentAndFamily) Destroy() error {
 func (app *StudentAndFamily) v1(appInstance *application.App) {
 	v1Binder := appInstance.Binder(application.RouterPrefix, "v1")
 
-	one2one.BindSimple(v1Binder, &one2one.Simple[student.Info, family.Info]{
+	one2one.BindSimple(v1Binder, &one2one.Simple[*student.Info, *family.Info]{
 		Left:                          &student.Entity{},
 		Right:                         &family.Entity{},
 		LeftTableName:                 student.TableName,

+ 2 - 2
framwork/domain/object.go

@@ -32,7 +32,7 @@ func SetField[T any](object Object, fieldName string, value T) error {
 }
 
 func Field[T any](object Object, fieldName string) (T, error) {
-	var zero T
+	zero := reflectutils.Zero[T]()
 
 	if object == nil {
 		return zero, fserr.New("领域对象为nil")
@@ -56,7 +56,7 @@ func Field[T any](object Object, fieldName string) (T, error) {
 }
 
 func ToConcrete[T Object](object Object) (T, error) {
-	var zero T
+	zero := reflectutils.Zero[T]()
 
 	if object == nil {
 		return zero, fserr.New("领域对象为nil")

+ 1 - 2
framwork/tag/sql/sql_mapping/usage.go

@@ -40,8 +40,7 @@ func defaultCallback(fields *[]Field) OnParsedFieldTagFunc {
 		}
 
 		if entityFieldElemValue.IsZero() && !reflectutils.IsValueTime(entityFieldElemValue) {
-			reflectutils.Zero(&entityFieldElemValue)
-			field.Value = entityFieldElemValue.Interface()
+			field.Value = reflectutils.ZeroValueToAny(entityFieldElemValue)
 			*fields = append(*fields, field)
 			return nil
 		}

+ 1 - 2
go.mod

@@ -3,7 +3,7 @@ module git.sxidc.com/go-framework/baize
 go 1.22.3
 
 require (
-	git.sxidc.com/go-tools/utils v1.5.11
+	git.sxidc.com/go-tools/utils v1.5.13
 	git.sxidc.com/service-supports/fserr v0.3.5
 	git.sxidc.com/service-supports/fslog v0.5.9
 	git.sxidc.com/service-supports/scm-sdk v0.1.0
@@ -11,7 +11,6 @@ require (
 	github.com/gin-gonic/gin v1.10.0
 	github.com/golang/protobuf v1.5.4
 	github.com/iancoleman/strcase v0.3.0
-	github.com/mitchellh/mapstructure v1.5.0
 	github.com/mwitkow/go-proto-validators v0.3.2
 	github.com/vrecan/death v3.0.1+incompatible
 	go.uber.org/zap v1.27.0

+ 2 - 4
go.sum

@@ -1,5 +1,5 @@
-git.sxidc.com/go-tools/utils v1.5.11 h1:Abpvvx+cntwRY3zNHor9hD5nI3Xx6uIfc5kup1kf+ok=
-git.sxidc.com/go-tools/utils v1.5.11/go.mod h1:fkobAXFpOMTvkZ82TQXWcpsayePcyk/MS5TN6GTlRDg=
+git.sxidc.com/go-tools/utils v1.5.13 h1:pqFzOlCjVyxVT8ebzFQ9+aZw6Ef87Ur/Hhr4cEAqyKw=
+git.sxidc.com/go-tools/utils v1.5.13/go.mod h1:fkobAXFpOMTvkZ82TQXWcpsayePcyk/MS5TN6GTlRDg=
 git.sxidc.com/service-supports/fserr v0.3.5 h1:1SDC60r3FIDd2iRq/oHRLK4OMa1gf67h9B7kierKTUE=
 git.sxidc.com/service-supports/fserr v0.3.5/go.mod h1:8U+W/ulZIGVPFojV6cE18shkGXqvaICuzaxIJpOcBqI=
 git.sxidc.com/service-supports/fslog v0.5.9 h1:q2XIK2o/fk/qmByy4x5kKLC+k7kolT5LrXHcWRSffXQ=
@@ -96,8 +96,6 @@ github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWE
 github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
 github.com/mitchellh/copystructure v1.0.0 h1:Laisrj+bAB6b/yJwB5Bt3ITZhGJdqmxquMKeZ+mmkFQ=
 github.com/mitchellh/copystructure v1.0.0/go.mod h1:SNtv71yrdKgLRyLFxmLdkAbkKEFWgYaq1OVrnRcwhnw=
-github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
-github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
 github.com/mitchellh/reflectwalk v1.0.0 h1:9D+8oIskB4VJBN5SFlmc27fSlIBZaov1Wpk/IfikLNY=
 github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
 github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=