// 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;
using EpicGames.Serialization;
namespace EpicGames.Horde.Compute
{
///
/// Identifier for a compute cluster
///
[LogValueType]
[JsonSchemaString]
[CbConverter(typeof(ClusterIdCbConverter))]
[JsonConverter(typeof(ClusterIdJsonConverter))]
[TypeConverter(typeof(ClusterIdTypeConverter))]
public readonly struct ClusterId : IEquatable
{
///
/// The text representing this id
///
readonly StringId _inner;
///
/// Constructor
///
/// Unique id for the string
public ClusterId(string input)
{
_inner = new StringId(input);
}
///
public override bool Equals(object? obj) => obj is ClusterId id && _inner.Equals(id._inner);
///
public override int GetHashCode() => _inner.GetHashCode();
///
public bool Equals(ClusterId other) => _inner.Equals(other._inner);
///
public override string ToString() => _inner.ToString();
///
public static bool operator ==(ClusterId left, ClusterId right) => left._inner == right._inner;
///
public static bool operator !=(ClusterId left, ClusterId right) => left._inner != right._inner;
}
///
/// Compact binary converter for ClusterId
///
sealed class ClusterIdCbConverter : CbConverter
{
///
public override ClusterId Read(CbField field) => new ClusterId(field.AsString());
///
public override void Write(CbWriter writer, ClusterId value) => writer.WriteStringValue(value.ToString());
///
public override void WriteNamed(CbWriter writer, CbFieldName name, ClusterId value) => writer.WriteString(name, value.ToString());
}
///
/// Type converter for ClusterId to and from JSON
///
sealed class ClusterIdJsonConverter : JsonConverter
{
///
public override ClusterId Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) => new ClusterId(reader.GetString() ?? String.Empty);
///
public override void Write(Utf8JsonWriter writer, ClusterId value, JsonSerializerOptions options) => writer.WriteStringValue(value.ToString());
}
///
/// Type converter from strings to ClusterId objects
///
sealed class ClusterIdTypeConverter : TypeConverter
{
///
public override bool CanConvertFrom(ITypeDescriptorContext? context, Type? sourceType) => sourceType == typeof(string);
///
public override object ConvertFrom(ITypeDescriptorContext? context, CultureInfo? culture, object? value) => new ClusterId((string)value!);
}
}