~ruther/NosSmooth

ref: c076813783b2051c6bdf20dd8862d4e322ec88ef NosSmooth/Packets/NosSmooth.PacketSerializersGenerator/Extensions/ParameterInfoExtensions.cs -rw-r--r-- 3.3 KiB
c0768137 — František Boháček feat: split packets definitions into separate assembly 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
//
//  ParameterInfoExtensions.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 Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using NosSmooth.PacketSerializersGenerator.Data;
using NosSmooth.PacketSerializersGenerator.Errors;

namespace NosSmooth.PacketSerializersGenerator.Extensions;

/// <summary>
/// Extensions for <see cref="ParameterInfo"/>.
/// </summary>
public static class ParameterInfoExtensions
{
    /// <summary>
    /// Gets the name of the error variable.
    /// </summary>
    /// <param name="parameterInfo">The parameter.</param>
    /// <returns>The name of the error variable.</returns>
    public static string GetErrorVariableName(this ParameterInfo parameterInfo)
    {
        return $"{parameterInfo.Name}Error";
    }

    /// <summary>
    /// Gets the name of the error variable.
    /// </summary>
    /// <param name="parameterInfo">The parameter.</param>
    /// <returns>The name of the error variable.</returns>
    public static string GetResultVariableName(this ParameterInfo parameterInfo)
    {
        return $"{parameterInfo.Name}Result";
    }

    /// <summary>
    /// Gets the name of the token variable.
    /// </summary>
    /// <param name="parameterInfo">The parameter.</param>
    /// <returns>The name of the token variable.</returns>
    public static string GetTokenVariableName(this ParameterInfo parameterInfo)
    {
        return $"{parameterInfo.Name}Token";
    }

    /// <summary>
    /// Gets the name of the error variable.
    /// </summary>
    /// <param name="parameterInfo">The parameter.</param>
    /// <returns>The name of the error variable.</returns>
    public static string GetVariableName(this ParameterInfo parameterInfo)
    {
        return parameterInfo.Name;
    }

    /// <summary>
    /// Gets the name of the nullable variable.
    /// </summary>
    /// <param name="parameterInfo">The parameter.</param>
    /// <returns>The name of the nullable variable.</returns>
    public static string GetNullableVariableName(this ParameterInfo parameterInfo)
    {
        return $"{parameterInfo.Name}Nullable";
    }

    /// <summary>
    /// Gets the type of the parameter as nullable.
    /// </summary>
    /// <param name="parameterInfo">The parameter.</param>
    /// <returns>The nullable type.</returns>
    public static string GetNullableType(this ParameterInfo parameterInfo)
    {
        return parameterInfo.Type.ToString().TrimEnd('?') + "?";
    }

    /// <summary>
    /// Gets the type of the parameter with ? if the parameter is nullable..
    /// </summary>
    /// <param name="parameterInfo">The parameter.</param>
    /// <returns>The type.</returns>
    public static string GetActualType(this ParameterInfo parameterInfo)
    {
        return parameterInfo.Type.ToString().TrimEnd('?') + (parameterInfo.Nullable ? "?" : string.Empty);
    }

    /// <summary>
    /// Gets whether the parameter is marked as optional.
    /// </summary>
    /// <param name="parameterInfo">The parameter info.</param>
    /// <returns>Whether the parameter is optional.</returns>
    public static bool IsOptional(this ParameterInfo parameterInfo)
    {
        return parameterInfo.Attributes.First().GetNamedValue("IsOptional", false);
    }
}
Do not follow this link