-
Notifications
You must be signed in to change notification settings - Fork 15.5k
/
FeatureSetDescriptor.cs
118 lines (106 loc) · 4.68 KB
/
FeatureSetDescriptor.cs
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
#region Copyright notice and license
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#endregion
using System;
using System.Collections.Concurrent;
using static Google.Protobuf.Reflection.FeatureSet.Types;
namespace Google.Protobuf.Reflection;
/// <summary>
/// A resolved set of features for a file, message etc.
/// </summary>
/// <remarks>
/// Only features supported by the C# runtime are exposed; currently
/// all enums in C# are open, and we never perform UTF-8 validation.
/// If either of those features are ever implemented in this runtime,
/// the feature settings will be exposed as properties in this class.
/// </remarks>
internal sealed class FeatureSetDescriptor
{
private static readonly ConcurrentDictionary<FeatureSet, FeatureSetDescriptor> cache = new();
// Note: this approach is deliberately chosen to circumvent bootstrapping issues.
// This can still be tested using the binary representation.
// TODO: Generate this code (as a partial class) from the binary representation.
private static readonly FeatureSetDescriptor edition2023Defaults = new FeatureSetDescriptor(
new FeatureSet
{
EnumType = EnumType.Open,
FieldPresence = FieldPresence.Explicit,
JsonFormat = JsonFormat.Allow,
MessageEncoding = MessageEncoding.LengthPrefixed,
RepeatedFieldEncoding = RepeatedFieldEncoding.Packed,
Utf8Validation = Utf8Validation.Verify,
});
private static readonly FeatureSetDescriptor proto2Defaults = new FeatureSetDescriptor(
new FeatureSet
{
EnumType = EnumType.Closed,
FieldPresence = FieldPresence.Explicit,
JsonFormat = JsonFormat.LegacyBestEffort,
MessageEncoding = MessageEncoding.LengthPrefixed,
RepeatedFieldEncoding = RepeatedFieldEncoding.Expanded,
Utf8Validation = Utf8Validation.None,
});
private static readonly FeatureSetDescriptor proto3Defaults = new FeatureSetDescriptor(
new FeatureSet
{
EnumType = EnumType.Open,
FieldPresence = FieldPresence.Implicit,
JsonFormat = JsonFormat.Allow,
MessageEncoding = MessageEncoding.LengthPrefixed,
RepeatedFieldEncoding = RepeatedFieldEncoding.Packed,
Utf8Validation = Utf8Validation.Verify,
});
internal static FeatureSetDescriptor GetEditionDefaults(Edition edition) =>
edition switch
{
Edition.Proto2 => proto2Defaults,
Edition.Proto3 => proto3Defaults,
Edition._2023 => edition2023Defaults,
_ => throw new ArgumentOutOfRangeException($"Unsupported edition: {edition}")
};
// Visible for testing. The underlying feature set proto, usually derived during
// feature resolution.
internal FeatureSet Proto { get; }
/// <summary>
/// Only relevant to fields. Indicates if a field has explicit presence.
/// </summary>
internal FieldPresence FieldPresence => Proto.FieldPresence;
/// <summary>
/// Only relevant to fields. Indicates how a repeated field should be encoded.
/// </summary>
internal RepeatedFieldEncoding RepeatedFieldEncoding => Proto.RepeatedFieldEncoding;
/// <summary>
/// Only relevant to fields. Indicates how a message-valued field should be encoded.
/// </summary>
internal MessageEncoding MessageEncoding => Proto.MessageEncoding;
private FeatureSetDescriptor(FeatureSet proto)
{
Proto = proto;
}
/// <summary>
/// Returns a new descriptor based on this one, with the specified overrides.
/// Multiple calls to this method that produce equivalent feature sets will return
/// the same instance.
/// </summary>
/// <param name="overrides">The proto representation of the "child" feature set to merge with this
/// one. May be null, in which case this descriptor is returned.</param>
/// <returns>A descriptor based on the current one, with the given set of overrides.</returns>
public FeatureSetDescriptor MergedWith(FeatureSet overrides)
{
if (overrides is null)
{
return this;
}
// Note: It would be nice if we could avoid cloning unless
// there are actual changes, but this won't happen that often;
// it'll be temporary garbage.
var clone = Proto.Clone();
clone.MergeFrom(overrides);
return cache.GetOrAdd(clone, clone => new FeatureSetDescriptor(clone));
}
}