improved deserialization method

This commit is contained in:
yusing
2024-12-18 07:18:18 +08:00
parent 6aefe4d5d9
commit f2a9ddd1a6
8 changed files with 310 additions and 98 deletions

View File

@@ -18,9 +18,9 @@ type Webhook struct {
Template string `json:"template" validate:"omitempty,oneof=discord"`
Payload string `json:"payload" validate:"jsonIfTemplateNotUsed"`
Tok string `json:"token"`
Meth string `json:"method" validate:"omitempty,oneof=GET POST PUT"`
Meth string `json:"method" validate:"oneof=GET POST PUT"`
MIMETyp string `json:"mime_type"`
ColorM string `json:"color_mode" validate:"omitempty,oneof=hex dec"`
ColorM string `json:"color_mode" validate:"oneof=hex dec"`
}
//go:embed templates/discord.json
@@ -30,6 +30,14 @@ var webhookTemplates = map[string]string{
"discord": discordPayload,
}
func DefaultValue() *Webhook {
return &Webhook{
Meth: "POST",
ColorM: "hex",
MIMETyp: "application/json",
}
}
func jsonIfTemplateNotUsed(fl validator.FieldLevel) bool {
template := fl.Parent().FieldByName("Template").String()
if template != "" {
@@ -40,6 +48,7 @@ func jsonIfTemplateNotUsed(fl validator.FieldLevel) bool {
}
func init() {
utils.RegisterDefaultValueFactory(DefaultValue)
err := utils.Validator().RegisterValidation("jsonIfTemplateNotUsed", jsonIfTemplateNotUsed)
if err != nil {
panic(err)
@@ -53,10 +62,7 @@ func (webhook *Webhook) Name() string {
// Method implements Provider.
func (webhook *Webhook) Method() string {
if webhook.Meth != "" {
return webhook.Meth
}
return http.MethodPost
return webhook.Meth
}
// URL implements Provider.
@@ -71,10 +77,7 @@ func (webhook *Webhook) Token() string {
// MIMEType implements Provider.
func (webhook *Webhook) MIMEType() string {
if webhook.MIMETyp != "" {
return webhook.MIMETyp
}
return "application/json"
return webhook.MIMETyp
}
func (webhook *Webhook) ColorMode() string {