From 182db73fd52906d5b573802fc5e760aa051d664e Mon Sep 17 00:00:00 2001 From: "m.huber" Date: Tue, 20 Feb 2024 15:03:58 +0100 Subject: [PATCH 01/11] WIP: make optional serializable --- modules/optional/serialization.go | 60 ++++++++++++++++++++++++++ modules/optional/serialization_test.go | 58 +++++++++++++++++++++++++ 2 files changed, 118 insertions(+) create mode 100644 modules/optional/serialization.go create mode 100644 modules/optional/serialization_test.go diff --git a/modules/optional/serialization.go b/modules/optional/serialization.go new file mode 100644 index 0000000000000..5b5e35eca2bab --- /dev/null +++ b/modules/optional/serialization.go @@ -0,0 +1,60 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package optional + +import ( + "encoding/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) + return nil +} + +func (o *Option[T]) MarshalJSON() ([]byte, error) { + if !o.Has() { + return nil, nil + } + + 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) + 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 +} diff --git a/modules/optional/serialization_test.go b/modules/optional/serialization_test.go new file mode 100644 index 0000000000000..bfd2e3b02cc10 --- /dev/null +++ b/modules/optional/serialization_test.go @@ -0,0 +1,58 @@ +// Copyright 2024 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package optional + +import ( + "encoding/json" + "testing" + + "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"` +} + +func TestOptionalToJson(t *testing.T) { + tests := []struct { + name string + obj *testSerializationStruct + want string + }{ + { + name: "empty", + obj: new(testSerializationStruct), + want: `{"normal_string":"","normal_bool":false}`, + }, + { + name: "empty", + obj: &testSerializationStruct{ + NormalString: "a string", + NormalBool: true, + OptBool: Some(false), + OptString: Some(""), + }, + want: `{"normal_string":"a string","normal_bool":true,"normal_bool":false,"optional_string":""}`, + }, + } + 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)) + }) + } +} + +func TestOptionalFromJson(t *testing.T) { +} + +func TestOptionalToYaml(t *testing.T) { +} + +func TestOptionalFromYaml(t *testing.T) { +} From ff3655e0f7b6bd3488672d9a084ae0e2028a2ad4 Mon Sep 17 00:00:00 2001 From: "m.huber" Date: Tue, 20 Feb 2024 15:08:40 +0100 Subject: [PATCH 02/11] next --- modules/optional/serialization_test.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/modules/optional/serialization_test.go b/modules/optional/serialization_test.go index bfd2e3b02cc10..ddba2b80fb635 100644 --- a/modules/optional/serialization_test.go +++ b/modules/optional/serialization_test.go @@ -15,6 +15,9 @@ type testSerializationStruct struct { 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"` + // TODO: fix case + // OptTwoBool Option[bool] `json:"optional_two_bool" yaml:"optional_twobool"` + // OptTwoString Option[string] `json:"optional_twostring" yaml:"optional_twostring"` } func TestOptionalToJson(t *testing.T) { @@ -29,14 +32,14 @@ func TestOptionalToJson(t *testing.T) { want: `{"normal_string":"","normal_bool":false}`, }, { - name: "empty", + name: "some", obj: &testSerializationStruct{ NormalString: "a string", NormalBool: true, OptBool: Some(false), OptString: Some(""), }, - want: `{"normal_string":"a string","normal_bool":true,"normal_bool":false,"optional_string":""}`, + want: `{"normal_string":"a string","normal_bool":true,"optional_bool":false,"optional_string":""}`, }, } for _, tc := range tests { From df0f7d33ff56ab8f7cf37a2424831afeb4a4fbdb Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 20 Feb 2024 15:54:50 +0100 Subject: [PATCH 03/11] expect the unexpected :/ --- modules/optional/serialization.go | 2 +- modules/optional/serialization_test.go | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/modules/optional/serialization.go b/modules/optional/serialization.go index 5b5e35eca2bab..f6228ab5fb411 100644 --- a/modules/optional/serialization.go +++ b/modules/optional/serialization.go @@ -4,7 +4,7 @@ package optional import ( - "encoding/json" + "code.gitea.io/gitea/modules/json" "gopkg.in/yaml.v3" ) diff --git a/modules/optional/serialization_test.go b/modules/optional/serialization_test.go index ddba2b80fb635..b34896d5f2579 100644 --- a/modules/optional/serialization_test.go +++ b/modules/optional/serialization_test.go @@ -4,9 +4,11 @@ package optional import ( - "encoding/json" + std_json "encoding/json" "testing" + "code.gitea.io/gitea/modules/json" + "github.com/stretchr/testify/assert" ) @@ -46,7 +48,11 @@ func TestOptionalToJson(t *testing.T) { 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)) + 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") }) } } From e3e93fdcf4748a4be01cf7dce9543a3223e81500 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Tue, 20 Feb 2024 16:06:07 +0100 Subject: [PATCH 04/11] more --- modules/optional/serialization.go | 2 +- modules/optional/serialization_test.go | 45 +++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/modules/optional/serialization.go b/modules/optional/serialization.go index f6228ab5fb411..7d6e369e70aa3 100644 --- a/modules/optional/serialization.go +++ b/modules/optional/serialization.go @@ -27,7 +27,7 @@ func (o *Option[T]) UnmarshalJSON(data []byte) error { func (o *Option[T]) MarshalJSON() ([]byte, error) { if !o.Has() { - return nil, nil + return []byte("null"), nil } return json.Marshal(o.Value()) diff --git a/modules/optional/serialization_test.go b/modules/optional/serialization_test.go index b34896d5f2579..42b0edb91bad9 100644 --- a/modules/optional/serialization_test.go +++ b/modules/optional/serialization_test.go @@ -17,9 +17,8 @@ type testSerializationStruct struct { 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"` - // TODO: fix case - // OptTwoBool Option[bool] `json:"optional_two_bool" yaml:"optional_twobool"` - // OptTwoString Option[string] `json:"optional_twostring" yaml:"optional_twostring"` + OptTwoBool Option[bool] `json:"optional_two_bool" yaml:"optional_twobool"` + OptTwoString Option[string] `json:"optional_twostring" yaml:"optional_twostring"` } func TestOptionalToJson(t *testing.T) { @@ -31,7 +30,7 @@ func TestOptionalToJson(t *testing.T) { { name: "empty", obj: new(testSerializationStruct), - want: `{"normal_string":"","normal_bool":false}`, + want: `{"normal_string":"","normal_bool":false,"optional_two_bool":null,"optional_twostring":null}`, }, { name: "some", @@ -41,7 +40,7 @@ func TestOptionalToJson(t *testing.T) { OptBool: Some(false), OptString: Some(""), }, - want: `{"normal_string":"a string","normal_bool":true,"optional_bool":false,"optional_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 { @@ -58,6 +57,42 @@ func TestOptionalToJson(t *testing.T) { } 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) { From a210c3d7ab3ae4069b43de25220763da763c77ba Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 22 Feb 2024 13:51:21 +0100 Subject: [PATCH 05/11] Apply suggestions from code review Co-authored-by: KN4CK3R --- modules/optional/serialization.go | 22 ++++------------------ 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/modules/optional/serialization.go b/modules/optional/serialization.go index 7d6e369e70aa3..6279aac499ca1 100644 --- a/modules/optional/serialization.go +++ b/modules/optional/serialization.go @@ -10,18 +10,11 @@ import ( ) func (o *Option[T]) UnmarshalJSON(data []byte) error { - var v []interface{} + var v *T 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) + *o = FromPtr(v) return nil } @@ -34,18 +27,11 @@ func (o *Option[T]) MarshalJSON() ([]byte, error) { } func (o *Option[T]) UnmarshalYAML(value *yaml.Node) error { - var v []interface{} + var v *T 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) + *o = FromPtr(v) return nil } From 53280267c3e7a507a430791022c1fd2bec39db19 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 22 Feb 2024 16:36:20 +0100 Subject: [PATCH 06/11] finish --- modules/optional/serialization.go | 2 +- modules/optional/serialization_test.go | 104 +++++++++++++++++++++++-- 2 files changed, 99 insertions(+), 7 deletions(-) diff --git a/modules/optional/serialization.go b/modules/optional/serialization.go index 6279aac499ca1..4635b7fb7c64e 100644 --- a/modules/optional/serialization.go +++ b/modules/optional/serialization.go @@ -35,7 +35,7 @@ func (o *Option[T]) UnmarshalYAML(value *yaml.Node) error { return nil } -func (o *Option[T]) MarshalYAML() (interface{}, error) { +func (o Option[T]) MarshalYAML() (interface{}, error) { if !o.Has() { return nil, nil } diff --git a/modules/optional/serialization_test.go b/modules/optional/serialization_test.go index 42b0edb91bad9..a140de1469a18 100644 --- a/modules/optional/serialization_test.go +++ b/modules/optional/serialization_test.go @@ -8,6 +8,7 @@ import ( "testing" "code.gitea.io/gitea/modules/json" + "gopkg.in/yaml.v3" "github.com/stretchr/testify/assert" ) @@ -17,8 +18,8 @@ type testSerializationStruct struct { 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"` + OptTwoBool Option[bool] `json:"optional_two_bool" yaml:"optional_two_bool"` + OptTwoString Option[string] `json:"optional_twostring" yaml:"optional_two_string"` } func TestOptionalToJson(t *testing.T) { @@ -26,11 +27,14 @@ func TestOptionalToJson(t *testing.T) { name string obj *testSerializationStruct want string + // TODO: report this bug to upstream, as `jsoniter.ConfigCompatibleWithStandardLibrary` claims to be std compatible + wantGitea string }{ { - name: "empty", - obj: new(testSerializationStruct), - want: `{"normal_string":"","normal_bool":false,"optional_two_bool":null,"optional_twostring":null}`, + name: "empty", + obj: new(testSerializationStruct), + want: `{"normal_string":"","normal_bool":false,"optional_two_bool":null,"optional_twostring":null}`, + wantGitea: `{"normal_string":"","normal_bool":false,"optional_bool":null,"optional_string":null,"optional_two_bool":null,"optional_twostring":null}`, }, { name: "some", @@ -47,7 +51,11 @@ func TestOptionalToJson(t *testing.T) { 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") + if tc.wantGitea != "" { + assert.EqualValues(t, tc.wantGitea, string(b), "gitea json module returned unexpected") + } else { + assert.EqualValues(t, tc.want, string(b), "gitea json module returned unexpected") + } b, err = std_json.Marshal(tc.obj) assert.NoError(t, err) @@ -96,7 +104,91 @@ func TestOptionalFromJson(t *testing.T) { } 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: Some(false), + OptString: 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: Some(false), + OptString: 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") + }) + } } From b67f8fed19f4c128398bfc9a62af527daa21b0d1 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 22 Feb 2024 16:37:51 +0100 Subject: [PATCH 07/11] finish 2 --- modules/optional/serialization.go | 2 +- modules/optional/serialization_test.go | 15 ++++----------- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/modules/optional/serialization.go b/modules/optional/serialization.go index 4635b7fb7c64e..6688e78cd1812 100644 --- a/modules/optional/serialization.go +++ b/modules/optional/serialization.go @@ -18,7 +18,7 @@ func (o *Option[T]) UnmarshalJSON(data []byte) error { return nil } -func (o *Option[T]) MarshalJSON() ([]byte, error) { +func (o Option[T]) MarshalJSON() ([]byte, error) { if !o.Has() { return []byte("null"), nil } diff --git a/modules/optional/serialization_test.go b/modules/optional/serialization_test.go index a140de1469a18..0f5daa6206ef2 100644 --- a/modules/optional/serialization_test.go +++ b/modules/optional/serialization_test.go @@ -27,14 +27,11 @@ func TestOptionalToJson(t *testing.T) { name string obj *testSerializationStruct want string - // TODO: report this bug to upstream, as `jsoniter.ConfigCompatibleWithStandardLibrary` claims to be std compatible - wantGitea string }{ { - name: "empty", - obj: new(testSerializationStruct), - want: `{"normal_string":"","normal_bool":false,"optional_two_bool":null,"optional_twostring":null}`, - wantGitea: `{"normal_string":"","normal_bool":false,"optional_bool":null,"optional_string":null,"optional_two_bool":null,"optional_twostring":null}`, + name: "empty", + obj: new(testSerializationStruct), + want: `{"normal_string":"","normal_bool":false,"optional_two_bool":null,"optional_twostring":null}`, }, { name: "some", @@ -51,11 +48,7 @@ func TestOptionalToJson(t *testing.T) { t.Run(tc.name, func(t *testing.T) { b, err := json.Marshal(tc.obj) assert.NoError(t, err) - if tc.wantGitea != "" { - assert.EqualValues(t, tc.wantGitea, string(b), "gitea json module returned unexpected") - } else { - assert.EqualValues(t, tc.want, string(b), "gitea json module returned unexpected") - } + assert.EqualValues(t, tc.want, string(b), "gitea json module returned unexpected") b, err = std_json.Marshal(tc.obj) assert.NoError(t, err) From 15f19b30b15132017e5075ff45964ec3df62917c Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 22 Feb 2024 16:41:44 +0100 Subject: [PATCH 08/11] fix lint --- modules/optional/serialization_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/optional/serialization_test.go b/modules/optional/serialization_test.go index 0f5daa6206ef2..db64ac006f163 100644 --- a/modules/optional/serialization_test.go +++ b/modules/optional/serialization_test.go @@ -8,9 +8,9 @@ import ( "testing" "code.gitea.io/gitea/modules/json" - "gopkg.in/yaml.v3" "github.com/stretchr/testify/assert" + "gopkg.in/yaml.v3" ) type testSerializationStruct struct { From b2007bc6a2eb61841611da9de118ed7f90ba9df0 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 22 Feb 2024 17:04:21 +0100 Subject: [PATCH 09/11] we want std explizite --- modules/optional/serialization_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/optional/serialization_test.go b/modules/optional/serialization_test.go index db64ac006f163..f2eefda8895f3 100644 --- a/modules/optional/serialization_test.go +++ b/modules/optional/serialization_test.go @@ -4,7 +4,7 @@ package optional import ( - std_json "encoding/json" + std_json "encoding/json" //nolint:depguard "testing" "code.gitea.io/gitea/modules/json" From 0725f8ce961aa3204757258740da11dffd832ccc Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Thu, 22 Feb 2024 17:10:37 +0100 Subject: [PATCH 10/11] use own test package for tests --- modules/optional/option_test.go | 21 ++++++++--------- modules/optional/serialization_test.go | 31 +++++++++++++------------- 2 files changed, 27 insertions(+), 25 deletions(-) diff --git a/modules/optional/option_test.go b/modules/optional/option_test.go index 7ec345b6ba0a0..1384444c1a182 100644 --- a/modules/optional/option_test.go +++ b/modules/optional/option_test.go @@ -1,48 +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" "code.gitea.io/gitea/modules/util" "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()) - opt1 := FromPtr(util.ToPointer(1)) + opt1 := optional.FromPtr(util.ToPointer(1)) 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()) } diff --git a/modules/optional/serialization_test.go b/modules/optional/serialization_test.go index f2eefda8895f3..937d4813400fa 100644 --- a/modules/optional/serialization_test.go +++ b/modules/optional/serialization_test.go @@ -1,25 +1,26 @@ // Copyright 2024 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT -package optional +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 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_two_bool"` - OptTwoString Option[string] `json:"optional_twostring" yaml:"optional_two_string"` + 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) { @@ -38,8 +39,8 @@ func TestOptionalToJson(t *testing.T) { obj: &testSerializationStruct{ NormalString: "a string", NormalBool: true, - OptBool: Some(false), - OptString: Some(""), + 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_twostring":null}`, }, @@ -76,8 +77,8 @@ func TestOptionalFromJson(t *testing.T) { want: testSerializationStruct{ NormalString: "a string", NormalBool: true, - OptBool: Some(false), - OptString: Some(""), + OptBool: optional.Some(false), + OptString: optional.Some(""), }, }, } @@ -116,8 +117,8 @@ optional_two_string: null obj: &testSerializationStruct{ NormalString: "a string", NormalBool: true, - OptBool: Some(false), - OptString: Some(""), + OptBool: optional.Some(false), + OptString: optional.Some(""), }, want: `normal_string: a string normal_bool: true @@ -171,8 +172,8 @@ optional_twostring: null want: testSerializationStruct{ NormalString: "a string", NormalBool: true, - OptBool: Some(false), - OptString: Some(""), + OptBool: optional.Some(false), + OptString: optional.Some(""), }, }, } From 78be0b3fbb45149c638c952c5938b7937dbd2fcc Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Fri, 23 Feb 2024 15:22:59 +0100 Subject: [PATCH 11/11] Update modules/optional/serialization_test.go Co-authored-by: KN4CK3R --- modules/optional/serialization_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/optional/serialization_test.go b/modules/optional/serialization_test.go index 937d4813400fa..09a4bddea02ff 100644 --- a/modules/optional/serialization_test.go +++ b/modules/optional/serialization_test.go @@ -41,6 +41,8 @@ func TestOptionalToJson(t *testing.T) { 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}`, },