// #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"; } var itemType = MapType(def.Items, currentNsid, rootNamespace); return $"System.Collections.Generic.List<{itemType}>"; } /// /// Determines if a type should be nullable based on being optional. /// 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 + "?"; } /// /// Checks if a C# type is a value type. /// 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 }; } /// /// Gets the default value for a type. /// 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 }; } }