@@ -30,93 +30,136 @@ extension ContainerLike{
30
30
31
31
extension LoroText : ContainerLike { }
32
32
extension LoroMap : ContainerLike {
33
- public func insertContainer( key: String , child: ContainerLike ) throws -> ContainerLike {
33
+ public func insertContainer< T: ContainerLike > ( key: String , child: T ) throws -> T {
34
+ let result : ContainerLike
34
35
if let list = child as? LoroList {
35
- return try self . insertListContainer ( key: key, child: list)
36
+ result = try self . insertListContainer ( key: key, child: list)
36
37
} else if let map = child as? LoroMap {
37
- return try self . insertMapContainer ( key: key, child: map)
38
+ result = try self . insertMapContainer ( key: key, child: map)
38
39
} else if let text = child as? LoroText {
39
- return try self . insertTextContainer ( key: key, child: text)
40
+ result = try self . insertTextContainer ( key: key, child: text)
40
41
} else if let tree = child as? LoroTree {
41
- return try self . insertTreeContainer ( key: key, child: tree)
42
+ result = try self . insertTreeContainer ( key: key, child: tree)
42
43
} else if let list = child as? LoroMovableList {
43
- return try self . insertMovableListContainer ( key: key, child: list)
44
+ result = try self . insertMovableListContainer ( key: key, child: list)
44
45
} else if let counter = child as? LoroCounter {
45
- return try self . insertCounterContainer ( key: key, child: counter)
46
+ result = try self . insertCounterContainer ( key: key, child: counter)
46
47
} else {
47
48
fatalError ( )
48
49
}
50
+ guard let typedResult = result as? T else {
51
+ fatalError ( " Type mismatch: expected \( T . self) , got \( type ( of: result) ) " )
52
+ }
53
+ return typedResult
54
+ }
55
+
56
+ public func getOrCreateContainer< T: ContainerLike > ( key: String , child: T ) throws -> T {
57
+ let result : ContainerLike
58
+ if let list = child as? LoroList {
59
+ result = try self . getOrCreateListContainer ( key: key, child: list)
60
+ } else if let map = child as? LoroMap {
61
+ result = try self . getOrCreateMapContainer ( key: key, child: map)
62
+ } else if let text = child as? LoroText {
63
+ result = try self . getOrCreateTextContainer ( key: key, child: text)
64
+ } else if let tree = child as? LoroTree {
65
+ result = try self . getOrCreateTreeContainer ( key: key, child: tree)
66
+ } else if let list = child as? LoroMovableList {
67
+ result = try self . getOrCreateMovableListContainer ( key: key, child: list)
68
+ } else if let counter = child as? LoroCounter {
69
+ result = try self . getOrCreateCounterContainer ( key: key, child: counter)
70
+ } else {
71
+ fatalError ( )
72
+ }
73
+ guard let typedResult = result as? T else {
74
+ fatalError ( " Type mismatch: expected \( T . self) , got \( type ( of: result) ) " )
75
+ }
76
+ return typedResult
49
77
}
50
78
}
51
79
extension LoroTree : ContainerLike { }
52
80
extension LoroMovableList : ContainerLike {
53
- public func pushContainer( child: ContainerLike ) throws -> ContainerLike {
81
+ public func pushContainer< T : ContainerLike > ( child: T ) throws -> T {
54
82
let idx = self . len ( )
55
83
return try self . insertContainer ( pos: idx, child: child)
56
84
}
57
85
58
- public func insertContainer( pos: UInt32 , child: ContainerLike ) throws -> ContainerLike {
86
+ public func insertContainer< T: ContainerLike > ( pos: UInt32 , child: T ) throws -> T {
87
+ let result : ContainerLike
59
88
if let list = child as? LoroList {
60
- return try self . insertListContainer ( pos: pos, child: list)
89
+ result = try self . insertListContainer ( pos: pos, child: list)
61
90
} else if let map = child as? LoroMap {
62
- return try self . insertMapContainer ( pos: pos, child: map)
91
+ result = try self . insertMapContainer ( pos: pos, child: map)
63
92
} else if let text = child as? LoroText {
64
- return try self . insertTextContainer ( pos: pos, child: text)
93
+ result = try self . insertTextContainer ( pos: pos, child: text)
65
94
} else if let tree = child as? LoroTree {
66
- return try self . insertTreeContainer ( pos: pos, child: tree)
95
+ result = try self . insertTreeContainer ( pos: pos, child: tree)
67
96
} else if let list = child as? LoroMovableList {
68
- return try self . insertMovableListContainer ( pos: pos, child: list)
97
+ result = try self . insertMovableListContainer ( pos: pos, child: list)
69
98
} else if let counter = child as? LoroCounter {
70
- return try self . insertCounterContainer ( pos: pos, child: counter)
99
+ result = try self . insertCounterContainer ( pos: pos, child: counter)
71
100
} else {
72
101
fatalError ( )
73
102
}
103
+ guard let typedResult = result as? T else {
104
+ fatalError ( " Type mismatch: expected \( T . self) , got \( type ( of: result) ) " )
105
+ }
106
+ return typedResult
74
107
}
75
108
76
- public func setContainer( pos: UInt32 , child: ContainerLike ) throws -> ContainerLike {
109
+ public func setContainer< T: ContainerLike > ( pos: UInt32 , child: T ) throws -> T {
110
+ let result : ContainerLike
77
111
if let list = child as? LoroList {
78
- return try self . setListContainer ( pos: pos, child: list)
112
+ result = try self . setListContainer ( pos: pos, child: list)
79
113
} else if let map = child as? LoroMap {
80
- return try self . setMapContainer ( pos: pos, child: map)
114
+ result = try self . setMapContainer ( pos: pos, child: map)
81
115
} else if let text = child as? LoroText {
82
- return try self . setTextContainer ( pos: pos, child: text)
116
+ result = try self . setTextContainer ( pos: pos, child: text)
83
117
} else if let tree = child as? LoroTree {
84
- return try self . setTreeContainer ( pos: pos, child: tree)
118
+ result = try self . setTreeContainer ( pos: pos, child: tree)
85
119
} else if let list = child as? LoroMovableList {
86
- return try self . setMovableListContainer ( pos: pos, child: list)
120
+ result = try self . setMovableListContainer ( pos: pos, child: list)
87
121
} else if let counter = child as? LoroCounter {
88
- return try self . setCounterContainer ( pos: pos, child: counter)
122
+ result = try self . setCounterContainer ( pos: pos, child: counter)
89
123
} else {
90
124
fatalError ( )
91
125
}
126
+ guard let typedResult = result as? T else {
127
+ fatalError ( " Type mismatch: expected \( T . self) , got \( type ( of: result) ) " )
128
+ }
129
+ return typedResult
92
130
}
93
131
}
94
132
extension LoroCounter : ContainerLike { }
95
133
extension LoroUnknown : ContainerLike { }
96
134
97
135
98
136
extension LoroList : ContainerLike {
99
- public func pushContainer( child: ContainerLike ) throws -> ContainerLike {
137
+ public func pushContainer< T : ContainerLike > ( child: T ) throws -> T {
100
138
let idx = self . len ( )
101
139
return try self . insertContainer ( pos: idx, child: child)
102
140
}
103
141
104
- public func insertContainer( pos: UInt32 , child: ContainerLike ) throws -> ContainerLike {
142
+ public func insertContainer< T: ContainerLike > ( pos: UInt32 , child: T ) throws -> T {
143
+ let result : ContainerLike
105
144
if let list = child as? LoroList {
106
- return try self . insertListContainer ( pos: pos, child: list)
145
+ result = try self . insertListContainer ( pos: pos, child: list)
107
146
} else if let map = child as? LoroMap {
108
- return try self . insertMapContainer ( pos: pos, child: map)
147
+ result = try self . insertMapContainer ( pos: pos, child: map)
109
148
} else if let text = child as? LoroText {
110
- return try self . insertTextContainer ( pos: pos, child: text)
149
+ result = try self . insertTextContainer ( pos: pos, child: text)
111
150
} else if let tree = child as? LoroTree {
112
- return try self . insertTreeContainer ( pos: pos, child: tree)
151
+ result = try self . insertTreeContainer ( pos: pos, child: tree)
113
152
} else if let list = child as? LoroMovableList {
114
- return try self . insertMovableListContainer ( pos: pos, child: list)
153
+ result = try self . insertMovableListContainer ( pos: pos, child: list)
115
154
} else if let counter = child as? LoroCounter {
116
- return try self . insertCounterContainer ( pos: pos, child: counter)
155
+ result = try self . insertCounterContainer ( pos: pos, child: counter)
117
156
} else {
118
157
fatalError ( )
119
158
}
159
+ guard let typedResult = result as? T else {
160
+ fatalError ( " Type mismatch: expected \( T . self) , got \( type ( of: result) ) " )
161
+ }
162
+ return typedResult
120
163
}
121
164
}
122
165
0 commit comments