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

Make optional.Option[T] type serializable #29282

Merged
merged 18 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
60 changes: 60 additions & 0 deletions modules/optional/serialization.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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 []interface{}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
if v == nil {
return nil
}
var someValue T
if err := json.Unmarshal(data, &someValue); err != nil {
return err
}
*o = Some[T](someValue)
6543 marked this conversation as resolved.
Show resolved Hide resolved
return nil
}

func (o *Option[T]) MarshalJSON() ([]byte, error) {
if !o.Has() {
return []byte("null"), nil
6543 marked this conversation as resolved.
Show resolved Hide resolved
}

return json.Marshal(o.Value())
}

func (o *Option[T]) UnmarshalYAML(value *yaml.Node) error {
var v []interface{}
if err := value.Decode(&v); err != nil {
return err
}
if v == nil {
return nil
}
var someValue T
if err := value.Decode(&someValue); err != nil {
return err
}
*o = Some[T](someValue)
6543 marked this conversation as resolved.
Show resolved Hide resolved
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
}
102 changes: 102 additions & 0 deletions modules/optional/serialization_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package optional

import (
std_json "encoding/json"

Check failure on line 7 in modules/optional/serialization_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

import 'encoding/json' is not allowed from list 'main': use gitea's modules/json instead of encoding/json (depguard)

Check failure on line 7 in modules/optional/serialization_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

import 'encoding/json' is not allowed from list 'main': use gitea's modules/json instead of encoding/json (depguard)

Check failure on line 7 in modules/optional/serialization_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

import 'encoding/json' is not allowed from list 'main': use gitea's modules/json instead of encoding/json (depguard)
"testing"

"code.gitea.io/gitea/modules/json"

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

type testSerializationStruct struct {
NormalString string `json:"normal_string" yaml:"normal_string"`
NormalBool bool `json:"normal_bool" yaml:"normal_bool"`
OptBool Option[bool] `json:"optional_bool,omitempty" yaml:"optional_bool,omitempty"`
OptString Option[string] `json:"optional_string,omitempty" yaml:"optional_string,omitempty"`
OptTwoBool Option[bool] `json:"optional_two_bool" yaml:"optional_twobool"`
OptTwoString Option[string] `json:"optional_twostring" yaml:"optional_twostring"`
}

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: Some(false),
OptString: Some(""),
},
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: Some(false),
OptString: 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) {
}

func TestOptionalFromYaml(t *testing.T) {
}
Loading