Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce generic type values.NullableValue and reimplement OptionalBool #29341

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion modules/indexer/issues/db/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
issue_model "code.gitea.io/gitea/models/issues"
"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/indexer/issues/internal"
"code.gitea.io/gitea/modules/util"
)

func ToDBOptions(ctx context.Context, options *internal.SearchOptions) (*issue_model.IssuesOptions, error) {
Expand Down Expand Up @@ -75,7 +76,7 @@ func ToDBOptions(ctx context.Context, options *internal.SearchOptions) (*issue_m
UpdatedAfterUnix: convertInt64(options.UpdatedAfterUnix),
UpdatedBeforeUnix: convertInt64(options.UpdatedBeforeUnix),
PriorityRepoID: 0,
IsArchived: 0,
IsArchived: util.OptionalBoolNone,
Org: nil,
Team: nil,
User: nil,
Expand Down
30 changes: 10 additions & 20 deletions modules/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,37 @@ import (
"strings"

"code.gitea.io/gitea/modules/optional"
"code.gitea.io/gitea/modules/values"

"golang.org/x/text/cases"
"golang.org/x/text/language"
)

// OptionalBool a boolean that can be "null"
type OptionalBool byte
type OptionalBool values.NullableValue[bool]

const (
var (
// OptionalBoolNone a "null" boolean value
OptionalBoolNone OptionalBool = iota
OptionalBoolNone = OptionalBool(values.None[bool]())
// OptionalBoolTrue a "true" boolean value
OptionalBoolTrue
OptionalBoolTrue = OptionalBool(values.Nullable(true))
// OptionalBoolFalse a "false" boolean value
OptionalBoolFalse
OptionalBoolFalse = OptionalBool(values.Nullable(false))
)

// IsTrue return true if equal to OptionalBoolTrue
func (o OptionalBool) IsTrue() bool {
return o == OptionalBoolTrue
return values.NullableValue[bool](o).Equal(true)
}

// IsFalse return true if equal to OptionalBoolFalse
func (o OptionalBool) IsFalse() bool {
return o == OptionalBoolFalse
return values.NullableValue[bool](o).Equal(false)
}

// IsNone return true if equal to OptionalBoolNone
func (o OptionalBool) IsNone() bool {
return o == OptionalBoolNone
return values.NullableValue[bool](o).IsNone()
}

// ToGeneric converts OptionalBool to optional.Option[bool]
Expand All @@ -52,20 +53,9 @@ func (o OptionalBool) ToGeneric() optional.Option[bool] {
return optional.Some[bool](o.IsTrue())
}

// OptionalBoolFromGeneric converts optional.Option[bool] to OptionalBool
func OptionalBoolFromGeneric(o optional.Option[bool]) OptionalBool {
if o.Has() {
return OptionalBoolOf(o.Value())
}
return OptionalBoolNone
}

// OptionalBoolOf get the corresponding OptionalBool of a bool
func OptionalBoolOf(b bool) OptionalBool {
if b {
return OptionalBoolTrue
}
return OptionalBoolFalse
return OptionalBool(values.Nullable(b))
}

// OptionalBoolParse get the corresponding OptionalBool of a string using strconv.ParseBool
Expand Down
30 changes: 30 additions & 0 deletions modules/values/nullable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package values

type NullableValue[T comparable] struct {
value *T
isNil bool
}

func (n NullableValue[T]) IsNone() bool {
return n.isNil
}

func (n NullableValue[T]) Value() T {
// check if the value IsNone first, otherwise panic
return *n.value
}

func (n NullableValue[T]) Equal(v T) bool {
return n.value != nil && *n.value == v
}

func None[T comparable]() NullableValue[T] {
return NullableValue[T]{nil, true}
}

func Nullable[T comparable](value T) NullableValue[T] {
return NullableValue[T]{&value, false}
}
21 changes: 21 additions & 0 deletions modules/values/nullable_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package values

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestNullable(t *testing.T) {
boolV := Nullable(true)
assert.True(t, boolV.Value())
assert.True(t, boolV.IsSome())

Check failure on line 15 in modules/values/nullable_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

boolV.IsSome undefined (type NullableValue[bool] has no field or method IsSome)

Check failure on line 15 in modules/values/nullable_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

boolV.IsSome undefined (type NullableValue[bool] has no field or method IsSome)

Check failure on line 15 in modules/values/nullable_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

boolV.IsSome undefined (type NullableValue[bool] has no field or method IsSome)

Check failure on line 15 in modules/values/nullable_test.go

View workflow job for this annotation

GitHub Actions / test-unit

boolV.IsSome undefined (type NullableValue[bool] has no field or method IsSome)
assert.False(t, boolV.IsNone())

nilBool := None[bool]()
assert.False(t, nilBool.IsSome())

Check failure on line 19 in modules/values/nullable_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

nilBool.IsSome undefined (type NullableValue[bool] has no field or method IsSome) (typecheck)

Check failure on line 19 in modules/values/nullable_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

nilBool.IsSome undefined (type NullableValue[bool] has no field or method IsSome) (typecheck)

Check failure on line 19 in modules/values/nullable_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

nilBool.IsSome undefined (type NullableValue[bool] has no field or method IsSome) (typecheck)

Check failure on line 19 in modules/values/nullable_test.go

View workflow job for this annotation

GitHub Actions / test-unit

nilBool.IsSome undefined (type NullableValue[bool] has no field or method IsSome)
assert.True(t, nilBool.IsNone())
}
Loading