Exemple #1
0
        public static IResult <Unit> FillObject(object obj, JsonObject jsonObject)
        {
            var evaluator = new PropertyEvaluator(obj);

            foreach (var member in jsonObject)
            {
                var signature = member.Name.ToUpper1();
                if (evaluator.Contains(signature))
                {
                    var type = evaluator.Type(signature);
                    if (GetMember(type, member).If(out obj, out var exception))
                    {
                        evaluator[signature] = obj;
                    }
                    else
                    {
                        return(failure <Unit>(exception));
                    }
                }
            }

            return(Unit.Success());
        }
Exemple #2
0
        public static ObjectGraph Serialize(object obj, Predicate <string> exclude, StringStringHash signatures)
        {
            assert(() => obj).Must().Not.BeNull().OrThrow();

            var graph     = RootObjectGraph();
            var type      = obj.GetType();
            var evaluator = new PropertyEvaluator(obj);
            var infos     = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty);

            foreach (var info in infos)
            {
                var signature = info.Name;
                var childName = signature.ToCamel();
                if (evaluator.Attribute <SignatureAttribute>(signature).If(out var a))
                {
                    childName = a.Signature;
                }

                if (exclude(signature))
                {
                    continue;
                }

                if (((IHash <string, object>)evaluator).If(signature, out var objectValue))
                {
                    if (objectValue.IsNull())
                    {
                        continue;
                    }

                    var childType = evaluator.Type(signature);
                    graph[childName] = getGraph(objectValue.Some(), childName, childType, exclude, signatures);
                }
            }

            return(graph);
        }
        protected Result <object> fillObject(object emptyObject, string prefix, string suffix, string commandLine)
        {
            try
            {
                var evaluator = new PropertyEvaluator(emptyObject);
                if (prefix == "/")
                {
                    prefix = "//";
                }

                var pattern = $"'{prefix}' /({REGEX_PARAMETER}) ('{suffix}' | ['+-'] ('{suffix}' | $) | $); f";
                if (commandLine.Matches(pattern).If(out var result))
                {
                    var isFirstMatch = true;
                    foreach (var match in result)
                    {
                        var name = xmlToPascal(match.FirstGroup);
                        var(text, index, length) = match;
                        if (text.EndsWith("-") || text.EndsWith("+"))
                        {
                            length--;
                        }

                        var rest = commandLine.Drop(index + length);
                        if (evaluator.ContainsKey(name))
                        {
                            var type = evaluator.Type(name);
                            if (type == typeof(bool))
                            {
                                evaluator[name] = getBoolean(rest, suffix);
                            }
                            else if (type == typeof(string) || type == typeof(Maybe <string>) || type == typeof(FileName) || type == typeof(FolderName) ||
                                     type == typeof(Maybe <FileName>) || type == typeof(Maybe <FolderName>))
                            {
                                evaluator[name] = getString(rest, type);
                            }
                            else if (type == typeof(int))
                            {
                                evaluator[name] = getInt32(rest);
                            }
                            else if (type == typeof(float) || type == typeof(double))
                            {
                                evaluator[name] = getFloatingPoint(rest, type);
                            }
                            else if (type.IsEnum)
                            {
                                evaluator[name] = getEnum(rest, type);
                            }
                            else if (type == typeof(string[]))
                            {
                                evaluator[name] = getStringArray(rest);
                            }
                            else
                            {
                                return($"No value for {name}".Failure <object>());
                            }
                        }
                        else
                        {
                            if (emptyObject is IMissingArgument missingArgument && missingArgument.Handled(name, rest, isFirstMatch))
                            {
                                continue;
                            }

                            if (isFirstMatch)
                            {
                                isFirstMatch = false;
                                return($"Did not understand command '{name}'".Failure <object>());
                            }
                            else
                            {
                                return($"Did not understand argument '{name}".Failure <object>());
                            }
                        }
                    }
                }

                return(evaluator.Object.Success());
            }
            catch (Exception exception)
            {
                return(failure <object>(exception));
            }
        }