瀏覽代碼

简化代码

yjp 1 年之前
父節點
當前提交
c393eb6836

+ 14 - 26
convenient/relation/one2one/service.go

@@ -1,7 +1,6 @@
 package one2one
 
 import (
-	"encoding/json"
 	"git.sxidc.com/go-framework/baize/convenient/binding"
 	"git.sxidc.com/go-framework/baize/convenient/binding/request"
 	"git.sxidc.com/go-framework/baize/framwork/api"
@@ -11,6 +10,7 @@ import (
 	"git.sxidc.com/go-framework/baize/framwork/infrastructure/database/sql"
 	"git.sxidc.com/go-tools/utils/strutils"
 	"git.sxidc.com/service-supports/fserr"
+	"github.com/mitchellh/mapstructure"
 	"reflect"
 )
 
@@ -195,6 +195,11 @@ func Query[TI any](fromTableName string, toTableName string, toRelationColumnNam
 	}
 }
 
+type WithInfo[FI any, TI any] struct {
+	Self FI `json:"self" mapstructure:"self"`
+	With TI `json:"with" mapstructure:"with"`
+}
+
 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
@@ -214,18 +219,11 @@ func QueryWithOtherInfo[FI any, TI any](fromTableName string, toTableName string
 
 		outputToZero = outputToZeroValue.Interface().(TI)
 
-		zeroInfo := &struct {
-			Self FI `json:"self"`
-			With TI `json:"with"`
-		}{Self: outputFromZero, With: outputToZero}
-
-		zeroInfoJson, err := json.Marshal(zeroInfo)
-		if err != nil {
-			return nil, err
-		}
-
 		zeroRetMap := make(map[string]any)
-		err = json.Unmarshal(zeroInfoJson, &zeroRetMap)
+		err := mapstructure.Decode(WithInfo[FI, TI]{
+			Self: outputFromZero,
+			With: outputToZero,
+		}, &zeroRetMap)
 		if err != nil {
 			return nil, err
 		}
@@ -297,21 +295,11 @@ func QueryWithOtherInfo[FI any, TI any](fromTableName string, toTableName string
 			return zeroRetMap, err
 		}
 
-		info := &struct {
-			Self FI `json:"self"`
-			With TI `json:"with"`
-		}{Self: fromInfo, With: toInfo}
-
-		infoJson, err := json.Marshal(info)
-		if err != nil {
-			return nil, err
-		}
-
 		retMap := make(map[string]any)
-		err = json.Unmarshal(infoJson, &retMap)
-		if err != nil {
-			return nil, err
-		}
+		err = mapstructure.Decode(WithInfo[FI, TI]{
+			Self: fromInfo,
+			With: toInfo,
+		}, &retMap)
 
 		return retMap, nil
 	}

+ 1 - 1
examples/examples/assign_tag/main.go

@@ -2,7 +2,7 @@ package main
 
 import (
 	"fmt"
-	"git.sxidc.com/go-framework/baize/tag/assign"
+	"git.sxidc.com/go-framework/baize/framwork/tag/assign"
 	"reflect"
 	"time"
 )

+ 4 - 4
examples/examples/quick_start/main.go

@@ -3,13 +3,13 @@ package main
 import (
 	"fmt"
 	"git.sxidc.com/go-framework/baize"
-	"git.sxidc.com/go-framework/baize/api"
-	"git.sxidc.com/go-framework/baize/application"
 	"git.sxidc.com/go-framework/baize/convenient/binding"
 	"git.sxidc.com/go-framework/baize/convenient/binding/request"
 	"git.sxidc.com/go-framework/baize/convenient/binding/response"
-	"git.sxidc.com/go-framework/baize/domain"
-	"git.sxidc.com/go-framework/baize/infrastructure"
+	"git.sxidc.com/go-framework/baize/framwork/api"
+	"git.sxidc.com/go-framework/baize/framwork/application"
+	"git.sxidc.com/go-framework/baize/framwork/domain"
+	"git.sxidc.com/go-framework/baize/framwork/infrastructure"
 	DEATH "github.com/vrecan/death"
 	"net/http"
 	"syscall"

+ 1 - 1
examples/examples/sql_mapping_tag/main.go

@@ -2,7 +2,7 @@ package main
 
 import (
 	"fmt"
-	"git.sxidc.com/go-framework/baize/tag/sql/sql_mapping"
+	"git.sxidc.com/go-framework/baize/framwork/tag/sql/sql_mapping"
 	"reflect"
 	"time"
 )

+ 1 - 1
examples/examples/sql_result_tag/main.go

@@ -2,7 +2,7 @@ package main
 
 import (
 	"fmt"
-	"git.sxidc.com/go-framework/baize/tag/sql/sql_result"
+	"git.sxidc.com/go-framework/baize/framwork/tag/sql/sql_result"
 	"reflect"
 	"time"
 )

+ 1 - 0
go.mod

@@ -11,6 +11,7 @@ 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 - 0
go.sum

@@ -96,6 +96,8 @@ 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=