コード例 #1
0
        public bool TryResolve(object value, IMessagePropertyValueFactory nest, out MessagePropertyValue result)
        {
            if (value is Interval interval)
            {
                if (interval.HasStart)
                {
                    // ReSharper disable once ConvertIfStatementToConditionalTernaryExpression
                    if (interval.HasEnd)
                    {
                        // start and end
                        result = nest.CreatePropertyValue(new { interval.Start, interval.End }, PropertyResolvingMode.Destructure);
                    }
                    else
                    {
                        // start only
                        result = nest.CreatePropertyValue(new { interval.Start }, PropertyResolvingMode.Destructure);
                    }
                }
                else if (interval.HasEnd)
                {
                    // end only
                    result = nest.CreatePropertyValue(new { interval.End }, PropertyResolvingMode.Destructure);
                }
                else
                {
                    // neither
                    result = new StructureValue(new MessageProperty[0]);
                }

                return(true);
            }

            result = null;
            return(false);
        }
コード例 #2
0
 private static void CheckParams(string name, MessagePropertyValue value)
 {
     if (string.IsNullOrWhiteSpace(name))
     {
         throw new ArgumentNullException(nameof(name));
     }
     if (value == null)
     {
         throw new ArgumentNullException(nameof(value));
     }
 }
コード例 #3
0
        public bool TryResolve(object value, IMessagePropertyValueFactory nest, out MessagePropertyValue result)
        {
            if (value is DateTimeZone dtz)
            {
                result = nest.CreatePropertyValue(dtz.Id, PropertyResolvingMode.Destructure);
                return(true);
            }

            result = null;
            return(false);
        }
コード例 #4
0
        public static bool TryResolve(this IScalarResolveRule[] rules, object value, out MessagePropertyValue result)
        {
            result = null;
            foreach (var rule in rules)
            {
                if (rule.TryResolve(value, out result))
                {
                    return(true);
                }
            }

            return(false);
        }
コード例 #5
0
        public bool TryResolve(object value, out MessagePropertyValue result)
        {
            if (value is byte[] valueBytes)
            {
                result = valueBytes.Length <= MaxLengthOfArray
                    ? new ScalarValue(valueBytes.ToArray())
                    : new ScalarValue($"{valueBytes.Take(16).Select(_ => _.ToString("X2"))}...({valueBytes.Length} bytes)");
                return(true);
            }

            result = null;
            return(false);
        }
コード例 #6
0
        public bool TryResolve(object value, IMessagePropertyValueFactory nest, out MessagePropertyValue result)
        {
            if (value is T t)
            {
                Validator?.Invoke(t);

                result = nest.CreatePropertyValue(Pattern.Format(t), PropertyResolvingMode.Default);
                return(true);
            }

            result = null;
            return(false);
        }
コード例 #7
0
ファイル: TypeFeeler.cs プロジェクト: alexinea/Cosmos.Logging
        public static bool TryResolveToEnumerable(object value, PropertyResolvingMode mode, NestParameterResolver nest, Type typeOfValue,
                                                  int maxLoopCountForCollection, out MessagePropertyValue result)
        {
            if (value is IEnumerable __enumerable)
            {
                if (TryGetDictionary(value, typeOfValue, out var dictionary))
                {
                    result = new DictionaryValue(DictionaryElements());
                    return(true);

                    IEnumerable <KeyValuePair <ScalarValue, MessagePropertyValue> > DictionaryElements()
                    {
                        var __index = 0;

                        foreach (DictionaryEntry __item in dictionary)
                        {
                            if (++__index > maxLoopCountForCollection)
                            {
                                yield break;
                            }
                            if (nest.CreatePropertyValue(__item.Key, mode) is ScalarValue __key)
                            {
                                var __value = nest.CreatePropertyValue(__item.Value, mode);
                                yield return(new KeyValuePair <ScalarValue, MessagePropertyValue>(__key, __value));
                            }
                        }
                    }
                }

                result = new SequenceValue(SequenceElements());
                return(true);

                IEnumerable <MessagePropertyValue> SequenceElements()
                {
                    var __index = 0;

                    foreach (var __item in __enumerable)
                    {
                        if (++__index > maxLoopCountForCollection)
                        {
                            yield break;
                        }
                        yield return(nest.CreatePropertyValue(__item, mode));
                    }
                }
            }

            result = null;
            return(false);
        }
コード例 #8
0
        /// <inheritdoc />
        public bool TryResolve(object value, IMessagePropertyValueFactory nest, out MessagePropertyValue result)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            if (_conditionFunc(value.GetType()))
            {
                result = nest.CreatePropertyValue(_convertFunc(value), PropertyResolvingMode.Destructure);
                return(true);
            }

            result = null;
            return(false);
        }
コード例 #9
0
ファイル: TypeFeeler.cs プロジェクト: alexinea/Cosmos.Logging
        public static bool TryResolveToValueTuple(object value, PropertyResolvingMode mode, NestParameterResolver nest, Type typeOfValue,
                                                  out MessagePropertyValue result)
        {
            result = CheckingValueTypeDefinition(typeOfValue, value) ? new SequenceValue(SequenceElements()) : null;
            return(result != null);

            IEnumerable <MessagePropertyValue> SequenceElements()
            {
                var __fields = typeOfValue.GetTypeInfo().DeclaredFields.Where(f => f.IsPublic && !f.IsStatic);

                foreach (var __field in __fields)
                {
                    yield return(nest.CreatePropertyValue(__field.GetValue(value), mode));
                }
            }
        }
コード例 #10
0
        public static bool TryResolve(this IDestructureResolveRule[] rules, object value, PropertyResolvingMode mode,
                                      NestParameterResolver nest, out MessagePropertyValue result)
        {
            result = null;
            if (mode == PropertyResolvingMode.Destructure)
            {
                foreach (var rule in rules)
                {
                    if (rule.TryResolve(value, nest, out result))
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
コード例 #11
0
 public bool TryResolve(object value, out MessagePropertyValue result)
 {
     result = value.GetType().IsEnum ? new ScalarValue(value) : null;
     return(result != null);
 }
コード例 #12
0
 /// <inheritdoc />
 public bool TryResolve(object value, IMessagePropertyValueFactory nest, out MessagePropertyValue result)
 {
     result = IsBasicReflectionType(value) ? new ScalarValue(value) : null;
     return(result != null);
 }
コード例 #13
0
 public ExtraMessageProperty(string name, MessagePropertyValue value)
 {
     CheckParams(name, value);
     Name  = name;
     Value = value;
 }
コード例 #14
0
ファイル: TypeFeeler.cs プロジェクト: alexinea/Cosmos.Logging
        public static bool TryResolveCompilerGeneratedType(object value, PropertyResolvingMode mode, NestParameterResolver nest, Type typeOfValue,
                                                           bool raiseException, int positionalValue, out MessagePropertyValue result)
        {
            if (mode == PropertyResolvingMode.Destructure)
            {
                result = new StructureValue(StructureElements(), Tag());
                return(true);

                string Tag()
                {
                    var __tag = typeOfValue.Name;

                    if (string.IsNullOrWhiteSpace(__tag) || typeOfValue.IsCompilerGeneratedType())
                    {
                        __tag = null;
                    }
                    return(__tag);
                }

                IEnumerable <MessageProperty> StructureElements()
                {
                    foreach (var property in value.GetType().GetPropertiesRecursive())
                    {
                        dynamic propertyValue;
                        try {
                            propertyValue = property.GetValue(value);
                        }
                        catch (TargetParameterCountException) {
                            InternalLogger.WriteLine("The property accessor '{0}' is a non-default indexer", property);
                            continue;
                        }
                        catch (TargetInvocationException ex) {
                            InternalLogger.WriteLine("The property accessor '{0}' threw exception: {1}", property, ex);
                            if (raiseException)
                            {
                                throw;
                            }
                            propertyValue = $"Threw an exception at: {ex.InnerException?.GetType().Name}";
                        }

                        yield return(new MessageProperty(property.Name, positionalValue, nest.CreatePropertyValue(propertyValue, PropertyResolvingMode.Destructure)));
                    }
                }
            }

            result = null;
            return(false);
        }
コード例 #15
0
 public bool TryResolve(object value, IMessagePropertyValueFactory factory, out MessagePropertyValue result)
 {
     result = value is Delegate @delegate ? new ScalarValue(@delegate) : null;
     return(result != null);
 }