forked from msgpack/msgpack-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessagePackRuntimeTypeAttributes.tt
More file actions
230 lines (221 loc) · 7.66 KB
/
MessagePackRuntimeTypeAttributes.tt
File metadata and controls
230 lines (221 loc) · 7.66 KB
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
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
#region -- License Terms --
//
// MessagePack for CLI
//
// Copyright (C) 2015 FUJIWARA, Yusuke
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion -- License Terms --
// This code is generated with T4Template from MessagePackRuntimeTypeAttributes.tt
using System;
using MsgPack.Serialization.Polymorphic;
namespace MsgPack.Serialization
{
<#
foreach( var item in
new []
{
new
{
Prefix = "",
Summary = "Marks that the runtime type of this member should be encoded with type information for polymorphism.",
RemarksLines =
new string[]
{
"When you apply this attribute to a member, the member will be serialized with .NET specific type information,",
},
Target = "Member",
OmitConstructor = false,
AllowMulitiple = false,
CanQualifyType = true,
},
new
{
Prefix = "CollectionItem",
Summary = "Marks that the runtime type of items/values of this collection/dictionary should be encoded with type information for polymorphism.",
RemarksLines =
new string[]
{
"When you apply this attribute to a member, the items/values of the collection/dictionary will be serialized with .NET specific type information,",
},
Target = "CollectionItem",
OmitConstructor = false,
AllowMulitiple = false,
CanQualifyType = true,
},
new
{
Prefix = "DictionaryKey",
Summary = "Marks that the runtime type of keys of this dictionary should be encoded with type information for polymorphism.",
RemarksLines =
new string[]
{
"When you apply this attribute to a member, the keys of the dictionary will be serialized with .NET specific type information,",
},
Target = "DictionaryKey",
OmitConstructor = false,
AllowMulitiple = false,
CanQualifyType = true,
},
new
{
Prefix = "TupleItem",
Summary = "Marks that the runtime type of specified item of the tuple should be encoded with type information for polymorphism.",
RemarksLines =
new string[]
{
"When you apply this attribute to a member, the item of tuple will be serialized with .NET specific type information,",
},
Target = "TupleItem",
OmitConstructor = true,
AllowMulitiple = true,
CanQualifyType = false,
},
}
)
{
var typeName = "MessagePackRuntime" + item.Prefix + "TypeAttribute";
#>
/// <summary>
/// <#= item.Summary #>
///
/// </summary>
/// <remarks>
<#
foreach ( var remarksLine in item.RemarksLines )
{
#>
/// <#= remarksLine #>
<#
}
#>
/// so deserializer will be able to deserialize object which is actual type when serialized instead of interoperability.
/// Because non-.NET enviroments (Java, Ruby, Go, etc.) cannot interpret .NET native type identifier,
/// you should not use this attribute when the serialized stream will be possible to be used from non-.NET environment.
/// The typed object will be encoded as 2 elements array as follows, so your deserializer can skip type information as needed:
/// [ <type-info>, <actual-value (array or map)>]
/// In this point, type-info will be encoded as compressed assembly qualified name as follows:
/// [ <compressed type full name>, <assembly simple name>, <version binary>, <culture string>, <public key token binary>]
/// If the type full name starts with its assembly simple name, then the prefix matched to assembly simple name will be omitted
/// (as a result, compressed type name starts with dot).
/// <note>
/// You should use this attribute CAREFULLY when you deserialize data from outside, because this feature can inject arbitary process in your code through
/// constructor and some virtual methods if exist.
/// It is highly recommended avoid using <see cref="Object" /> type as member's declaring type,
/// you should specify your base class which and derived typed are fully controled under your organization instead.
/// It mitigate chance of potential exploits.
/// </note>
/// </remarks>
<#
if ( item.CanQualifyType )
{
#>
[AttributeUsage( AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Interface | AttributeTargets.Field | AttributeTargets.Property<#= item.AllowMulitiple ? ", AllowMultiple = true" : String.Empty #> )]
<#
}
else
{
#>
[AttributeUsage( AttributeTargets.Field | AttributeTargets.Property<#= item.AllowMulitiple ? ", AllowMultiple = true" : String.Empty #> )]
<#
}
#>
public sealed <#= item.OmitConstructor ? "partial " : String.Empty #>class <#= typeName #> : Attribute, IPolymorphicRuntimeTypeAttribute
{
PolymorphismTarget IPolymorphicHelperAttribute.Target
{
get { return PolymorphismTarget.<#= item.Target #>; }
}
/// <summary>
/// Gets or sets the type which implement type verfier method.
/// </summary>
/// <value>
/// The type which implement type verfier method.
/// The default is <c>null</c>, which indicates that no type verification will be processed.
/// </value>
public Type VerifierType
{
get;
set;
}
/// <summary>
/// Gets or sets the name of the method which implement type verification.
/// </summary>
/// <value>
/// The name of the method which implement type verification
/// The default is <c>null</c>, which indicates that no type verification will be processed.
/// </value>
/// <remarks>
/// The type verfication method must be following:
/// <list type="bullet">
/// <item>The method has just only one parameter, which type must be <see cref="PolymorphicTypeVerificationContext" />.</item>
/// <item>The method must return <see cref="Boolean" />. <c>true</c> indicates the verification result is OK; <c>false</c> indicates not OK.</item>
/// <item>The method can be static method or instance method. The accessibility is not limited.</item>
/// </list>
/// </remarks>
public string VerifierMethodName
{
get;
set;
}
<#
if ( !item.OmitConstructor )
{
#>
/// <summary>
/// Initializes a new instance of the <see cref="<#= typeName #>"/> class.
/// </summary>
public <#= typeName #>()
{
}
<#
}
#>
}
<#
}
#>
partial class MessagePackRuntimeTupleItemTypeAttribute : IPolymorphicTupleItemTypeAttribute
{
/// <summary>
/// Gets the target tuple item's number.
/// </summary>
/// <value>
/// The 1-based target tuple item's number.
/// </value>
/// <remarks>
/// <para>
/// If this value is not valid for the tuple, this whole instance should be ignored.
/// </para>
/// <para>
/// If same values are specified multiply, the result is undefined.
/// </para>
/// </remarks>
public int ItemNumber { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="MessagePackKnownTupleItemTypeAttribute"/> class.
/// </summary>
/// <param name="itemNumber">The 1-based target item number of the tuple. The attribute which has invalid value should be ignored.</param>
public MessagePackRuntimeTupleItemTypeAttribute( int itemNumber )
{
this.ItemNumber = itemNumber;
}
}
}