//
#nullable enable
using System;
using System.Collections.Generic;
using CarpaNet.Models;
namespace CarpaNet.Utilities;
///
/// Maps Lexicon types to C# types.
///
public static class TypeMapper
{
///
/// Maps a Lexicon type to a C# type string.
///
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"
};
}
///
/// Maps a string type with format to the appropriate C# type.
///
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"
};
}
///
/// Maps a ref type to the referenced C# type.
///
public static string MapRefType(string? refString, string currentNsid, string? rootNamespace = null)
{
if (string.IsNullOrEmpty(refString))
{
return "object";
}
return NsidHelper.RefToFullTypeName(refString!, currentNsid, rootNamespace);
}
///
/// Maps a union type to an interface name.
///
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";
}
///
/// Maps an array type to List<T>.
///
public static string MapArrayType(LexiconDefinition def, string currentNsid, string? rootNamespace = null)
{
if (def.Items == null)
{
return "System.Collections.Generic.List