sync from project

This commit is contained in:
2025-06-18 10:12:19 +08:00
parent 61ffeeb3b8
commit fb579e8689
20 changed files with 1332 additions and 103 deletions

View File

@ -2,8 +2,15 @@ package magic
import (
"encoding/json"
"io"
"net/http"
"reflect"
"strings"
"github.com/go-playground/validator/v10"
)
var (
jsonValidator = NewValidator("json")
)
type JsonDecodeError struct {
@ -18,10 +25,25 @@ func (err JsonDecodeError) WriteResponse(rw http.ResponseWriter) {
rw.WriteHeader(http.StatusBadRequest)
}
type Json[T any] struct {
Data T
type ValidateError []string
func (err ValidateError) Error() string {
return "invalid or missing fields: [" + strings.Join(err, ", ") + "]"
}
func (v ValidateError) WriteResponse(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(v.Error()))
}
type Json[T any] struct {
Data T `json:"data"`
}
// TakeRequestBody is a marker method to indicate that this extractor takes the request body.
func (j Json[T]) TakeRequestBody() {}
func NewJson[T any](data T) Json[T] {
return Json[T]{
Data: data,
@ -29,35 +51,41 @@ func NewJson[T any](data T) Json[T] {
}
func (data *Json[T]) FromRequest(request *http.Request) error {
if request.Body == nil {
panic("body is already taken")
}
bodyReader := request.Body
defer bodyReader.Close()
request.Body = http.NoBody
request.Body = nil
if err := json.NewDecoder(bodyReader).Decode(&data.Data); err != nil {
return JsonDecodeError{inner: err}
}
value := reflect.ValueOf(data.Data)
if value.Kind() == reflect.Struct || (value.Kind() == reflect.Ptr && value.Elem().Kind() == reflect.Struct) {
if err := jsonValidator.StructCtx(request.Context(), data.Data); err == nil {
return nil
} else if validateErr, isValidateErr := err.(validator.ValidationErrors); isValidateErr {
fields := make([]string, len(validateErr))
for idx, err := range validateErr {
fields[idx] = err.Field()
}
return ValidateError(fields)
} else {
return err
}
}
return nil
}
type counter struct {
counter int64
}
func (c *counter) Write(b []byte) (int, error) {
n := len(b)
c.counter += int64(n)
return n, nil
}
func (container Json[T]) Write(w io.Writer) (int64, error) {
counter := &counter{}
err := json.NewEncoder(io.MultiWriter(counter, w)).Encode(container.Data)
return counter.counter, err
}
func (json Json[T]) WriteResponse(w http.ResponseWriter) {
json.Write(w)
func (data *Json[T]) WriteResponse(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json")
if data == nil {
return
}
json.NewEncoder(w).Encode(data)
}