aboutsummaryrefslogtreecommitdiffstats
path: root/modules/optional
diff options
context:
space:
mode:
authorKN4CK3R <admin@oldschoolhack.me>2024-02-04 14:29:09 +0100
committerGitHub <noreply@github.com>2024-02-04 13:29:09 +0000
commitf8b471ace1b59bd3fc3a04c9ddb5f62dd1dd5396 (patch)
tree371d8477bd0cd4e41881b91fdb670eb33e38e8b1 /modules/optional
parentb4513f48ce3e748ac621a6f3ddf082feca67e938 (diff)
downloadgitea-f8b471ace1b59bd3fc3a04c9ddb5f62dd1dd5396.tar.gz
gitea-f8b471ace1b59bd3fc3a04c9ddb5f62dd1dd5396.zip
Unify user update methods (#28733)
Fixes #28660 Fixes an admin api bug related to `user.LoginSource` Fixed `/user/emails` response not identical to GitHub api This PR unifies the user update methods. The goal is to keep the logic only at one place (having audit logs in mind). For example, do the password checks only in one method not everywhere a password is updated. After that PR is merged, the user creation should be next.
Diffstat (limited to 'modules/optional')
-rw-r--r--modules/optional/option.go45
-rw-r--r--modules/optional/option_test.go48
2 files changed, 93 insertions, 0 deletions
diff --git a/modules/optional/option.go b/modules/optional/option.go
new file mode 100644
index 0000000000..af9e5ac852
--- /dev/null
+++ b/modules/optional/option.go
@@ -0,0 +1,45 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package optional
+
+type Option[T any] []T
+
+func None[T any]() Option[T] {
+ return nil
+}
+
+func Some[T any](v T) Option[T] {
+ return Option[T]{v}
+}
+
+func FromPtr[T any](v *T) Option[T] {
+ if v == nil {
+ return None[T]()
+ }
+ return Some(*v)
+}
+
+func FromNonDefault[T comparable](v T) Option[T] {
+ var zero T
+ if v == zero {
+ return None[T]()
+ }
+ return Some(v)
+}
+
+func (o Option[T]) Has() bool {
+ return o != nil
+}
+
+func (o Option[T]) Value() T {
+ var zero T
+ return o.ValueOrDefault(zero)
+}
+
+func (o Option[T]) ValueOrDefault(v T) T {
+ if o.Has() {
+ return o[0]
+ }
+ return v
+}
diff --git a/modules/optional/option_test.go b/modules/optional/option_test.go
new file mode 100644
index 0000000000..7ec345b6ba
--- /dev/null
+++ b/modules/optional/option_test.go
@@ -0,0 +1,48 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package optional
+
+import (
+ "testing"
+
+ "code.gitea.io/gitea/modules/util"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestOption(t *testing.T) {
+ var uninitialized Option[int]
+ assert.False(t, uninitialized.Has())
+ assert.Equal(t, int(0), uninitialized.Value())
+ assert.Equal(t, int(1), uninitialized.ValueOrDefault(1))
+
+ none := None[int]()
+ assert.False(t, none.Has())
+ assert.Equal(t, int(0), none.Value())
+ assert.Equal(t, int(1), none.ValueOrDefault(1))
+
+ some := Some[int](1)
+ assert.True(t, some.Has())
+ assert.Equal(t, int(1), some.Value())
+ assert.Equal(t, int(1), some.ValueOrDefault(2))
+
+ var ptr *int
+ assert.False(t, FromPtr(ptr).Has())
+
+ opt1 := FromPtr(util.ToPointer(1))
+ assert.True(t, opt1.Has())
+ assert.Equal(t, int(1), opt1.Value())
+
+ assert.False(t, FromNonDefault("").Has())
+
+ opt2 := FromNonDefault("test")
+ assert.True(t, opt2.Has())
+ assert.Equal(t, "test", opt2.Value())
+
+ assert.False(t, FromNonDefault(0).Has())
+
+ opt3 := FromNonDefault(1)
+ assert.True(t, opt3.Has())
+ assert.Equal(t, int(1), opt3.Value())
+}