@@ -157,22 +157,36 @@ class LookupInResult
157157 # @return [Integer] holds the CAS value of the fetched document
158158 attr_accessor :cas
159159
160- # Decodes the content at the given index
160+ # Decodes the content at the given index (or path)
161161 #
162- # @param [Integer] index the index of the subdocument value to decode
162+ # @param [Integer, String] path_or_index the index (or path) of the subdocument value to decode
163163 #
164164 # @return [Object] the decoded
165- def content ( index , transcoder = self . transcoder )
166- transcoder . decode ( get_field_at_index ( index ) . value , :json )
165+ def content ( path_or_index , transcoder = self . transcoder )
166+ field = get_field_at_index ( path_or_index )
167+ raise Error ::PathNotFound , "Path is not found: #{ path_or_index } " unless field . exists
168+
169+ transcoder . decode ( field . value , :json )
167170 end
168171
169172 # Allows to check if a value at the given index exists
170173 #
171- # @param [Integer] index the index of the subdocument value to check
174+ # @param [Integer, String] path_or_index the index (or path) of the subdocument value to check
172175 #
173176 # @return [Boolean] true if a value is present at the index, false otherwise
174- def exists? ( index )
175- !encoded [ index ] . nil? && encoded [ index ] . exists
177+ def exists? ( path_or_index )
178+ field =
179+ case path_or_index
180+ when String
181+ encoded . find { |f | f . path == path_or_index }
182+ else
183+ return false unless path_or_index >= 0 && path_or_index < encoded . size
184+
185+ encoded [ path_or_index ]
186+ end
187+ return false unless field
188+
189+ field . exists
176190 end
177191
178192 # @return [Array<SubDocumentField>] holds the encoded subdocument responses
@@ -201,29 +215,27 @@ def deleted?
201215
202216 private
203217
204- def get_field_at_index ( index )
205- raise Error ::PathInvalid , "Index is out of bounds: #{ index } " unless index >= 0 && index < encoded . size
206-
207- field = encoded [ index ]
208- raise field . error unless field . success?
218+ def get_field_at_index ( path_or_index )
219+ case path_or_index
220+ when String
221+ encoded . find { |field | field . path == path_or_index } or raise Error ::PathInvalid , "Path is not found: #{ path_or_index } "
222+ else
223+ raise Error ::PathInvalid , "Index is out of bounds: #{ path_or_index } " unless path_or_index >= 0 && path_or_index < encoded . size
209224
210- field
225+ encoded [ path_or_index ]
226+ end
211227 end
212228 end
213229
214230 class MutateInResult < MutationResult
215231 # Decodes the content at the given index
216232 #
217- # @param [Integer] index the index of the subdocument value to decode
233+ # @param [Integer, String] path_or_index the index (or path) of the subdocument value to decode
218234 #
219235 # @return [Object] the decoded
220- def content ( index , transcoder = self . transcoder )
221- field = get_field_at_index ( index )
222- if field . type == :counter
223- field . value
224- else
225- transcoder . decode ( field . value , :json )
226- end
236+ def content ( path_or_index , transcoder = self . transcoder )
237+ field = get_field_at_index ( path_or_index )
238+ transcoder . decode ( field . value , :json )
227239 end
228240
229241 # @yieldparam [MutateInResult] self
@@ -232,24 +244,10 @@ def initialize
232244 yield self if block_given?
233245 end
234246
235- # @api private
236- def success?
237- first_error_index . nil?
238- end
239-
240- # @api private
241- def first_error
242- encoded [ first_error_index ] . error unless success?
243- end
244-
245247 # @return [Array<SubDocumentField>] holds the encoded subdocument responses
246248 # @api private
247249 attr_accessor :encoded
248250
249- # @return [Integer, nil] index of first operation entry that generated an error
250- # @api private
251- attr_accessor :first_error_index
252-
253251 # @return [JsonTranscoder] The default transcoder which should be used
254252 attr_accessor :transcoder
255253
@@ -267,13 +265,15 @@ def deleted?
267265
268266 private
269267
270- def get_field_at_index ( index )
271- raise Error ::PathInvalid , "Index is out of bounds: #{ index } " unless index >= 0 && index < encoded . size
272-
273- field = encoded [ index ]
274- raise field . error unless field . success?
268+ def get_field_at_index ( path_or_index )
269+ case path_or_index
270+ when String
271+ encoded . find { |field | field . path == path_or_index } or raise Error ::PathInvalid , "Path is not found: #{ path_or_index } "
272+ else
273+ raise Error ::PathInvalid , "Index is out of bounds: #{ path_or_index } " unless path_or_index >= 0 && path_or_index < encoded . size
275274
276- field
275+ encoded [ path_or_index ]
276+ end
277277 end
278278 end
279279
@@ -291,64 +291,10 @@ class SubDocumentField
291291 # @return [String] path
292292 attr_accessor :path
293293
294- # Operation type
295- #
296- # * +:set_doc+
297- # * +:counter+
298- # * +:replace+
299- # * +:dict_add+
300- # * +:dict_upsert+
301- # * +:array_push_first+
302- # * +:array_push_last+
303- # * +:array_add_unique+
304- # * +:array_insert+
305- # * +:delete+
306- # * +:get+
307- # * +:exists+
308- # * +:count+
309- # * +:get_doc+
310- #
311- # @return [Symbol]
312- attr_accessor :type
313-
314- # Status of the subdocument path operation.
315- #
316- # [+:success+] Indicates a successful response in general.
317- # [+:path_not_found+] The provided path does not exist in the document
318- # [+:path_mismatch+] One of path components treats a non-dictionary as a dictionary, or a non-array as an array, or value the path
319- # points to is not a number
320- # [+:path_invalid+] The path's syntax was incorrect
321- # [+:path_too_big+] The path provided is too large: either the string is too long, or it contains too many components
322- # [+:value_cannot_insert+] The value provided will invalidate the JSON if inserted
323- # [+:doc_not_json+] The existing document is not valid JSON
324- # [+:num_range+] The existing number is out of the valid range for arithmetic operations
325- # [+:delta_invalid+] The operation would result in a number outside the valid range
326- # [+:path_exists+] The requested operation requires the path to not already exist, but it exists
327- # [+:value_too_deep+] Inserting the value would cause the document to be too deep
328- # [+:invalid_combo+] An invalid combination of commands was specified
329- # [+:xattr_invalid_flag_combo+] An invalid combination of operations, using macros when not using extended attributes
330- # [+:xattr_invalid_key_combo+] Only single xattr key may be accessed at the same time
331- # [+:xattr_unknown_macro+] The server has no knowledge of the requested macro
332- # [+:xattr_unknown_vattr+] Unknown virtual attribute.
333- # [+:xattr_cannot_modify_vattr+] Cannot modify this virtual attribute.
334- # [+:unknown+] Unknown error.
335- #
336- # @return [Symbol]
337- attr_accessor :status
338-
339- # @return [nil, Exception]
340- attr_accessor :error
341-
342294 # @yieldparam [SubDocumentField] self
343295 def initialize
344- @status = :unknown
345296 yield self if block_given?
346297 end
347-
348- # @return [Boolean] true if the path does not have associated error
349- def success?
350- status == :success
351- end
352298 end
353299 end
354300end
0 commit comments