2023-04-10 04:29:13 +03:00
|
|
|
package kind
|
|
|
|
|
|
|
|
// MungeFunc is a function that accepts a value and returns a munged
|
|
|
|
// value with the appropriate Kind. For example, a Datetime MungeFunc
|
|
|
|
// would accept string "2020-06-11T02:50:54Z" and return a time.Time.
|
|
|
|
type MungeFunc func(any) (any, error)
|
|
|
|
|
|
|
|
var _ MungeFunc = MungeEmptyStringAsNil
|
|
|
|
|
|
|
|
// MungeEmptyStringAsNil munges v to nil if v
|
|
|
|
// is an empty string.
|
|
|
|
func MungeEmptyStringAsNil(v any) (any, error) {
|
|
|
|
switch v := v.(type) {
|
|
|
|
case nil:
|
2023-06-13 19:06:18 +03:00
|
|
|
return nil, nil //nolint:nilnil
|
2023-04-10 04:29:13 +03:00
|
|
|
case *string:
|
|
|
|
if len(*v) == 0 {
|
2023-06-13 19:06:18 +03:00
|
|
|
return nil, nil //nolint:nilnil
|
2023-04-10 04:29:13 +03:00
|
|
|
}
|
|
|
|
case string:
|
|
|
|
if len(v) == 0 {
|
2023-06-13 19:06:18 +03:00
|
|
|
return nil, nil //nolint:nilnil
|
2023-04-10 04:29:13 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return v, nil
|
|
|
|
}
|