diff options
author | 2024-11-26 08:23:00 -0800 | |
---|---|---|
committer | 2024-11-26 08:23:00 -0800 | |
commit | 6a8af426474acd1ffd5cb3265a2372003977326a (patch) | |
tree | 9f22c7d3eabc784728da22d60f0ec05ed93ac6ac /internal/api/util/parseform.go | |
parent | [chore] Sign the bloody thing, fix the other bloody thing (#3572) (diff) | |
download | gotosocial-6a8af426474acd1ffd5cb3265a2372003977326a.tar.xz |
[bugfix] Allow unsetting filter expiration dates (#3560)
* Regression tests for #3497 (v1 and v2)
* use Nullable type for v2 form.expires_in
---------
Co-authored-by: tobi <tobi.smethurst@protonmail.com>
Diffstat (limited to 'internal/api/util/parseform.go')
-rw-r--r-- | internal/api/util/parseform.go | 34 |
1 files changed, 26 insertions, 8 deletions
diff --git a/internal/api/util/parseform.go b/internal/api/util/parseform.go index 19e24189f..3eab065f2 100644 --- a/internal/api/util/parseform.go +++ b/internal/api/util/parseform.go @@ -20,12 +20,13 @@ package util import ( "fmt" "strconv" + + apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" + "github.com/superseriousbusiness/gotosocial/internal/util" ) -// ParseDuration parses the given raw interface belonging to +// ParseDuration parses the given raw interface belonging // the given fieldName as an integer duration. -// -// Will return nil, nil if rawI is the zero value of its type. func ParseDuration(rawI any, fieldName string) (*int, error) { var ( asInteger int @@ -60,11 +61,28 @@ func ParseDuration(rawI any, fieldName string) (*int, error) { return nil, err } - // Someone submitted 0, - // don't point to this. - if asInteger == 0 { - return nil, nil + return &asInteger, nil +} + +// ParseNullableDuration is like ParseDuration, but +// for JSON values that may have been sent as `null`. +// +// IsSpecified should be checked and "true" on the +// given nullable before calling this function. +func ParseNullableDuration( + nullable apimodel.Nullable[any], + fieldName string, +) (*int, error) { + if nullable.IsNull() { + // Was specified as `null`, + // return pointer to zero value. + return util.Ptr(0), nil } - return &asInteger, nil + rawI, err := nullable.Get() + if err != nil { + return nil, err + } + + return ParseDuration(rawI, fieldName) } |