Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions VYaml.SourceGenerator/Emitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,14 @@ static void EmitCCtor(TypeMeta typeMeta, CodeWriter codeWriter)
static bool TryEmitRegisterMethod(TypeMeta typeMeta, CodeWriter codeWriter, in SourceProductionContext context)
{
codeWriter.AppendLine("[VYaml.Annotations.Preserve]");
using var _ = codeWriter.BeginBlockScope("public static void __RegisterVYamlFormatter()");

// Use 'new' modifier when base type also has [YamlObject] to avoid CS0108
var hasBaseYamlObject = typeMeta.Symbol.BaseType != null &&
typeMeta.Symbol.BaseType.GetAttributes().Any(a =>
a.AttributeClass != null &&
a.AttributeClass.ToDisplayString() == "VYaml.Annotations.YamlObjectAttribute");
var modifier = hasBaseYamlObject ? "public static new" : "public static";
using var _ = codeWriter.BeginBlockScope($"{modifier} void __RegisterVYamlFormatter()");

var typeName = typeMeta.TypeName.Replace("<", "_").Replace(">", "_").Replace(",", "_").Replace(" ", "");
codeWriter.AppendLine($"global::VYaml.Serialization.GeneratedResolver.Register(new {typeName}GeneratedFormatter());");
return true;
Expand All @@ -232,8 +238,8 @@ static bool TryEmitFormatter(
? typeMeta.FullTypeName
: $"{typeMeta.FullTypeName}?";

codeWriter.AppendLine("[VYaml.Annotations.Preserve]");
codeWriter.AppendLine("[VYaml.Annotations.Preserve]");

var typeName = typeMeta.TypeName.Replace("<", "_").Replace(">", "_").Replace(",", "_").Replace(" ", "");
using var _ = codeWriter.BeginBlockScope($"public class {typeName}GeneratedFormatter : IYamlFormatter<{returnType}>");

Expand Down
2 changes: 1 addition & 1 deletion VYaml.SourceGenerator/Shims/CSharpSyntaxHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
}
else
{
targets.Append(container);
targets.Append(container!);

Check warning on line 59 in VYaml.SourceGenerator/Shims/CSharpSyntaxHelper.cs

View workflow job for this annotation

GitHub Actions / test-dotnet

Possible null reference argument for parameter 'item' in 'void ValueListBuilder<SyntaxNode>.Append(SyntaxNode item)'.
}
}

Expand Down
42 changes: 21 additions & 21 deletions VYaml/Serialization/Formatters/ByteArrayFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@

namespace VYaml.Serialization
{
internal static class SpanExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToBase64String(this ReadOnlySpan<byte> bytes,
Base64FormattingOptions options = Base64FormattingOptions.None)
{
#if NETSTANDARD2_0
return Convert.ToBase64String(bytes.ToArray(), options);
#else
return Convert.ToBase64String(bytes, options);
#endif
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToBase64String(this Span<byte> bytes,
Base64FormattingOptions options = Base64FormattingOptions.None)
{
ReadOnlySpan<byte> robytes = bytes;
return robytes.ToBase64String(options);
}
internal static class SpanExtensions
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToBase64String(this ReadOnlySpan<byte> bytes,
Base64FormattingOptions options = Base64FormattingOptions.None)
{
#if NETSTANDARD2_0
return Convert.ToBase64String(bytes.ToArray(), options);
#else
return Convert.ToBase64String(bytes, options);
#endif
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToBase64String(this Span<byte> bytes,
Base64FormattingOptions options = Base64FormattingOptions.None)
{
ReadOnlySpan<byte> robytes = bytes;
return robytes.ToBase64String(options);
}
}

public sealed class ByteArrayFormatter : IYamlFormatter<byte[]?>
Expand Down Expand Up @@ -126,7 +126,7 @@
public void Serialize(ref Utf8YamlEmitter emitter, ArraySegment<byte> value, YamlSerializationContext context)
{
emitter.WriteString(
Convert.ToBase64String(value.Array, value.Offset, value.Count, Base64FormattingOptions.None),
Convert.ToBase64String(value.Array!, value.Offset, value.Count, Base64FormattingOptions.None),

Check warning on line 129 in VYaml/Serialization/Formatters/ByteArrayFormatter.cs

View workflow job for this annotation

GitHub Actions / test-dotnet

Possible null reference argument for parameter 'inArray' in 'string Convert.ToBase64String(byte[] inArray, int offset, int length, Base64FormattingOptions options)'.
ScalarStyle.Plain);
}

Expand Down
2 changes: 1 addition & 1 deletion VYaml/Serialization/Formatters/EnumAsStringFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
// TODO:
static class EnumAsStringNonGenericHelper
{
static readonly ConcurrentDictionary<object, string> AliasStringValues = new();
static readonly ConcurrentDictionary<object, string?> AliasStringValues = new();
static readonly ConcurrentDictionary<Type, NamingConvention?> NamingConventionsByType = new();

static readonly Func<object, Type, string?> AliasStringValueFactory = AnalyzeAliasStringValue;
static readonly Func<Type, NamingConvention?> NamingConventionFactory = AnalyzeNamingConventionByType;

public static string? GetAliasStringValue(Type type, object value) => AliasStringValues.GetOrAdd(value, AliasStringValueFactory!, type);

Check warning on line 22 in VYaml/Serialization/Formatters/EnumAsStringFormatter.cs

View workflow job for this annotation

GitHub Actions / test-dotnet

Argument of type 'ConcurrentDictionary<object, string>' cannot be used for parameter 'dict' of type 'ConcurrentDictionary<object, string?>' in 'string? NetStandardCompat.GetOrAdd<object, string?, Type>(ConcurrentDictionary<object, string?> dict, object key, Func<object, Type, string?> fkt, Type param)' due to differences in the nullability of reference types.
public static NamingConvention? GetNamingConventionByType(Type type) => NamingConventionsByType.GetOrAdd(type, NamingConventionFactory);

public static void Serialize(ref Utf8YamlEmitter emitter, Type type, object value, YamlSerializationContext context)
Expand All @@ -39,7 +39,7 @@
while (!mutator.TryMutate(name.AsSpan(), destination, out written))
{
// ReSharper disable once StackAllocInsideLoop
destination = stackalloc char[destination.Length * 2];

Check warning on line 42 in VYaml/Serialization/Formatters/EnumAsStringFormatter.cs

View workflow job for this annotation

GitHub Actions / test-dotnet

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)
}

emitter.WriteString(destination[..written].ToString());
Expand Down Expand Up @@ -94,12 +94,12 @@
{
var mutator = NamingConventionMutator.Of(NamingConventionByType ?? YamlSerializerOptions.DefaultNamingConvention);
var name = Enum.GetName(type, value)!;
Span<char> destination = stackalloc char[name.Length];

Check warning on line 97 in VYaml/Serialization/Formatters/EnumAsStringFormatter.cs

View workflow job for this annotation

GitHub Actions / test-dotnet

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)
int written;
while (!mutator.TryMutate(name.AsSpan(), destination, out written))
{
// ReSharper disable once StackAllocInsideLoop
destination = stackalloc char[destination.Length * 2];

Check warning on line 102 in VYaml/Serialization/Formatters/EnumAsStringFormatter.cs

View workflow job for this annotation

GitHub Actions / test-dotnet

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)
}

var stringValue = destination[..written].ToString();
Expand Down Expand Up @@ -131,7 +131,7 @@
while (!mutator.TryMutate(stringValue.AsSpan(), buffer, out bytesWritten))
{
// ReSharper disable once StackAllocInsideLoop
buffer = stackalloc char[buffer.Length * 2];

Check warning on line 134 in VYaml/Serialization/Formatters/EnumAsStringFormatter.cs

View workflow job for this annotation

GitHub Actions / test-dotnet

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)
}

unsafe
Expand Down Expand Up @@ -164,7 +164,7 @@
while (!mutator.TryMutate(scalar.AsSpan(), buffer, out bytesWritten))
{
// ReSharper disable once StackAllocInsideLoop
buffer = stackalloc char[buffer.Length * 2];

Check warning on line 167 in VYaml/Serialization/Formatters/EnumAsStringFormatter.cs

View workflow job for this annotation

GitHub Actions / test-dotnet

Potential stack overflow. Move the stackalloc out of the loop. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca2014)
}

var mutatedScalar = buffer[..bytesWritten].ToString();
Expand Down
4 changes: 2 additions & 2 deletions VYaml/Serialization/Formatters/PrimitiveObjectFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@
break;
case ParseEventType.MappingStart:
{
var dict = new Dictionary<object?, object?>();
var dict = new Dictionary<object, object?>();

Check warning on line 169 in VYaml/Serialization/Formatters/PrimitiveObjectFormatter.cs

View workflow job for this annotation

GitHub Actions / test-dotnet

The type 'object?' cannot be used as type parameter 'TKey' in the generic type or method 'Dictionary<TKey, TValue>'. Nullability of type argument 'object?' doesn't match 'notnull' constraint.
parser.Read();
while (!parser.End && parser.CurrentEventType != ParseEventType.MappingEnd)
{
var key = context.DeserializeWithAlias(this, ref parser);
var value = context.DeserializeWithAlias(this, ref parser);
dict.Add(key, value);
dict.Add(key!, value);
}
parser.ReadWithVerify(ParseEventType.MappingEnd);
result = dict;
Expand Down
Loading