/// <summary>
        /// Determina se o tipo é suportado pelo algoritmo de serialização.
        /// </summary>
        /// <param name="item">O item analisado.</param>
        /// <returns>Verdadeiro se o item é serializável; Falso caso contrário.</returns>
        public static bool IsSerializable(object item)
        {
            if (item == null || item is PropertyMap)
            {
                return(true);
            }

            if (item is IEnumerable list && !(item is string))
            {
                if (item is IDictionary map)
                {
                    var isSerializable =
                        map.Keys.Cast <object>().All(IsStringCompatible) &&
                        map.Values.Cast <object>().All(IsSerializable);
                    return(isSerializable);
                }

                var elementType = TypeOf.CollectionElement(item);
                if (elementType.IsGenericType && elementType.GetGenericTypeDefinition() == typeof(KeyValuePair <,>))
                {
                    return(false);
                }

                return(list.Cast <object>().All(IsSerializable));
            }

            if (item.GetType().IsGenericType&& item.GetType().GetGenericTypeDefinition() == typeof(KeyValuePair <,>))
            {
                return(false);
            }

            return(item.GetType().IsValueType || IsStringCompatible(item));
        }
Ejemplo n.º 2
0
            public Bag NewBag()
            {
                var property     = properties.LastOrDefault();
                var propertyType = Host._GetPropertyType(property);
                var elementType  = TypeOf.CollectionElement(propertyType);

                return(new Bag {
                    ElementType = elementType
                });
            }
Ejemplo n.º 3
0
        private Entity FormatPayload(object payload)
        {
            var type   = payload.GetType();
            var entity = new Entity();

            if (payload is ICollection list)
            {
                entity.Add(Class.Records);

                var itemType = TypeOf.CollectionElement(type);
                var headers  = itemType.GetProperties().Select(x => (VName)x.Name).ToArray();

                entity.Add(new Property("__meta", new
                {
                    records = new
                    {
                        headers = headers.ToArray()
                    }
                }));

                foreach (var item in list)
                {
                    var child = FormatPayload(item);
                    entity.Add(child);
                }
            }
            else
            {
                entity.Add(new Class(type.FullName));
                entity.Add(Class.Record);

                var headers           = type.GetProperties().Select(x => (VName)x.Name).ToArray();
                var payloadProperties = Value.CreateObject(payload);

                entity.Add(new Property("__meta", new
                {
                    record = new
                    {
                        headers = headers.ToArray()
                    }
                }));

                entity.AddMany(payloadProperties);

                if (payload is Beans.Catalog)
                {
                    //entity.WithLinks().Add(new Link { Href = Req.RequestUri.Append("/Papers") });
                }
            }
            return(entity);
        }
Ejemplo n.º 4
0
            public Bag NewBag()
            {
                var property     = properties.LastOrDefault();
                var propertyType = Host._GetPropertyType(property);
                var elementType  = TypeOf.CollectionElement(propertyType);

                if (elementType == null || elementType == typeof(object) || elementType.IsInterface)
                {
                    elementType = typeof(HashMap);
                }
                return(new Bag
                {
                    ElementType = elementType
                });
            }
Ejemplo n.º 5
0
        public object CreateGraph(NodeModel document, Type graphType)
        {
            if (document.IsDocument)
            {
                document = ((DocumentModel)document).Root;
            }

            if (typeof(IGraphDeserializer).IsAssignableFrom(graphType))
            {
                var graph = (IGraphDeserializer)Activator.CreateInstance(graphType);
                graph.Deserialize(document, this);
                return(graph);
            }

            if (document.IsObject)
            {
                var graph = Activator.CreateInstance(graphType);
                foreach (var property in document.ChildProperties())
                {
                    SetProperty(graph, property.Name, property.Value);
                }
                return(graph);
            }

            if (document.IsCollection)
            {
                var list     = new ArrayList();
                var itemType = TypeOf.CollectionElement(graphType);
                foreach (var child in document.Children())
                {
                    var item = CreateGraph(child, itemType);
                    list.Add(item);
                }
                var graph = (ICollection)Change.To(list, graphType);
                return(graph);
            }

            return(document.SerializationValue);
        }
Ejemplo n.º 6
0
        private object[] CreateParameters(PaperContext context, IPaper paper, MethodInfo method, Args args, Entity form)
        {
            var methodArgs = new List <object>();

            var argKeys  = args?.Keys.ToList() ?? new List <string>();
            var formKeys = form?.Properties?.Keys.ToList() ?? new List <string>();

            foreach (var parameter in method.GetParameters())
            {
                var name = parameter.Name;

                if (typeof(Sort).IsAssignableFrom(parameter.ParameterType))
                {
                    var sort = (Sort)context.Paper.Create(objectFactory, parameter.ParameterType);
                    sort.CopyFrom(args);
                    methodArgs.Add(sort);
                    continue;
                }

                if (typeof(Page).IsAssignableFrom(parameter.ParameterType))
                {
                    var page = (Page)context.Paper.Create(objectFactory, parameter.ParameterType);
                    page.CopyFrom(args);
                    methodArgs.Add(page);
                    continue;
                }

                if (typeof(IFilter).IsAssignableFrom(parameter.ParameterType))
                {
                    var filter = CreateCompatibleFilter(context, args, parameter.ParameterType);
                    methodArgs.Add(filter);
                    continue;
                }

                string key = null;

                key = argKeys.FirstOrDefault(x => x.EqualsIgnoreCase(name));
                if (key != null)
                {
                    var value           = args[key];
                    var compatibleValue = CreateCompatibleValue(context, value, parameter.ParameterType);
                    methodArgs.Add(compatibleValue);
                    argKeys.Remove(key);
                    continue;
                }

                key = formKeys.FirstOrDefault(x => x.EqualsIgnoreCase(name));
                if (key != null)
                {
                    var value           = form.Properties[key];
                    var compatibleValue = CreateCompatibleValue(context, value, parameter.ParameterType);
                    methodArgs.Add(compatibleValue);
                    formKeys.Remove(key);
                    continue;
                }

                if (Is.Collection(parameter.ParameterType))
                {
                    var records  = form.Children();
                    var itemType = TypeOf.CollectionElement(parameter.ParameterType);
                    var items    = records.Select(record =>
                                                  CreateCompatibleValue(context, record.Properties, itemType)
                                                  ).ToArray();
                    var compatibleValue = CreateCompatibleValue(context, items, parameter.ParameterType);
                    methodArgs.Add(compatibleValue);
                }
                else
                {
                    var record          = form?.Children().FirstOrDefault();
                    var compatibleValue = CreateCompatibleValue(context, record.Properties, parameter.ParameterType);
                    methodArgs.Add(compatibleValue);
                }
            }

            return(methodArgs.ToArray());
        }
Ejemplo n.º 7
0
        public static IFormatter MakeTable <T>(IEnumerable <T> records)
        {
            var recordType = TypeOf.CollectionElement(records);

            return(MakeTable(recordType));
        }
Ejemplo n.º 8
0
        private void RenderForm(Caller caller, PaperContext context, Entity entity, object[] args, object graph = null)
        {
            var href = new UriString(context.Path.Substring(1)).Append($"-{caller.Method.Name}");

            var action = new EntityAction();

            action.Name   = caller.Method.Name;
            action.Title  = caller.Method.Name.ChangeCase(TextCase.ProperCase);
            action.Href   = href;
            action.Method = MethodNames.Post;

            var parameters = caller.Method.GetParameters();

            for (var i = 0; i < parameters.Length; i++)
            {
                var parameter      = parameters[i];
                var parameterValue = caller.Args[i];

                var name = Conventions.MakeName(parameter.Name);

                var isValue = IsValue(parameter.ParameterType);
                var isArray = !isValue && typeof(IEnumerable).IsAssignableFrom(parameter.ParameterType);
                var isForm  = !isValue && !isArray && typeof(IForm).IsAssignableFrom(parameter.ParameterType);

                if (isValue)
                {
                    action.AddField($"Form.{name}", opt =>
                    {
                        opt.SetDefaults(parameter);
                        opt.SetHidden(true);
                        if (parameterValue != null)
                        {
                            opt.SetValue(parameterValue);
                        }
                    });
                }
                else if (isForm)
                {
                    var properties = parameter.ParameterType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                    foreach (var property in properties)
                    {
                        var fieldName  = Conventions.MakeName(property.Name);
                        var fieldValue = (parameterValue != null) ? property.GetValue(parameterValue) : null;
                        action.AddField($"Form.{name}.{fieldName}", opt =>
                        {
                            opt.SetDefaults(property);
                            if (fieldValue != null)
                            {
                                opt.SetValue(fieldValue);
                            }
                        });
                    }
                }
                else if (isArray)
                {
                    var elementType = TypeOf.CollectionElement(parameter.ParameterType);
                    var properties  = elementType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
                    var keys        = (
                        from property in properties
                        select Conventions.MakeName(property.Name)
                        ).ToArray();

                    action.AddField("Records", opt => opt

                                    // XXX: FIXME: Esta propriedade agora deve pertencer a uma entidade.
                                    //.SetTitle("Registros Afetados")

                                    .SetPlaceholder("Selecione os registros afetados")
                                    .SetType(FieldTypeNames.SelectRecord)
                                    .SetDataType(DataTypeNames.Record)
                                    .SetMultiline(true)
                                    .SetProvider(provider => provider
                                                 .AddRel(RelNames.Self)
                                                 .SetKeys(keys)
                                                 )
                                    .SetAllowMany()
                                    .SetRequired()
                                    );
                }
                else
                {
                    foreach (var propertyName in parameterValue._GetPropertyNames())
                    {
                        var property   = parameterValue._GetPropertyInfo(propertyName);
                        var fieldName  = Conventions.MakeName(propertyName);
                        var fieldValue = parameterValue._Get(propertyName);
                        action.AddField($"Record.{fieldName}", opt =>
                        {
                            opt.SetDefaults(property);
                            opt.SetHidden(true);
                            if (fieldValue != null)
                            {
                                opt.SetValue(fieldValue);
                            }
                        });
                    }
                }
            }

            entity.AddAction(action);
        }