-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Make optional.Option[T] type serializable #29282
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
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
182db73
WIP: make optional serializable
6543 ff3655e
next
6543 df0f7d3
expect the unexpected :/
6543 e3e93fd
more
6543 a210c3d
Apply suggestions from code review
6543 50d2376
Merge branch 'main' into make-optional-serialize
6543 30d5122
Merge branch 'main' into make-optional-serialize
6543 5328026
finish
6543 b67f8fe
finish 2
6543 15f19b3
fix lint
6543 b2007bc
we want std explizite
6543 0725f8c
use own test package for tests
6543 79a1be1
Merge branch 'main' into make-optional-serialize
6543 137648d
Merge branch 'main' into make-optional-serialize
6543 bae707a
Merge branch 'main' into make-optional-serialize
6543 78be0b3
Update modules/optional/serialization_test.go
6543 7300ae5
Merge branch 'main' into make-optional-serialize
6543 a03f6d1
Merge branch 'main' into make-optional-serialize
6543 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,49 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package optional | ||
package optional_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"code.gitea.io/gitea/modules/optional" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestOption(t *testing.T) { | ||
var uninitialized Option[int] | ||
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 := None[int]() | ||
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 := Some[int](1) | ||
some := optional.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()) | ||
assert.False(t, optional.FromPtr(ptr).Has()) | ||
|
||
int1 := 1 | ||
opt1 := FromPtr(&int1) | ||
opt1 := optional.FromPtr(&int1) | ||
assert.True(t, opt1.Has()) | ||
assert.Equal(t, int(1), opt1.Value()) | ||
|
||
assert.False(t, FromNonDefault("").Has()) | ||
assert.False(t, optional.FromNonDefault("").Has()) | ||
|
||
opt2 := FromNonDefault("test") | ||
opt2 := optional.FromNonDefault("test") | ||
assert.True(t, opt2.Has()) | ||
assert.Equal(t, "test", opt2.Value()) | ||
|
||
assert.False(t, FromNonDefault(0).Has()) | ||
assert.False(t, optional.FromNonDefault(0).Has()) | ||
|
||
opt3 := FromNonDefault(1) | ||
opt3 := optional.FromNonDefault(1) | ||
assert.True(t, opt3.Has()) | ||
assert.Equal(t, int(1), opt3.Value()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() (interface{}, error) { | ||
if !o.Has() { | ||
return nil, nil | ||
} | ||
|
||
value := new(yaml.Node) | ||
err := value.Encode(o.Value()) | ||
return value, err | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,190 @@ | ||
// 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" | ||
"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(""), | ||
6543 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, tc.want, string(b), "gitea json module returned unexpected") | ||
|
||
b, err = std_json.Marshal(tc.obj) | ||
assert.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) | ||
assert.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) | ||
assert.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) | ||
assert.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) | ||
assert.NoError(t, err) | ||
assert.EqualValues(t, tc.want, obj, "yaml module returned unexpected") | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.