package middleware

import (
	"encoding/json"
	"game-driver/internal/schema"
	"game-driver/leaf"
	"go.uber.org/zap"
)

type JSONKey string

const PayloadJSONKey JSONKey = "payload_json"

// PayloadJSON 解析报文
func PayloadJSON[T schema.JsonModel]() leaf.HandlerFunc {
	return func(c *leaf.Context) {
		pm := new(T)
		err := json.Unmarshal(c.Payload, pm)
		if err != nil {
			zap.S().Panicln("报文解析错误", err)
		}
		leaf.WithValue[*T](c, PayloadJSONKey, pm)
		c.Next()
	}
}
