BillingData: Upgrade GetSelection()

This commit is contained in:
2024-01-20 00:48:23 +01:00
parent ee161b149b
commit 75e9d756d2
2 changed files with 9 additions and 16 deletions

View File

@ -146,7 +146,12 @@ namespace Elwig.Helpers.Billing {
return dict;
}
protected static Dictionary<string, JsonValue> GetSelection(JsonObject data, IEnumerable<string> attributeVariants) {
protected static Dictionary<string, JsonValue> GetSelection(JsonNode value, IEnumerable<string> attributeVariants) {
if (value is JsonValue flatRate) {
return attributeVariants.ToDictionary(e => e, _ => flatRate);
} if (value is not JsonObject data) {
throw new InvalidOperationException();
}
Dictionary<string, JsonValue> dict;
if (data["default"] is JsonValue def) {
dict = attributeVariants.ToDictionary(e => e, _ => def);

View File

@ -25,17 +25,12 @@ namespace Elwig.Helpers.Billing {
return new(ParseJson(json), attributeVariants);
}
private Dictionary<string, Curve> GetData(JsonObject data) {
private Dictionary<string, Curve> GetData(JsonNode data) {
return GetSelection(data, AttributeVariants).ToDictionary(e => e.Key, e => LookupCurve(e.Value));
}
protected Dictionary<string, Curve> GetPaymentData() {
var p = GetPaymentEntry();
if (p is JsonValue val) {
var c = LookupCurve(val);
return AttributeVariants.ToDictionary(e => e, _ => c);
}
return GetData(p?.AsObject() ?? throw new InvalidOperationException());
return GetData(GetPaymentEntry());
}
protected Dictionary<string, Curve> GetQualityData() {
@ -44,14 +39,7 @@ namespace Elwig.Helpers.Billing {
if (q == null) return dict;
foreach (var (qualid, data) in q) {
Dictionary<string, Curve> qualDict;
if (data is JsonValue val) {
var c = LookupCurve(val);
qualDict = AttributeVariants.ToDictionary(e => e, _ => c);
} else {
qualDict = GetData(data?.AsObject() ?? throw new InvalidOperationException());
}
foreach (var (idx, d) in qualDict) {
foreach (var (idx, d) in GetData(data ?? throw new InvalidOperationException())) {
dict[$"{qualid}/{idx}"] = d;
}
}