From 16cad11e894562107f052c31cbf9e46ee3ae9fb9 Mon Sep 17 00:00:00 2001 From: yusing Date: Sat, 11 Oct 2025 16:54:52 +0800 Subject: [PATCH] fix(notif): format not being applied correctly --- internal/notif/base.go | 20 +++++++++++++----- internal/notif/body.go | 41 ++++++++++--------------------------- internal/notif/providers.go | 3 +++ 3 files changed, 29 insertions(+), 35 deletions(-) diff --git a/internal/notif/base.go b/internal/notif/base.go index 30e4a169..87a55366 100644 --- a/internal/notif/base.go +++ b/internal/notif/base.go @@ -10,10 +10,10 @@ import ( ) type ProviderBase struct { - Name string `json:"name" validate:"required"` - URL string `json:"url" validate:"url"` - Token string `json:"token"` - Format *LogFormat `json:"format"` + Name string `json:"name" validate:"required"` + URL string `json:"url" validate:"url"` + Token string `json:"token"` + Format LogFormat `json:"format"` } type rawError []byte @@ -30,9 +30,19 @@ var ( // Validate implements the utils.CustomValidator interface. func (base *ProviderBase) Validate() gperr.Error { - if base.Format == nil || base.Format.string == "" { + switch base.Format { + case "": base.Format = LogFormatMarkdown + case LogFormatPlain, LogFormatMarkdown: + default: + return gperr.Multiline(). + Addf("invalid log format %s, supported formats:", base.Format). + AddLines( + LogFormatPlain, + LogFormatMarkdown, + ) } + if !strings.HasPrefix(base.URL, "http://") && !strings.HasPrefix(base.URL, "https://") { return ErrURLMissingScheme } diff --git a/internal/notif/body.go b/internal/notif/body.go index 01e8fd2c..e68b9d8b 100644 --- a/internal/notif/body.go +++ b/internal/notif/body.go @@ -13,11 +13,9 @@ type ( Name string `json:"name"` Value string `json:"value"` } - LogFormat struct { - string - } - LogBody interface { - Format(format *LogFormat) ([]byte, error) + LogFormat string + LogBody interface { + Format(format LogFormat) ([]byte, error) } ) @@ -30,10 +28,10 @@ type ( } ) -var ( - LogFormatMarkdown = &LogFormat{"markdown"} - LogFormatPlain = &LogFormat{"plain"} - LogFormatRawJSON = &LogFormat{"json"} // internal use only +const ( + LogFormatMarkdown LogFormat = "markdown" + LogFormatPlain LogFormat = "plain" + LogFormatRawJSON LogFormat = "json" // internal use only ) func MakeLogFields(fields ...LogField) LogBody { @@ -44,28 +42,11 @@ func ErrorBody(err error) LogBody { return errorBody{Error: err} } -func (f *LogFormat) Parse(format string) error { - switch format { - case "": - f.string = LogFormatMarkdown.string - case LogFormatPlain.string, LogFormatMarkdown.string: - f.string = format - default: - return gperr.Multiline(). - Addf("invalid log format %s, supported formats:", format). - AddLines( - LogFormatPlain, - LogFormatMarkdown, - ) - } - return nil -} - func (f *FieldsBody) Add(name, value string) { *f = append(*f, LogField{Name: name, Value: value}) } -func (f FieldsBody) Format(format *LogFormat) ([]byte, error) { +func (f FieldsBody) Format(format LogFormat) ([]byte, error) { switch format { case LogFormatMarkdown: var msg bytes.Buffer @@ -92,7 +73,7 @@ func (f FieldsBody) Format(format *LogFormat) ([]byte, error) { return f.Format(LogFormatMarkdown) } -func (l ListBody) Format(format *LogFormat) ([]byte, error) { +func (l ListBody) Format(format LogFormat) ([]byte, error) { switch format { case LogFormatPlain: return []byte(strings.Join(l, "\n")), nil @@ -110,7 +91,7 @@ func (l ListBody) Format(format *LogFormat) ([]byte, error) { return l.Format(LogFormatMarkdown) } -func (m MessageBody) Format(format *LogFormat) ([]byte, error) { +func (m MessageBody) Format(format LogFormat) ([]byte, error) { switch format { case LogFormatPlain, LogFormatMarkdown: return []byte(m), nil @@ -120,7 +101,7 @@ func (m MessageBody) Format(format *LogFormat) ([]byte, error) { return m.Format(LogFormatMarkdown) } -func (e errorBody) Format(format *LogFormat) ([]byte, error) { +func (e errorBody) Format(format LogFormat) ([]byte, error) { switch format { case LogFormatRawJSON: return sonic.Marshal(e.Error) diff --git a/internal/notif/providers.go b/internal/notif/providers.go index ace7235f..8cae2a35 100644 --- a/internal/notif/providers.go +++ b/internal/notif/providers.go @@ -64,6 +64,9 @@ func (msg *LogMessage) notify(ctx context.Context, provider Provider) error { } provider.SetHeaders(msg, req.Header) + fmt.Println(req.Header) + fmt.Println(string(body)) + resp, err := http.DefaultClient.Do(req) if err != nil { return err