Replace Optional{Int,Int64,Float64} with a generic function OptionalNumber()

This commit is contained in:
Frédéric Guillot 2024-03-17 12:03:28 -07:00
parent c29ca0e313
commit f6404290ba
3 changed files with 16 additions and 30 deletions

View file

@ -3,34 +3,20 @@
package model // import "miniflux.app/v2/internal/model" 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 { func OptionalString(value string) *string {
if value != "" { if value != "" {
return &value return &value
} }
return nil 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
}

View file

@ -59,7 +59,7 @@ func (h *handler) updateFeed(w http.ResponseWriter, r *http.Request) {
FeedURL: model.OptionalString(feedForm.FeedURL), FeedURL: model.OptionalString(feedForm.FeedURL),
SiteURL: model.OptionalString(feedForm.SiteURL), SiteURL: model.OptionalString(feedForm.SiteURL),
Title: model.OptionalString(feedForm.Title), Title: model.OptionalString(feedForm.Title),
CategoryID: model.OptionalInt64(feedForm.CategoryID), CategoryID: model.OptionalNumber(feedForm.CategoryID),
BlocklistRules: model.OptionalString(feedForm.BlocklistRules), BlocklistRules: model.OptionalString(feedForm.BlocklistRules),
KeeplistRules: model.OptionalString(feedForm.KeeplistRules), KeeplistRules: model.OptionalString(feedForm.KeeplistRules),
UrlRewriteRules: model.OptionalString(feedForm.UrlRewriteRules), UrlRewriteRules: model.OptionalString(feedForm.UrlRewriteRules),

View file

@ -56,13 +56,13 @@ func (h *handler) updateSettings(w http.ResponseWriter, r *http.Request) {
Language: model.OptionalString(settingsForm.Language), Language: model.OptionalString(settingsForm.Language),
Timezone: model.OptionalString(settingsForm.Timezone), Timezone: model.OptionalString(settingsForm.Timezone),
EntryDirection: model.OptionalString(settingsForm.EntryDirection), EntryDirection: model.OptionalString(settingsForm.EntryDirection),
EntriesPerPage: model.OptionalInt(settingsForm.EntriesPerPage), EntriesPerPage: model.OptionalNumber(settingsForm.EntriesPerPage),
DisplayMode: model.OptionalString(settingsForm.DisplayMode), DisplayMode: model.OptionalString(settingsForm.DisplayMode),
GestureNav: model.OptionalString(settingsForm.GestureNav), GestureNav: model.OptionalString(settingsForm.GestureNav),
DefaultReadingSpeed: model.OptionalInt(settingsForm.DefaultReadingSpeed), DefaultReadingSpeed: model.OptionalNumber(settingsForm.DefaultReadingSpeed),
CJKReadingSpeed: model.OptionalInt(settingsForm.CJKReadingSpeed), CJKReadingSpeed: model.OptionalNumber(settingsForm.CJKReadingSpeed),
DefaultHomePage: model.OptionalString(settingsForm.DefaultHomePage), 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 { if validationErr := validator.ValidateUserModification(h.store, loggedUser.ID, userModificationRequest); validationErr != nil {