Replace Optional{Int,Int64,Float64} with a generic function OptionalNumber()
This commit is contained in:
parent
c29ca0e313
commit
f6404290ba
3 changed files with 16 additions and 30 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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),
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in a new issue