Пример #1
0
        private IParsedParam InternalOptionalMethodParams(IParsedParam param, ParameterDefinition def)
        {
            if (def.MemberType != MemberTypes.METHOD)
            {
                return(param);
            }

            ResolveNameOfUnnamedParams(param, def);

            var optionalParams = def.SubParams.Where(p => !p.Options.IsRequired).ToList();

            if (optionalParams.Count == 0)
            {
                return(param);
            }

            var requiredParams = def.SubParams.Except(optionalParams);

            if (!(param is ComplexParam))
            {
                var subs = new List <IParsedParam>();


                if (param != null)
                {
                    subs.Add(param);
                }

                for (int paramIndex = 0; paramIndex < def.SubParams.Length; paramIndex++)
                {
                    var sub = def.SubParams[paramIndex];

                    if (param?.Name == sub.Name)
                    {
                        continue;
                    }

                    //if( param?.Index == sub.Options.Order )
                    //    continue;

                    if (!sub.Options.IsRequired)
                    {
                        if (sub.Type.IsBuiltIn(false))
                        {
                            subs.Add(new SimpleParam()
                            {
                                Name = sub.Name, Index = sub.Options.Order, Value = sub.DefaultValue?.ToString()
                            });
                        }
                        else if (sub.Type.IsEnumerable())
                        {
                            subs.Add(new ArrayParam()
                            {
                                Name = sub.Name, Index = sub.Options.Order
                            });
                        }
                        else
                        {
                            subs.Add(new ComplexParam()
                            {
                                Name = sub.Name, Index = sub.Options.Order
                            });
                        }
                    }
                }

                return(new ComplexParam()
                {
                    SubParams = subs.ToArray()
                });
            }
            else if (((ComplexParam)param).SubParams.Length < def.SubParams.Length)
            {
                var paramsSubs = new List <IParsedParam>();
                paramsSubs.AddRange(((ComplexParam)param).SubParams);

                for (int paramIndex = 0; paramIndex < def.SubParams.Length; paramIndex++)
                {
                    var defSub = def.SubParams[paramIndex];

                    if (paramsSubs.Any(s => s.Name == defSub.Name))
                    {
                        continue;
                    }

                    //if( paramIndex < paramsSubs.Count &&
                    //    paramsSubs[ paramIndex ].Index == defSub.Options.Order )
                    //    continue;

                    if (!defSub.Options.IsRequired)
                    {
                        if (defSub.Type.IsBuiltIn(false))
                        {
                            paramsSubs.Add(new SimpleParam()
                            {
                                Name = defSub.Name, Index = defSub.Options.Order, Value = defSub.DefaultValue?.ToString()
                            });
                        }
                        else if (defSub.Type.IsEnumerable())
                        {
                            paramsSubs.Add(new ArrayParam()
                            {
                                Name = defSub.Name, Index = defSub.Options.Order
                            });
                        }
                        else
                        {
                            paramsSubs.Add(new ComplexParam()
                            {
                                Name = defSub.Name, Index = defSub.Options.Order
                            });
                        }
                    }
                }

                return(new ComplexParam()
                {
                    SubParams = paramsSubs.ToArray()
                });
            }

            return(param);
        }
Пример #2
0
        private IParsedParam Internal(IParsedParam param, ParameterDefinition def)
        {
            if (param == null)
            {
                return(null);
            }

            switch (param)
            {
            case ArrayParam _: return(param);

            case SimpleParam sp:

                if (def.Type?.IsBuiltIn(true) == true)
                {
                    return(sp);
                }

                return(new ComplexParam()
                {
                    Name = def.Name,
                    Index = 0,
                    SubParams = new[] { sp }
                });

            case ComplexParam cp:
            {
                var subParamsDef = def.SubParams;
                if (subParamsDef.All(s => s.Type.IsBuiltIn(true)))
                {
                    _complexParamChecks.Checks(cp, def.Type, def.SubParams);
                    return(cp);
                }

                IEnumerable <IParsedParam> getSubparams(ComplexParam localcp)
                {
                    for (int i = 0; i < localcp.SubParams.Length; i++)
                    {
                        var item = localcp.SubParams[i];

                        var defSub = def.SubParams.FirstOrDefault(k => k.Name.ToLower() == item.Name.ToLower());
                        if (defSub == null)
                        {
                            defSub = def.SubParams.FirstOrDefault(k => k.Options.Order == item.Index);
                        }

                        var newitem = Internal(item, defSub);
                        yield return(newitem);
                    }
                };

                if (def.SubParams.Length == 1)
                {
                    //nesting just to check properly
                    var temp = new ComplexParam()
                    {
                        Name      = "",
                        SubParams = new[] { cp }
                    };

                    _complexParamChecks.Checks(temp, def.Type, def.SubParams);

                    var subparams = getSubparams(temp).ToArray();
                    return(subparams.First());
                }
                else
                {
                    _complexParamChecks.Checks(cp, def.Type, def.SubParams);

                    var subparams = getSubparams(cp).ToArray();

                    return(new ComplexParam()
                        {
                            Name = def.Name,
                            Index = 0,
                            SubParams = subparams
                        });
                }
            }
            }

            throw new NotSupportedException();
        }