Skip to content

Commit f9b0119

Browse files
refactor(client): make unknown variants implicit
1 parent 64ba8ff commit f9b0119

File tree

113 files changed

+9926
-16375
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+9926
-16375
lines changed

src/Orb/Models/AdjustmentInterval.cs

Lines changed: 52 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,14 @@ IReadOnlyDictionary<string, JsonElement> properties
193193
[JsonConverter(typeof(AdjustmentIntervalAdjustmentConverter))]
194194
public record class AdjustmentIntervalAdjustment
195195
{
196-
public object Value { get; private init; }
196+
public object? Value { get; } = null;
197+
198+
JsonElement? _json = null;
199+
200+
public JsonElement Json
201+
{
202+
get { return this._json ??= JsonSerializer.SerializeToElement(this.Value); }
203+
}
197204

198205
public string ID
199206
{
@@ -265,39 +272,48 @@ public string? ReplacesAdjustmentID
265272
}
266273
}
267274

268-
public AdjustmentIntervalAdjustment(PlanPhaseUsageDiscountAdjustment value)
269-
{
270-
Value = value;
271-
}
272-
273-
public AdjustmentIntervalAdjustment(PlanPhaseAmountDiscountAdjustment value)
275+
public AdjustmentIntervalAdjustment(
276+
PlanPhaseUsageDiscountAdjustment value,
277+
JsonElement? json = null
278+
)
274279
{
275-
Value = value;
280+
this.Value = value;
281+
this._json = json;
276282
}
277283

278-
public AdjustmentIntervalAdjustment(PlanPhasePercentageDiscountAdjustment value)
284+
public AdjustmentIntervalAdjustment(
285+
PlanPhaseAmountDiscountAdjustment value,
286+
JsonElement? json = null
287+
)
279288
{
280-
Value = value;
289+
this.Value = value;
290+
this._json = json;
281291
}
282292

283-
public AdjustmentIntervalAdjustment(PlanPhaseMinimumAdjustment value)
293+
public AdjustmentIntervalAdjustment(
294+
PlanPhasePercentageDiscountAdjustment value,
295+
JsonElement? json = null
296+
)
284297
{
285-
Value = value;
298+
this.Value = value;
299+
this._json = json;
286300
}
287301

288-
public AdjustmentIntervalAdjustment(PlanPhaseMaximumAdjustment value)
302+
public AdjustmentIntervalAdjustment(PlanPhaseMinimumAdjustment value, JsonElement? json = null)
289303
{
290-
Value = value;
304+
this.Value = value;
305+
this._json = json;
291306
}
292307

293-
AdjustmentIntervalAdjustment(UnknownVariant value)
308+
public AdjustmentIntervalAdjustment(PlanPhaseMaximumAdjustment value, JsonElement? json = null)
294309
{
295-
Value = value;
310+
this.Value = value;
311+
this._json = json;
296312
}
297313

298-
public static AdjustmentIntervalAdjustment CreateUnknownVariant(JsonElement value)
314+
public AdjustmentIntervalAdjustment(JsonElement json)
299315
{
300-
return new(new UnknownVariant(value));
316+
this._json = json;
301317
}
302318

303319
public bool TryPickPlanPhaseUsageDiscount(
@@ -411,15 +427,13 @@ PlanPhaseMaximumAdjustment value
411427

412428
public void Validate()
413429
{
414-
if (this.Value is UnknownVariant)
430+
if (this.Value == null)
415431
{
416432
throw new OrbInvalidDataException(
417433
"Data did not match any variant of AdjustmentIntervalAdjustment"
418434
);
419435
}
420436
}
421-
422-
record struct UnknownVariant(JsonElement value);
423437
}
424438

425439
sealed class AdjustmentIntervalAdjustmentConverter : JsonConverter<AdjustmentIntervalAdjustment>
@@ -445,8 +459,6 @@ JsonSerializerOptions options
445459
{
446460
case "usage_discount":
447461
{
448-
List<OrbInvalidDataException> exceptions = [];
449-
450462
try
451463
{
452464
var deserialized = JsonSerializer.Deserialize<PlanPhaseUsageDiscountAdjustment>(
@@ -456,26 +468,19 @@ JsonSerializerOptions options
456468
if (deserialized != null)
457469
{
458470
deserialized.Validate();
459-
return new AdjustmentIntervalAdjustment(deserialized);
471+
return new(deserialized, json);
460472
}
461473
}
462474
catch (System::Exception e)
463475
when (e is JsonException || e is OrbInvalidDataException)
464476
{
465-
exceptions.Add(
466-
new OrbInvalidDataException(
467-
"Data does not match union variant 'PlanPhaseUsageDiscountAdjustment'",
468-
e
469-
)
470-
);
477+
// ignore
471478
}
472479

473-
throw new System::AggregateException(exceptions);
480+
return new(json);
474481
}
475482
case "amount_discount":
476483
{
477-
List<OrbInvalidDataException> exceptions = [];
478-
479484
try
480485
{
481486
var deserialized =
@@ -486,26 +491,19 @@ JsonSerializerOptions options
486491
if (deserialized != null)
487492
{
488493
deserialized.Validate();
489-
return new AdjustmentIntervalAdjustment(deserialized);
494+
return new(deserialized, json);
490495
}
491496
}
492497
catch (System::Exception e)
493498
when (e is JsonException || e is OrbInvalidDataException)
494499
{
495-
exceptions.Add(
496-
new OrbInvalidDataException(
497-
"Data does not match union variant 'PlanPhaseAmountDiscountAdjustment'",
498-
e
499-
)
500-
);
500+
// ignore
501501
}
502502

503-
throw new System::AggregateException(exceptions);
503+
return new(json);
504504
}
505505
case "percentage_discount":
506506
{
507-
List<OrbInvalidDataException> exceptions = [];
508-
509507
try
510508
{
511509
var deserialized =
@@ -516,26 +514,19 @@ JsonSerializerOptions options
516514
if (deserialized != null)
517515
{
518516
deserialized.Validate();
519-
return new AdjustmentIntervalAdjustment(deserialized);
517+
return new(deserialized, json);
520518
}
521519
}
522520
catch (System::Exception e)
523521
when (e is JsonException || e is OrbInvalidDataException)
524522
{
525-
exceptions.Add(
526-
new OrbInvalidDataException(
527-
"Data does not match union variant 'PlanPhasePercentageDiscountAdjustment'",
528-
e
529-
)
530-
);
523+
// ignore
531524
}
532525

533-
throw new System::AggregateException(exceptions);
526+
return new(json);
534527
}
535528
case "minimum":
536529
{
537-
List<OrbInvalidDataException> exceptions = [];
538-
539530
try
540531
{
541532
var deserialized = JsonSerializer.Deserialize<PlanPhaseMinimumAdjustment>(
@@ -545,26 +536,19 @@ JsonSerializerOptions options
545536
if (deserialized != null)
546537
{
547538
deserialized.Validate();
548-
return new AdjustmentIntervalAdjustment(deserialized);
539+
return new(deserialized, json);
549540
}
550541
}
551542
catch (System::Exception e)
552543
when (e is JsonException || e is OrbInvalidDataException)
553544
{
554-
exceptions.Add(
555-
new OrbInvalidDataException(
556-
"Data does not match union variant 'PlanPhaseMinimumAdjustment'",
557-
e
558-
)
559-
);
545+
// ignore
560546
}
561547

562-
throw new System::AggregateException(exceptions);
548+
return new(json);
563549
}
564550
case "maximum":
565551
{
566-
List<OrbInvalidDataException> exceptions = [];
567-
568552
try
569553
{
570554
var deserialized = JsonSerializer.Deserialize<PlanPhaseMaximumAdjustment>(
@@ -574,27 +558,20 @@ JsonSerializerOptions options
574558
if (deserialized != null)
575559
{
576560
deserialized.Validate();
577-
return new AdjustmentIntervalAdjustment(deserialized);
561+
return new(deserialized, json);
578562
}
579563
}
580564
catch (System::Exception e)
581565
when (e is JsonException || e is OrbInvalidDataException)
582566
{
583-
exceptions.Add(
584-
new OrbInvalidDataException(
585-
"Data does not match union variant 'PlanPhaseMaximumAdjustment'",
586-
e
587-
)
588-
);
567+
// ignore
589568
}
590569

591-
throw new System::AggregateException(exceptions);
570+
return new(json);
592571
}
593572
default:
594573
{
595-
throw new OrbInvalidDataException(
596-
"Could not find valid union variant to represent data"
597-
);
574+
return new AdjustmentIntervalAdjustment(json);
598575
}
599576
}
600577
}
@@ -605,7 +582,6 @@ public override void Write(
605582
JsonSerializerOptions options
606583
)
607584
{
608-
object variant = value.Value;
609-
JsonSerializer.Serialize(writer, variant, options);
585+
JsonSerializer.Serialize(writer, value.Json, options);
610586
}
611587
}

0 commit comments

Comments
 (0)