Something went wrong. Try again.
.NET ATProtocol Library - Built for Source Gen
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161// <auto-generated />#nullable enable
using System;using System.Collections.Generic;using CarpaNet.Models;
namespace CarpaNet.Utilities;
/// <summary>/// Maps Lexicon types to C# types./// </summary>public static class TypeMapper{ /// <summary> /// Maps a Lexicon type to a C# type string. /// </summary> public static string MapType(LexiconDefinition def, string currentNsid, string? rootNamespace = null) { return def.Type switch { "boolean" => "bool", "integer" => "long", "string" => MapStringType(def), "bytes" => "byte[]", "cid-link" => "CarpaNet.ATCid", "blob" => "CarpaNet.ATBlob", "unknown" => "System.Text.Json.JsonElement", "ref" => MapRefType(def.Ref, currentNsid, rootNamespace), "union" => MapUnionType(def, currentNsid, rootNamespace), "array" => MapArrayType(def, currentNsid, rootNamespace), "object" => "object", // Will be replaced by generated class name "token" => "string", _ => "object" }; }
/// <summary> /// Maps a string type with format to the appropriate C# type. /// </summary> public static string MapStringType(LexiconDefinition def) { if (def.Format == null) { return "string"; }
return def.Format switch { "at-identifier" => "CarpaNet.ATIdentifier", "at-uri" => "CarpaNet.ATUri", "cid" => "string", "datetime" => "System.DateTimeOffset", "did" => "CarpaNet.ATDid", "handle" => "CarpaNet.ATHandle", "nsid" => "string", "tid" => "string", "record-key" => "string", "uri" => "string", "language" => "string", _ => "string" }; }
/// <summary> /// Maps a ref type to the referenced C# type. /// </summary> public static string MapRefType(string? refString, string currentNsid, string? rootNamespace = null) { if (string.IsNullOrEmpty(refString)) { return "object"; }
return NsidHelper.RefToFullTypeName(refString!, currentNsid, rootNamespace); }
/// <summary> /// Maps a union type to an interface name. /// </summary> public static string MapUnionType(LexiconDefinition def, string currentNsid, string? rootNamespace = null) { // Union types are represented as interfaces // The actual interface name will be determined by the property name or context return "object"; }
/// <summary> /// Maps an array type to List<T>. /// </summary> public static string MapArrayType(LexiconDefinition def, string currentNsid, string? rootNamespace = null) { if (def.Items == null) { return "System.Collections.Generic.List<object>"; }
var itemType = MapType(def.Items, currentNsid, rootNamespace); return $"System.Collections.Generic.List<{itemType}>"; }
/// <summary> /// Determines if a type should be nullable based on being optional. /// </summary> public static string MakeNullable(string typeName) { // Value types need ? if (IsValueType(typeName)) { return typeName + "?"; }
// Reference types are already nullable with nullable reference types enabled return typeName + "?"; }
/// <summary> /// Checks if a C# type is a value type. /// </summary> public static bool IsValueType(string typeName) { return typeName switch { "bool" => true, "long" => true, "int" => true, "short" => true, "byte" => true, "float" => true, "double" => true, "decimal" => true, "System.DateTimeOffset" => true, _ when typeName.StartsWith("System.DateTimeOffset") => true, _ => false }; }
/// <summary> /// Gets the default value for a type. /// </summary> public static string? GetDefaultValue(LexiconDefinition def) { if (!def.Default.HasValue) { return null; }
var jsonDefault = def.Default.Value;
return jsonDefault.ValueKind switch { System.Text.Json.JsonValueKind.True => "true", System.Text.Json.JsonValueKind.False => "false", System.Text.Json.JsonValueKind.Number => jsonDefault.GetRawText(), System.Text.Json.JsonValueKind.String => $"\"{jsonDefault.GetString()}\"", System.Text.Json.JsonValueKind.Null => "null", _ => null }; }}