-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): json serialization/deserialization and refactoring
Option values are now wrapped in a struct, replacing previous empty interface handling. Also, custom functions for handling equality have been introduced, utilizing 'github.com/google/go-cmp/cmp'. This change supports cleaner code and comparisons. In addition, option values can now be serialized/deserialized to/from JSON. This upgrade enhances the data interchangeability and compatibility of the package with JSON data formats.
- Loading branch information
1 parent
848fc55
commit a106af3
Showing
10 changed files
with
238 additions
and
52 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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,26 @@ | ||
package option | ||
|
||
import ( | ||
"encoding/json" | ||
"strings" | ||
) | ||
|
||
func (opt *Option[T]) UnmarshalJSON(data []byte) error { | ||
if strings.ToLower(string(data)) == "null" { | ||
*opt = Option[T]{None[T]()} | ||
return nil | ||
} | ||
var v T | ||
if err := json.Unmarshal(data, &v); err != nil { | ||
return err | ||
} | ||
*opt = Option[T]{Some[T](v)} | ||
return nil | ||
} | ||
|
||
func (opt Option[T]) MarshalJSON() ([]byte, error) { | ||
if opt.Empty() { | ||
return json.Marshal(nil) | ||
} | ||
return json.Marshal(opt.Get()) | ||
} |
This file contains 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,84 @@ | ||
package option | ||
|
||
import ( | ||
"github.com/google/go-cmp/cmp" | ||
"testing" | ||
) | ||
|
||
type T = int | ||
|
||
func TestOption_UnmarshalJSON(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
jsonStr string | ||
expected Option[T] | ||
expectedErr bool | ||
}{ | ||
{ | ||
name: "Test UnmarshalJSON with null value", | ||
jsonStr: "null", | ||
expected: None[T](), | ||
expectedErr: false, | ||
}, | ||
{ | ||
name: "Test UnmarshalJSON with normal value", | ||
jsonStr: "123", | ||
expected: Some[T](123), | ||
expectedErr: false, | ||
}, | ||
{ | ||
name: "Test UnmarshalJSON with invalid value", | ||
jsonStr: "-", | ||
expected: None[T](), | ||
expectedErr: true, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
var actual Option[T] | ||
err := actual.UnmarshalJSON([]byte(tc.jsonStr)) | ||
if !tc.expectedErr { | ||
if err != nil { | ||
t.Errorf("UnmarshalJSON() error = %v", err) | ||
} | ||
if !cmp.Equal(actual, tc.expected) { | ||
t.Errorf("UnmarshalJSON() = %v, want %v", actual, tc.expected) | ||
} | ||
} else if err == nil { | ||
t.Error("UnmarshalJSON() expected error, but there is no error") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestOption_MarshalJSON(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
obj Option[T] | ||
expected string | ||
}{ | ||
{ | ||
name: "Test MarshalJSON with None value", | ||
obj: None[T](), | ||
expected: "null", | ||
}, | ||
{ | ||
name: "Test MarshalJSON with Some value", | ||
obj: Some[T](123), | ||
expected: "123", | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
actual, err := tc.obj.MarshalJSON() | ||
if err != nil { | ||
t.Errorf("MarshalJSON() error = %v", err) | ||
} | ||
if string(actual) != tc.expected { | ||
t.Errorf("MarshalJSON() = %v, want %v", string(actual), tc.expected) | ||
} | ||
}) | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.