From f6404290ba208c088de3724fff9fe20442dcb7ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Sun, 17 Mar 2024 12:03:28 -0700 Subject: [PATCH] Replace Optional{Int,Int64,Float64} with a generic function OptionalNumber() --- internal/model/model.go | 36 +++++++++++----------------------- internal/ui/feed_update.go | 2 +- internal/ui/settings_update.go | 8 ++++---- 3 files changed, 16 insertions(+), 30 deletions(-) diff --git a/internal/model/model.go b/internal/model/model.go index aed0fe14..86f1e370 100644 --- a/internal/model/model.go +++ b/internal/model/model.go @@ -3,34 +3,20 @@ package model // import "miniflux.app/v2/internal/model" -// OptionalString populates an optional string field. +type Number interface { + int | int64 | float64 +} + +func OptionalNumber[T Number](value T) *T { + if value > 0 { + return &value + } + return nil +} + func OptionalString(value string) *string { if value != "" { return &value } return nil } - -// OptionalInt populates an optional int field. -func OptionalInt(value int) *int { - if value > 0 { - return &value - } - return nil -} - -// OptionalInt64 populates an optional int64 field. -func OptionalInt64(value int64) *int64 { - if value > 0 { - return &value - } - return nil -} - -// OptionalFloat populates an optional float64 field. -func OptionalFloat(value float64) *float64 { - if value > 0 { - return &value - } - return nil -} diff --git a/internal/ui/feed_update.go b/internal/ui/feed_update.go index 52cdad3d..da75d59f 100644 --- a/internal/ui/feed_update.go +++ b/internal/ui/feed_update.go @@ -59,7 +59,7 @@ func (h *handler) updateFeed(w http.ResponseWriter, r *http.Request) { FeedURL: model.OptionalString(feedForm.FeedURL), SiteURL: model.OptionalString(feedForm.SiteURL), Title: model.OptionalString(feedForm.Title), - CategoryID: model.OptionalInt64(feedForm.CategoryID), + CategoryID: model.OptionalNumber(feedForm.CategoryID), BlocklistRules: model.OptionalString(feedForm.BlocklistRules), KeeplistRules: model.OptionalString(feedForm.KeeplistRules), UrlRewriteRules: model.OptionalString(feedForm.UrlRewriteRules), diff --git a/internal/ui/settings_update.go b/internal/ui/settings_update.go index e0e3c0af..fceec0dc 100644 --- a/internal/ui/settings_update.go +++ b/internal/ui/settings_update.go @@ -56,13 +56,13 @@ func (h *handler) updateSettings(w http.ResponseWriter, r *http.Request) { Language: model.OptionalString(settingsForm.Language), Timezone: model.OptionalString(settingsForm.Timezone), EntryDirection: model.OptionalString(settingsForm.EntryDirection), - EntriesPerPage: model.OptionalInt(settingsForm.EntriesPerPage), + EntriesPerPage: model.OptionalNumber(settingsForm.EntriesPerPage), DisplayMode: model.OptionalString(settingsForm.DisplayMode), GestureNav: model.OptionalString(settingsForm.GestureNav), - DefaultReadingSpeed: model.OptionalInt(settingsForm.DefaultReadingSpeed), - CJKReadingSpeed: model.OptionalInt(settingsForm.CJKReadingSpeed), + DefaultReadingSpeed: model.OptionalNumber(settingsForm.DefaultReadingSpeed), + CJKReadingSpeed: model.OptionalNumber(settingsForm.CJKReadingSpeed), DefaultHomePage: model.OptionalString(settingsForm.DefaultHomePage), - MediaPlaybackRate: model.OptionalFloat(settingsForm.MediaPlaybackRate), + MediaPlaybackRate: model.OptionalNumber(settingsForm.MediaPlaybackRate), } if validationErr := validator.ValidateUserModification(h.store, loggedUser.ID, userModificationRequest); validationErr != nil {