Skip to content

Commit

Permalink
fix: use reflect
Browse files Browse the repository at this point in the history
liubf21 committed Jan 4, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 73418cf commit 3a1f369
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions go-backend/internal/handler/handler.go
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import (
"encoding/json"
"log"
"net/http"
"reflect"
"strings"
"v-helper/internal/model"
"v-helper/internal/service"
@@ -179,16 +180,19 @@ func DecryptionAndBindingMiddleware(targetType interface{}) gin.HandlerFunc {
return
}

if err := json.Unmarshal([]byte(decryptedData), targetType); err != nil {
// 创建一个新的实例
newTarget := reflect.New(reflect.TypeOf(targetType).Elem()).Interface()

if err := json.Unmarshal([]byte(decryptedData), newTarget); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid data format"})
c.Abort()
return
}
log.Println("encryptedRequest:", encryptedRequest)
log.Println("decryptedData:", decryptedData)
log.Println("parsedData:", targetType)
log.Println("parsedData:", newTarget)

c.Set("parsedData", targetType)
c.Set("parsedData", newTarget)
c.Next()
}
}

0 comments on commit 3a1f369

Please sign in to comment.