@@ -73,6 +73,16 @@ func TestDefaultPanics(t *testing.T) {
7373 _ = None [WithEnabled ]()
7474 })
7575
76+ assert .Panics (t , func () {
77+ opt := None [int ]()
78+ _ = opt .GetOrInsertDefault ()
79+ })
80+
81+ assert .Panics (t , func () {
82+ var opt Optional [WithEnabled ]
83+ _ = opt .GetOrInsertDefault ()
84+ })
85+
7686 assert .NotPanics (t , func () {
7787 _ = Default (NoMapstructure {})
7888 })
@@ -100,26 +110,126 @@ func TestNoneZeroVal(t *testing.T) {
100110 var none Optional [Sub ]
101111 require .False (t , none .HasValue ())
102112 require .Nil (t , none .Get ())
113+
114+ var zeroVal Sub
115+ ret := none .GetOrInsertDefault ()
116+ require .True (t , none .HasValue ())
117+ assert .Equal (t , & zeroVal , ret )
103118}
104119
105120func TestNone (t * testing.T ) {
106121 none := None [Sub ]()
107122 require .False (t , none .HasValue ())
108123 require .Nil (t , none .Get ())
124+
125+ var zeroVal Sub
126+ ret := none .GetOrInsertDefault ()
127+ require .True (t , none .HasValue ())
128+ assert .Equal (t , & zeroVal , ret )
129+ }
130+
131+ func ExampleNone () {
132+ type Person struct {
133+ Name string
134+ Age int
135+ }
136+
137+ opt := None [Person ]()
138+
139+ // A None has no value.
140+ fmt .Println (opt .HasValue ())
141+ fmt .Println (opt .Get ())
142+
143+ // GetOrInsertDefault places the zero value
144+ // and returns it, allowing you to modify it.
145+ opt .GetOrInsertDefault ().Name = "John Doe"
146+ fmt .Println (opt .HasValue ())
147+ fmt .Println (opt .Get ())
148+
149+ // Output:
150+ // false
151+ // <nil>
152+ // true
153+ // &{John Doe 0}
109154}
110155
111156func TestSome (t * testing.T ) {
112157 some := Some (Sub {
113158 Foo : "foobar" ,
114159 })
115160 require .True (t , some .HasValue ())
116- assert .Equal (t , "foobar" , some .Get ().Foo )
161+ retGet := some .Get ()
162+ assert .Equal (t , "foobar" , retGet .Foo )
163+
164+ retGetOrInsertDefault := some .GetOrInsertDefault ()
165+ require .True (t , some .HasValue ())
166+ assert .Equal (t , retGet , retGetOrInsertDefault )
167+ }
168+
169+ func ExampleSome () {
170+ type Person struct {
171+ Name string
172+ Age int
173+ }
174+
175+ opt := Some (Person {
176+ Name : "John Doe" ,
177+ Age : 42 ,
178+ })
179+
180+ // A Some has a value.
181+ fmt .Println (opt .HasValue ())
182+ fmt .Println (opt .Get ())
183+
184+ // GetOrInsertDefault only returns a reference
185+ // to the inner value without modifying it.
186+ opt .GetOrInsertDefault ().Name = "Jane Doe"
187+ fmt .Println (opt .HasValue ())
188+ fmt .Println (opt .Get ())
189+
190+ // Output:
191+ // true
192+ // &{John Doe 42}
193+ // true
194+ // &{Jane Doe 42}
117195}
118196
119197func TestDefault (t * testing.T ) {
120- defaultSub := Default (& subDefault )
198+ defaultSub := Default (subDefault )
121199 require .False (t , defaultSub .HasValue ())
122200 require .Nil (t , defaultSub .Get ())
201+
202+ ret := defaultSub .GetOrInsertDefault ()
203+ require .True (t , defaultSub .HasValue ())
204+ assert .Equal (t , & subDefault , ret )
205+ }
206+
207+ func ExampleDefault () {
208+ type Person struct {
209+ Name string
210+ Age int
211+ }
212+
213+ opt := Default (Person {
214+ Name : "John Doe" ,
215+ Age : 42 ,
216+ })
217+
218+ // A Default has no value.
219+ fmt .Println (opt .HasValue ())
220+ fmt .Println (opt .Get ())
221+
222+ // GetOrInsertDefault places the default value
223+ // and returns it, allowing you to modify it.
224+ opt .GetOrInsertDefault ().Age = 38
225+ fmt .Println (opt .HasValue ())
226+ fmt .Println (opt .Get ())
227+
228+ // Output:
229+ // false
230+ // <nil>
231+ // true
232+ // &{John Doe 38}
123233}
124234
125235func TestUnmarshalOptional (t * testing.T ) {
0 commit comments