internal static CodeTypeReference GetParameterTypeReference(CodeTypeDeclaration classDeclaration, IDiscoveryParameter param) { Type underlyingType = GetParameterType(param); CodeTypeReference paramTypeRef = new CodeTypeReference(underlyingType); bool isValueType = underlyingType.IsValueType; // Check if we need to declare a custom type for this parameter. // If the parameter is an enum, try finding the matching enumeration in the current class if (!param.EnumValues.IsNullOrEmpty()) { // Naming scheme: MethodnameParametername CodeTypeReference enumReference = DecoratorUtil.FindFittingEnumeration( classDeclaration, param.EnumValues, param.EnumValueDescriptions); if (enumReference != null) { paramTypeRef = enumReference; isValueType = true; } } // Check if this is an optional value parameter. if (isValueType && !param.IsRequired) { paramTypeRef = new CodeTypeReference(typeof(Nullable <>)) { TypeArguments = { paramTypeRef.BaseType } }; // An optional value parameter has to be nullable. } return(paramTypeRef); }
private Dictionary <string, IDiscoveryParameter> FetchParameters() { if (information.ContainsKey(ServiceFactory.Parameters) == false) { return(new Dictionary <string, IDiscoveryParameter> { }); } JsonDictionary js = information[ServiceFactory.Parameters] as JsonDictionary; if (js == null) { return(new Dictionary <string, IDiscoveryParameter> { }); } var parameters = new Dictionary <string, IDiscoveryParameter>(); foreach (KeyValuePair <string, object> kvp in js) { IDiscoveryParameter p = CreateParameter(kvp); parameters.Add(kvp.Key, p); } return(parameters); }
protected void AddParameterComment(IMethodCommentCreator commentCreator, CodeMemberMethod member, IDiscoveryParameter param, string parameterName) { if (commentCreator != null) { member.Comments.AddRange(commentCreator.CreateParameterComment(param, parameterName)); } }
internal static Type GetParameterType(IDiscoveryParameter param) { param.ThrowIfNull("param"); Type baseType = GetUnderlyingParameterType(param); // If this is a repeatable parameter, wrap the underlying type into a Repeatable<T>. if (param.IsRepeatable) { return(typeof(Repeatable <>).MakeGenericType(baseType)); } return(baseType); }
internal string GetParameterMetaData(IDiscoveryParameter param, string programaticName) { var strings = new List <string>(); if (param.Name != programaticName && param.Name.IsNotNullOrEmpty()) { strings.Add(param.Name); } if (param.IsRequired) { strings.Add("Required"); } else { strings.Add("Optional"); } if (param.Pattern.IsNotNullOrEmpty()) { strings.Add("Must match pattern " + param.Pattern); } if (param.Minimum.IsNotNullOrEmpty()) { strings.Add("Minimum value of " + param.Minimum); } if (param.Maximum.IsNotNullOrEmpty()) { strings.Add("Maximum value of " + param.Maximum); } IList <string> enumValues = param.EnumValues != null?param.EnumValues.ToList() : null; if (enumValues.IsNotNullOrEmpty()) { StringBuilder sb = new StringBuilder(); sb.Append("Must be one of the following values ["); sb.Append(string.Join(", ", enumValues.ToArray())); sb.Append("]"); strings.Add(sb.ToString()); } if (param.Description.IsNotNullOrEmpty()) { strings.Add(param.Description); } return(string.Join(" - ", strings.ToArray())); }
public CodeCommentStatementCollection CreateParameterComment(IDiscoveryParameter parameter, string programaticName) { parameter.ThrowIfNull("parameter"); programaticName.ThrowIfNullOrEmpty("programaticName"); var commentText = string.Format( "<param name=\"{0}\">{1}</param>", programaticName, XmlEscapeComment(GetParameterMetaData(parameter, programaticName))); var ret = new CodeCommentStatementCollection(); ret.Add(new CodeCommentStatement(new CodeComment(commentText, true))); return(ret); }
/// <summary> /// Creates a declaration for the specified parameter. /// </summary> protected CodeParameterDeclarationExpression DeclareInputParameter( CodeTypeDeclaration classDeclaration, IDiscoveryParameter param, IMethod method) { CodeTypeReference paramTypeRef = GetParameterTypeReference(classDeclaration, param); var decl = new CodeParameterDeclarationExpression( paramTypeRef, GeneratorUtils.GetParameterName(param, method.Parameters.Keys.Without(param.Name).Concat(method.Name))); // If this parameter is optional, mark it as as a C# 4.0 optional parameter: if (!param.IsRequired) { // [Optional] decl.CustomAttributes.Add( new CodeAttributeDeclaration(new CodeTypeReference(typeof(OptionalAttribute)))); } return(decl); }
internal CodeTypeMemberCollection GenerateParameterProperty(IDiscoveryParameter parameter, IMethod method, CodeTypeDeclaration resourceClass, IEnumerable <string> usedNames) { // Get the name and return type of this parameter. string name = parameter.Name; CodeTypeReference returnType = ResourceBaseGenerator.GetParameterTypeReference( resourceClass, parameter); // Generate the property and field. CodeTypeMemberCollection newMembers = DecoratorUtil.CreateAutoProperty( name, parameter.Description, returnType, usedNames, parameter.IsRequired, null); // Add the KeyAttribute to the property. foreach (CodeTypeMember member in newMembers) { CodeMemberProperty property = member as CodeMemberProperty; if (property == null) { continue; } RequestParameterType requestParameterType = parameter.ParameterType == PathParameter ? RequestParameterType.Path : RequestParameterType.Query; // Declare the RequestParameter attribute. CodeTypeReference attributeType = new CodeTypeReference(typeof(RequestParameterAttribute)); // Generates something like.. // [Google.Apis.Util.RequestParameterAttribute("prettyPrint", // Google.Apis.Util.RequestParameterType.Query)] CodeAttributeDeclaration attribute = new CodeAttributeDeclaration(attributeType); attribute.Arguments.Add(new CodeAttributeArgument(new CodePrimitiveExpression(parameter.Name))); attribute.Arguments.Add( new CodeAttributeArgument( new CodeFieldReferenceExpression( new CodeTypeReferenceExpression(typeof(RequestParameterType)), Enum.GetName(typeof(RequestParameterType), requestParameterType)))); property.CustomAttributes.Add(attribute); } return(newMembers); }
/// <summary> /// Retrieves the underlying, unmodified type of a parameter. /// </summary> private static Type GetUnderlyingParameterType(IDiscoveryParameter param) { param.ThrowIfNull("param"); switch (param.ValueType) { case null: case "": case "string": return(typeof(string)); case "boolean": return(typeof(bool)); case "integer": return(typeof(long)); default: logger.Error( "FAIL - found unkown parameter.type [" + param.ValueType + "] for parameter [" + param.Name + "]"); return(typeof(string)); } }
/// <summary> /// Return all IDiscoveryParameter from the given method. /// Sorted by proposed order, then required, then optional, alphabeticly within those groupings. /// </summary> public static IEnumerable <IDiscoveryParameter> GetAllParametersSorted(this IMethod method) { if (method.Parameters == null) { yield break; } var remainingParameters = new List <IDiscoveryParameter>(method.Parameters.Values); // First add all parameters in the suggested order. if (method.ParameterOrder != null) { foreach (string parameterName in method.ParameterOrder) { if (method.Parameters.ContainsKey(parameterName)) { // Return the parameter, and remove it from the lit of remaining parameters. IDiscoveryParameter parameter = method.Parameters[parameterName]; remainingParameters.Remove(parameter); yield return(parameter); } } } // Get a ordered list of all the parameters of this method. // First required, then optional ones. In each category the set is then sorted alphabetically. var sortedParameters = (from parameter in remainingParameters orderby parameter.IsRequired descending, parameter.Name select parameter); foreach (IDiscoveryParameter parameter in sortedParameters) { yield return(parameter); } }