mirror of https://github.com/go-gitea/gitea.git
Move ParseBool to optional (#33979)
parent
08510adefe
commit
25b6f38865
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
package optional
|
package optional
|
||||||
|
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
type Option[T any] []T
|
type Option[T any] []T
|
||||||
|
|
||||||
func None[T any]() Option[T] {
|
func None[T any]() Option[T] {
|
||||||
|
@ -43,3 +45,12 @@ func (o Option[T]) ValueOrDefault(v T) T {
|
||||||
}
|
}
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseBool get the corresponding optional.Option[bool] of a string using strconv.ParseBool
|
||||||
|
func ParseBool(s string) Option[bool] {
|
||||||
|
v, e := strconv.ParseBool(s)
|
||||||
|
if e != nil {
|
||||||
|
return None[bool]()
|
||||||
|
}
|
||||||
|
return Some(v)
|
||||||
|
}
|
||||||
|
|
|
@ -57,3 +57,16 @@ func TestOption(t *testing.T) {
|
||||||
assert.True(t, opt3.Has())
|
assert.True(t, opt3.Has())
|
||||||
assert.Equal(t, int(1), opt3.Value())
|
assert.Equal(t, int(1), opt3.Value())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_ParseBool(t *testing.T) {
|
||||||
|
assert.Equal(t, optional.None[bool](), optional.ParseBool(""))
|
||||||
|
assert.Equal(t, optional.None[bool](), optional.ParseBool("x"))
|
||||||
|
|
||||||
|
assert.Equal(t, optional.Some(false), optional.ParseBool("0"))
|
||||||
|
assert.Equal(t, optional.Some(false), optional.ParseBool("f"))
|
||||||
|
assert.Equal(t, optional.Some(false), optional.ParseBool("False"))
|
||||||
|
|
||||||
|
assert.Equal(t, optional.Some(true), optional.ParseBool("1"))
|
||||||
|
assert.Equal(t, optional.Some(true), optional.ParseBool("t"))
|
||||||
|
assert.Equal(t, optional.Some(true), optional.ParseBool("True"))
|
||||||
|
}
|
||||||
|
|
|
@ -12,8 +12,8 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/log"
|
"code.gitea.io/gitea/modules/log"
|
||||||
|
"code.gitea.io/gitea/modules/optional"
|
||||||
"code.gitea.io/gitea/modules/user"
|
"code.gitea.io/gitea/modules/user"
|
||||||
"code.gitea.io/gitea/modules/util"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// settings
|
// settings
|
||||||
|
@ -159,7 +159,7 @@ func loadRunModeFrom(rootCfg ConfigProvider) {
|
||||||
// The following is a purposefully undocumented option. Please do not run Gitea as root. It will only cause future headaches.
|
// The following is a purposefully undocumented option. Please do not run Gitea as root. It will only cause future headaches.
|
||||||
// Please don't use root as a bandaid to "fix" something that is broken, instead the broken thing should instead be fixed properly.
|
// Please don't use root as a bandaid to "fix" something that is broken, instead the broken thing should instead be fixed properly.
|
||||||
unsafeAllowRunAsRoot := ConfigSectionKeyBool(rootSec, "I_AM_BEING_UNSAFE_RUNNING_AS_ROOT")
|
unsafeAllowRunAsRoot := ConfigSectionKeyBool(rootSec, "I_AM_BEING_UNSAFE_RUNNING_AS_ROOT")
|
||||||
unsafeAllowRunAsRoot = unsafeAllowRunAsRoot || util.OptionalBoolParse(os.Getenv("GITEA_I_AM_BEING_UNSAFE_RUNNING_AS_ROOT")).Value()
|
unsafeAllowRunAsRoot = unsafeAllowRunAsRoot || optional.ParseBool(os.Getenv("GITEA_I_AM_BEING_UNSAFE_RUNNING_AS_ROOT")).Value()
|
||||||
RunMode = os.Getenv("GITEA_RUN_MODE")
|
RunMode = os.Getenv("GITEA_RUN_MODE")
|
||||||
if RunMode == "" {
|
if RunMode == "" {
|
||||||
RunMode = rootSec.Key("RUN_MODE").MustString("prod")
|
RunMode = rootSec.Key("RUN_MODE").MustString("prod")
|
||||||
|
|
|
@ -11,21 +11,10 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/optional"
|
|
||||||
|
|
||||||
"golang.org/x/text/cases"
|
"golang.org/x/text/cases"
|
||||||
"golang.org/x/text/language"
|
"golang.org/x/text/language"
|
||||||
)
|
)
|
||||||
|
|
||||||
// OptionalBoolParse get the corresponding optional.Option[bool] of a string using strconv.ParseBool
|
|
||||||
func OptionalBoolParse(s string) optional.Option[bool] {
|
|
||||||
v, e := strconv.ParseBool(s)
|
|
||||||
if e != nil {
|
|
||||||
return optional.None[bool]()
|
|
||||||
}
|
|
||||||
return optional.Some(v)
|
|
||||||
}
|
|
||||||
|
|
||||||
// IsEmptyString checks if the provided string is empty
|
// IsEmptyString checks if the provided string is empty
|
||||||
func IsEmptyString(s string) bool {
|
func IsEmptyString(s string) bool {
|
||||||
return len(strings.TrimSpace(s)) == 0
|
return len(strings.TrimSpace(s)) == 0
|
||||||
|
|
|
@ -8,8 +8,6 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"code.gitea.io/gitea/modules/optional"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -175,19 +173,6 @@ func Test_RandomBytes(t *testing.T) {
|
||||||
assert.NotEqual(t, bytes3, bytes4)
|
assert.NotEqual(t, bytes3, bytes4)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestOptionalBoolParse(t *testing.T) {
|
|
||||||
assert.Equal(t, optional.None[bool](), OptionalBoolParse(""))
|
|
||||||
assert.Equal(t, optional.None[bool](), OptionalBoolParse("x"))
|
|
||||||
|
|
||||||
assert.Equal(t, optional.Some(false), OptionalBoolParse("0"))
|
|
||||||
assert.Equal(t, optional.Some(false), OptionalBoolParse("f"))
|
|
||||||
assert.Equal(t, optional.Some(false), OptionalBoolParse("False"))
|
|
||||||
|
|
||||||
assert.Equal(t, optional.Some(true), OptionalBoolParse("1"))
|
|
||||||
assert.Equal(t, optional.Some(true), OptionalBoolParse("t"))
|
|
||||||
assert.Equal(t, optional.Some(true), OptionalBoolParse("True"))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test case for any function which accepts and returns a single string.
|
// Test case for any function which accepts and returns a single string.
|
||||||
type StringTest struct {
|
type StringTest struct {
|
||||||
in, out string
|
in, out string
|
||||||
|
|
|
@ -22,7 +22,6 @@ import (
|
||||||
"code.gitea.io/gitea/modules/optional"
|
"code.gitea.io/gitea/modules/optional"
|
||||||
"code.gitea.io/gitea/modules/setting"
|
"code.gitea.io/gitea/modules/setting"
|
||||||
"code.gitea.io/gitea/modules/templates"
|
"code.gitea.io/gitea/modules/templates"
|
||||||
"code.gitea.io/gitea/modules/util"
|
|
||||||
"code.gitea.io/gitea/modules/web"
|
"code.gitea.io/gitea/modules/web"
|
||||||
"code.gitea.io/gitea/routers/web/explore"
|
"code.gitea.io/gitea/routers/web/explore"
|
||||||
user_setting "code.gitea.io/gitea/routers/web/user/setting"
|
user_setting "code.gitea.io/gitea/routers/web/user/setting"
|
||||||
|
@ -72,11 +71,11 @@ func Users(ctx *context.Context) {
|
||||||
PageSize: setting.UI.Admin.UserPagingNum,
|
PageSize: setting.UI.Admin.UserPagingNum,
|
||||||
},
|
},
|
||||||
SearchByEmail: true,
|
SearchByEmail: true,
|
||||||
IsActive: util.OptionalBoolParse(statusFilterMap["is_active"]),
|
IsActive: optional.ParseBool(statusFilterMap["is_active"]),
|
||||||
IsAdmin: util.OptionalBoolParse(statusFilterMap["is_admin"]),
|
IsAdmin: optional.ParseBool(statusFilterMap["is_admin"]),
|
||||||
IsRestricted: util.OptionalBoolParse(statusFilterMap["is_restricted"]),
|
IsRestricted: optional.ParseBool(statusFilterMap["is_restricted"]),
|
||||||
IsTwoFactorEnabled: util.OptionalBoolParse(statusFilterMap["is_2fa_enabled"]),
|
IsTwoFactorEnabled: optional.ParseBool(statusFilterMap["is_2fa_enabled"]),
|
||||||
IsProhibitLogin: util.OptionalBoolParse(statusFilterMap["is_prohibit_login"]),
|
IsProhibitLogin: optional.ParseBool(statusFilterMap["is_prohibit_login"]),
|
||||||
IncludeReserved: true, // administrator needs to list all accounts include reserved, bot, remote ones
|
IncludeReserved: true, // administrator needs to list all accounts include reserved, bot, remote ones
|
||||||
}, tplUsers)
|
}, tplUsers)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue