add json unmarshaling helper

This commit is contained in:
Pavel 2023-10-18 14:41:37 +03:00
parent a2eb85eb25
commit 2d1b2351ee

View File

@ -1,11 +1,13 @@
package util package util
import ( import (
"bytes"
// nolint:gosec // nolint:gosec
"crypto/sha1" "crypto/sha1"
"crypto/sha256" "crypto/sha256"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"net/http" "net/http"
"regexp" "regexp"
"strings" "strings"
@ -16,6 +18,7 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager" "github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/gin-gonic/gin"
retailcrm "github.com/retailcrm/api-client-go/v2" retailcrm "github.com/retailcrm/api-client-go/v2"
v1 "github.com/retailcrm/mg-transport-api-client-go/v1" v1 "github.com/retailcrm/mg-transport-api-client-go/v1"
@ -281,3 +284,15 @@ func GetCurrencySymbol(code string) string {
func FormatCurrencyValue(value float32) string { func FormatCurrencyValue(value float32) string {
return fmt.Sprintf("%.2f", value) return fmt.Sprintf("%.2f", value)
} }
// BindJSONWithRaw will perform usual ShouldBindJSON and will return the original body data.
func BindJSONWithRaw(c *gin.Context, obj any) ([]byte, error) {
closer := c.Request.Body
defer func() { _ = closer.Close() }()
data, err := io.ReadAll(closer)
if err != nil {
return []byte, err
}
c.Request.Body = io.NopCloser(bytes.NewReader(data))
return data, c.ShouldBindJSON(obj)
}