public void DecorateClass(IResource resource,
                                  string className,
                                  CodeTypeDeclaration resourceClass,
                                  ResourceClassGenerator generator,
                                  string serviceClassName,
                                  IEnumerable <IResourceDecorator> allDecorators)
        {
            foreach (IMethod method in resource.Methods.Values)
            {
                foreach (IDiscoveryParameter parameter in method.Parameters.Values)
                {
                    if (parameter.EnumValues.IsNullOrEmpty())
                    {
                        continue; // Not an enumeration type.
                    }

                    // Check whether the type already exists.
                    if (DecoratorUtil.FindFittingEnumeration(
                            resourceClass, parameter.EnumValues, parameter.EnumValueDescriptions) != null)
                    {
                        continue;
                    }

                    // Create and add the enumeration.
                    resourceClass.Members.Add(GenerateEnum(
                                                  resourceClass, parameter.Name, parameter.Description, parameter.EnumValues,
                                                  parameter.EnumValueDescriptions));
                }
            }
        }
Ejemplo n.º 2
0
        internal static CodeTypeReference GetParameterTypeReference(CodeTypeDeclaration classDeclaration,
                                                                    IParameter 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);
        }
        public void FindFittingEnumerationTest()
        {
            CodeTypeDeclaration  decl        = new CodeTypeDeclaration();
            IEnumerable <string> enumValues  = new[] { "foo", "bar", "42" };
            IEnumerable <string> enumValues2 = new[] { "foo", "bar", "cake" };
            IEnumerable <string> enumValues3 = new[] { "foo", "bar" };

            // Test parameter validation.
            Assert.Throws <ArgumentNullException>(() => DecoratorUtil.FindFittingEnumeration(decl, null, null));
            Assert.Throws <ArgumentNullException>(() => DecoratorUtil.FindFittingEnumeration(null, enumValues, null));

            // Test with empty class.
            Assert.IsNull(DecoratorUtil.FindFittingEnumeration(decl, enumValues, null));

            // Add an enum, and check whether it can be found.
            CodeTypeDeclaration testEnum = EnumResourceDecorator.GenerateEnum(
                decl, "SomeName", "SomeDescription", enumValues, null);

            decl.Members.Add(testEnum);
            Assert.AreEqual(testEnum.Name, DecoratorUtil.FindFittingEnumeration(decl, enumValues, null).BaseType);

            // Confirm that the other values are not found.
            Assert.IsNull(DecoratorUtil.FindFittingEnumeration(decl, enumValues2, null));
            Assert.IsNull(DecoratorUtil.FindFittingEnumeration(decl, enumValues3, null));

            // Check that the code also works with two enumerations
            CodeTypeDeclaration testEnum3 = EnumResourceDecorator.GenerateEnum(
                decl, "SomeOtherName", "SomeDescription", enumValues3, null);

            decl.Members.Add(testEnum3);

            Assert.AreEqual(testEnum.Name, DecoratorUtil.FindFittingEnumeration(decl, enumValues, null).BaseType);
            Assert.AreEqual(testEnum3.Name, DecoratorUtil.FindFittingEnumeration(decl, enumValues3, null).BaseType);
            Assert.IsNull(DecoratorUtil.FindFittingEnumeration(decl, enumValues2, null));
        }