File tree 2 files changed +23
-9
lines changed
2 files changed +23
-9
lines changed Original file line number Diff line number Diff line change @@ -56,6 +56,14 @@ func None[T any]() Option[T] {
56
56
return map [bool ]T {}
57
57
}
58
58
59
+ // FromNillable converts a nillable value to an Option.
60
+ func FromNillable [T any ](v * T ) Option [T ] {
61
+ if v == nil {
62
+ return None [T ]()
63
+ }
64
+ return Some (* v )
65
+ }
66
+
59
67
// IsSome returns whether the Option has a value or not.
60
68
func (o Option [T ]) IsSome () bool {
61
69
return len (o ) != 0
@@ -234,11 +242,3 @@ func (o *Option[T]) UnmarshalJSON(data []byte) error {
234
242
* o = Some (v )
235
243
return nil
236
244
}
237
-
238
- // FromNillable converts a nillable value to an Option.
239
- func (o Option [T ]) FromNillable (v * T ) Option [T ] {
240
- if v == nil {
241
- return None [T ]()
242
- }
243
- return Some (* v )
244
- }
Original file line number Diff line number Diff line change 8
8
"time"
9
9
10
10
"github.com/stretchr/testify/assert"
11
- "github.com/tapp-ai/go-optional-v2"
11
+ optionalv2 "github.com/tapp-ai/go-optional-v2"
12
12
)
13
13
14
14
// Custom type for testing fmt.Stringer interface
@@ -34,6 +34,20 @@ func TestOption(t *testing.T) {
34
34
assert .True (t , optNone .IsNone ())
35
35
})
36
36
37
+ // Test FromNillable method
38
+ t .Run ("FromNillable" , func (t * testing.T ) {
39
+ var value * int = nil
40
+ opt := optionalv2 .FromNillable (value )
41
+ assert .False (t , opt .IsSome ())
42
+ assert .True (t , opt .IsNone ())
43
+
44
+ value = new (int )
45
+ * value = 42
46
+ opt = optionalv2 .FromNillable (value )
47
+ assert .True (t , opt .IsSome ())
48
+ assert .Equal (t , 42 , opt .Unwrap ())
49
+ })
50
+
37
51
// Test Unwrap and UnwrapAsPtr methods
38
52
t .Run ("UnwrapMethods" , func (t * testing.T ) {
39
53
optSome := optionalv2 .Some ("Hello" )
You can’t perform that action at this time.
0 commit comments