Skip to content

Commit

Permalink
Un-yield yiel return and yield break uses
Browse files Browse the repository at this point in the history
# Motivations
`yield return` and `yield break` are not compatable in mono, they must be un-yielded for compatability with FiveM
  • Loading branch information
Twinki14 committed Dec 30, 2023
1 parent d5db0e3 commit 998ea48
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 52 deletions.
14 changes: 9 additions & 5 deletions src/Serilog/Capturing/GetablePropertyFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,32 @@ static class GetablePropertyFinder
internal static IEnumerable<PropertyInfo> GetPropertiesRecursive(this Type type)
{
var seenNames = new HashSet<string>();
var result = new List<PropertyInfo>();

var currentTypeInfo = type.GetTypeInfo();

while (currentTypeInfo.AsType() != typeof(object))
{
var unseenProperties = currentTypeInfo.DeclaredProperties.Where(p => p.CanRead &&
p.GetMethod!.IsPublic && !p.GetMethod.IsStatic &&
(p.Name != "Item" || p.GetIndexParameters().Length == 0) && !seenNames.Contains(p.Name));
var unseenProperties = currentTypeInfo.DeclaredProperties
.Where(p => p.CanRead &&
p.GetMethod!.IsPublic && !p.GetMethod.IsStatic &&
(p.Name != "Item" || p.GetIndexParameters().Length == 0) && !seenNames.Contains(p.Name));

foreach (var propertyInfo in unseenProperties)
{
seenNames.Add(propertyInfo.Name);
yield return propertyInfo;
result.Add(propertyInfo);
}

var baseType = currentTypeInfo.BaseType;
if (baseType == null)
{
yield break;
return result;
}

currentTypeInfo = baseType.GetTypeInfo();
}

return result;
}
}
84 changes: 43 additions & 41 deletions src/Serilog/Capturing/PropertyValueConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,59 +173,61 @@ bool TryConvertEnumerable(object value, Destructuring destructuring, [NotNullWhe

if (value is IEnumerable enumerable)
{
// Only dictionaries with 'scalar' keys are permitted, as
// more complex keys may not serialize to unique values for
// representation in sinks. This check strengthens the expectation
// that resulting dictionary is representable in JSON as well
// as richer formats (e.g. XML, .NET type-aware...).
// Only actual dictionaries are supported, as arbitrary types
// can implement multiple IDictionary interfaces and thus introduce
// multiple different interpretations.
if (TryGetDictionary(value, valueType, out var dictionary))
{
result = new DictionaryValue(MapToDictionaryElements(dictionary, destructuring));
var dictionaryElements = MapToDictionaryElements(dictionary, destructuring);
result = new DictionaryValue(dictionaryElements);
return true;

IEnumerable<KeyValuePair<ScalarValue, LogEventPropertyValue>> MapToDictionaryElements(IDictionary dictionaryEntries, Destructuring destructure)
{
var count = 0;
foreach (DictionaryEntry entry in dictionaryEntries)
{
if (++count > _maximumCollectionCount)
{
yield break;
}

var pair = new KeyValuePair<ScalarValue, LogEventPropertyValue>(
(ScalarValue)_depthLimiter.CreatePropertyValue(entry.Key, destructure),
_depthLimiter.CreatePropertyValue(entry.Value, destructure));

if (pair.Key.Value != null)
yield return pair;
}
}
}

result = new SequenceValue(MapToSequenceElements(enumerable, destructuring));
var sequenceElements = MapToSequenceElements(enumerable, destructuring);
result = new SequenceValue(sequenceElements);
return true;
}

result = null;
return false;
}

IEnumerable<KeyValuePair<ScalarValue, LogEventPropertyValue>> MapToDictionaryElements(IDictionary dictionaryEntries, Destructuring destructure)
{
var elements = new List<KeyValuePair<ScalarValue, LogEventPropertyValue>>();
var count = 0;

IEnumerable<LogEventPropertyValue> MapToSequenceElements(IEnumerable sequence, Destructuring destructure)
foreach (DictionaryEntry entry in dictionaryEntries)
{
if (++count > _maximumCollectionCount)
{
var count = 0;
foreach (var element in sequence)
{
if (++count > _maximumCollectionCount)
{
yield break;
}
break;
}

yield return _depthLimiter.CreatePropertyValue(element, destructure);
}
var pair = new KeyValuePair<ScalarValue, LogEventPropertyValue>(
(ScalarValue)_depthLimiter.CreatePropertyValue(entry.Key, destructure),
_depthLimiter.CreatePropertyValue(entry.Value, destructure));

if (pair.Key.Value != null)
elements.Add(pair);
}

return elements;
}

IEnumerable<LogEventPropertyValue> MapToSequenceElements(IEnumerable sequence, Destructuring destructure)
{
var elements = new List<LogEventPropertyValue>();
var count = 0;

foreach (var element in sequence)
{
if (++count > _maximumCollectionCount)
{
break;
}

elements.Add(_depthLimiter.CreatePropertyValue(element, destructure));
}

result = null;
return false;
return elements;
}

#if FEATURE_ITUPLE
Expand Down
14 changes: 8 additions & 6 deletions src/Serilog/Parsing/MessageTemplateParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ public MessageTemplate Parse(string messageTemplate)

static IEnumerable<MessageTemplateToken> Tokenize(string messageTemplate)
{
var result = new List<MessageTemplateToken>();

if (messageTemplate.Length == 0)
{
yield return new TextToken("", 0);
yield break;
result.Add(new TextToken("", 0));
return result;
}

var nextIndex = 0;
Expand All @@ -50,18 +52,18 @@ static IEnumerable<MessageTemplateToken> Tokenize(string messageTemplate)
var beforeText = nextIndex;
var tt = ParseTextToken(nextIndex, messageTemplate, out nextIndex);
if (nextIndex > beforeText)
yield return tt;
result.Add(tt);

if (nextIndex == messageTemplate.Length)
yield break;
return result;

var beforeProp = nextIndex;
var pt = ParsePropertyToken(nextIndex, messageTemplate, out nextIndex);
if (beforeProp < nextIndex)
yield return pt;
result.Add(pt);

if (nextIndex == messageTemplate.Length)
yield break;
return result;
}
}

Expand Down

0 comments on commit 998ea48

Please sign in to comment.