Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Definitions.cs « CodeAnalysis « MessagePack.GeneratorCore « src - github.com/aspnet/MessagePack-CSharp.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 30aaedb98db631542aadc39b386f32cb649b25e5 (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// Copyright (c) All contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections.Generic;
using System.Linq;

#pragma warning disable SA1649 // File name should match first type name

namespace MessagePackCompiler.CodeAnalysis
{
    public interface INamespaceInfo
    {
        string? Namespace { get; }
    }

    public interface IResolverRegisterInfo
    {
        string FullName { get; }

        string FormatterName { get; }
    }

    public class ObjectSerializationInfo : IResolverRegisterInfo, INamespaceInfo
    {
        public string Name { get; }

        public string FullName { get; }

        public string? Namespace { get; }

        public GenericTypeParameterInfo[] GenericTypeParameters { get; }

        public bool IsOpenGenericType { get; }

        public bool IsIntKey { get; }

        public bool IsStringKey
        {
            get { return !this.IsIntKey; }
        }

        public bool IsClass { get; }

        public MemberSerializationInfo[] ConstructorParameters { get; }

        public MemberSerializationInfo[] Members { get; }

        public bool HasIMessagePackSerializationCallbackReceiver { get; }

        public bool NeedsCastOnBefore { get; }

        public bool NeedsCastOnAfter { get; }

        public string FormatterName => this.Namespace == null ? FormatterNameWithoutNameSpace : this.Namespace + "." + FormatterNameWithoutNameSpace;

        public string FormatterNameWithoutNameSpace => this.Name + "Formatter" + (this.IsOpenGenericType ? $"<{string.Join(", ", this.GenericTypeParameters.Select(x => x.Name))}>" : string.Empty);

        public int WriteCount
        {
            get
            {
                if (this.IsStringKey)
                {
                    return this.Members.Count(x => x.IsReadable);
                }
                else
                {
                    return this.MaxKey;
                }
            }
        }

        public int MaxKey
        {
            get
            {
                return this.Members.Where(x => x.IsReadable).Select(x => x.IntKey).DefaultIfEmpty(-1).Max();
            }
        }

        public MemberSerializationInfo? GetMember(int index)
        {
            return this.Members.FirstOrDefault(x => x.IntKey == index);
        }

        public string GetConstructorString()
        {
            var args = string.Join(", ", this.ConstructorParameters.Select(x => "__" + x.Name + "__"));
            return $"{this.FullName}({args})";
        }

        public ObjectSerializationInfo(bool isClass, bool isOpenGenericType, GenericTypeParameterInfo[] genericTypeParameterInfos, MemberSerializationInfo[] constructorParameters, bool isIntKey, MemberSerializationInfo[] members, string name, string fullName, string? @namespace, bool hasSerializationConstructor, bool needsCastOnAfter, bool needsCastOnBefore)
        {
            IsClass = isClass;
            IsOpenGenericType = isOpenGenericType;
            GenericTypeParameters = genericTypeParameterInfos;
            ConstructorParameters = constructorParameters;
            IsIntKey = isIntKey;
            Members = members;
            Name = name;
            FullName = fullName;
            Namespace = @namespace;
            HasIMessagePackSerializationCallbackReceiver = hasSerializationConstructor;
            NeedsCastOnAfter = needsCastOnAfter;
            NeedsCastOnBefore = needsCastOnBefore;
        }
    }

    public class GenericTypeParameterInfo
    {
        public string Name { get; }

        public string Constraints { get; }

        public bool HasConstraints { get; }

        public GenericTypeParameterInfo(string name, string constraints)
        {
            Name = name ?? throw new ArgumentNullException(nameof(name));
            Constraints = constraints ?? throw new ArgumentNullException(nameof(name));
            HasConstraints = constraints != string.Empty;
        }
    }

    public class MemberSerializationInfo
    {
        public bool IsProperty { get; }

        public bool IsWritable { get; }

        public bool IsReadable { get; }

        public int IntKey { get; }

        public string StringKey { get; }

        public string Type { get; }

        public string Name { get; }

        public string ShortTypeName { get; }

        public string? CustomFormatterTypeName { get; }

        private readonly HashSet<string> primitiveTypes = new(Generator.ShouldUseFormatterResolverHelper.PrimitiveTypes);

        public MemberSerializationInfo(bool isProperty, bool isWritable, bool isReadable, int intKey, string stringKey, string name, string type, string shortTypeName, string? customFormatterTypeName)
        {
            IsProperty = isProperty;
            IsWritable = isWritable;
            IsReadable = isReadable;
            IntKey = intKey;
            StringKey = stringKey;
            Type = type;
            Name = name;
            ShortTypeName = shortTypeName;
            CustomFormatterTypeName = customFormatterTypeName;
        }

        public string GetSerializeMethodString()
        {
            if (CustomFormatterTypeName != null)
            {
                return $"this.__{this.Name}CustomFormatter__.Serialize(ref writer, value.{this.Name}, options)";
            }
            else if (this.primitiveTypes.Contains(this.Type))
            {
                return "writer.Write(value." + this.Name + ")";
            }
            else
            {
                return $"global::MessagePack.FormatterResolverExtensions.GetFormatterWithVerify<{this.Type}>(formatterResolver).Serialize(ref writer, value.{this.Name}, options)";
            }
        }

        public string GetDeserializeMethodString()
        {
            if (CustomFormatterTypeName != null)
            {
                return $"this.__{this.Name}CustomFormatter__.Deserialize(ref reader, options)";
            }
            else if (this.primitiveTypes.Contains(this.Type))
            {
                if (this.Type == "byte[]")
                {
                    return "global::MessagePack.Internal.CodeGenHelpers.GetArrayFromNullableSequence(reader.ReadBytes())";
                }
                else
                {
                    return $"reader.Read{this.ShortTypeName!.Replace("[]", "s")}()";
                }
            }
            else
            {
                return $"global::MessagePack.FormatterResolverExtensions.GetFormatterWithVerify<{this.Type}>(formatterResolver).Deserialize(ref reader, options)";
            }
        }
    }

    public class EnumSerializationInfo : IResolverRegisterInfo, INamespaceInfo
    {
        public EnumSerializationInfo(string? @namespace, string name, string fullName, string underlyingType)
        {
            Namespace = @namespace;
            Name = name;
            FullName = fullName;
            UnderlyingType = underlyingType;
        }

        public string? Namespace { get; }

        public string Name { get; }

        public string FullName { get; }

        public string UnderlyingType { get; }

        public string FormatterName => (this.Namespace == null ? this.Name : this.Namespace + "." + this.Name) + "Formatter";
    }

    public class GenericSerializationInfo : IResolverRegisterInfo, IEquatable<GenericSerializationInfo>
    {
        public string FullName { get; }

        public string FormatterName { get; }

        public bool IsOpenGenericType { get; }

        public bool Equals(GenericSerializationInfo? other)
        {
            return this.FullName.Equals(other?.FullName);
        }

        public override int GetHashCode()
        {
            return this.FullName.GetHashCode();
        }

        public GenericSerializationInfo(string fullName, string formatterName, bool isOpenGenericType)
        {
            FullName = fullName;
            FormatterName = formatterName;
            IsOpenGenericType = isOpenGenericType;
        }
    }

    public class UnionSerializationInfo : IResolverRegisterInfo, INamespaceInfo
    {
        public string? Namespace { get; }

        public string Name { get; }

        public string FullName { get; }

        public string FormatterName => (this.Namespace == null ? this.Name : this.Namespace + "." + this.Name) + "Formatter";

        public UnionSubTypeInfo[] SubTypes { get; }

        public UnionSerializationInfo(string? @namespace, string name, string fullName, UnionSubTypeInfo[] subTypes)
        {
            Namespace = @namespace;
            Name = name;
            FullName = fullName;
            SubTypes = subTypes;
        }
    }

    public class UnionSubTypeInfo
    {
        public UnionSubTypeInfo(int key, string type)
        {
            Key = key;
            Type = type;
        }

        public int Key { get; }

        public string Type { get; }
    }
}