//
// PacketConditionalListIndexAttributeGenerator.cs
//
// Copyright (c) František Boháček. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.CodeDom.Compiler;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using NosSmooth.PacketSerializersGenerator.Data;
using NosSmooth.PacketSerializersGenerator.Errors;
using NosSmooth.PacketSerializersGenerator.Extensions;
namespace NosSmooth.PacketSerializersGenerator.AttributeGenerators;
///
public class PacketConditionalListIndexAttributeGenerator : IParameterGenerator
{
private readonly PacketListIndexAttributeGenerator _listIndexGenerator;
private readonly InlineTypeConverterGenerator _inlineTypeConverterGenerators;
///
/// Initializes a new instance of the class.
///
/// The generator for types.
public PacketConditionalListIndexAttributeGenerator(InlineTypeConverterGenerator inlineTypeConverterGenerators)
{
_inlineTypeConverterGenerators = inlineTypeConverterGenerators;
_listIndexGenerator = new PacketListIndexAttributeGenerator(inlineTypeConverterGenerators)
{
PacketListIndexAttributeFullName = PacketConditionalListIndexAttributeFullName
};
}
///
/// Gets the full name of the packet index attribute.
///
public static string PacketConditionalListIndexAttributeFullName
=> "NosSmooth.PacketSerializer.Abstractions.Attributes.PacketConditionalListIndexAttribute";
///
public bool ShouldHandle(ParameterInfo parameter)
=> parameter.Attributes.Any(x => x.FullName == PacketConditionalListIndexAttributeFullName);
///
public IError? CheckParameter(PacketInfo packet, ParameterInfo parameter)
{
if (!parameter.Nullable)
{
return new DiagnosticError
(
"SGNull",
"Conditional parameters must be nullable",
"The parameter {0} in {1} has to be nullable, because it is conditional.",
parameter.Parameter.SyntaxTree,
parameter.Parameter.FullSpan,
new List