in src/Microsoft.Extensions.Configuration.AzureAppConfiguration/FeatureManagement/FeatureManagementKeyValueAdapter.cs [667:880]
private FeatureAllocation ParseFeatureAllocation(ref Utf8JsonReader reader, string settingKey)
{
var featureAllocation = new FeatureAllocation();
while (reader.Read() && reader.TokenType != JsonTokenType.EndObject)
{
if (reader.TokenType != JsonTokenType.PropertyName)
{
continue;
}
string allocationPropertyName = reader.GetString();
switch (allocationPropertyName)
{
case FeatureManagementConstants.DefaultWhenDisabled:
{
if (reader.Read() && reader.TokenType == JsonTokenType.String)
{
featureAllocation.DefaultWhenDisabled = reader.GetString();
}
else if (reader.TokenType != JsonTokenType.Null)
{
throw CreateFeatureFlagFormatException(
FeatureManagementConstants.DefaultWhenDisabled,
settingKey,
reader.TokenType.ToString(),
JsonTokenType.String.ToString());
}
break;
}
case FeatureManagementConstants.DefaultWhenEnabled:
{
if (reader.Read() && reader.TokenType == JsonTokenType.String)
{
featureAllocation.DefaultWhenEnabled = reader.GetString();
}
else if (reader.TokenType != JsonTokenType.Null)
{
throw CreateFeatureFlagFormatException(
FeatureManagementConstants.DefaultWhenEnabled,
settingKey,
reader.TokenType.ToString(),
JsonTokenType.String.ToString());
}
break;
}
case FeatureManagementConstants.UserAllocation:
{
if (reader.Read() && reader.TokenType == JsonTokenType.StartArray)
{
List<FeatureUserAllocation> userAllocations = new List<FeatureUserAllocation>();
int i = 0;
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
{
if (reader.TokenType == JsonTokenType.StartObject)
{
FeatureUserAllocation featureUserAllocation = ParseFeatureUserAllocation(ref reader, settingKey);
if (featureUserAllocation.Variant != null ||
(featureUserAllocation.Users != null &&
featureUserAllocation.Users.Any()))
{
userAllocations.Add(featureUserAllocation);
}
}
else if (reader.TokenType != JsonTokenType.Null)
{
throw CreateFeatureFlagFormatException(
$"{FeatureManagementConstants.UserAllocation}[{i}]",
settingKey,
reader.TokenType.ToString(),
JsonTokenType.StartObject.ToString());
}
i++;
}
featureAllocation.User = userAllocations;
}
else if (reader.TokenType != JsonTokenType.Null)
{
throw CreateFeatureFlagFormatException(
FeatureManagementConstants.UserAllocation,
settingKey,
reader.TokenType.ToString(),
JsonTokenType.StartArray.ToString());
}
break;
}
case FeatureManagementConstants.GroupAllocation:
{
if (reader.Read() && reader.TokenType == JsonTokenType.StartArray)
{
List<FeatureGroupAllocation> groupAllocations = new List<FeatureGroupAllocation>();
int i = 0;
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
{
if (reader.TokenType == JsonTokenType.StartObject)
{
FeatureGroupAllocation featureGroupAllocation = ParseFeatureGroupAllocation(ref reader, settingKey);
if (featureGroupAllocation.Variant != null ||
(featureGroupAllocation.Groups != null &&
featureGroupAllocation.Groups.Any()))
{
groupAllocations.Add(featureGroupAllocation);
}
}
else if (reader.TokenType != JsonTokenType.Null)
{
throw CreateFeatureFlagFormatException(
$"{FeatureManagementConstants.GroupAllocation}[{i}]",
settingKey,
reader.TokenType.ToString(),
JsonTokenType.StartObject.ToString());
}
i++;
}
featureAllocation.Group = groupAllocations;
}
else if (reader.TokenType != JsonTokenType.Null)
{
throw CreateFeatureFlagFormatException(
FeatureManagementConstants.GroupAllocation,
settingKey,
reader.TokenType.ToString(),
JsonTokenType.StartArray.ToString());
}
break;
}
case FeatureManagementConstants.PercentileAllocation:
{
if (reader.Read() && reader.TokenType == JsonTokenType.StartArray)
{
List<FeaturePercentileAllocation> percentileAllocations = new List<FeaturePercentileAllocation>();
int i = 0;
while (reader.Read() && reader.TokenType != JsonTokenType.EndArray)
{
if (reader.TokenType == JsonTokenType.StartObject)
{
FeaturePercentileAllocation featurePercentileAllocation = ParseFeaturePercentileAllocation(ref reader, settingKey);
percentileAllocations.Add(featurePercentileAllocation);
}
else if (reader.TokenType != JsonTokenType.Null)
{
throw CreateFeatureFlagFormatException(
$"{FeatureManagementConstants.PercentileAllocation}[{i}]",
settingKey,
reader.TokenType.ToString(),
JsonTokenType.StartObject.ToString());
}
i++;
}
featureAllocation.Percentile = percentileAllocations;
}
else if (reader.TokenType != JsonTokenType.Null)
{
throw CreateFeatureFlagFormatException(
FeatureManagementConstants.PercentileAllocation,
settingKey,
reader.TokenType.ToString(),
JsonTokenType.StartArray.ToString());
}
break;
}
case FeatureManagementConstants.Seed:
{
if (reader.Read() && reader.TokenType == JsonTokenType.String)
{
featureAllocation.Seed = reader.GetString();
}
else if (reader.TokenType != JsonTokenType.Null)
{
throw CreateFeatureFlagFormatException(
FeatureManagementConstants.Seed,
settingKey,
reader.TokenType.ToString(),
JsonTokenType.String.ToString());
}
break;
}
default:
reader.Skip();
break;
}
}
return featureAllocation;
}