예제 #1
0
        public void DecorateClass(IResource resource,
                                  string className,
                                  CodeTypeDeclaration resourceClass,
                                  ResourceClassGenerator generator,
                                  string serviceClassName,
                                  IEnumerable<IResourceDecorator> allDecorators)
        {
            foreach (IMethod method in resource.Methods.Values)
            {
                foreach (IParameter 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));
                }
            }
        }
        public void DecorateClass(IResource resource,
                                  string className,
                                  CodeTypeDeclaration resourceClass,
                                  ResourceClassGenerator generator,
                                  string serviceClassName,
                                  IEnumerable<IResourceDecorator> allDecorators)
        {
            resource.ThrowIfNull("resource");
            className.ThrowIfNull("className");
            resourceClass.ThrowIfNull("resourceClass");
            generator.ThrowIfNull("generator");
            serviceClassName.ThrowIfNull("serviceClassName");
            allDecorators.ThrowIfNull("allDecorators");

            if (!resource.IsServiceResource) // Only add subresources of normal resources, not the root resource.
            {
                // Add all subresources.
                foreach (IResource subresource in resource.Resources.Values)
                {
                    // Consider all members in the current class as invalid names.
                    var forbiddenWords = from CodeTypeMember m in resourceClass.Members select m.Name;

                    // Generate and add the subresource.
                    CodeTypeDeclaration decl = GenerateSubresource(
                        subresource, serviceClassName, allDecorators, generator.RequestGenerator,
                        generator.ContainerGenerator, forbiddenWords);
                    resourceClass.Members.Add(decl);
                }
            }
        }
        public void DecorateClassRenamingTest()
        {
            var dec = new SubresourceClassDecorator();
            var resource = new MockResource();
            const string className = "TestClass";
            var decl = new CodeTypeDeclaration();
            const string serviceName = "TestService";
            var decorators = new IResourceDecorator[0];

            // Create generators.
            var resourceGen = new ResourceContainerGenerator(new IResourceContainerDecorator[0]);
            var requestGen = new RequestClassGenerator(new IRequestDecorator[0]);
            var gen = new ResourceClassGenerator(
                resource, serviceName, decorators, requestGen, resourceGen, new string[0]);

            // Add subresources.
            resource.Resources.Add("Sub1", new MockResource { Name = "Sub1" });
            resource.Resources.Add("Sub2", new MockResource { Name = "Sub2" });

            // Add existing members
            decl.Members.Add(new CodeMemberField { Name = "Sub1Resource" });

            // Test decoration.
            dec.DecorateClass(resource, className, decl, gen, serviceName, decorators);
            Assert.AreEqual(3, decl.Members.Count);
            Assert.AreEqual("Sub1Resource", decl.Members[0].Name);
            Assert.AreEqual("Sub1Res", decl.Members[1].Name);
            Assert.AreEqual("Sub2Resource", decl.Members[2].Name);
        }
        public void DecorateClass(IResource resource,
            string className,
            CodeTypeDeclaration resourceClass,
            ResourceClassGenerator generator,
            string serviceClassName,
            IEnumerable<IResourceDecorator> allDecorators)
        {
            var gen = new ResourceGenerator(className, objectTypeProvider, commentCreator);
            foreach (var method in resource.Methods.Values)
            {
                logger.Debug("Adding RequestObject Method {0}.{1}", resource.Name, method.Name);

                // Add the default request method to the class:
                CodeTypeMember convenienceMethod = gen.CreateMethod(resourceClass, resource, method, false);
                if (convenienceMethod != null)
                {
                    resourceClass.Members.Add(convenienceMethod);
                }

                // Add the request method specifiying all parameters (also optional ones) to the class:
                if (AddOptionalParameters && method.HasOptionalParameters())
                {
                    convenienceMethod = gen.CreateMethod(resourceClass, resource, method, true);
                    if (convenienceMethod != null)
                    {
                        resourceClass.Members.Add(convenienceMethod);
                    }
                }
            }
        }
예제 #5
0
 public void DecorateClass(IResource resource,
                           string className,
                           CodeTypeDeclaration resourceClass,
                           ResourceClassGenerator generator,
                           string serviceClassName,
                           IEnumerable<IResourceDecorator> allDecorators)
 {
     DecorateClassCalled += 1;
 }
 public void DecorateClass(IResource resource,
                           string className,
                           CodeTypeDeclaration resourceClass,
                           ResourceClassGenerator generator,
                           string serviceClassName,
                           IEnumerable<IResourceDecorator> allDecorators)
 {
     string fullName = resource.Path;
     resourceClass.Members.Add(CreateResourceNameConst(fullName));
 }
 public void DecorateClass(IResource resource,
     string className,
     CodeTypeDeclaration resourceClass,
     ResourceClassGenerator generator,
     string serviceClassName,
     IEnumerable<IResourceDecorator> allDecorators)
 {
     if (!resource.IsServiceResource) // Only add this constructor to non-service resources.
     {
         logger.Debug("Adding standard constructor to Resource[{0}]", resource.Name);
         resourceClass.Members.Add(CreateConstructor(serviceClassName, resource));
     }
 }
        internal CodeTypeDeclaration GenerateSubresource(IResource subresource,
                                     string serviceClassName,
                                     IEnumerable<IResourceDecorator> allDecorators,
                                     RequestClassGenerator requestClassGenerator,
                                     ResourceContainerGenerator containerGenerator,
                                     IEnumerable<string> forbiddenWords)
        {
            // Create a new generator for this subresource
            var subgenerator = new ResourceClassGenerator(
                subresource, serviceClassName, allDecorators, requestClassGenerator, containerGenerator,
                forbiddenWords);

            // Generate and add the subresource.
            return subgenerator.CreateClass();
        }
 public void DecorateClass(IResource resource,
                           string className,
                           CodeTypeDeclaration resourceClass,
                           ResourceClassGenerator generator,
                           string serviceClassName,
                           IEnumerable<IResourceDecorator> allDecorators)
 {
     if (resource.IsServiceResource)
     {
         resourceClass.Members.Add(CreateFakeServiceField());
     }
     else
     {
         resourceClass.Members.Add(CreateServiceField(serviceClassName));
         resourceClass.Members.Add(CreateAuthenticatorField());
     }
 }
예제 #10
0
        public void DecorateClass(IResource resource,
                                  string className,
                                  CodeTypeDeclaration resourceClass,
                                  ResourceClassGenerator generator,
                                  string serviceClassName,
                                  IEnumerable<IResourceDecorator> allDecorators)
        {
            //public static log4net.ILog Logger = log4net.LogManager.GetLogger(typeof(<className>));
            logger.Debug("Adding logger to ResourceClass.");
            var loggerField = new CodeMemberField("log4net.ILog", LoggerName);
            var initialiser = new CodeMethodInvokeExpression();
            initialiser.Method = new CodeMethodReferenceExpression(
                new CodeTypeReferenceExpression("log4net.LogManager"), "GetLogger");
            initialiser.Parameters.Add(new CodeTypeOfExpression(className));
            loggerField.InitExpression = initialiser;

            resourceClass.Members.Add(loggerField);
        }
예제 #11
0
        private void CreateResources(CodeNamespace clientNamespace,
            string serviceClassName,
            IResource resource,
            RequestClassGenerator requestClassGenerator,
            ResourceContainerGenerator resourceContainerGenerator)
        {
            foreach (var res in resource.Resources.Values.Concat(resource))
            {
                // Create the current list of used names.
                IEnumerable<string> usedNames = resource.Resources.Keys;

                // Create a class for the resource.
                logger.Debug("Adding Resource {0}", res.Name);
                var resourceGenerator = new ResourceClassGenerator(
                    res, serviceClassName, resourceDecorators, requestClassGenerator, resourceContainerGenerator,
                    usedNames);
                var generatedClass = resourceGenerator.CreateClass();
                clientNamespace.Types.Add(generatedClass);
            }
        }
        public void DecorateClassTest()
        {
            var dec = new SubresourceClassDecorator();
            var resource = new MockResource();
            const string className = "TestClass";
            var decl = new CodeTypeDeclaration();
            const string serviceName = "TestService";
            var decorators = new IResourceDecorator[0];

            // Create generators.
            var resourceGen = new ResourceContainerGenerator(new IResourceContainerDecorator[0]);
            var requestGen = new RequestClassGenerator(new IRequestDecorator[0]);
            var gen = new ResourceClassGenerator(
                resource, serviceName, decorators, requestGen, resourceGen, new string[0]);

            // Add subresources.
            resource.Resources.Add("Sub1", new MockResource { Name = "Sub1" });
            resource.Resources.Add("Sub2", new MockResource { Name = "Sub2" });

            // Test decoration.
            dec.DecorateClass(resource, className, decl, gen, serviceName, decorators);
            Assert.AreEqual(2, decl.Members.Count);
            Assert.IsInstanceOf<CodeTypeDeclaration>(decl.Members[0]);
            Assert.IsInstanceOf<CodeTypeDeclaration>(decl.Members[1]);
        }
        public void DecorateClassValidationTest()
        {
            var dec = new SubresourceClassDecorator();
            var resource = new MockResource();
            const string className = "TestClass";
            var decl = new CodeTypeDeclaration();
            const string serviceName = "TestService";
            IEnumerable<IResourceDecorator> decorators =
                GoogleServiceGenerator.GetSchemaAwareResourceDecorators("Generated.Data.");

            // Create generators.
            var resourceGen = new ResourceContainerGenerator(
                GoogleServiceGenerator.StandardResourceContainerDecorator);
            var requestGen = new RequestClassGenerator(new IRequestDecorator[0]);
            var gen = new ResourceClassGenerator(
                resource, serviceName, decorators, requestGen, resourceGen, new string[0]);

            // Validate method:
            Assert.Throws<ArgumentNullException>(
                () => dec.DecorateClass(null, className, decl, gen, serviceName, decorators));
            Assert.Throws<ArgumentNullException>(
                () => dec.DecorateClass(resource, null, decl, gen, serviceName, decorators));
            Assert.Throws<ArgumentNullException>(
                () => dec.DecorateClass(resource, className, null, gen, serviceName, decorators));
            Assert.Throws<ArgumentNullException>(
                () => dec.DecorateClass(resource, className, decl, null, serviceName, decorators));
            Assert.Throws<ArgumentNullException>(
                () => dec.DecorateClass(resource, className, decl, gen, null, decorators));
            Assert.Throws<ArgumentNullException>(
                () => dec.DecorateClass(resource, className, decl, gen, serviceName, null));

            // Test normal use case.
            dec.DecorateClass(resource, className, decl, gen, serviceName, decorators);
        }