summaryrefslogtreecommitdiffstats
path: root/modules/optional
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--modules/optional/option.go45
-rw-r--r--modules/optional/option_test.go59
-rw-r--r--modules/optional/serialization.go46
-rw-r--r--modules/optional/serialization_test.go191
4 files changed, 341 insertions, 0 deletions
diff --git a/modules/optional/option.go b/modules/optional/option.go
new file mode 100644
index 0000000..af9e5ac
--- /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 0000000..203e922
--- /dev/null
+++ b/modules/optional/option_test.go
@@ -0,0 +1,59 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package optional_test
+
+import (
+ "testing"
+
+ "code.gitea.io/gitea/modules/optional"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestOption(t *testing.T) {
+ var uninitialized optional.Option[int]
+ assert.False(t, uninitialized.Has())
+ assert.Equal(t, int(0), uninitialized.Value())
+ assert.Equal(t, int(1), uninitialized.ValueOrDefault(1))
+
+ none := optional.None[int]()
+ assert.False(t, none.Has())
+ assert.Equal(t, int(0), none.Value())
+ assert.Equal(t, int(1), none.ValueOrDefault(1))
+
+ some := optional.Some(1)
+ assert.True(t, some.Has())
+ assert.Equal(t, int(1), some.Value())
+ assert.Equal(t, int(1), some.ValueOrDefault(2))
+
+ noneBool := optional.None[bool]()
+ assert.False(t, noneBool.Has())
+ assert.False(t, noneBool.Value())
+ assert.True(t, noneBool.ValueOrDefault(true))
+
+ someBool := optional.Some(true)
+ assert.True(t, someBool.Has())
+ assert.True(t, someBool.Value())
+ assert.True(t, someBool.ValueOrDefault(false))
+
+ var ptr *int
+ assert.False(t, optional.FromPtr(ptr).Has())
+
+ int1 := 1
+ opt1 := optional.FromPtr(&int1)
+ assert.True(t, opt1.Has())
+ assert.Equal(t, int(1), opt1.Value())
+
+ assert.False(t, optional.FromNonDefault("").Has())
+
+ opt2 := optional.FromNonDefault("test")
+ assert.True(t, opt2.Has())
+ assert.Equal(t, "test", opt2.Value())
+
+ assert.False(t, optional.FromNonDefault(0).Has())
+
+ opt3 := optional.FromNonDefault(1)
+ assert.True(t, opt3.Has())
+ assert.Equal(t, int(1), opt3.Value())
+}
diff --git a/modules/optional/serialization.go b/modules/optional/serialization.go
new file mode 100644
index 0000000..b120a0e
--- /dev/null
+++ b/modules/optional/serialization.go
@@ -0,0 +1,46 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package optional
+
+import (
+ "code.gitea.io/gitea/modules/json"
+
+ "gopkg.in/yaml.v3"
+)
+
+func (o *Option[T]) UnmarshalJSON(data []byte) error {
+ var v *T
+ if err := json.Unmarshal(data, &v); err != nil {
+ return err
+ }
+ *o = FromPtr(v)
+ return nil
+}
+
+func (o Option[T]) MarshalJSON() ([]byte, error) {
+ if !o.Has() {
+ return []byte("null"), nil
+ }
+
+ return json.Marshal(o.Value())
+}
+
+func (o *Option[T]) UnmarshalYAML(value *yaml.Node) error {
+ var v *T
+ if err := value.Decode(&v); err != nil {
+ return err
+ }
+ *o = FromPtr(v)
+ return nil
+}
+
+func (o Option[T]) MarshalYAML() (any, error) {
+ if !o.Has() {
+ return nil, nil
+ }
+
+ value := new(yaml.Node)
+ err := value.Encode(o.Value())
+ return value, err
+}
diff --git a/modules/optional/serialization_test.go b/modules/optional/serialization_test.go
new file mode 100644
index 0000000..c852b8a
--- /dev/null
+++ b/modules/optional/serialization_test.go
@@ -0,0 +1,191 @@
+// Copyright 2024 The Gitea Authors. All rights reserved.
+// SPDX-License-Identifier: MIT
+
+package optional_test
+
+import (
+ std_json "encoding/json" //nolint:depguard
+ "testing"
+
+ "code.gitea.io/gitea/modules/json"
+ "code.gitea.io/gitea/modules/optional"
+
+ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+ "gopkg.in/yaml.v3"
+)
+
+type testSerializationStruct struct {
+ NormalString string `json:"normal_string" yaml:"normal_string"`
+ NormalBool bool `json:"normal_bool" yaml:"normal_bool"`
+ OptBool optional.Option[bool] `json:"optional_bool,omitempty" yaml:"optional_bool,omitempty"`
+ OptString optional.Option[string] `json:"optional_string,omitempty" yaml:"optional_string,omitempty"`
+ OptTwoBool optional.Option[bool] `json:"optional_two_bool" yaml:"optional_two_bool"`
+ OptTwoString optional.Option[string] `json:"optional_twostring" yaml:"optional_two_string"`
+}
+
+func TestOptionalToJson(t *testing.T) {
+ tests := []struct {
+ name string
+ obj *testSerializationStruct
+ want string
+ }{
+ {
+ name: "empty",
+ obj: new(testSerializationStruct),
+ want: `{"normal_string":"","normal_bool":false,"optional_two_bool":null,"optional_twostring":null}`,
+ },
+ {
+ name: "some",
+ obj: &testSerializationStruct{
+ NormalString: "a string",
+ NormalBool: true,
+ OptBool: optional.Some(false),
+ OptString: optional.Some(""),
+ OptTwoBool: optional.None[bool](),
+ OptTwoString: optional.None[string](),
+ },
+ want: `{"normal_string":"a string","normal_bool":true,"optional_bool":false,"optional_string":"","optional_two_bool":null,"optional_twostring":null}`,
+ },
+ }
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ b, err := json.Marshal(tc.obj)
+ require.NoError(t, err)
+ assert.EqualValues(t, tc.want, string(b), "gitea json module returned unexpected")
+
+ b, err = std_json.Marshal(tc.obj)
+ require.NoError(t, err)
+ assert.EqualValues(t, tc.want, string(b), "std json module returned unexpected")
+ })
+ }
+}
+
+func TestOptionalFromJson(t *testing.T) {
+ tests := []struct {
+ name string
+ data string
+ want testSerializationStruct
+ }{
+ {
+ name: "empty",
+ data: `{}`,
+ want: testSerializationStruct{
+ NormalString: "",
+ },
+ },
+ {
+ name: "some",
+ data: `{"normal_string":"a string","normal_bool":true,"optional_bool":false,"optional_string":"","optional_two_bool":null,"optional_twostring":null}`,
+ want: testSerializationStruct{
+ NormalString: "a string",
+ NormalBool: true,
+ OptBool: optional.Some(false),
+ OptString: optional.Some(""),
+ },
+ },
+ }
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ var obj1 testSerializationStruct
+ err := json.Unmarshal([]byte(tc.data), &obj1)
+ require.NoError(t, err)
+ assert.EqualValues(t, tc.want, obj1, "gitea json module returned unexpected")
+
+ var obj2 testSerializationStruct
+ err = std_json.Unmarshal([]byte(tc.data), &obj2)
+ require.NoError(t, err)
+ assert.EqualValues(t, tc.want, obj2, "std json module returned unexpected")
+ })
+ }
+}
+
+func TestOptionalToYaml(t *testing.T) {
+ tests := []struct {
+ name string
+ obj *testSerializationStruct
+ want string
+ }{
+ {
+ name: "empty",
+ obj: new(testSerializationStruct),
+ want: `normal_string: ""
+normal_bool: false
+optional_two_bool: null
+optional_two_string: null
+`,
+ },
+ {
+ name: "some",
+ obj: &testSerializationStruct{
+ NormalString: "a string",
+ NormalBool: true,
+ OptBool: optional.Some(false),
+ OptString: optional.Some(""),
+ },
+ want: `normal_string: a string
+normal_bool: true
+optional_bool: false
+optional_string: ""
+optional_two_bool: null
+optional_two_string: null
+`,
+ },
+ }
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ b, err := yaml.Marshal(tc.obj)
+ require.NoError(t, err)
+ assert.EqualValues(t, tc.want, string(b), "yaml module returned unexpected")
+ })
+ }
+}
+
+func TestOptionalFromYaml(t *testing.T) {
+ tests := []struct {
+ name string
+ data string
+ want testSerializationStruct
+ }{
+ {
+ name: "empty",
+ data: ``,
+ want: testSerializationStruct{},
+ },
+ {
+ name: "empty but init",
+ data: `normal_string: ""
+normal_bool: false
+optional_bool:
+optional_two_bool:
+optional_two_string:
+`,
+ want: testSerializationStruct{},
+ },
+ {
+ name: "some",
+ data: `
+normal_string: a string
+normal_bool: true
+optional_bool: false
+optional_string: ""
+optional_two_bool: null
+optional_twostring: null
+`,
+ want: testSerializationStruct{
+ NormalString: "a string",
+ NormalBool: true,
+ OptBool: optional.Some(false),
+ OptString: optional.Some(""),
+ },
+ },
+ }
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ var obj testSerializationStruct
+ err := yaml.Unmarshal([]byte(tc.data), &obj)
+ require.NoError(t, err)
+ assert.EqualValues(t, tc.want, obj, "yaml module returned unexpected")
+ })
+ }
+}