コード例 #1
0
        public static IDictionary <string, object> GetMorestachioFormatterDocumentation()
        {
            var values         = new Dictionary <string, object>();
            var formatterTypes = new List <FormatterData>();

            values["Data"] = formatterTypes;

            var formatterService = ContextObject.DefaultFormatter as MorestachioFormatterService;

            foreach (var formatterServiceFormatter in formatterService
                     .Formatters
                     .SelectMany(f => f.Value)
                     .GroupBy(e => e.Function.DeclaringType))
            {
                var formatter = new FormatterData();
                formatter.DeclaringType = formatterServiceFormatter.Key;

                var methods = new List <FormatterData.FormatterMethod>();
                formatter.Methods = methods;
                foreach (var formatterMethod in formatterServiceFormatter
                         .GroupBy(e => e.Function))
                {
                    foreach (var morestachioFormatterModel in formatterMethod.GroupBy(e => e.IsGlobalFormatter))
                    {
                        var methodMeta = new FormatterData.FormatterMethod();
                        methodMeta.MethodName = formatterMethod.Key.Name;

                        var methodFunctions = new List <FormatterData.FormatterMethod.MethodFunction>();
                        foreach (var fncGrouped in morestachioFormatterModel)
                        {
                            var function = new FormatterData.FormatterMethod.MethodFunction();
                            function.FormatterName = string.IsNullOrWhiteSpace(fncGrouped.Name) ? "{Null}" : fncGrouped.Name;
                            var mOperator = MorestachioOperator.Operators.FirstOrDefault(f => ("op_" + f.Key.ToString()) == function.FormatterName).Value;
                            if (mOperator != null)
                            {
                                function.FormatterName = mOperator.OperatorText;
                                function.IsOperator    = true;
                            }
                            function.Returns     = fncGrouped.OutputType ?? fncGrouped.Function.ReturnType;
                            function.Description = fncGrouped.Description;
                            methodFunctions.Add(function);
                        }


                        methodMeta.Functions = methodFunctions;
                        var parameters = new List <FormatterData.FormatterMethod.MethodParameter>();
                        var metaData   = morestachioFormatterModel.First();
                        methodMeta.Returns = metaData.Function.ReturnType;
                        foreach (var inputDescription in metaData.MetaData)
                        {
                            var paramter = new FormatterData.FormatterMethod.MethodParameter();
                            paramter.Name           = inputDescription.Name;
                            paramter.Type           = inputDescription.ParameterType;
                            paramter.IsOptional     = inputDescription.IsOptional;
                            paramter.IsSourceObject = inputDescription.IsSourceObject;
                            paramter.IsInjected     = inputDescription.IsInjected;
                            paramter.IsRestObject   = inputDescription.IsRestObject;
                            parameters.Add(paramter);
                        }

                        methodMeta.Parameters = parameters;
                        methods.Add(methodMeta);
                    }
                }
                formatterTypes.Add(formatter);
            }

            return(values);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: aTiKhan/morestachio
        public static IDictionary <string, object> GetMorestachioFormatterDocumentation()
        {
            FormatterData EnumerateFormatters(IGrouping <Type, MorestachioFormatterModel> formatterServiceFormatter, bool includeInstanceMethods)
            {
                var formatter = new FormatterData();

                formatter.DeclaringType = formatterServiceFormatter.Key;

                var methods = new List <FormatterData.FormatterMethod>();

                formatter.Methods = methods;
                foreach (var formatterMethod in formatterServiceFormatter
                         .Where(e => e.LinkFunctionTarget == includeInstanceMethods)
                         .GroupBy(e => e.Function))
                {
                    foreach (var morestachioFormatterModel in formatterMethod.GroupBy(e => e.IsGlobalFormatter))
                    {
                        var methodMeta = new FormatterData.FormatterMethod();
                        methodMeta.MethodName = formatterMethod.Key.Name;

                        var methodFunctions = new List <FormatterData.FormatterMethod.MethodFunction>();
                        foreach (var fncGrouped in morestachioFormatterModel)
                        {
                            var function = new FormatterData.FormatterMethod.MethodFunction();
                            function.FormatterName = string.IsNullOrWhiteSpace(fncGrouped.Name) ? "{Null}" : fncGrouped.Name;
                            var mOperator = MorestachioOperator.Operators
                                            .FirstOrDefault(f => ("op_" + f.Key.ToString()) == function.FormatterName).Value;
                            if (mOperator != null)
                            {
                                function.FormatterName = mOperator.OperatorText;
                                function.IsOperator    = true;
                            }

                            function.Returns            = fncGrouped.OutputType ?? fncGrouped.Function.ReturnType;
                            function.Description        = fncGrouped.Description;
                            function.IsInstanceFunction = fncGrouped.LinkFunctionTarget;
                            methodFunctions.Add(function);
                        }


                        methodMeta.Functions = methodFunctions;
                        var parameters = new List <FormatterData.FormatterMethod.MethodParameter>();
                        var metaData   = morestachioFormatterModel.First();
                        methodMeta.Returns = metaData.Function.ReturnType;
                        foreach (var inputDescription in metaData.MetaData)
                        {
                            var paramter = new FormatterData.FormatterMethod.MethodParameter();
                            paramter.Name           = inputDescription.Name;
                            paramter.Type           = inputDescription.ParameterType;
                            paramter.IsOptional     = inputDescription.IsOptional;
                            paramter.IsSourceObject = inputDescription.IsSourceObject;
                            paramter.IsInjected     = inputDescription.IsInjected;
                            paramter.IsRestObject   = inputDescription.IsRestObject;
                            parameters.Add(paramter);
                        }

                        methodMeta.Parameters = parameters;
                        methods.Add(methodMeta);
                    }
                }

                return(formatter);
            }

            ServicePropertyType EnumerateObject(Type csType,
                                                MorestachioFormatterService formatterService,
                                                ICollection <ServicePropertyType> servicePropertyTypes)
            {
                var type = new ServicePropertyType();

                type.Type            = csType;
                type.IsFrameworkType = true;
                servicePropertyTypes.Add(type);
                foreach (var formatterServiceFormatter in formatterService
                         .Formatters
                         .SelectMany(f => f.Value)
                         .Where(e => e.LinkFunctionTarget)
                         .Where(e => e.Function.DeclaringType == csType)
                         .GroupBy(e => e.Function.DeclaringType))
                {
                    type.Formatter = EnumerateFormatters(formatterServiceFormatter, true);
                }

                foreach (var propertyInfo in csType.GetProperties(BindingFlags.Public | BindingFlags.Instance))
                {
                    var prop = new ServiceProperty();
                    prop.Name = propertyInfo.Name;

                    if (propertyInfo.PropertyType.Namespace.StartsWith("Morestachio"))
                    {
                        prop.PropType = EnumerateObject(propertyInfo.PropertyType, formatterService, servicePropertyTypes);
                    }
                    else
                    {
                        prop.PropType = new ServicePropertyType()
                        {
                            Type            = propertyInfo.PropertyType,
                            IsFrameworkType = false
                        };
                    }

                    type.Properties.Add(prop);
                }

                return(type);
            }

            var values         = new Dictionary <string, object>();
            var formatterTypes = new List <FormatterData>();

            values["FormatterData"] = formatterTypes;

            var parserOptions = new ParserOptions();

            parserOptions
            .RegisterFileSystem(() => new FileSystemService())
            .RegisterLocalizationService(() => new MorestachioLocalizationService());

            var formatterService = parserOptions.Formatters as MorestachioFormatterService;

            foreach (var formatterServiceFormatter in formatterService
                     .Formatters
                     .SelectMany(f => f.Value)
                     .GroupBy(e => e.Function.DeclaringType))
            {
                var data = EnumerateFormatters(formatterServiceFormatter, false);
                if (data.Methods.Any())
                {
                    formatterTypes.Add(data);
                }
            }

            var services = new List <ServiceData>();

            values["ServiceData"] = services;
            foreach (var service in formatterService.Services.Enumerate())
            {
                var serviceData = new ServiceData();
                serviceData.ServiceName = service.Key.Name;
                serviceData.Types       = new HashSet <ServicePropertyType>();
                EnumerateObject(service.Key, formatterService, serviceData.Types);
                services.Add(serviceData);
            }

            var constants = new List <ServiceData>();

            values["ConstData"] = constants;
            foreach (var service in formatterService.Constants)
            {
                var serviceData = new ServiceData();
                serviceData.ServiceName = service.Key;
                serviceData.Types       = new HashSet <ServicePropertyType>();
                EnumerateObject(service.Value.GetType(), formatterService, serviceData.Types);
                constants.Add(serviceData);
            }

            return(values);
        }