Esempio n. 1
0
        public static Object GetMappedValue(object map, string key)
        {
            if (map == null)
            {
                return null;
            }

            var magicMap = MagicMarker.GetStringDictionary(map);
            return magicMap.Get(key);
        }
        private JObject EncodeMap(MapSchema schema, object value)
        {
            var valueType = schema.ValueSchema;
            var valueDict = MagicMarker.GetStringDictionary(value);
            var jvalue    = new JObject();

            foreach (var valueKeyPair in valueDict)
            {
                jvalue.Add(new JProperty(valueKeyPair.Key, Encode(valueType, valueKeyPair.Value)));
            }

            return(jvalue);
        }
Esempio n. 3
0
        public static bool GetMappedPropertyExists(Object value, String key)
        {
            if (value == null)
            {
                return(false);
            }
            if (value is DataMap)
            {
                return(((DataMap)value).ContainsKey(key));
            }
            if (value.GetType().IsGenericStringDictionary())
            {
                return(MagicMarker.GetStringDictionary(value).ContainsKey(key));
            }

            return(false);
        }
Esempio n. 4
0
        public static Object GetMappedPropertyValue(Object value, String key)
        {
            if (value == null)
            {
                return(null);
            }
            if (value is DataMap)
            {
                return(((DataMap)value).Get(key));
            }
            if (value.GetType().IsGenericStringDictionary())
            {
                return(MagicMarker.GetStringDictionary(value).Get(key));
            }

            return(null);
        }
Esempio n. 5
0
        public static IDictionary <string, object> AsStringDictionary(this object value)
        {
            if (value == null)
            {
                return(null);
            }
            if (value is IDictionary <string, object> )
            {
                return((IDictionary <string, object>)value);
            }
            if (value.GetType().IsGenericDictionary())
            {
                return(MagicMarker.GetStringDictionary(value));
            }

            throw new ArgumentException("invalid value for string dictionary", "value");
        }
Esempio n. 6
0
        private static void RecursiveRender(EventBean theEvent, StringBuilder buf, int level, RendererMeta meta, RendererMetaOptions rendererOptions)
        {
            var delimiter = "";

            var renderActions = new SortedList <string, Action>();

            // simple properties
            var simpleProps = meta.SimpleProperties;

            if (rendererOptions.Renderer == null)
            {
                foreach (var simpleProp in simpleProps.OrderBy(prop => prop.Name))
                {
                    var name  = simpleProp.Name;
                    var value = simpleProp.Getter.Get(theEvent);
                    renderActions.Add(name, () =>
                    {
                        WriteDelimitedIndentedProp(buf, delimiter, level, name);
                        simpleProp.Output.Render(value, buf);
                        delimiter = COMMA_DELIMITER_NEWLINE;
                    });
                }
            }
            else
            {
                var context = rendererOptions.RendererContext;
                context.SetStringBuilderAndReset(buf);
                foreach (var simpleProp in simpleProps.OrderBy(prop => prop.Name))
                {
                    var name  = simpleProp.Name;
                    var value = simpleProp.Getter.Get(theEvent);
                    renderActions.Add(name, () =>
                    {
                        WriteDelimitedIndentedProp(buf, delimiter, level, simpleProp.Name);
                        context.DefaultRenderer = simpleProp.Output;
                        context.PropertyName    = simpleProp.Name;
                        context.PropertyValue   = value;
                        rendererOptions.Renderer.Render(context);
                        delimiter = COMMA_DELIMITER_NEWLINE;
                    });
                }
            }

            var indexProps = meta.IndexProperties;

            foreach (var indexProp in indexProps.OrderBy(prop => prop.Name))
            {
                var name = indexProp.Name;

                renderActions.Add(
                    name, () =>
                {
                    WriteDelimitedIndentedProp(buf, delimiter, level, name);

                    var value = indexProp.Getter.Get(theEvent);
                    if (value == null)
                    {
                        buf.Append("null");
                    }
                    else if (value is string)
                    {
                        if (rendererOptions.Renderer == null)
                        {
                            indexProp.Output.Render(value, buf);
                        }
                        else
                        {
                            var context = rendererOptions.RendererContext;
                            context.SetStringBuilderAndReset(buf);
                            context.DefaultRenderer = indexProp.Output;
                            context.PropertyName    = name;
                            context.PropertyValue   = value;
                            rendererOptions.Renderer.Render(context);
                        }
                    }
                    else
                    {
                        var asArray = value as Array;
                        if (asArray == null)
                        {
                            buf.Append("[]");
                        }
                        else
                        {
                            buf.Append('[');

                            var arrayDelimiter = "";

                            if (rendererOptions.Renderer == null)
                            {
                                for (var i = 0; i < asArray.Length; i++)
                                {
                                    var arrayItem = asArray.GetValue(i);
                                    buf.Append(arrayDelimiter);
                                    indexProp.Output.Render(arrayItem, buf);
                                    arrayDelimiter = ", ";
                                }
                            }
                            else
                            {
                                var context = rendererOptions.RendererContext;
                                context.SetStringBuilderAndReset(buf);
                                for (var i = 0; i < asArray.Length; i++)
                                {
                                    var arrayItem = asArray.GetValue(i);
                                    buf.Append(arrayDelimiter);
                                    context.PropertyName         = indexProp.Name;
                                    context.PropertyValue        = arrayItem;
                                    context.IndexedPropertyIndex = i;
                                    context.DefaultRenderer      = indexProp.Output;
                                    rendererOptions.Renderer.Render(context);
                                    arrayDelimiter = ", ";
                                }
                            }

                            buf.Append(']');
                        }
                    }

                    delimiter = COMMA_DELIMITER_NEWLINE;
                });
            }

            var mappedProps = meta.MappedProperties;

            foreach (var mappedProp in mappedProps.OrderBy(prop => prop.Name))
            {
                var name  = mappedProp.Name;
                var value = mappedProp.Getter.Get(theEvent);

                if ((value != null) && (!(value.GetType().IsGenericStringDictionary())))
                {
                    Log.Warn("Property '" + mappedProp.Name + "' expected to return Map and returned " + value.GetType() + " instead");
                    continue;
                }

                renderActions.Add(
                    name, () =>
                {
                    WriteDelimitedIndentedProp(buf, delimiter, level, mappedProp.Name);

                    if (value == null)
                    {
                        buf.Append("null");
                        buf.Append(NEWLINE);
                    }
                    else
                    {
                        var map = MagicMarker.GetStringDictionary(value);
                        if (map.IsEmpty())
                        {
                            buf.Append("{}");
                            buf.Append(NEWLINE);
                        }
                        else
                        {
                            buf.Append('{');
                            buf.Append(NEWLINE);

                            var localDelimiter = "";
                            foreach (var entry in map)
                            {
                                if (entry.Key == null)
                                {
                                    continue;
                                }

                                buf.Append(localDelimiter);
                                Ident(buf, level + 1);
                                buf.Append('\"');
                                buf.Append(entry.Key);
                                buf.Append("\": ");

                                if (entry.Value == null)
                                {
                                    buf.Append("null");
                                }
                                else
                                {
                                    var outRenderer =
                                        OutputValueRendererFactory.GetOutputValueRenderer(
                                            entry.Value.GetType(), rendererOptions);
                                    if (rendererOptions.Renderer == null)
                                    {
                                        outRenderer.Render(entry.Value, buf);
                                    }
                                    else
                                    {
                                        var context = rendererOptions.RendererContext;
                                        context.SetStringBuilderAndReset(buf);
                                        context.PropertyName      = mappedProp.Name;
                                        context.PropertyValue     = entry.Value;
                                        context.MappedPropertyKey = entry.Key;
                                        context.DefaultRenderer   = outRenderer;
                                        rendererOptions.Renderer.Render(context);
                                    }
                                }

                                localDelimiter = COMMA_DELIMITER_NEWLINE;
                            }

                            buf.Append(NEWLINE);
                            Ident(buf, level);
                            buf.Append('}');
                        }
                    }

                    delimiter = COMMA_DELIMITER_NEWLINE;
                });
            }

            var nestedProps = meta.NestedProperties;

            foreach (var nestedProp in nestedProps.OrderBy(prop => prop.Name))
            {
                var name  = nestedProp.Name;
                var value = nestedProp.Getter.GetFragment(theEvent);

                renderActions.Add(
                    name, () =>
                {
                    WriteDelimitedIndentedProp(buf, delimiter, level, nestedProp.Name);

                    if (value == null)
                    {
                        buf.Append("null");
                    }
                    else if (!nestedProp.IsArray)
                    {
                        if (!(value is EventBean))
                        {
                            Log.Warn(
                                "Property '" + nestedProp.Name + "' expected to return EventBean and returned " +
                                value.GetType() + " instead");
                            buf.Append("null");
                            return;
                        }

                        var nestedEventBean = (EventBean)value;
                        buf.Append('{');
                        buf.Append(NEWLINE);

                        RecursiveRender(nestedEventBean, buf, level + 1, nestedProp.Metadata, rendererOptions);

                        Ident(buf, level);
                        buf.Append('}');
                    }
                    else
                    {
                        if (!(value is EventBean[]))
                        {
                            Log.Warn(
                                "Property '" + nestedProp.Name + "' expected to return EventBean[] and returned " +
                                value.GetType() + " instead");
                            buf.Append("null");
                            return;
                        }

                        var arrayDelimiterBuf = new StringBuilder();
                        arrayDelimiterBuf.Append(',');
                        arrayDelimiterBuf.Append(NEWLINE);
                        Ident(arrayDelimiterBuf, level + 1);

                        var nestedEventArray = (EventBean[])value;
                        var arrayDelimiter   = "";
                        buf.Append('[');

                        for (var i = 0; i < nestedEventArray.Length; i++)
                        {
                            var arrayItem = nestedEventArray[i];
                            buf.Append(arrayDelimiter);
                            arrayDelimiter = arrayDelimiterBuf.ToString();

                            buf.Append('{');
                            buf.Append(NEWLINE);

                            RecursiveRender(arrayItem, buf, level + 2, nestedProp.Metadata, rendererOptions);

                            Ident(buf, level + 1);
                            buf.Append('}');
                        }

                        buf.Append(']');
                    }

                    delimiter = COMMA_DELIMITER_NEWLINE;
                });
            }

            foreach (var entry in renderActions)
            {
                entry.Value.Invoke();
            }

            buf.Append(NEWLINE);
        }