Something went wrong. Try again.
.NET ATProtocol Library - Built for Source Gen
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247// <auto-generated />#nullable enable
using System;using System.Text;
namespace CarpaNet.Utilities;
/// <summary>/// Helper methods for working with NSIDs (Namespaced Identifiers)./// </summary>public static class NsidHelper{ /// <summary> /// Converts an NSID to a C# namespace. /// Example: "app.bsky.feed.post" -> "AppBsky.Feed" /// </summary> public static string ToNamespace(string nsid, string? rootNamespace = null) { if (string.IsNullOrEmpty(nsid)) { return rootNamespace ?? "ATProto"; }
var parts = nsid.Split('.'); if (parts.Length < 3) { return rootNamespace ?? "ATProto"; }
var sb = new StringBuilder();
// First two parts become the root namespace (e.g., "app.bsky" -> "AppBsky") sb.Append(ToPascalCase(parts[0])); sb.Append(ToPascalCase(parts[1]));
// Middle parts become nested namespaces (excluding the last part which is the type name) for (int i = 2; i < parts.Length - 1; i++) { sb.Append('.'); sb.Append(ToPascalCase(parts[i])); }
if (!string.IsNullOrEmpty(rootNamespace)) { return $"{rootNamespace}.{sb}"; }
return sb.ToString(); }
/// <summary> /// Extracts the type name from an NSID. /// Example: "app.bsky.feed.post" -> "Post" /// </summary> public static string ToTypeName(string nsid) { if (string.IsNullOrEmpty(nsid)) { return "Unknown"; }
var parts = nsid.Split('.'); if (parts.Length == 0) { return "Unknown"; }
return ToPascalCase(parts[parts.Length - 1]); }
/// <summary> /// Extracts the type name from a reference string. /// Handles both global refs (NSID) and local refs (#name). /// </summary> public static string RefToTypeName(string refString) { if (string.IsNullOrEmpty(refString)) { return "Unknown"; }
// Local reference: #typeName if (refString.StartsWith("#")) { return ToPascalCase(refString.Substring(1)); }
// Global reference with fragment: nsid#typeName var hashIndex = refString.IndexOf('#'); if (hashIndex >= 0) { return ToPascalCase(refString.Substring(hashIndex + 1)); }
// Global reference without fragment (main type) return ToTypeName(refString); }
/// <summary> /// Extracts the namespace from a reference string. /// </summary> public static string RefToNamespace(string refString, string currentNsid, string? rootNamespace = null) { if (string.IsNullOrEmpty(refString)) { return ToNamespace(currentNsid, rootNamespace); }
// Local reference: use current namespace if (refString.StartsWith("#")) { return ToNamespace(currentNsid, rootNamespace); }
// Global reference with fragment var hashIndex = refString.IndexOf('#'); if (hashIndex >= 0) { var nsid = refString.Substring(0, hashIndex); return ToNamespace(nsid, rootNamespace); }
// Global reference without fragment return ToNamespace(refString, rootNamespace); }
/// <summary> /// Gets the full qualified type name from a reference. /// </summary> public static string RefToFullTypeName(string refString, string currentNsid, string? rootNamespace = null) { var ns = RefToNamespace(refString, currentNsid, rootNamespace); var typeName = RefToTypeName(refString); return $"{ns}.{typeName}"; }
/// <summary> /// Converts a string to PascalCase. /// </summary> public static string ToPascalCase(string input) { if (string.IsNullOrEmpty(input)) { return input; }
var sb = new StringBuilder(); bool capitalizeNext = true;
foreach (char c in input) { if (c == '-' || c == '_') { capitalizeNext = true; continue; }
if (capitalizeNext) { sb.Append(char.ToUpperInvariant(c)); capitalizeNext = false; } else { sb.Append(c); } }
return sb.ToString(); }
/// <summary> /// Converts a string to camelCase. /// </summary> public static string ToCamelCase(string input) { var pascal = ToPascalCase(input); if (string.IsNullOrEmpty(pascal)) { return pascal; }
return char.ToLowerInvariant(pascal[0]) + pascal.Substring(1); }
/// <summary> /// Checks if a string is a valid C# identifier and escapes reserved keywords. /// </summary> public static string EscapeIdentifier(string identifier) { if (string.IsNullOrEmpty(identifier)) { return "_"; }
// Reserved C# keywords var keywords = new[] { "abstract", "as", "base", "bool", "break", "byte", "case", "catch", "char", "checked", "class", "const", "continue", "decimal", "default", "delegate", "do", "double", "else", "enum", "event", "explicit", "extern", "false", "finally", "fixed", "float", "for", "foreach", "goto", "if", "implicit", "in", "int", "interface", "internal", "is", "lock", "long", "namespace", "new", "null", "object", "operator", "out", "override", "params", "private", "protected", "public", "readonly", "ref", "return", "sbyte", "sealed", "short", "sizeof", "stackalloc", "static", "string", "struct", "switch", "this", "throw", "true", "try", "typeof", "uint", "ulong", "unchecked", "unsafe", "ushort", "using", "virtual", "void", "volatile", "while", "record" };
foreach (var keyword in keywords) { if (identifier.Equals(keyword, StringComparison.OrdinalIgnoreCase)) { return "@" + identifier; } }
// Ensure first character is valid if (!char.IsLetter(identifier[0]) && identifier[0] != '_') { identifier = "_" + identifier; }
return identifier; }
/// <summary> /// Strips the @ escape prefix from an identifier if present. /// Useful for generating derived names (like interface names) from escaped identifiers. /// </summary> public static string StripEscapePrefix(string identifier) { if (string.IsNullOrEmpty(identifier)) { return identifier; }
if (identifier.StartsWith("@")) { return identifier.Substring(1); }
return identifier; }}