示例#1
0
        /// <summary>
        /// Add a query map to the query parameters list, where the type of value is scalar
        /// </summary>
        /// <typeparam name="TKey">Type of key in the query map</typeparam>
        /// <typeparam name="TValue">Type of value in the query map</typeparam>
        /// <param name="serializationMethod">Method to use to serialize the value</param>
        /// <param name="queryMap">Query map to add</param>
        public void AddQueryMap <TKey, TValue>(QuerySerializationMethod serializationMethod, IDictionary <TKey, TValue> queryMap)
        {
            if (queryMap == null)
            {
                return;
            }

            foreach (var kvp in queryMap)
            {
                if (kvp.Key == null)
                {
                    continue;
                }

                // Backwards compat: if it's a dictionary of object, see if it's ienumerable.
                // If it is, treat it as an ienumerable<object> (yay covariance)
                if (serializationMethod == QuerySerializationMethod.ToString &&
                    typeof(TValue) == typeof(object) &&
                    kvp.Value is IEnumerable <object> &&
                    !(kvp.Value is string))
                {
                    this._queryParams.Add(new QueryCollectionParameterInfo <object>(serializationMethod, kvp.Key.ToString(), (IEnumerable <object>)kvp.Value));
                }
                else
                {
                    this._queryParams.Add(new QueryParameterInfo <TValue>(serializationMethod, kvp.Key.ToString(), kvp.Value));
                }
            }
        }
示例#2
0
 /// <summary>
 /// Initialises a new instance of the <see cref="QueryCollectionParameterInfo{T}"/> class
 /// </summary>
 /// <param name="serializationMethod">Method to use the serialize the query values</param>
 /// <param name="name">Name of the name/values pair</param>
 /// <param name="values">Values of the name/values pair</param>
 /// <param name="format">Format string to use</param>
 public QueryCollectionParameterInfo(QuerySerializationMethod serializationMethod, string name, IEnumerable <T> values, string?format)
 {
     this.SerializationMethod = serializationMethod;
     this.name   = name;
     this.values = values;
     this.format = format;
 }
示例#3
0
        /// <summary>
        /// Add a collection of query parameter values under the same name
        /// </summary>
        /// <typeparam name="T">Type of the value to add</typeparam>
        /// <param name="serializationMethod">Method to use to serialize the value</param>
        /// <param name="name">Name of the name/values pair</param>
        /// <param name="values">Values of the name/values pairs</param>
        /// <param name="format">
        /// Format string to be passed to the custom serializer (if serializationMethod is <see cref="QuerySerializationMethod.Serialized"/>),
        /// or to the value's ToString() method (if serializationMethod is <see cref="QuerySerializationMethod.ToString"/> and value implements
        /// <see cref="IFormattable"/>)
        /// </param>
        public void AddQueryCollectionParameter <T>(QuerySerializationMethod serializationMethod, string name, IEnumerable <T> values, string format = null)
        {
            if (this._queryParams == null)
            {
                this._queryParams = new List <QueryParameterInfo>();
            }

            this._queryParams.Add(new QueryCollectionParameterInfo <T>(serializationMethod, name, values, format));
        }
示例#4
0
        public void EmitAddQueryProperty(EmittedProperty property, QuerySerializationMethod serializationMethod)
        {
            Assert(property.PropertyModel.QueryAttribute != null);
            var attribute = property.PropertyModel.QueryAttribute.Attribute;

            this.writer.WriteLine(this.requestInfoLocalName + ".AddQueryProperty(" + EnumValue(serializationMethod) + ", " +
                                  QuoteString(property.PropertyModel.QueryAttributeName) + ", " + ReferenceTo(property.PropertyModel) + ", " +
                                  QuoteString(attribute.Format) + ");");
        }
示例#5
0
        /// <summary>
        /// Add a query parameter
        /// </summary>
        /// <remarks>value may be an IEnumerable, in which case each value is added separately</remarks>
        /// <typeparam name="T">Type of the value to add</typeparam>
        /// <param name="serializationMethod">Method to use to serialize the value</param>
        /// <param name="name">Name of the name/value pair</param>
        /// <param name="value">Value of the name/value pair</param>
        /// <param name="format">
        /// Format string to be passed to the custom serializer (if serializationMethod is <see cref="QuerySerializationMethod.Serialized"/>),
        /// or to the value's ToString() method (if serializationMethod is <see cref="QuerySerializationMethod.ToString"/> and value implements
        /// <see cref="IFormattable"/>)
        /// </param>
        public void AddQueryParameter <T>(QuerySerializationMethod serializationMethod, string name, T value, string format = null)
        {
            if (this._queryParams == null)
            {
                this._queryParams = new List <QueryParameterInfo>();
            }

            this._queryParams.Add(new QueryParameterInfo <T>(serializationMethod, name, value, format));
        }
示例#6
0
        public void EmitAddQueryParameter(ParameterModel parameter, QuerySerializationMethod serializationMethod)
        {
            // The attribute might be null, if it's a plain parameter
            string name           = parameter.QueryAttribute == null ? parameter.Name : parameter.QueryAttributeName !;
            var    collectionType = this.CollectionTypeOfType(parameter.ParameterSymbol.Type);
            string methodName     = (collectionType == null) ? "AddQueryParameter" : "AddQueryCollectionParameter";

            this.writer.WriteLine(this.requestInfoLocalName + "." + methodName + "(" + EnumValue(serializationMethod) + ", " +
                                  QuoteString(name) + ", " + ReferenceTo(parameter) + ", " + QuoteString(parameter.QueryAttribute?.Attribute.Format) + ");");
        }
示例#7
0
        public bool TryEmitAddQueryMapParameter(ParameterModel parameter, QuerySerializationMethod serializationMethod)
        {
            string?methodName = this.GetQueryMapMethodName(parameter.ParameterSymbol.Type);

            if (methodName == null)
            {
                return(false);
            }

            this.writer.WriteLine(this.requestInfoLocalName + "." + methodName + "(" +
                                  EnumValue(serializationMethod) + ", " + ReferenceTo(parameter) + ");");
            return(true);
        }
示例#8
0
        public void EmitAddQueryParameter(ParameterModel parameter, QuerySerializationMethod serializationMethod)
        {
            // The attribute might be null, if it's a plain parameter
            string?name       = parameter.QueryAttribute == null ? parameter.Name : parameter.QueryAttributeName;
            var    methodInfo = MakeQueryParameterMethodInfo(parameter.ParameterInfo.ParameterType);

            this.ilGenerator.Emit(OpCodes.Ldloc, this.requestInfoLocal);
            this.ilGenerator.Emit(OpCodes.Ldc_I4, (int)serializationMethod);
            this.LoadString(name);
            this.ilGenerator.Emit(OpCodes.Ldarg, parameter.ParameterInfo.Position + 1);
            this.LoadString(parameter.QueryAttribute?.Attribute.Format);
            this.ilGenerator.Emit(OpCodes.Callvirt, methodInfo);
        }
        public QuerySerializationMethod ResolveQuery(QuerySerializationMethod parameterMethod)
        {
            if (parameterMethod != QuerySerializationMethod.Default)
                return parameterMethod;

            if (this.MethodAttribute != null && this.MethodAttribute.Query != QuerySerializationMethod.Default)
                return this.MethodAttribute.Query;

            if (this.ClassAttribute != null && this.ClassAttribute.Query != QuerySerializationMethod.Default)
                return this.ClassAttribute.Query;

            return DefaultQuerySerializationMethod;
        }
示例#10
0
        public void EmitAddQueryProperty(EmittedProperty property, QuerySerializationMethod serializationMethod)
        {
            Assert(property.PropertyModel.QueryAttribute != null);
            var attribute   = property.PropertyModel.QueryAttribute.Attribute;
            var typedMethod = MethodInfos.RequestInfo_AddQueryProperty.MakeGenericMethod(property.FieldBuilder.FieldType);

            this.ilGenerator.Emit(OpCodes.Ldloc, this.requestInfoLocal);
            this.ilGenerator.Emit(OpCodes.Ldc_I4, (int)serializationMethod);
            this.ilGenerator.Emit(OpCodes.Ldstr, property.PropertyModel.QueryAttributeName);
            this.ilGenerator.Emit(OpCodes.Ldarg_0);
            this.ilGenerator.Emit(OpCodes.Ldfld, property.FieldBuilder);
            this.LoadString(attribute.Format);
            this.ilGenerator.Emit(OpCodes.Callvirt, typedMethod);
        }
示例#11
0
        /// <summary>
        /// Add a query map to the query parameters list, where the type of value is enumerable
        /// </summary>
        /// <typeparam name="TKey">Type of key in the query map</typeparam>
        /// <typeparam name="TValue">Type of value in the query map</typeparam>
        /// <typeparam name="TElement">Type of element in the value</typeparam>
        /// <param name="serializationMethod">Method to use to serialize the value</param>
        /// <param name="queryMap">Query map to add</param>
        public void AddQueryCollectionMap <TKey, TValue, TElement>(QuerySerializationMethod serializationMethod, IDictionary <TKey, TValue> queryMap) where TValue : IEnumerable <TElement>
        {
            if (queryMap == null)
            {
                return;
            }

            foreach (var kvp in queryMap)
            {
                if (kvp.Key != null)
                {
                    this._queryParams.Add(new QueryCollectionParameterInfo <TElement>(serializationMethod, kvp.Key.ToString(), kvp.Value));
                }
            }
        }
示例#12
0
        public bool TryEmitAddQueryMapParameter(ParameterModel parameter, QuerySerializationMethod serializationMethod)
        {
            var method = MakeQueryMapMethodInfo(parameter.ParameterInfo.ParameterType);

            if (method == null)
            {
                return(false);
            }

            this.ilGenerator.Emit(OpCodes.Ldloc, this.requestInfoLocal);
            this.ilGenerator.Emit(OpCodes.Ldc_I4, (int)serializationMethod);
            this.ilGenerator.Emit(OpCodes.Ldarg, parameter.ParameterInfo.Position + 1);
            if (parameter.ParameterInfo.ParameterType.GetTypeInfo().IsValueType)
            {
                this.ilGenerator.Emit(OpCodes.Box);
            }
            this.ilGenerator.Emit(OpCodes.Callvirt, method);
            return(true);
        }
示例#13
0
        public QuerySerializationMethod ResolveQuery(QuerySerializationMethod parameterMethod)
        {
            if (parameterMethod != QuerySerializationMethod.Default)
            {
                return(parameterMethod);
            }

            if (this.MethodAttribute != null && this.MethodAttribute.Query != QuerySerializationMethod.Default)
            {
                return(this.MethodAttribute.Query);
            }

            if (this.ClassAttribute != null && this.ClassAttribute.Query != QuerySerializationMethod.Default)
            {
                return(this.ClassAttribute.Query);
            }

            return(DefaultQuerySerializationMethod);
        }
示例#14
0
 /// <summary>
 /// Initialises a new instance of the <see cref="QueryAttribute"/> class, with the given name and serialization method
 /// </summary>
 /// <param name="name">Name of the query parameter</param>
 /// <param name="serializationMethod">Serialization method to use to serialize the value</param>
 public QueryAttribute(string name, QuerySerializationMethod serializationMethod)
     : this(name, true, serializationMethod)
 {
 }
示例#15
0
        private void AddQueryMap(ILGenerator methodIlGenerator, Type parameterType, short parameterIndex, MethodInfo method, QuerySerializationMethod serializationMethod)
        {
            // Equivalent C#:
            // requestInfo.AddQueryMap<TKey, TValue>(value) or requestInfo.AddQueryMap<TKey, TValue, TElement>(value)
            // They might possible potentially provide a struct here (although it's unlikely), so we need to box

            methodIlGenerator.Emit(OpCodes.Dup);
            // Load the serialization method onto the stack
            // Stack: [..., requestInfo, requestInfo, serializationMethod]
            methodIlGenerator.Emit(OpCodes.Ldc_I4, (int)serializationMethod);
            methodIlGenerator.Emit(OpCodes.Ldarg, parameterIndex);
            if (parameterType.IsValueType)
            {
                methodIlGenerator.Emit(OpCodes.Box);
            }
            methodIlGenerator.Emit(OpCodes.Callvirt, method);
        }
 /// <summary>
 /// Initialises a new instance of the <see cref="SerializationMethodsAttribute"/> class
 /// </summary>
 public SerializationMethodsAttribute()
 {
     this.Body = BodySerializationMethod.Default;
     this.Query = QuerySerializationMethod.Default;
 }
示例#17
0
 /// <summary>
 /// Add a collection of query parameter values under the same name
 /// </summary>
 /// <typeparam name="T">Type of the value to add</typeparam>
 /// <param name="serializationMethod">Method to use to serialize the value</param>
 /// <param name="name">Name of the name/values pair</param>
 /// <param name="values">Values of the name/values pairs</param>
 public void AddQueryCollectionParameter <T>(QuerySerializationMethod serializationMethod, string name, IEnumerable <T> values)
 {
     this._queryParams.Add(new QueryCollectionParameterInfo <T>(serializationMethod, name, values));
 }
示例#18
0
 /// <summary>
 /// Initialises a new instance of the <see cref="QueryMapAttribute"/> with the given serialization method
 /// </summary>
 /// <param name="serializationMethod">Serialization method to use to serialize the value</param>
 public QueryMapAttribute(QuerySerializationMethod serializationMethod)
 {
     this.SerializationMethod = serializationMethod;
 }
 /// <summary>
 /// Initialises a new instance of the <see cref="SerializationMethodsAttribute"/> class
 /// </summary>
 public SerializationMethodsAttribute()
 {
     this.Body  = BodySerializationMethod.Default;
     this.Query = QuerySerializationMethod.Default;
 }
示例#20
0
 /// <summary>
 /// Add a query parameter
 /// </summary>
 /// <remarks>value may be an IEnumerable, in which case each value is added separately</remarks>
 /// <typeparam name="T">Type of the value to add</typeparam>
 /// <param name="serializationMethod">Method to use to serialize the value</param>
 /// <param name="name">Name of the name/value pair</param>
 /// <param name="value">Value of the name/value pair</param>
 public void AddQueryParameter <T>(QuerySerializationMethod serializationMethod, string name, T value)
 {
     this._queryParams.Add(new QueryParameterInfo <T>(serializationMethod, name, value));
 }
示例#21
0
 /// <summary>
 /// Initialises a new instance of the <see cref="QueryMapAttribute"/> with the given serialization method
 /// </summary>
 /// <param name="serializationMethod">Serialization method to use to serialize the value</param>
 public QueryMapAttribute(QuerySerializationMethod serializationMethod)
 {
     this.SerializationMethod = serializationMethod;
 }
示例#22
0
 private QueryAttribute(string name, bool hasName, QuerySerializationMethod serializationMethod)
 {
     this.Name                = name;
     this.HasName             = hasName;
     this.SerializationMethod = serializationMethod;
 }
示例#23
0
 /// <summary>
 /// Initialises a new instance of the <see cref="QueryAttribute"/> class, with the given name and serialization method
 /// </summary>
 /// <param name="name">Name of the query parameter</param>
 /// <param name="serializationMethod">Serialization method to use to serialize the value</param>
 public QueryAttribute(string name, QuerySerializationMethod serializationMethod)
 {
     this.Name = name;
     this.SerializationMethod = serializationMethod;
 }
示例#24
0
 /// <summary>
 /// Initialises a new instance of the <see cref="QueryAttribute"/> class, with the given serialization method
 /// </summary>
 /// <param name="serializationMethod">Serialization method to use to serialize the value</param>
 public QueryAttribute(QuerySerializationMethod serializationMethod)
     : this(null, serializationMethod)
 {
 }
示例#25
0
 /// <summary>
 /// Initialises a new instance of the <see cref="QueryAttribute"/> class, with the given name and serialization method
 /// </summary>
 /// <param name="name">Name of the query parameter</param>
 /// <param name="serializationMethod">Serialization method to use to serialize the value</param>
 public QueryAttribute(string name, QuerySerializationMethod serializationMethod)
 {
     this.Name = name;
     this.SerializationMethod = serializationMethod;
 }
示例#26
0
        private void AddQueryParam(ILGenerator methodIlGenerator, string name, short parameterIndex, MethodInfo methodToCall, QuerySerializationMethod serializationMethod)
        {
            // Equivalent C#:
            // requestInfo.AddQueryParameter(serializationMethod, name, value) (or AddQueryCollectionParameter)

            // Stack: [..., requestInfo, requestInfo]
            methodIlGenerator.Emit(OpCodes.Dup);
            // Load the serialization method onto the stack
            // Stack: [..., requestInfo, requestInfo, serializationMethod]
            methodIlGenerator.Emit(OpCodes.Ldc_I4, (int)serializationMethod);
            // Load the name onto the stack
            // Stack: [..., requestInfo, requestInfo, serializationMethod, name]
            methodIlGenerator.Emit(OpCodes.Ldstr, name);
            // Load the param onto the stack
            // Stack: [..., requestInfo, requestInfo, serializationMethod, name, value]
            methodIlGenerator.Emit(OpCodes.Ldarg, parameterIndex);
            // Call AddPathParameter
            // Stack: [..., requestInfo]
            methodIlGenerator.Emit(OpCodes.Callvirt, methodToCall);
        }
示例#27
0
 /// <summary>
 /// Initialises a new instance of the <see cref="QueryAttribute"/> class, with the given serialization method
 /// </summary>
 /// <param name="serializationMethod">Serialization method to use to serialize the value</param>
 public QueryAttribute(QuerySerializationMethod serializationMethod)
     : this(null, serializationMethod)
 {
 }