~ruther/NosSmooth

ref: ba49ad7e1b0431be4a22b3209670e439050b2548 NosSmooth/Core/NosSmooth.PacketSerializersGenerator/Extensions/AttributeInfoExtensions.cs -rw-r--r-- 3.7 KiB
ba49ad7e — František Boháček Merge pull request #14 from Rutherther/packets-span 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
//
//  AttributeInfoExtensions.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.Diagnostics;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using NosSmooth.PacketSerializersGenerator.Data;

namespace NosSmooth.PacketSerializersGenerator.Extensions;

/// <summary>
/// Extension methods for <see cref="AttributeInfo"/>.
/// </summary>
public static class AttributeInfoExtensions
{
    /// <summary>
    /// Get value of a named parameter.
    /// </summary>
    /// <param name="attributeInfo">The attribute information.</param>
    /// <param name="name">The name of the parameter.</param>
    /// <param name="default">The default value to return if not found.</param>
    /// <typeparam name="TValue">The value type.</typeparam>
    /// <returns>The value of the attribute.</returns>
    public static TValue? GetNamedValue<TValue>(this AttributeInfo attributeInfo, string name, TValue? @default)
    {
        if (attributeInfo.NamedAttributeArguments.TryGetValue(name, out var value))
        {
            if (typeof(TValue) == typeof(string))
            {
                return (TValue?)(object?)value.VisualValue;
            }

            return (TValue?)value.RealValue;
        }

        return @default;
    }

    /// <summary>
    /// Get value of a named parameter.
    /// </summary>
    /// <param name="attributeInfo">The attribute information.</param>
    /// <param name="index">The index of the parameter.</param>
    /// <typeparam name="TValue">The value type.</typeparam>
    /// <returns>The value of the attribute.</returns>
    public static TValue? GetIndexedValue<TValue>(this AttributeInfo attributeInfo, int index)
    {
        var value = attributeInfo.IndexedAttributeArguments[index];

        if (typeof(TValue) == typeof(string))
        {
            return (TValue?)(object?)value?.VisualValue;
        }

        return (TValue?)value.RealValue;
    }

    /// <summary>
    /// Gets visual values of params parameters in the constructor.
    /// </summary>
    /// <param name="attributeInfo">The attribute info.</param>
    /// <param name="startingIndex">The index the values start at.</param>
    /// <returns>A list containing all the values.</returns>
    public static IReadOnlyList<string> GetParamsVisualValues(this AttributeInfo attributeInfo, int startingIndex)
    {
        if (attributeInfo.IndexedAttributeArguments.Count - 1 < startingIndex)
        {
            return Array.Empty<string>();
        }

        if (attributeInfo.IndexedAttributeArguments[startingIndex].IsArray)
        {
            return attributeInfo.IndexedAttributeArguments[startingIndex].GetArrayVisualValues();
        }

        return attributeInfo.IndexedAttributeArguments
            .Skip(startingIndex)
            .Select(x => x.VisualValue)
            .ToArray();
    }

    /// <summary>
    /// Gets the visual values of the array.
    /// </summary>
    /// <param name="attributeArgumentInfo">The attribute argument.</param>
    /// <returns>The list of the elements.</returns>
    public static IReadOnlyList<string> GetArrayVisualValues(this AttributeArgumentInfo attributeArgumentInfo)
    {
        var arrayCreation = attributeArgumentInfo.Argument.Expression as ArrayCreationExpressionSyntax;
        if (arrayCreation is null)
        {
            throw new ArgumentException($"The given attribute argument is not an array creation.", nameof(attributeArgumentInfo));
        }

        if (arrayCreation.Initializer is null)
        {
            return Array.Empty<string>();
        }

        return arrayCreation.Initializer.Expressions
            .Select(x => x.ToString())
            .ToArray();
    }
}
Do not follow this link