/// <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)); } } }
/// <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; }
/// <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)); }
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) + ");"); }
/// <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)); }
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) + ");"); }
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); }
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; }
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); }
/// <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)); } } }
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); }
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); }
/// <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) { }
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; }
/// <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)); }
/// <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> /// 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)); }
private QueryAttribute(string name, bool hasName, QuerySerializationMethod serializationMethod) { this.Name = name; this.HasName = hasName; this.SerializationMethod = serializationMethod; }
/// <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; }
/// <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) { }
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); }