2.0 DP3 IViewQuery -> Passing values using Key(), Keys(), StarKey(),


#1

Hi,

I’ve seen the values are directly passed to the query string and must be encoded as Json value. IMHO this is a little bit unfriendly to use. Won’t something like

public IViewQuery StartKey(object startKey)
{
_startKey = Uri.EscapeDataString(JsonConvert.SerializeObject(startKey));
return this;
}

within ViewQuery.cs make life much easier?

Maybe I am missing something here but I would like to propose the following change to ViewQuery.cs, which automatically creates a json representation of the values provided and does the uri encoding:

/// <summary>
        /// Returns the raw REST URI which can be executed in a browser or using curl.
        /// </summary>
        /// <returns></returns>
        public Uri RawUri()
        {
            var sb = new StringBuilder();
            sb.Append(_baseUri);

            if (!_baseUri.EndsWith(ForwardSlash))
            {
                sb.Append(ForwardSlash);
            }

            if (!string.IsNullOrWhiteSpace(_bucketName) && !_baseUri.Contains(_bucketName))
            {
                sb.Append(_bucketName);
                sb.Append(ForwardSlash);
            }
            sb.Append(Design);
            sb.Append(ForwardSlash);

            if (_development.HasValue && _development.Value)
            {
                sb.Append(DevelopmentViewPrefix);
            }

            sb.Append(_designDoc);
            sb.Append(ForwardSlash);
            sb.Append(ViewMethod);
            sb.Append(ForwardSlash);
            sb.Append(_viewName);
            sb.Append(QueryOperator);

            if (_staleState != StaleState.None)
            {
                sb.AppendFormat(QueryArgPattern, QueryArguments.Stale, _staleState.ToLowerString());
            }
            if (_descending.HasValue)
            {
                sb.AppendFormat(QueryArgPattern, QueryArguments.Descending, _descending.ToLowerString());
            }
            if (_continueOnError.HasValue)
            {
                sb.AppendFormat(QueryArgPattern, QueryArguments.OnError, _continueOnError.Value ? "continue" : "stop");
            }
            if (_endDocId != null)
            {
				sb.AppendFormat(QueryArgPattern, QueryArguments.EndKeyDocId, Uri.EscapeDataString(JsonConvert.SerializeObject(_endDocId)));
            }
            if (_endKey != null)
            {
                sb.AppendFormat(QueryArgPattern, QueryArguments.EndKey, Uri.EscapeDataString(JsonConvert.SerializeObject(_endKey)));
            }
            if (_fullSet.HasValue)
            {
                sb.AppendFormat(QueryArgPattern, QueryArguments.FullSet, _fullSet.ToLowerString());
            }
            if (_group.HasValue)
            {
                sb.AppendFormat(QueryArgPattern, QueryArguments.Group, _group.ToLowerString());
            }
            if (_groupLevel.HasValue)
            {
                sb.AppendFormat(QueryArgPattern, QueryArguments.GroupLevel, _groupLevel);
            }
            if (_inclusiveEnd.HasValue)
            {
                sb.AppendFormat(QueryArgPattern, QueryArguments.InclusiveEnd, _inclusiveEnd.ToLowerString());
            }
            if (_key != null)
            {
                sb.AppendFormat(QueryArgPattern, QueryArguments.Key, Uri.EscapeDataString(JsonConvert.SerializeObject(_key)));
            }
            if (_keys != null)
            {
                sb.AppendFormat(QueryArgPattern, QueryArguments.Keys, Uri.EscapeDataString(JsonConvert.SerializeObject(_keys)));
            }
            if (_limit.HasValue)
            {
                sb.AppendFormat(QueryArgPattern, QueryArguments.Limit, _limit);
            }
            if (_reduce.HasValue)
            {
                sb.AppendFormat(QueryArgPattern, QueryArguments.Reduce, _reduce.ToLowerString());
            }
            if (_startKey != null)
            {
                sb.AppendFormat(QueryArgPattern, QueryArguments.StartKey, Uri.EscapeDataString(JsonConvert.SerializeObject(_startKey)));
            }
            if (_startKeyDocId != null)
            {
                sb.AppendFormat(QueryArgPattern, QueryArguments.StartKeyDocId, Uri.EscapeDataString(JsonConvert.SerializeObject(_startKeyDocId)));
            }
            if (_skipCount.HasValue)
            {
                sb.AppendFormat(QueryArgPattern, QueryArguments.Skip, _skipCount);
            }
            if (_connectionTimeout.HasValue)
            {
                sb.AppendFormat(QueryArgPattern, QueryArguments.ConnectionTimeout, _connectionTimeout);
            }

            var requestUri = sb.ToString().TrimEnd('&');
            Log.Debug(m=>m(requestUri));
			return new Uri(requestUri);
        }

#2

coboluxx -

I like it, It does add quite a bit more convenience. I created a ticket for this: http://www.couchbase.com/issues/browse/NCBC-714

Remember, it’s an open source project and we do accept pull requests :smile:

Thanks,

Jeff