示例#1
0
        public void ArrayInsert_Multiple_For_Xattr_Sets_Correct_Flag(SubdocMutateFlags flags, byte expected)
        {
            var mockResult = new Mock <IDocumentFragment <dynamic> >();

            var mockedInvoker = new Mock <ISubdocInvoker>();

            mockedInvoker.Setup(x => x.Invoke(It.IsAny <MutateInBuilder <dynamic> >()))
            .Returns(mockResult.Object);

            var mutateBuilder = new MutateInBuilder <dynamic>(mockedInvoker.Object, () => new DefaultSerializer(), "mykey");

            var value  = new object[] { 1, 2, 3 };
            var result = mutateBuilder.ArrayInsert("path", flags, value)
                         .Execute();

            Assert.AreSame(mockResult.Object, result);
            mockedInvoker.Verify(
                invoker => invoker.Invoke(It.Is <MutateInBuilder <dynamic> >(
                                              builder =>
                                              builder.FirstSpec().OpCode == OperationCode.SubArrayInsert &&
                                              builder.FirstSpec().Path == "path" &&
                                              builder.FirstSpec().Flags == expected &&
                                              builder.FirstSpec().Value == value
                                              )
                                          ), Times.Once
                );
        }
        private static byte GetFlagsValue(SubdocMutateFlags flags)
        {
            if (flags.HasFlag(SubdocMutateFlags.ExpandMacro) && !flags.HasFlag(SubdocMutateFlags.XattrPath))
            {
                flags |= SubdocMutateFlags.XattrPath;
            }

            return((byte)flags);
        }
        /// <summary>
        /// Inserts a value to the beginning of an array in a JSON document at a given path.
        /// </summary>
        /// <param name="path">A string (N1QL syntax) used to specify a location within the document.</param>
        /// <param name="value">An array value.</param>
        /// <param name="flags">The subdocument flags. Defaults to <see cref="F:SubdocLookupFlags.CreatePath"/>.</param>
        /// <returns>
        /// An <see cref="T:Couchbase.Core.IMutateInBuilder`1" /> reference for chaining operations.
        /// </returns>
        public IMutateInBuilder <TDocument> ArrayPrepend(string path, object value, SubdocMutateFlags flags)
        {
            _commands.Enqueue(new OperationSpec
            {
                OpCode = OperationCode.SubArrayPushFirst,
                Path   = path,
                Value  = value,
                Flags  = GetFlagsValue(flags)
            });

            return(this);
        }
        /// <summary>
        /// Inserts one or more values to the beginning of an array in a JSON document at a given path.
        /// </summary>
        /// <param name="path">A string (N1QL syntax) used to specify a location within the document.</param>
        /// <param name="flags">The subdocument flags. Defaults to <see cref="F:SubdocLookupFlags.None"/>.</param>
        /// <param name="values">One or more values.</param>
        /// <returns>
        /// An <see cref="T:Couchbase.Core.IMutateInBuilder`1" /> reference for chaining operations.
        /// </returns>
        public IMutateInBuilder <TDocument> ArrayPrepend(string path, SubdocMutateFlags flags = SubdocMutateFlags.None, params object[] values)
        {
            _commands.Enqueue(new OperationSpec
            {
                OpCode         = OperationCode.SubArrayPushFirst,
                Path           = path,
                Value          = values,
                Flags          = GetFlagsValue(flags),
                RemoveBrackets = true
            });

            return(this);
        }
        /// <summary>
        /// Removes an element or value from a JSON document at a given path.
        /// </summary>
        /// <param name="path">A string (N1QL syntax) used to specify a location within the document.</param>
        /// <param name="flags">The subdocument flags. Defaults to <see cref="F:SubdocLookupFlags.None"/>.</param>
        /// <returns>
        /// An <see cref="T:Couchbase.Core.IMutateInBuilder`1" /> reference for chaining operations.
        /// </returns>
        /// <exception cref="System.ArgumentException">Path cannot be empty for an Remove.</exception>
        public IMutateInBuilder <TDocument> Remove(string path, SubdocMutateFlags flags = SubdocMutateFlags.None)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentException("Path cannot be empty for an Remove.");
            }

            _commands.Enqueue(new OperationSpec
            {
                OpCode = OperationCode.SubDelete,
                Path   = path,
                Flags  = GetFlagsValue(flags)
            });

            return(this);
        }
        /// <summary>
        /// Performs an arithmetic operation on a numeric value in a document.
        /// </summary>
        /// <param name="path">A string (N1QL syntax) used to specify a location within the document.</param>
        /// <param name="delta">The value to increment or decrement the original value by.</param>
        /// <param name="flags">The subdocument flags. Defaults to <see cref="F:SubdocLookupFlags.CreatePath"/>.</param>
        /// <returns>
        /// An <see cref="T:Couchbase.Core.IMutateInBuilder`1" /> reference for chaining operations.
        /// </returns>
        /// <exception cref="System.ArgumentException">Path cannot be empty for a Counter.</exception>
        public IMutateInBuilder <TDocument> Counter(string path, long delta, SubdocMutateFlags flags = SubdocMutateFlags.CreatePath)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentException("Path cannot be empty for a Counter.");
            }

            _commands.Enqueue(new OperationSpec
            {
                OpCode = OperationCode.SubCounter,
                Path   = path,
                Value  = delta,
                Flags  = GetFlagsValue(flags)
            });

            return(this);
        }
        /// <summary>
        /// Inserts a value at a given position within an array. The position is indicated as part of the path.
        /// </summary>
        /// <param name="path">A string (N1QL syntax) used to specify a location within the document.</param>
        /// <param name="value">A value.</param>
        /// <param name="flags">The subdocument flags. Defaults to <see cref="F:SubdocLookupFlags.None"/>.</param>
        /// <returns>
        /// An <see cref="T:Couchbase.Core.IMutateInBuilder`1" /> reference for chaining operations.
        /// </returns>
        /// <exception cref="System.ArgumentException">Path cannot be empty for an ArrayInsert.</exception>
        public IMutateInBuilder <TDocument> ArrayInsert(string path, object value, SubdocMutateFlags flags = SubdocMutateFlags.None)
        {
            if (string.IsNullOrWhiteSpace(path))
            {
                throw new ArgumentException("Path cannot be empty for an ArrayInsert.");
            }

            _commands.Enqueue(new OperationSpec
            {
                OpCode = OperationCode.SubArrayInsert,
                Path   = path,
                Value  = value,
                Flags  = GetFlagsValue(flags)
            });

            return(this);
        }