File tree 3 files changed +51
-2
lines changed
3 files changed +51
-2
lines changed Original file line number Diff line number Diff line change @@ -129,8 +129,6 @@ extension LoroMovableList: ContainerLike{
129
129
return typedResult
130
130
}
131
131
}
132
- extension LoroCounter : ContainerLike { }
133
- extension LoroUnknown : ContainerLike { }
134
132
135
133
136
134
extension LoroList : ContainerLike {
@@ -163,5 +161,31 @@ extension LoroList: ContainerLike{
163
161
}
164
162
}
165
163
164
+ extension LoroCounter : ContainerLike { }
165
+ extension LoroUnknown : ContainerLike { }
166
+
167
+ // Extension for handling nil input
168
+ // Although we extend Optional, we still need to specify the type explicitly
169
+ // e.g. `nil as String?`. This is not convenient in some scenarios.
170
+ extension LoroList {
171
+ public func insert( pos: UInt32 , v: LoroValueLike ? ) throws {
172
+ try self . insert ( pos: pos, v: v? . asLoroValue ( ) ?? . null)
173
+ }
174
+ }
175
+
176
+ extension LoroMap {
177
+ public func insert( key: String , v: LoroValueLike ? ) throws {
178
+ try self . insert ( key: key, v: v? . asLoroValue ( ) ?? . null)
179
+ }
180
+ }
166
181
182
+ extension LoroMovableList {
183
+ public func insert( pos: UInt32 , v: LoroValueLike ? ) throws {
184
+ try self . insert ( pos: pos, v: v? . asLoroValue ( ) ?? . null)
185
+ }
186
+
187
+ public func set( pos: UInt32 , v: LoroValueLike ? ) throws {
188
+ try self . set ( pos: pos, value: v? . asLoroValue ( ) ?? . null)
189
+ }
190
+ }
167
191
Original file line number Diff line number Diff line change @@ -12,6 +12,16 @@ extension LoroValue: LoroValueLike {
12
12
}
13
13
}
14
14
15
+ extension Optional : LoroValueLike where Wrapped: LoroValueLike {
16
+ public func asLoroValue( ) -> LoroValue {
17
+ if let value = self {
18
+ return value. asLoroValue ( )
19
+ } else {
20
+ return . null
21
+ }
22
+ }
23
+ }
24
+
15
25
extension Bool : LoroValueLike {
16
26
public func asLoroValue( ) -> LoroValue {
17
27
return LoroValue . bool ( value: self )
Original file line number Diff line number Diff line change @@ -14,6 +14,21 @@ final class LoroTests: XCTestCase {
14
14
sub. detach ( )
15
15
XCTAssertEqual ( num, 1 )
16
16
}
17
+
18
+ func testOptional( ) {
19
+ let doc = LoroDoc ( )
20
+ let list = doc. getList ( id: " list " )
21
+ try ! list. insert ( pos: 0 , v: nil )
22
+ let map = doc. getMap ( id: " map " )
23
+ try ! map. insert ( key: " key " , v: nil )
24
+ let movableList = doc. getMovableList ( id: " movableList " )
25
+ try ! movableList. insert ( pos: 0 , v: nil )
26
+ try ! movableList. set ( pos: 0 , v: nil )
27
+ doc. commit ( )
28
+ XCTAssertEqual ( list. get ( index: 0 ) !. asValue ( ) !, LoroValue . null)
29
+ XCTAssertEqual ( map. get ( key: " key " ) !. asValue ( ) !, LoroValue . null)
30
+ XCTAssertEqual ( movableList. get ( index: 0 ) !. asValue ( ) !, LoroValue . null)
31
+ }
17
32
18
33
func testText( ) {
19
34
let doc = LoroDoc ( )
You can’t perform that action at this time.
0 commit comments