// Copyright Epic Games, Inc. All Rights Reserved. using System; using System.ComponentModel; using System.Globalization; using System.Text.Json; using System.Text.Json.Serialization; using EpicGames.Core; namespace EpicGames.Horde.Storage { /// /// Identifier for a storage namespace /// [LogValueType] [JsonSchemaString] [JsonConverter(typeof(NamespaceIdJsonConverter))] [TypeConverter(typeof(NamespaceIdTypeConverter))] public readonly struct NamespaceId : IEquatable { /// /// The text representing this id /// public StringId Text { get; } /// /// Constructor /// /// Unique id for the namespace public NamespaceId(string text) : this(new Utf8String(text)) { } /// /// Constructor /// /// Unique id for the namespace public NamespaceId(Utf8String text) { Text = new StringId(text); } /// public override bool Equals(object? obj) => obj is NamespaceId id && Text.Equals(id.Text); /// public override int GetHashCode() => Text.GetHashCode(); /// public bool Equals(NamespaceId other) => Text.Equals(other.Text); /// public override string ToString() => Text.ToString(); /// public static bool operator ==(NamespaceId left, NamespaceId right) => left.Text == right.Text; /// public static bool operator !=(NamespaceId left, NamespaceId right) => left.Text != right.Text; } /// /// Type converter for NamespaceId to and from JSON /// sealed class NamespaceIdJsonConverter : JsonConverter { /// public override NamespaceId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => new NamespaceId(new Utf8String(reader.GetUtf8String().ToArray())); /// public override void Write(Utf8JsonWriter writer, NamespaceId value, JsonSerializerOptions options) => writer.WriteStringValue(value.Text.Span); } /// /// Type converter from strings to NamespaceId objects /// sealed class NamespaceIdTypeConverter : TypeConverter { /// public override bool CanConvertFrom(ITypeDescriptorContext? context, Type sourceType) => sourceType == typeof(string); /// public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object value) => new NamespaceId(new Utf8String((string)value)); } }