Emits a schema enum as a string-backed value type that preserves unknown runtime values.
(
parentClassName: string,
propName: string,
values: string[],
enumOutput: string[],
description?: string,
enumValueDescriptions?: EnumValueDescriptions,
explicitName?: string,
deprecated?: boolean,
experimental?: boolean
)
| 556 | |
| 557 | /** Emits a schema enum as a string-backed value type that preserves unknown runtime values. */ |
| 558 | function getOrCreateEnum( |
| 559 | parentClassName: string, |
| 560 | propName: string, |
| 561 | values: string[], |
| 562 | enumOutput: string[], |
| 563 | description?: string, |
| 564 | enumValueDescriptions?: EnumValueDescriptions, |
| 565 | explicitName?: string, |
| 566 | deprecated?: boolean, |
| 567 | experimental?: boolean |
| 568 | ): string { |
| 569 | const enumName = explicitName ?? `${parentClassName}${propName}`; |
| 570 | const existing = generatedEnums.get(enumName); |
| 571 | if (existing) return existing.enumName; |
| 572 | generatedEnums.set(enumName, { enumName, values }); |
| 573 | |
| 574 | const lines: string[] = []; |
| 575 | lines.push(...xmlDocEnumComment(description, "")); |
| 576 | if (experimental) pushExperimentalAttribute(lines); |
| 577 | if (deprecated) pushObsoleteAttributes(lines); |
| 578 | lines.push(`[JsonConverter(typeof(Converter))]`); |
| 579 | lines.push(`[DebuggerDisplay("{Value,nq}")]`); |
| 580 | lines.push(`public readonly struct ${enumName} : IEquatable<${enumName}>`); |
| 581 | lines.push(`{`); |
| 582 | lines.push(` private readonly string? _value;`, ""); |
| 583 | lines.push(` /// <summary>Initializes a new instance of the <see cref="${enumName}"/> struct.</summary>`); |
| 584 | lines.push(` /// <param name="value">The value to associate with this <see cref="${enumName}"/>.</param>`); |
| 585 | lines.push(` [JsonConstructor]`); |
| 586 | lines.push(` public ${enumName}(string value)`); |
| 587 | lines.push(` {`); |
| 588 | lines.push(` ArgumentException.ThrowIfNullOrWhiteSpace(value);`); |
| 589 | lines.push(` _value = value;`); |
| 590 | lines.push(` }`, ""); |
| 591 | lines.push(` /// <summary>Gets the value associated with this <see cref="${enumName}"/>.</summary>`); |
| 592 | lines.push(` public string Value => _value ?? string.Empty;`, ""); |
| 593 | const usedMemberNames = new Set(STRING_ENUM_RESERVED_MEMBER_NAMES); |
| 594 | for (const value of values) { |
| 595 | const memberName = uniqueCSharpIdentifier(value, usedMemberNames, "Value"); |
| 596 | lines.push(...xmlDocEnumMemberComment(enumValueDescriptions, value)); |
| 597 | lines.push(` public static ${enumName} ${memberName} { get; } = new("${escapeCSharpStringLiteral(value)}");`, ""); |
| 598 | } |
| 599 | lines.push(` /// <summary>Returns a value indicating whether two <see cref="${enumName}"/> instances are equivalent.</summary>`); |
| 600 | lines.push(` public static bool operator ==(${enumName} left, ${enumName} right) => left.Equals(right);`, ""); |
| 601 | lines.push(` /// <summary>Returns a value indicating whether two <see cref="${enumName}"/> instances are not equivalent.</summary>`); |
| 602 | lines.push(` public static bool operator !=(${enumName} left, ${enumName} right) => !(left == right);`, ""); |
| 603 | lines.push(` /// <inheritdoc />`); |
| 604 | lines.push(` public override bool Equals(object? obj) => obj is ${enumName} other && Equals(other);`, ""); |
| 605 | lines.push(` /// <inheritdoc />`); |
| 606 | lines.push(` public bool Equals(${enumName} other) => string.Equals(Value, other.Value, StringComparison.OrdinalIgnoreCase);`, ""); |
| 607 | lines.push(` /// <inheritdoc />`); |
| 608 | lines.push(` public override int GetHashCode() => StringComparer.OrdinalIgnoreCase.GetHashCode(Value);`, ""); |
| 609 | lines.push(` /// <inheritdoc />`); |
| 610 | lines.push(` public override string ToString() => Value;`, ""); |
| 611 | lines.push(` /// <summary>Provides a <see cref="JsonConverter{${enumName}}"/> for serializing <see cref="${enumName}"/> instances.</summary>`); |
| 612 | lines.push(` [EditorBrowsable(EditorBrowsableState.Never)]`); |
| 613 | lines.push(` public sealed class Converter : JsonConverter<${enumName}>`); |
| 614 | lines.push(` {`); |
| 615 | lines.push(` /// <inheritdoc />`); |
no test coverage detected
searching dependent graphs…