diff --git a/src/SDK/Language/Unity.php b/src/SDK/Language/Unity.php index e4c7b45225..e1d9015ae0 100644 --- a/src/SDK/Language/Unity.php +++ b/src/SDK/Language/Unity.php @@ -669,7 +669,7 @@ public function getFiles(): array public function getFilters(): array { return [ - new TwigFilter('unityComment', function ($value) { + new TwigFilter('dotnetComment', function ($value) { $value = explode("\n", $value); foreach ($value as $key => $line) { $value[$key] = " /// " . wordwrap($line, 75, "\n /// "); diff --git a/templates/dotnet/Package/Converters/ObjectToInferredTypesConverter.cs.twig b/templates/dotnet/Package/Converters/ObjectToInferredTypesConverter.cs.twig index 563f92992a..758e6b659c 100644 --- a/templates/dotnet/Package/Converters/ObjectToInferredTypesConverter.cs.twig +++ b/templates/dotnet/Package/Converters/ObjectToInferredTypesConverter.cs.twig @@ -1,44 +1 @@ -using System; -using System.Collections.Generic; -using System.Text.Json; -using System.Text.Json.Serialization; - -namespace {{ spec.title | caseUcfirst }}.Converters -{ - public class ObjectToInferredTypesConverter : JsonConverter - { - public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - switch (reader.TokenType) - { - case JsonTokenType.True: - return true; - case JsonTokenType.False: - return false; - case JsonTokenType.Number: - if (reader.TryGetInt64(out long l)) - { - return l; - } - return reader.GetDouble(); - case JsonTokenType.String: - if (reader.TryGetDateTime(out DateTime datetime)) - { - return datetime; - } - return reader.GetString()!; - case JsonTokenType.StartObject: - return JsonSerializer.Deserialize>(ref reader, options)!; - case JsonTokenType.StartArray: - return JsonSerializer.Deserialize(ref reader, options)!; - default: - return JsonDocument.ParseValue(ref reader).RootElement.Clone(); - } - } - - public override void Write(Utf8JsonWriter writer, object objectToWrite, JsonSerializerOptions options) - { - JsonSerializer.Serialize(writer, objectToWrite, objectToWrite.GetType(), options); - } - } -} +{% include 'dotnet/shared/Converters/ObjectToInferredTypesConverter.cs.twig' %} \ No newline at end of file diff --git a/templates/dotnet/Package/Converters/ValueClassConverter.cs.twig b/templates/dotnet/Package/Converters/ValueClassConverter.cs.twig index 1b4fda3681..d14d96aaa6 100644 --- a/templates/dotnet/Package/Converters/ValueClassConverter.cs.twig +++ b/templates/dotnet/Package/Converters/ValueClassConverter.cs.twig @@ -1,39 +1 @@ -using System; -using System.Text.Json; -using System.Text.Json.Serialization; -using {{ spec.title | caseUcfirst }}.Enums; - -namespace {{ spec.title | caseUcfirst }}.Converters -{ - public class ValueClassConverter : JsonConverter - { - public override bool CanConvert(Type objectType) - { - return typeof(IEnum).IsAssignableFrom(objectType); - } - - public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - var value = reader.GetString(); - var constructor = typeToConvert.GetConstructor(new[] { typeof(string) }); - var obj = constructor?.Invoke(new object[] { value! }); - - return Convert.ChangeType(obj, typeToConvert)!; - } - - public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options) - { - var type = value.GetType(); - var property = type.GetProperty(nameof(IEnum.Value)); - var propertyValue = property?.GetValue(value); - - if (propertyValue == null) - { - writer.WriteNullValue(); - return; - } - - writer.WriteStringValue(propertyValue.ToString()); - } - } -} +{% include 'dotnet/shared/Converters/ValueClassConverter.cs.twig' %} \ No newline at end of file diff --git a/templates/dotnet/Package/Enums/Enum.cs.twig b/templates/dotnet/Package/Enums/Enum.cs.twig index 6720ce59bb..eaaf2d1998 100644 --- a/templates/dotnet/Package/Enums/Enum.cs.twig +++ b/templates/dotnet/Package/Enums/Enum.cs.twig @@ -1,19 +1 @@ -using System; - -namespace {{ spec.title | caseUcfirst }}.Enums -{ - public class {{ enum.name | caseUcfirst | overrideIdentifier }} : IEnum - { - public string Value { get; private set; } - - public {{ enum.name | caseUcfirst | overrideIdentifier }}(string value) - { - Value = value; - } - - {%~ for value in enum.enum %} - {%~ set key = enum.keys is empty ? value : enum.keys[loop.index0] %} - public static {{ enum.name | caseUcfirst | overrideIdentifier }} {{ key | caseEnumKey }} => new {{ enum.name | caseUcfirst | overrideIdentifier }}("{{ value }}"); - {%~ endfor %} - } -} +{% include 'dotnet/shared/Enums/Enum.cs.twig' %} \ No newline at end of file diff --git a/templates/dotnet/Package/Enums/IEnum.cs.twig b/templates/dotnet/Package/Enums/IEnum.cs.twig index 5d7744d128..7e249d14bd 100644 --- a/templates/dotnet/Package/Enums/IEnum.cs.twig +++ b/templates/dotnet/Package/Enums/IEnum.cs.twig @@ -1,9 +1 @@ -using System; - -namespace {{ spec.title | caseUcfirst }}.Enums -{ - public interface IEnum - { - public string Value { get; } - } -} +{% include 'dotnet/shared/Enums/IEnum.cs.twig' %} \ No newline at end of file diff --git a/templates/dotnet/Package/Exception.cs.twig b/templates/dotnet/Package/Exception.cs.twig index e78d78c2cc..e34d16077b 100644 --- a/templates/dotnet/Package/Exception.cs.twig +++ b/templates/dotnet/Package/Exception.cs.twig @@ -1,27 +1 @@ -using System; - -namespace {{spec.title | caseUcfirst}} -{ - public class {{spec.title | caseUcfirst}}Exception : Exception - { - public int? Code { get; set; } - public string? Type { get; set; } = null; - public string? Response { get; set; } = null; - - public {{spec.title | caseUcfirst}}Exception( - string? message = null, - int? code = null, - string? type = null, - string? response = null) : base(message) - { - this.Code = code; - this.Type = type; - this.Response = response; - } - public {{spec.title | caseUcfirst}}Exception(string message, Exception inner) - : base(message, inner) - { - } - } -} - +{% include 'dotnet/shared/Exception.cs.twig' %} \ No newline at end of file diff --git a/templates/dotnet/Package/Extensions/Extensions.cs.twig b/templates/dotnet/Package/Extensions/Extensions.cs.twig index d57318077e..45a30f4f24 100644 --- a/templates/dotnet/Package/Extensions/Extensions.cs.twig +++ b/templates/dotnet/Package/Extensions/Extensions.cs.twig @@ -1,627 +1 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Text.Json; - -namespace {{ spec.title | caseUcfirst }}.Extensions -{ - public static class Extensions - { - public static string ToJson(this Dictionary dict) - { - return JsonSerializer.Serialize(dict, Client.SerializerOptions); - } - - public static string ToQueryString(this Dictionary parameters) - { - var query = new List(); - - foreach (var kvp in parameters) - { - switch (kvp.Value) - { - case null: - continue; - case IList list: - foreach (var item in list) - { - query.Add($"{kvp.Key}[]={item}"); - } - break; - default: - query.Add($"{kvp.Key}={kvp.Value.ToString()}"); - break; - } - } - - return Uri.EscapeUriString(string.Join("&", query)); - } - - private static IDictionary _mappings = new Dictionary(StringComparer.InvariantCultureIgnoreCase) { - - #region Mime Types - {".323", "text/h323"}, - {".3g2", "video/3gpp2"}, - {".3gp", "video/3gpp"}, - {".3gp2", "video/3gpp2"}, - {".3gpp", "video/3gpp"}, - {".7z", "application/x-7z-compressed"}, - {".aa", "audio/audible"}, - {".AAC", "audio/aac"}, - {".aaf", "application/octet-stream"}, - {".aax", "audio/vnd.audible.aax"}, - {".ac3", "audio/ac3"}, - {".aca", "application/octet-stream"}, - {".accda", "application/msaccess.addin"}, - {".accdb", "application/msaccess"}, - {".accdc", "application/msaccess.cab"}, - {".accde", "application/msaccess"}, - {".accdr", "application/msaccess.runtime"}, - {".accdt", "application/msaccess"}, - {".accdw", "application/msaccess.webapplication"}, - {".accft", "application/msaccess.ftemplate"}, - {".acx", "application/internet-property-stream"}, - {".AddIn", "text/xml"}, - {".ade", "application/msaccess"}, - {".adobebridge", "application/x-bridge-url"}, - {".adp", "application/msaccess"}, - {".ADT", "audio/vnd.dlna.adts"}, - {".ADTS", "audio/aac"}, - {".afm", "application/octet-stream"}, - {".ai", "application/postscript"}, - {".aif", "audio/x-aiff"}, - {".aifc", "audio/aiff"}, - {".aiff", "audio/aiff"}, - {".air", "application/vnd.adobe.air-application-installer-package+zip"}, - {".amc", "application/x-mpeg"}, - {".application", "application/x-ms-application"}, - {".art", "image/x-jg"}, - {".asa", "application/xml"}, - {".asax", "application/xml"}, - {".ascx", "application/xml"}, - {".asd", "application/octet-stream"}, - {".asf", "video/x-ms-asf"}, - {".ashx", "application/xml"}, - {".asi", "application/octet-stream"}, - {".asm", "text/plain"}, - {".asmx", "application/xml"}, - {".aspx", "application/xml"}, - {".asr", "video/x-ms-asf"}, - {".asx", "video/x-ms-asf"}, - {".atom", "application/atom+xml"}, - {".au", "audio/basic"}, - {".avi", "video/x-msvideo"}, - {".axs", "application/olescript"}, - {".bas", "text/plain"}, - {".bcpio", "application/x-bcpio"}, - {".bin", "application/octet-stream"}, - {".bmp", "image/bmp"}, - {".c", "text/plain"}, - {".cab", "application/octet-stream"}, - {".caf", "audio/x-caf"}, - {".calx", "application/vnd.ms-office.calx"}, - {".cat", "application/vnd.ms-pki.seccat"}, - {".cc", "text/plain"}, - {".cd", "text/plain"}, - {".cdda", "audio/aiff"}, - {".cdf", "application/x-cdf"}, - {".cer", "application/x-x509-ca-cert"}, - {".chm", "application/octet-stream"}, - {".class", "application/x-java-applet"}, - {".clp", "application/x-msclip"}, - {".cmx", "image/x-cmx"}, - {".cnf", "text/plain"}, - {".cod", "image/cis-cod"}, - {".config", "application/xml"}, - {".contact", "text/x-ms-contact"}, - {".coverage", "application/xml"}, - {".cpio", "application/x-cpio"}, - {".cpp", "text/plain"}, - {".crd", "application/x-mscardfile"}, - {".crl", "application/pkix-crl"}, - {".crt", "application/x-x509-ca-cert"}, - {".cs", "text/plain"}, - {".csdproj", "text/plain"}, - {".csh", "application/x-csh"}, - {".csproj", "text/plain"}, - {".css", "text/css"}, - {".csv", "text/csv"}, - {".cur", "application/octet-stream"}, - {".cxx", "text/plain"}, - {".dat", "application/octet-stream"}, - {".datasource", "application/xml"}, - {".dbproj", "text/plain"}, - {".dcr", "application/x-director"}, - {".def", "text/plain"}, - {".deploy", "application/octet-stream"}, - {".der", "application/x-x509-ca-cert"}, - {".dgml", "application/xml"}, - {".dib", "image/bmp"}, - {".dif", "video/x-dv"}, - {".dir", "application/x-director"}, - {".disco", "text/xml"}, - {".dll", "application/x-msdownload"}, - {".dll.config", "text/xml"}, - {".dlm", "text/dlm"}, - {".doc", "application/msword"}, - {".docm", "application/vnd.ms-word.document.macroEnabled.12"}, - {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}, - {".dot", "application/msword"}, - {".dotm", "application/vnd.ms-word.template.macroEnabled.12"}, - {".dotx", "application/vnd.openxmlformats-officedocument.wordprocessingml.template"}, - {".dsp", "application/octet-stream"}, - {".dsw", "text/plain"}, - {".dtd", "text/xml"}, - {".dtsConfig", "text/xml"}, - {".dv", "video/x-dv"}, - {".dvi", "application/x-dvi"}, - {".dwf", "drawing/x-dwf"}, - {".dwp", "application/octet-stream"}, - {".dxr", "application/x-director"}, - {".eml", "message/rfc822"}, - {".emz", "application/octet-stream"}, - {".eot", "application/octet-stream"}, - {".eps", "application/postscript"}, - {".etl", "application/etl"}, - {".etx", "text/x-setext"}, - {".evy", "application/envoy"}, - {".exe", "application/octet-stream"}, - {".exe.config", "text/xml"}, - {".fdf", "application/vnd.fdf"}, - {".fif", "application/fractals"}, - {".filters", "Application/xml"}, - {".fla", "application/octet-stream"}, - {".flr", "x-world/x-vrml"}, - {".flv", "video/x-flv"}, - {".fsscript", "application/fsharp-script"}, - {".fsx", "application/fsharp-script"}, - {".generictest", "application/xml"}, - {".gif", "image/gif"}, - {".group", "text/x-ms-group"}, - {".gsm", "audio/x-gsm"}, - {".gtar", "application/x-gtar"}, - {".gz", "application/x-gzip"}, - {".h", "text/plain"}, - {".hdf", "application/x-hdf"}, - {".hdml", "text/x-hdml"}, - {".hhc", "application/x-oleobject"}, - {".hhk", "application/octet-stream"}, - {".hhp", "application/octet-stream"}, - {".hlp", "application/winhlp"}, - {".hpp", "text/plain"}, - {".hqx", "application/mac-binhex40"}, - {".hta", "application/hta"}, - {".htc", "text/x-component"}, - {".htm", "text/html"}, - {".html", "text/html"}, - {".htt", "text/webviewhtml"}, - {".hxa", "application/xml"}, - {".hxc", "application/xml"}, - {".hxd", "application/octet-stream"}, - {".hxe", "application/xml"}, - {".hxf", "application/xml"}, - {".hxh", "application/octet-stream"}, - {".hxi", "application/octet-stream"}, - {".hxk", "application/xml"}, - {".hxq", "application/octet-stream"}, - {".hxr", "application/octet-stream"}, - {".hxs", "application/octet-stream"}, - {".hxt", "text/html"}, - {".hxv", "application/xml"}, - {".hxw", "application/octet-stream"}, - {".hxx", "text/plain"}, - {".i", "text/plain"}, - {".ico", "image/x-icon"}, - {".ics", "application/octet-stream"}, - {".idl", "text/plain"}, - {".ief", "image/ief"}, - {".iii", "application/x-iphone"}, - {".inc", "text/plain"}, - {".inf", "application/octet-stream"}, - {".inl", "text/plain"}, - {".ins", "application/x-internet-signup"}, - {".ipa", "application/x-itunes-ipa"}, - {".ipg", "application/x-itunes-ipg"}, - {".ipproj", "text/plain"}, - {".ipsw", "application/x-itunes-ipsw"}, - {".iqy", "text/x-ms-iqy"}, - {".isp", "application/x-internet-signup"}, - {".ite", "application/x-itunes-ite"}, - {".itlp", "application/x-itunes-itlp"}, - {".itms", "application/x-itunes-itms"}, - {".itpc", "application/x-itunes-itpc"}, - {".IVF", "video/x-ivf"}, - {".jar", "application/java-archive"}, - {".java", "application/octet-stream"}, - {".jck", "application/liquidmotion"}, - {".jcz", "application/liquidmotion"}, - {".jfif", "image/pjpeg"}, - {".jnlp", "application/x-java-jnlp-file"}, - {".jpb", "application/octet-stream"}, - {".jpe", "image/jpeg"}, - {".jpeg", "image/jpeg"}, - {".jpg", "image/jpeg"}, - {".js", "application/x-javascript"}, - {".json", "application/json"}, - {".jsx", "text/jscript"}, - {".jsxbin", "text/plain"}, - {".latex", "application/x-latex"}, - {".library-ms", "application/windows-library+xml"}, - {".lit", "application/x-ms-reader"}, - {".loadtest", "application/xml"}, - {".lpk", "application/octet-stream"}, - {".lsf", "video/x-la-asf"}, - {".lst", "text/plain"}, - {".lsx", "video/x-la-asf"}, - {".lzh", "application/octet-stream"}, - {".m13", "application/x-msmediaview"}, - {".m14", "application/x-msmediaview"}, - {".m1v", "video/mpeg"}, - {".m2t", "video/vnd.dlna.mpeg-tts"}, - {".m2ts", "video/vnd.dlna.mpeg-tts"}, - {".m2v", "video/mpeg"}, - {".m3u", "audio/x-mpegurl"}, - {".m3u8", "audio/x-mpegurl"}, - {".m4a", "audio/m4a"}, - {".m4b", "audio/m4b"}, - {".m4p", "audio/m4p"}, - {".m4r", "audio/x-m4r"}, - {".m4v", "video/x-m4v"}, - {".mac", "image/x-macpaint"}, - {".mak", "text/plain"}, - {".man", "application/x-troff-man"}, - {".manifest", "application/x-ms-manifest"}, - {".map", "text/plain"}, - {".master", "application/xml"}, - {".mda", "application/msaccess"}, - {".mdb", "application/x-msaccess"}, - {".mde", "application/msaccess"}, - {".mdp", "application/octet-stream"}, - {".me", "application/x-troff-me"}, - {".mfp", "application/x-shockwave-flash"}, - {".mht", "message/rfc822"}, - {".mhtml", "message/rfc822"}, - {".mid", "audio/mid"}, - {".midi", "audio/mid"}, - {".mix", "application/octet-stream"}, - {".mk", "text/plain"}, - {".mmf", "application/x-smaf"}, - {".mno", "text/xml"}, - {".mny", "application/x-msmoney"}, - {".mod", "video/mpeg"}, - {".mov", "video/quicktime"}, - {".movie", "video/x-sgi-movie"}, - {".mp2", "video/mpeg"}, - {".mp2v", "video/mpeg"}, - {".mp3", "audio/mpeg"}, - {".mp4", "video/mp4"}, - {".mp4v", "video/mp4"}, - {".mpa", "video/mpeg"}, - {".mpe", "video/mpeg"}, - {".mpeg", "video/mpeg"}, - {".mpf", "application/vnd.ms-mediapackage"}, - {".mpg", "video/mpeg"}, - {".mpp", "application/vnd.ms-project"}, - {".mpv2", "video/mpeg"}, - {".mqv", "video/quicktime"}, - {".ms", "application/x-troff-ms"}, - {".msi", "application/octet-stream"}, - {".mso", "application/octet-stream"}, - {".mts", "video/vnd.dlna.mpeg-tts"}, - {".mtx", "application/xml"}, - {".mvb", "application/x-msmediaview"}, - {".mvc", "application/x-miva-compiled"}, - {".mxp", "application/x-mmxp"}, - {".nc", "application/x-netcdf"}, - {".nsc", "video/x-ms-asf"}, - {".nws", "message/rfc822"}, - {".ocx", "application/octet-stream"}, - {".oda", "application/oda"}, - {".odc", "text/x-ms-odc"}, - {".odh", "text/plain"}, - {".odl", "text/plain"}, - {".odp", "application/vnd.oasis.opendocument.presentation"}, - {".ods", "application/oleobject"}, - {".odt", "application/vnd.oasis.opendocument.text"}, - {".one", "application/onenote"}, - {".onea", "application/onenote"}, - {".onepkg", "application/onenote"}, - {".onetmp", "application/onenote"}, - {".onetoc", "application/onenote"}, - {".onetoc2", "application/onenote"}, - {".orderedtest", "application/xml"}, - {".osdx", "application/opensearchdescription+xml"}, - {".p10", "application/pkcs10"}, - {".p12", "application/x-pkcs12"}, - {".p7b", "application/x-pkcs7-certificates"}, - {".p7c", "application/pkcs7-mime"}, - {".p7m", "application/pkcs7-mime"}, - {".p7r", "application/x-pkcs7-certreqresp"}, - {".p7s", "application/pkcs7-signature"}, - {".pbm", "image/x-portable-bitmap"}, - {".pcast", "application/x-podcast"}, - {".pct", "image/pict"}, - {".pcx", "application/octet-stream"}, - {".pcz", "application/octet-stream"}, - {".pdf", "application/pdf"}, - {".pfb", "application/octet-stream"}, - {".pfm", "application/octet-stream"}, - {".pfx", "application/x-pkcs12"}, - {".pgm", "image/x-portable-graymap"}, - {".pic", "image/pict"}, - {".pict", "image/pict"}, - {".pkgdef", "text/plain"}, - {".pkgundef", "text/plain"}, - {".pko", "application/vnd.ms-pki.pko"}, - {".pls", "audio/scpls"}, - {".pma", "application/x-perfmon"}, - {".pmc", "application/x-perfmon"}, - {".pml", "application/x-perfmon"}, - {".pmr", "application/x-perfmon"}, - {".pmw", "application/x-perfmon"}, - {".png", "image/png"}, - {".pnm", "image/x-portable-anymap"}, - {".pnt", "image/x-macpaint"}, - {".pntg", "image/x-macpaint"}, - {".pnz", "image/png"}, - {".pot", "application/vnd.ms-powerpoint"}, - {".potm", "application/vnd.ms-powerpoint.template.macroEnabled.12"}, - {".potx", "application/vnd.openxmlformats-officedocument.presentationml.template"}, - {".ppa", "application/vnd.ms-powerpoint"}, - {".ppam", "application/vnd.ms-powerpoint.addin.macroEnabled.12"}, - {".ppm", "image/x-portable-pixmap"}, - {".pps", "application/vnd.ms-powerpoint"}, - {".ppsm", "application/vnd.ms-powerpoint.slideshow.macroEnabled.12"}, - {".ppsx", "application/vnd.openxmlformats-officedocument.presentationml.slideshow"}, - {".ppt", "application/vnd.ms-powerpoint"}, - {".pptm", "application/vnd.ms-powerpoint.presentation.macroEnabled.12"}, - {".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"}, - {".prf", "application/pics-rules"}, - {".prm", "application/octet-stream"}, - {".prx", "application/octet-stream"}, - {".ps", "application/postscript"}, - {".psc1", "application/PowerShell"}, - {".psd", "application/octet-stream"}, - {".psess", "application/xml"}, - {".psm", "application/octet-stream"}, - {".psp", "application/octet-stream"}, - {".pub", "application/x-mspublisher"}, - {".pwz", "application/vnd.ms-powerpoint"}, - {".qht", "text/x-html-insertion"}, - {".qhtm", "text/x-html-insertion"}, - {".qt", "video/quicktime"}, - {".qti", "image/x-quicktime"}, - {".qtif", "image/x-quicktime"}, - {".qtl", "application/x-quicktimeplayer"}, - {".qxd", "application/octet-stream"}, - {".ra", "audio/x-pn-realaudio"}, - {".ram", "audio/x-pn-realaudio"}, - {".rar", "application/octet-stream"}, - {".ras", "image/x-cmu-raster"}, - {".rat", "application/rat-file"}, - {".rc", "text/plain"}, - {".rc2", "text/plain"}, - {".rct", "text/plain"}, - {".rdlc", "application/xml"}, - {".resx", "application/xml"}, - {".rf", "image/vnd.rn-realflash"}, - {".rgb", "image/x-rgb"}, - {".rgs", "text/plain"}, - {".rm", "application/vnd.rn-realmedia"}, - {".rmi", "audio/mid"}, - {".rmp", "application/vnd.rn-rn_music_package"}, - {".roff", "application/x-troff"}, - {".rpm", "audio/x-pn-realaudio-plugin"}, - {".rqy", "text/x-ms-rqy"}, - {".rtf", "application/rtf"}, - {".rtx", "text/richtext"}, - {".ruleset", "application/xml"}, - {".s", "text/plain"}, - {".safariextz", "application/x-safari-safariextz"}, - {".scd", "application/x-msschedule"}, - {".sct", "text/scriptlet"}, - {".sd2", "audio/x-sd2"}, - {".sdp", "application/sdp"}, - {".sea", "application/octet-stream"}, - {".searchConnector-ms", "application/windows-search-connector+xml"}, - {".setpay", "application/set-payment-initiation"}, - {".setreg", "application/set-registration-initiation"}, - {".settings", "application/xml"}, - {".sgimb", "application/x-sgimb"}, - {".sgml", "text/sgml"}, - {".sh", "application/x-sh"}, - {".shar", "application/x-shar"}, - {".shtml", "text/html"}, - {".sit", "application/x-stuffit"}, - {".sitemap", "application/xml"}, - {".skin", "application/xml"}, - {".sldm", "application/vnd.ms-powerpoint.slide.macroEnabled.12"}, - {".sldx", "application/vnd.openxmlformats-officedocument.presentationml.slide"}, - {".slk", "application/vnd.ms-excel"}, - {".sln", "text/plain"}, - {".slupkg-ms", "application/x-ms-license"}, - {".smd", "audio/x-smd"}, - {".smi", "application/octet-stream"}, - {".smx", "audio/x-smd"}, - {".smz", "audio/x-smd"}, - {".snd", "audio/basic"}, - {".snippet", "application/xml"}, - {".snp", "application/octet-stream"}, - {".sol", "text/plain"}, - {".sor", "text/plain"}, - {".spc", "application/x-pkcs7-certificates"}, - {".spl", "application/futuresplash"}, - {".src", "application/x-wais-source"}, - {".srf", "text/plain"}, - {".SSISDeploymentManifest", "text/xml"}, - {".ssm", "application/streamingmedia"}, - {".sst", "application/vnd.ms-pki.certstore"}, - {".stl", "application/vnd.ms-pki.stl"}, - {".sv4cpio", "application/x-sv4cpio"}, - {".sv4crc", "application/x-sv4crc"}, - {".svc", "application/xml"}, - {".swf", "application/x-shockwave-flash"}, - {".t", "application/x-troff"}, - {".tar", "application/x-tar"}, - {".tcl", "application/x-tcl"}, - {".testrunconfig", "application/xml"}, - {".testsettings", "application/xml"}, - {".tex", "application/x-tex"}, - {".texi", "application/x-texinfo"}, - {".texinfo", "application/x-texinfo"}, - {".tgz", "application/x-compressed"}, - {".thmx", "application/vnd.ms-officetheme"}, - {".thn", "application/octet-stream"}, - {".tif", "image/tiff"}, - {".tiff", "image/tiff"}, - {".tlh", "text/plain"}, - {".tli", "text/plain"}, - {".toc", "application/octet-stream"}, - {".tr", "application/x-troff"}, - {".trm", "application/x-msterminal"}, - {".trx", "application/xml"}, - {".ts", "video/vnd.dlna.mpeg-tts"}, - {".tsv", "text/tab-separated-values"}, - {".ttf", "application/octet-stream"}, - {".tts", "video/vnd.dlna.mpeg-tts"}, - {".txt", "text/plain"}, - {".u32", "application/octet-stream"}, - {".uls", "text/iuls"}, - {".user", "text/plain"}, - {".ustar", "application/x-ustar"}, - {".vb", "text/plain"}, - {".vbdproj", "text/plain"}, - {".vbk", "video/mpeg"}, - {".vbproj", "text/plain"}, - {".vbs", "text/vbscript"}, - {".vcf", "text/x-vcard"}, - {".vcproj", "Application/xml"}, - {".vcs", "text/plain"}, - {".vcxproj", "Application/xml"}, - {".vddproj", "text/plain"}, - {".vdp", "text/plain"}, - {".vdproj", "text/plain"}, - {".vdx", "application/vnd.ms-visio.viewer"}, - {".vml", "text/xml"}, - {".vscontent", "application/xml"}, - {".vsct", "text/xml"}, - {".vsd", "application/vnd.visio"}, - {".vsi", "application/ms-vsi"}, - {".vsix", "application/vsix"}, - {".vsixlangpack", "text/xml"}, - {".vsixmanifest", "text/xml"}, - {".vsmdi", "application/xml"}, - {".vspscc", "text/plain"}, - {".vss", "application/vnd.visio"}, - {".vsscc", "text/plain"}, - {".vssettings", "text/xml"}, - {".vssscc", "text/plain"}, - {".vst", "application/vnd.visio"}, - {".vstemplate", "text/xml"}, - {".vsto", "application/x-ms-vsto"}, - {".vsw", "application/vnd.visio"}, - {".vsx", "application/vnd.visio"}, - {".vtx", "application/vnd.visio"}, - {".wav", "audio/wav"}, - {".wave", "audio/wav"}, - {".wax", "audio/x-ms-wax"}, - {".wbk", "application/msword"}, - {".wbmp", "image/vnd.wap.wbmp"}, - {".wcm", "application/vnd.ms-works"}, - {".wdb", "application/vnd.ms-works"}, - {".wdp", "image/vnd.ms-photo"}, - {".webarchive", "application/x-safari-webarchive"}, - {".webtest", "application/xml"}, - {".wiq", "application/xml"}, - {".wiz", "application/msword"}, - {".wks", "application/vnd.ms-works"}, - {".WLMP", "application/wlmoviemaker"}, - {".wlpginstall", "application/x-wlpg-detect"}, - {".wlpginstall3", "application/x-wlpg3-detect"}, - {".wm", "video/x-ms-wm"}, - {".wma", "audio/x-ms-wma"}, - {".wmd", "application/x-ms-wmd"}, - {".wmf", "application/x-msmetafile"}, - {".wml", "text/vnd.wap.wml"}, - {".wmlc", "application/vnd.wap.wmlc"}, - {".wmls", "text/vnd.wap.wmlscript"}, - {".wmlsc", "application/vnd.wap.wmlscriptc"}, - {".wmp", "video/x-ms-wmp"}, - {".wmv", "video/x-ms-wmv"}, - {".wmx", "video/x-ms-wmx"}, - {".wmz", "application/x-ms-wmz"}, - {".wpl", "application/vnd.ms-wpl"}, - {".wps", "application/vnd.ms-works"}, - {".wri", "application/x-mswrite"}, - {".wrl", "x-world/x-vrml"}, - {".wrz", "x-world/x-vrml"}, - {".wsc", "text/scriptlet"}, - {".wsdl", "text/xml"}, - {".wvx", "video/x-ms-wvx"}, - {".x", "application/directx"}, - {".xaf", "x-world/x-vrml"}, - {".xaml", "application/xaml+xml"}, - {".xap", "application/x-silverlight-app"}, - {".xbap", "application/x-ms-xbap"}, - {".xbm", "image/x-xbitmap"}, - {".xdr", "text/plain"}, - {".xht", "application/xhtml+xml"}, - {".xhtml", "application/xhtml+xml"}, - {".xla", "application/vnd.ms-excel"}, - {".xlam", "application/vnd.ms-excel.addin.macroEnabled.12"}, - {".xlc", "application/vnd.ms-excel"}, - {".xld", "application/vnd.ms-excel"}, - {".xlk", "application/vnd.ms-excel"}, - {".xll", "application/vnd.ms-excel"}, - {".xlm", "application/vnd.ms-excel"}, - {".xls", "application/vnd.ms-excel"}, - {".xlsb", "application/vnd.ms-excel.sheet.binary.macroEnabled.12"}, - {".xlsm", "application/vnd.ms-excel.sheet.macroEnabled.12"}, - {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, - {".xlt", "application/vnd.ms-excel"}, - {".xltm", "application/vnd.ms-excel.template.macroEnabled.12"}, - {".xltx", "application/vnd.openxmlformats-officedocument.spreadsheetml.template"}, - {".xlw", "application/vnd.ms-excel"}, - {".xml", "text/xml"}, - {".xmta", "application/xml"}, - {".xof", "x-world/x-vrml"}, - {".XOML", "text/plain"}, - {".xpm", "image/x-xpixmap"}, - {".xps", "application/vnd.ms-xpsdocument"}, - {".xrm-ms", "text/xml"}, - {".xsc", "application/xml"}, - {".xsd", "text/xml"}, - {".xsf", "text/xml"}, - {".xsl", "text/xml"}, - {".xslt", "text/xml"}, - {".xsn", "application/octet-stream"}, - {".xss", "application/xml"}, - {".xtp", "application/octet-stream"}, - {".xwd", "image/x-xwindowdump"}, - {".z", "application/x-compress"}, - {".zip", "application/x-zip-compressed"}, - #endregion - - }; - - public static string GetMimeTypeFromExtension(string extension) - { - if (extension == null) - { - throw new ArgumentNullException("extension"); - } - - if (!extension.StartsWith(".")) - { - extension = "." + extension; - } - - return _mappings.TryGetValue(extension, out var mime) ? mime : "application/octet-stream"; - } - - public static string GetMimeType(this string path) - { - return GetMimeTypeFromExtension(System.IO.Path.GetExtension(path)); - } - } -} \ No newline at end of file +{% include 'dotnet/shared/Extensions/Extensions.cs.twig' %} \ No newline at end of file diff --git a/templates/dotnet/Package/ID.cs.twig b/templates/dotnet/Package/ID.cs.twig index 1d59b3fe99..02a1d4cbcd 100644 --- a/templates/dotnet/Package/ID.cs.twig +++ b/templates/dotnet/Package/ID.cs.twig @@ -1,42 +1 @@ -using System; - -namespace {{ spec.title | caseUcfirst }} -{ - public static class ID - { - // Generate an hex ID based on timestamp - // Recreated from https://www.php.net/manual/en/function.uniqid.php - private static string HexTimestamp() - { - var now = DateTime.UtcNow; - var epoch = (now - new DateTime(1970, 1, 1)); - var sec = (long)epoch.TotalSeconds; - var usec = (long)((epoch.TotalMilliseconds * 1000) % 1000); - - // Convert to hexadecimal - var hexTimestamp = sec.ToString("x") + usec.ToString("x").PadLeft(5, '0'); - return hexTimestamp; - } - - // Generate a unique ID with padding to have a longer ID - public static string Unique(int padding = 7) - { - var random = new Random(); - var baseId = HexTimestamp(); - var randomPadding = ""; - - for (int i = 0; i < padding; i++) - { - var randomHexDigit = random.Next(0, 16).ToString("x"); - randomPadding += randomHexDigit; - } - - return baseId + randomPadding; - } - - public static string Custom(string id) - { - return id; - } - } -} +{% include 'dotnet/shared/ID.cs.twig' %} \ No newline at end of file diff --git a/templates/dotnet/Package/Models/InputFile.cs.twig b/templates/dotnet/Package/Models/InputFile.cs.twig index 241a3adad5..84355821f6 100644 --- a/templates/dotnet/Package/Models/InputFile.cs.twig +++ b/templates/dotnet/Package/Models/InputFile.cs.twig @@ -1,41 +1 @@ -using System.IO; -using Appwrite.Extensions; - -namespace {{ spec.title | caseUcfirst }}.Models -{ - public class InputFile - { - public string Path { get; set; } = string.Empty; - public string Filename { get; set; } = string.Empty; - public string MimeType { get; set; } = string.Empty; - public string SourceType { get; set; } = string.Empty; - public object Data { get; set; } = new object(); - - public static InputFile FromPath(string path) => new InputFile - { - Path = path, - Filename = System.IO.Path.GetFileName(path), - MimeType = path.GetMimeType(), - SourceType = "path" - }; - - public static InputFile FromFileInfo(FileInfo fileInfo) => - InputFile.FromPath(fileInfo.FullName); - - public static InputFile FromStream(Stream stream, string filename, string mimeType) => new InputFile - { - Data = stream, - Filename = filename, - MimeType = mimeType, - SourceType = "stream" - }; - - public static InputFile FromBytes(byte[] bytes, string filename, string mimeType) => new InputFile - { - Data = bytes, - Filename = filename, - MimeType = mimeType, - SourceType = "bytes" - }; - } -} \ No newline at end of file +{% include 'dotnet/shared/Models/InputFile.cs.twig' %} \ No newline at end of file diff --git a/templates/dotnet/Package/Models/Model.cs.twig b/templates/dotnet/Package/Models/Model.cs.twig index ff46ff18e4..aed099061e 100644 --- a/templates/dotnet/Package/Models/Model.cs.twig +++ b/templates/dotnet/Package/Models/Model.cs.twig @@ -1,104 +1 @@ -{% macro sub_schema(property) %}{% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | caseUcfirst | overrideIdentifier}}>{% else %}{{property.sub_schema | caseUcfirst | overrideIdentifier}}{% endif %}{% else %}{{property | typeName}}{% endif %}{% if not property.required %}?{% endif %}{% endmacro %} -{% macro property_name(definition, property) %}{{ property.name | caseUcfirst | removeDollarSign | escapeKeyword }}{% endmacro %} - -using System; -using System.Linq; -using System.Collections.Generic; -using System.Text.Json; -using System.Text.Json.Serialization; - -namespace {{ spec.title | caseUcfirst }}.Models -{ - public class {{ definition.name | caseUcfirst | overrideIdentifier }} - { - {%~ for property in definition.properties %} - [JsonPropertyName("{{ property.name }}")] - public {{ _self.sub_schema(property) }} {{ _self.property_name(definition, property) | overrideProperty(definition.name) }} { get; private set; } - - {%~ endfor %} - {%~ if definition.additionalProperties %} - public Dictionary Data { get; private set; } - - {%~ endif %} - public {{ definition.name | caseUcfirst | overrideIdentifier }}( - {%~ for property in definition.properties %} - {{ _self.sub_schema(property) }} {{ property.name | caseCamel | escapeKeyword }}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %} - - {%~ endfor %} - {%~ if definition.additionalProperties %} - Dictionary data - {%~ endif %} - ) { - {%~ for property in definition.properties %} - {{ _self.property_name(definition, property) | overrideProperty(definition.name) }} = {{ property.name | caseCamel | escapeKeyword }}; - {%~ endfor %} - {%~ if definition.additionalProperties %} - Data = data; - {%~ endif %} - } - - public static {{ definition.name | caseUcfirst | overrideIdentifier }} From(Dictionary map) => new {{ definition.name | caseUcfirst | overrideIdentifier }}( - {%~ for property in definition.properties %} - {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}:{{' '}} - {%- if property.sub_schema %} - {%- if property.type == 'array' -%} - map["{{ property.name }}"] is JsonElement jsonArray{{ loop.index }} ? jsonArray{{ loop.index }}.Deserialize>>()!.Select(it => {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: it)).ToList() : ((IEnumerable>)map["{{ property.name }}"]).Select(it => {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: it)).ToList() - {%- else -%} - {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: map["{{ property.name }}"] is JsonElement jsonObj{{ loop.index }} ? jsonObj{{ loop.index }}.Deserialize>()! : (Dictionary)map["{{ property.name }}"]) - {%- endif %} - {%- else %} - {%- if property.type == 'array' -%} - map["{{ property.name }}"] is JsonElement jsonArrayProp{{ loop.index }} ? jsonArrayProp{{ loop.index }}.Deserialize<{{ property | typeName }}>()! : ({{ property | typeName }})map["{{ property.name }}"] - {%- else %} - {%- if property.type == "integer" or property.type == "number" %} - {%- if not property.required -%}map["{{ property.name }}"] == null ? null :{% endif %}Convert.To{% if property.type == "integer" %}Int64{% else %}Double{% endif %}(map["{{ property.name }}"]) - {%- else %} - {%- if property.type == "boolean" -%} - ({{ property | typeName }}{% if not property.required %}?{% endif %})map["{{ property.name }}"] - {%- else %} - {%- if not property.required -%} - map.TryGetValue("{{ property.name }}", out var {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}) ? {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}?.ToString() : null - {%- else -%} - map["{{ property.name }}"].ToString() - {%- endif %} - {%- endif %} - {%~ endif %} - {%~ endif %} - {%~ endif %} - {%- if not loop.last or (loop.last and definition.additionalProperties) %}, - {%~ endif %} - {%~ endfor %} - {%- if definition.additionalProperties %} - data: map - {%- endif ~%} - ); - - public Dictionary ToMap() => new Dictionary() - { - {%~ for property in definition.properties %} - { "{{ property.name }}", {% if property.sub_schema %}{% if property.type == 'array' %}{{ _self.property_name(definition, property) | overrideProperty(definition.name) }}.Select(it => it.ToMap()){% else %}{{ _self.property_name(definition, property) | overrideProperty(definition.name) }}.ToMap(){% endif %}{% else %}{{ _self.property_name(definition, property) | overrideProperty(definition.name) }}{% endif %}{{ ' }' }}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %} - - {%~ endfor %} - {%~ if definition.additionalProperties %} - { "data", Data } - {%~ endif %} - }; - {%~ if definition.additionalProperties %} - - public T ConvertTo(Func, T> fromJson) => - fromJson.Invoke(Data); - {%~ endif %} - {%~ for property in definition.properties %} - {%~ if property.sub_schema %} - {%~ for def in spec.definitions %} - {%~ if def.name == property.sub_schema and def.additionalProperties and property.type == 'array' %} - - public T ConvertTo(Func, T> fromJson) => - (T){{ property.name | caseUcfirst | escapeKeyword }}.Select(it => it.ConvertTo(fromJson)); - - {%~ endif %} - {%~ endfor %} - {%~ endif %} - {%~ endfor %} - } -} +{% include 'dotnet/shared/Models/Model.cs.twig' %} \ No newline at end of file diff --git a/templates/dotnet/Package/Models/OrderType.cs.twig b/templates/dotnet/Package/Models/OrderType.cs.twig index 12852880f6..e70a6497bb 100644 --- a/templates/dotnet/Package/Models/OrderType.cs.twig +++ b/templates/dotnet/Package/Models/OrderType.cs.twig @@ -1,8 +1 @@ -namespace {{ spec.title | caseUcfirst }} -{ - public enum OrderType - { - ASC, - DESC - } -} +{% include 'dotnet/shared/Models/OrderType.cs.twig' %} \ No newline at end of file diff --git a/templates/dotnet/Package/Models/UploadProgress.cs.twig b/templates/dotnet/Package/Models/UploadProgress.cs.twig index 47c78391ce..ebc7be9d76 100644 --- a/templates/dotnet/Package/Models/UploadProgress.cs.twig +++ b/templates/dotnet/Package/Models/UploadProgress.cs.twig @@ -1,26 +1 @@ -namespace {{ spec.title | caseUcfirst }} -{ - public class UploadProgress - { - public string Id { get; private set; } - public double Progress { get; private set; } - public long SizeUploaded { get; private set; } - public long ChunksTotal { get; private set; } - public long ChunksUploaded { get; private set; } - - public UploadProgress( - string id, - double progress, - long sizeUploaded, - long chunksTotal, - long chunksUploaded - ) - { - Id = id; - Progress = progress; - SizeUploaded = sizeUploaded; - ChunksTotal = chunksTotal; - ChunksUploaded = chunksUploaded; - } - } -} \ No newline at end of file +{% include 'dotnet/shared/Models/UploadProgress.cs.twig' %} \ No newline at end of file diff --git a/templates/dotnet/Package/Permission.cs.twig b/templates/dotnet/Package/Permission.cs.twig index 5bde420f15..8a1c97604d 100644 --- a/templates/dotnet/Package/Permission.cs.twig +++ b/templates/dotnet/Package/Permission.cs.twig @@ -1,30 +1 @@ -namespace {{ spec.title | caseUcfirst }} -{ - public static class Permission - { - public static string Read(string role) - { - return $"read(\"{role}\")"; - } - - public static string Write(string role) - { - return $"write(\"{role}\")"; - } - - public static string Create(string role) - { - return $"create(\"{role}\")"; - } - - public static string Update(string role) - { - return $"update(\"{role}\")"; - } - - public static string Delete(string role) - { - return $"delete(\"{role}\")"; - } - } -} +{% include 'dotnet/shared/Permission.cs.twig' %} \ No newline at end of file diff --git a/templates/dotnet/Package/Query.cs.twig b/templates/dotnet/Package/Query.cs.twig index 18359f30c2..9ac20c20f9 100644 --- a/templates/dotnet/Package/Query.cs.twig +++ b/templates/dotnet/Package/Query.cs.twig @@ -1,161 +1 @@ -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Text.Json; -using System.Text.Json.Serialization; - - -namespace {{ spec.title | caseUcfirst }} -{ - public class Query - { - [JsonPropertyName("method")] - public string Method { get; set; } = string.Empty; - - [JsonPropertyName("attribute")] - public string? Attribute { get; set; } - - [JsonPropertyName("values")] - public List? Values { get; set; } - - public Query() - { - } - - public Query(string method, string? attribute, object? values) - { - this.Method = method; - this.Attribute = attribute; - - if (values is IList valuesList) - { - this.Values = new List(); - foreach (var value in valuesList) - { - this.Values.Add(value); // Automatically boxes if value is a value type - } - } - else if (values != null) - { - this.Values = new List { values }; - } - } - - override public string ToString() - { - return JsonSerializer.Serialize(this, Client.SerializerOptions); - } - - public static string Equal(string attribute, object value) - { - return new Query("equal", attribute, value).ToString(); - } - - public static string NotEqual(string attribute, object value) - { - return new Query("notEqual", attribute, value).ToString(); - } - - public static string LessThan(string attribute, object value) - { - return new Query("lessThan", attribute, value).ToString(); - } - - public static string LessThanEqual(string attribute, object value) - { - return new Query("lessThanEqual", attribute, value).ToString(); - } - - public static string GreaterThan(string attribute, object value) - { - return new Query("greaterThan", attribute, value).ToString(); - } - - public static string GreaterThanEqual(string attribute, object value) - { - return new Query("greaterThanEqual", attribute, value).ToString(); - } - - public static string Search(string attribute, string value) - { - return new Query("search", attribute, value).ToString(); - } - - public static string IsNull(string attribute) - { - return new Query("isNull", attribute, null).ToString(); - } - - public static string IsNotNull(string attribute) - { - return new Query("isNotNull", attribute, null).ToString(); - } - - public static string StartsWith(string attribute, string value) - { - return new Query("startsWith", attribute, value).ToString(); - } - - public static string EndsWith(string attribute, string value) - { - return new Query("endsWith", attribute, value).ToString(); - } - - public static string Between(string attribute, string start, string end) - { - return new Query("between", attribute, new List { start, end }).ToString(); - } - - public static string Between(string attribute, int start, int end) - { - return new Query("between", attribute, new List { start, end }).ToString(); - } - - public static string Between(string attribute, double start, double end) - { - return new Query("between", attribute, new List { start, end }).ToString(); - } - - public static string Select(List attributes) - { - return new Query("select", null, attributes).ToString(); - } - - public static string CursorAfter(string documentId) - { - return new Query("cursorAfter", null, documentId).ToString(); - } - - public static string CursorBefore(string documentId) { - return new Query("cursorBefore", null, documentId).ToString(); - } - - public static string OrderAsc(string attribute) { - return new Query("orderAsc", attribute, null).ToString(); - } - - public static string OrderDesc(string attribute) { - return new Query("orderDesc", attribute, null).ToString(); - } - - public static string Limit(int limit) { - return new Query("limit", null, limit).ToString(); - } - - public static string Offset(int offset) { - return new Query("offset", null, offset).ToString(); - } - - public static string Contains(string attribute, object value) { - return new Query("contains", attribute, value).ToString(); - } - - public static string Or(List queries) { - return new Query("or", null, queries.Select(q => JsonSerializer.Deserialize(q, Client.DeserializerOptions)).ToList()).ToString(); - } - - public static string And(List queries) { - return new Query("and", null, queries.Select(q => JsonSerializer.Deserialize(q, Client.DeserializerOptions)).ToList()).ToString(); - } - } -} \ No newline at end of file +{% include 'dotnet/shared/Query.cs.twig' %} \ No newline at end of file diff --git a/templates/dotnet/Package/Role.cs.twig b/templates/dotnet/Package/Role.cs.twig index b3ecf2610b..abc2773ab2 100644 --- a/templates/dotnet/Package/Role.cs.twig +++ b/templates/dotnet/Package/Role.cs.twig @@ -1,92 +1 @@ -namespace Appwrite -{ - /// - /// Helper class to generate role strings for Permission. - /// - public static class Role - { - /// - /// Grants access to anyone. - /// - /// This includes authenticated and unauthenticated users. - /// - /// - public static string Any() - { - return "any"; - } - - /// - /// Grants access to a specific user by user ID. - /// - /// You can optionally pass verified or unverified for - /// status to target specific types of users. - /// - /// - public static string User(string id, string status = "") - { - return status == string.Empty - ? $"user:{id}" - : $"user:{id}/{status}"; - } - - /// - /// Grants access to any authenticated or anonymous user. - /// - /// You can optionally pass verified or unverified for - /// status to target specific types of users. - /// - /// - public static string Users(string status = "") - { - return status == string.Empty - ? "users" : - $"users/{status}"; - } - - /// - /// Grants access to any guest user without a session. - /// - /// Authenticated users don't have access to this role. - /// - /// - public static string Guests() - { - return "guests"; - } - - /// - /// Grants access to a team by team ID. - /// - /// You can optionally pass a role for role to target - /// team members with the specified role. - /// - /// - public static string Team(string id, string role = "") - { - return role == string.Empty - ? $"team:{id}" - : $"team:{id}/{role}"; - } - - /// - /// Grants access to a specific member of a team. - /// - /// When the member is removed from the team, they will - /// no longer have access. - /// - /// - public static string Member(string id) - { - return $"member:{id}"; - } - - /// - /// Grants access to a user with the specified label. - /// - public static string Label(string name) - { - return $"label:{name}"; - } - } -} \ No newline at end of file +{% include 'dotnet/shared/Role.cs.twig' %} \ No newline at end of file diff --git a/templates/dotnet/Package/Services/Service.cs.twig b/templates/dotnet/Package/Services/Service.cs.twig index c093d50c06..7ca1cbd13b 100644 --- a/templates/dotnet/Package/Services/Service.cs.twig +++ b/templates/dotnet/Package/Services/Service.cs.twig @@ -1,12 +1 @@ -namespace {{ spec.title | caseUcfirst }} -{ - public abstract class Service - { - protected readonly Client _client; - - public Service(Client client) - { - this._client = client; - } - } -} +{% include 'dotnet/shared/Services/Service.cs.twig' %} \ No newline at end of file diff --git a/templates/dotnet/Package/Services/ServiceTemplate.cs.twig b/templates/dotnet/Package/Services/ServiceTemplate.cs.twig index 99cf15653b..91210a8a9f 100644 --- a/templates/dotnet/Package/Services/ServiceTemplate.cs.twig +++ b/templates/dotnet/Package/Services/ServiceTemplate.cs.twig @@ -1,64 +1 @@ -{% import 'dotnet/base/utils.twig' as utils %} - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -{% if spec.definitions is not empty %} -using {{ spec.title | caseUcfirst }}.Models; -{% endif %} -{% if spec.enums is not empty %} -using {{ spec.title | caseUcfirst }}.Enums; -{% endif %} - -namespace {{ spec.title | caseUcfirst }}.Services -{ - public class {{ service.name | caseUcfirst }} : Service - { - public {{ service.name | caseUcfirst }}(Client client) : base(client) - { - } - - {%~ for method in service.methods %} - {%~ if method.description %} - /// - {{~ method.description | dotnetComment }} - /// - {%~ endif %} - /// - {%~ if method.deprecated %} - {%~ if method.since and method.replaceWith %} - [Obsolete("This API has been deprecated since {{ method.since }}. Please use `{{ method.replaceWith | capitalizeFirst }}` instead.")] - {%~ else %} - [Obsolete("This API has been deprecated.")] - {%~ endif %} - {%~ endif %} - public Task{% if method.type == "webAuth" %}{% else %}<{{ utils.resultType(spec.title, method) }}>{% endif %} {{ method.name | caseUcfirst }}({{ utils.method_parameters(method.parameters, method.consumes) }}) - { - var apiPath = "{{ method.path }}"{% if method.parameters.path | length == 0 %};{% endif %} - - {{~ include('dotnet/base/params.twig') }} - - {%~ if method.responseModel %} - static {{ utils.resultType(spec.title, method) }} Convert(Dictionary it) => - {%~ if method.responseModel == 'any' %} - it; - {%~ else %} - {{ utils.resultType(spec.title, method) }}.From(map: it); - {%~ endif %} - {%~ endif %} - - {%~ if method.type == 'location' %} - {{~ include('dotnet/base/requests/location.twig') }} - {%~ elseif method.type == 'webAuth' %} - {{~ include('dotnet/base/requests/oauth.twig') }} - {%~ elseif 'multipart/form-data' in method.consumes %} - {{~ include('dotnet/base/requests/file.twig') }} - {%~ else %} - {{~ include('dotnet/base/requests/api.twig')}} - {%~ endif %} - } - - {%~ endfor %} - } -} +{% include 'dotnet/shared/Services/ServiceTemplate.cs.twig' %} \ No newline at end of file diff --git a/templates/dotnet/base/utils.twig b/templates/dotnet/base/utils.twig index 35ec0a8b81..19ea870059 100644 --- a/templates/dotnet/base/utils.twig +++ b/templates/dotnet/base/utils.twig @@ -6,7 +6,7 @@ {% if parameters.all|length > 0 %}{% for parameter in parameters.all | filter((param) => not param.isGlobal) %}{{ _self.parameter(parameter) }}{% if not loop.last %}{{ ', ' }}{% endif %}{% endfor %}{% if 'multipart/form-data' in consumes %},{% endif %}{% endif %}{% if 'multipart/form-data' in consumes %} Action? onProgress = null{% endif %} {% endmacro %} {% macro map_parameter(parameter) %} -{% if parameter.name == 'orderType' %}{{ parameter.name | caseCamel ~ '.ToString()'}}{% elseif parameter.isGlobal %}{{ parameter.name | caseUcfirst | escapeKeyword }}{% else %}{{ parameter.name | caseCamel | escapeKeyword }}{% endif %} +{% if parameter.name == 'orderType' %}{{ parameter.name | caseCamel ~ '.ToString()'}}{% elseif parameter.isGlobal %}{{ parameter.name | caseUcfirst | escapeKeyword }}{% elseif parameter.enumValues is not empty %}{{ parameter.name | caseCamel | escapeKeyword }}?.Value{% else %}{{ parameter.name | caseCamel | escapeKeyword }}{% endif %} {% endmacro %} {% macro methodNeedsSecurityParameters(method) %} {% if (method.type == "webAuth" or method.type == "location") and method.auth|length > 0 %}{{ true }}{% else %}{{false}}{% endif %} diff --git a/templates/dotnet/shared/Converters/ObjectToInferredTypesConverter.cs.twig b/templates/dotnet/shared/Converters/ObjectToInferredTypesConverter.cs.twig new file mode 100644 index 0000000000..fd9512f9bd --- /dev/null +++ b/templates/dotnet/shared/Converters/ObjectToInferredTypesConverter.cs.twig @@ -0,0 +1,72 @@ +using System; +using System.Collections.Generic; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace {{ spec.title | caseUcfirst }}.Converters +{ + public class ObjectToInferredTypesConverter : JsonConverter + { + public override object? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + using (JsonDocument document = JsonDocument.ParseValue(ref reader)) + { + return ConvertElement(document.RootElement); + } + } + + private object? ConvertElement(JsonElement element) + { + switch (element.ValueKind) + { + case JsonValueKind.Object: + var dictionary = new Dictionary(); + foreach (var property in element.EnumerateObject()) + { + dictionary[property.Name] = ConvertElement(property.Value); + } + return dictionary; + + case JsonValueKind.Array: + var list = new List(); + foreach (var item in element.EnumerateArray()) + { + list.Add(ConvertElement(item)); + } + return list; + + case JsonValueKind.String: + if (element.TryGetDateTime(out DateTime datetime)) + { + return datetime; + } + return element.GetString(); + + case JsonValueKind.Number: + if (element.TryGetInt64(out long l)) + { + return l; + } + return element.GetDouble(); + + case JsonValueKind.True: + return true; + + case JsonValueKind.False: + return false; + + case JsonValueKind.Null: + case JsonValueKind.Undefined: + return null; + + default: + throw new JsonException($"Unsupported JsonValueKind: {element.ValueKind}"); + } + } + + public override void Write(Utf8JsonWriter writer, object objectToWrite, JsonSerializerOptions options) + { + JsonSerializer.Serialize(writer, objectToWrite, objectToWrite.GetType(), options); + } + } +} \ No newline at end of file diff --git a/templates/dotnet/shared/Converters/ValueClassConverter.cs.twig b/templates/dotnet/shared/Converters/ValueClassConverter.cs.twig new file mode 100644 index 0000000000..1b4fda3681 --- /dev/null +++ b/templates/dotnet/shared/Converters/ValueClassConverter.cs.twig @@ -0,0 +1,39 @@ +using System; +using System.Text.Json; +using System.Text.Json.Serialization; +using {{ spec.title | caseUcfirst }}.Enums; + +namespace {{ spec.title | caseUcfirst }}.Converters +{ + public class ValueClassConverter : JsonConverter + { + public override bool CanConvert(Type objectType) + { + return typeof(IEnum).IsAssignableFrom(objectType); + } + + public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) + { + var value = reader.GetString(); + var constructor = typeToConvert.GetConstructor(new[] { typeof(string) }); + var obj = constructor?.Invoke(new object[] { value! }); + + return Convert.ChangeType(obj, typeToConvert)!; + } + + public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options) + { + var type = value.GetType(); + var property = type.GetProperty(nameof(IEnum.Value)); + var propertyValue = property?.GetValue(value); + + if (propertyValue == null) + { + writer.WriteNullValue(); + return; + } + + writer.WriteStringValue(propertyValue.ToString()); + } + } +} diff --git a/templates/dotnet/shared/Enums/Enum.cs.twig b/templates/dotnet/shared/Enums/Enum.cs.twig new file mode 100644 index 0000000000..d3c768a4e7 --- /dev/null +++ b/templates/dotnet/shared/Enums/Enum.cs.twig @@ -0,0 +1,19 @@ +using System; + +namespace {{ spec.title | caseUcfirst }}.Enums +{ + public class {{ enum.name | caseUcfirst | overrideIdentifier }} : IEnum + { + public string Value { get; private set; } + + public {{ enum.name | caseUcfirst | overrideIdentifier }}(string value) + { + Value = value; + } + + {%~ for value in enum.enum %} + {%~ set key = enum.keys is empty ? value : enum.keys[loop.index0] %} + public static {{ enum.name | caseUcfirst | overrideIdentifier }} {{ key | caseEnumKey }} => new {{ enum.name | caseUcfirst | overrideIdentifier }}("{{ value }}"); + {%~ endfor %} + } +} \ No newline at end of file diff --git a/templates/dotnet/shared/Enums/IEnum.cs.twig b/templates/dotnet/shared/Enums/IEnum.cs.twig new file mode 100644 index 0000000000..5d7744d128 --- /dev/null +++ b/templates/dotnet/shared/Enums/IEnum.cs.twig @@ -0,0 +1,9 @@ +using System; + +namespace {{ spec.title | caseUcfirst }}.Enums +{ + public interface IEnum + { + public string Value { get; } + } +} diff --git a/templates/dotnet/shared/Exception.cs.twig b/templates/dotnet/shared/Exception.cs.twig new file mode 100644 index 0000000000..31d9c70adc --- /dev/null +++ b/templates/dotnet/shared/Exception.cs.twig @@ -0,0 +1,27 @@ +using System; + +namespace {{spec.title | caseUcfirst}} +{ + public class {{spec.title | caseUcfirst}}Exception : Exception + { + public int? Code { get; set; } + public string? Type { get; set; } = null; + public string? Response { get; set; } = null; + + public {{spec.title | caseUcfirst}}Exception( + string? message = null, + int? code = null, + string? type = null, + string? response = null) : base(message) + { + this.Code = code; + this.Type = type; + this.Response = response; + } + + public {{spec.title | caseUcfirst}}Exception(string message, Exception inner) + : base(message, inner) + { + } + } +} diff --git a/templates/dotnet/shared/Extensions/Extensions.cs.twig b/templates/dotnet/shared/Extensions/Extensions.cs.twig new file mode 100644 index 0000000000..d57318077e --- /dev/null +++ b/templates/dotnet/shared/Extensions/Extensions.cs.twig @@ -0,0 +1,627 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Text.Json; + +namespace {{ spec.title | caseUcfirst }}.Extensions +{ + public static class Extensions + { + public static string ToJson(this Dictionary dict) + { + return JsonSerializer.Serialize(dict, Client.SerializerOptions); + } + + public static string ToQueryString(this Dictionary parameters) + { + var query = new List(); + + foreach (var kvp in parameters) + { + switch (kvp.Value) + { + case null: + continue; + case IList list: + foreach (var item in list) + { + query.Add($"{kvp.Key}[]={item}"); + } + break; + default: + query.Add($"{kvp.Key}={kvp.Value.ToString()}"); + break; + } + } + + return Uri.EscapeUriString(string.Join("&", query)); + } + + private static IDictionary _mappings = new Dictionary(StringComparer.InvariantCultureIgnoreCase) { + + #region Mime Types + {".323", "text/h323"}, + {".3g2", "video/3gpp2"}, + {".3gp", "video/3gpp"}, + {".3gp2", "video/3gpp2"}, + {".3gpp", "video/3gpp"}, + {".7z", "application/x-7z-compressed"}, + {".aa", "audio/audible"}, + {".AAC", "audio/aac"}, + {".aaf", "application/octet-stream"}, + {".aax", "audio/vnd.audible.aax"}, + {".ac3", "audio/ac3"}, + {".aca", "application/octet-stream"}, + {".accda", "application/msaccess.addin"}, + {".accdb", "application/msaccess"}, + {".accdc", "application/msaccess.cab"}, + {".accde", "application/msaccess"}, + {".accdr", "application/msaccess.runtime"}, + {".accdt", "application/msaccess"}, + {".accdw", "application/msaccess.webapplication"}, + {".accft", "application/msaccess.ftemplate"}, + {".acx", "application/internet-property-stream"}, + {".AddIn", "text/xml"}, + {".ade", "application/msaccess"}, + {".adobebridge", "application/x-bridge-url"}, + {".adp", "application/msaccess"}, + {".ADT", "audio/vnd.dlna.adts"}, + {".ADTS", "audio/aac"}, + {".afm", "application/octet-stream"}, + {".ai", "application/postscript"}, + {".aif", "audio/x-aiff"}, + {".aifc", "audio/aiff"}, + {".aiff", "audio/aiff"}, + {".air", "application/vnd.adobe.air-application-installer-package+zip"}, + {".amc", "application/x-mpeg"}, + {".application", "application/x-ms-application"}, + {".art", "image/x-jg"}, + {".asa", "application/xml"}, + {".asax", "application/xml"}, + {".ascx", "application/xml"}, + {".asd", "application/octet-stream"}, + {".asf", "video/x-ms-asf"}, + {".ashx", "application/xml"}, + {".asi", "application/octet-stream"}, + {".asm", "text/plain"}, + {".asmx", "application/xml"}, + {".aspx", "application/xml"}, + {".asr", "video/x-ms-asf"}, + {".asx", "video/x-ms-asf"}, + {".atom", "application/atom+xml"}, + {".au", "audio/basic"}, + {".avi", "video/x-msvideo"}, + {".axs", "application/olescript"}, + {".bas", "text/plain"}, + {".bcpio", "application/x-bcpio"}, + {".bin", "application/octet-stream"}, + {".bmp", "image/bmp"}, + {".c", "text/plain"}, + {".cab", "application/octet-stream"}, + {".caf", "audio/x-caf"}, + {".calx", "application/vnd.ms-office.calx"}, + {".cat", "application/vnd.ms-pki.seccat"}, + {".cc", "text/plain"}, + {".cd", "text/plain"}, + {".cdda", "audio/aiff"}, + {".cdf", "application/x-cdf"}, + {".cer", "application/x-x509-ca-cert"}, + {".chm", "application/octet-stream"}, + {".class", "application/x-java-applet"}, + {".clp", "application/x-msclip"}, + {".cmx", "image/x-cmx"}, + {".cnf", "text/plain"}, + {".cod", "image/cis-cod"}, + {".config", "application/xml"}, + {".contact", "text/x-ms-contact"}, + {".coverage", "application/xml"}, + {".cpio", "application/x-cpio"}, + {".cpp", "text/plain"}, + {".crd", "application/x-mscardfile"}, + {".crl", "application/pkix-crl"}, + {".crt", "application/x-x509-ca-cert"}, + {".cs", "text/plain"}, + {".csdproj", "text/plain"}, + {".csh", "application/x-csh"}, + {".csproj", "text/plain"}, + {".css", "text/css"}, + {".csv", "text/csv"}, + {".cur", "application/octet-stream"}, + {".cxx", "text/plain"}, + {".dat", "application/octet-stream"}, + {".datasource", "application/xml"}, + {".dbproj", "text/plain"}, + {".dcr", "application/x-director"}, + {".def", "text/plain"}, + {".deploy", "application/octet-stream"}, + {".der", "application/x-x509-ca-cert"}, + {".dgml", "application/xml"}, + {".dib", "image/bmp"}, + {".dif", "video/x-dv"}, + {".dir", "application/x-director"}, + {".disco", "text/xml"}, + {".dll", "application/x-msdownload"}, + {".dll.config", "text/xml"}, + {".dlm", "text/dlm"}, + {".doc", "application/msword"}, + {".docm", "application/vnd.ms-word.document.macroEnabled.12"}, + {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}, + {".dot", "application/msword"}, + {".dotm", "application/vnd.ms-word.template.macroEnabled.12"}, + {".dotx", "application/vnd.openxmlformats-officedocument.wordprocessingml.template"}, + {".dsp", "application/octet-stream"}, + {".dsw", "text/plain"}, + {".dtd", "text/xml"}, + {".dtsConfig", "text/xml"}, + {".dv", "video/x-dv"}, + {".dvi", "application/x-dvi"}, + {".dwf", "drawing/x-dwf"}, + {".dwp", "application/octet-stream"}, + {".dxr", "application/x-director"}, + {".eml", "message/rfc822"}, + {".emz", "application/octet-stream"}, + {".eot", "application/octet-stream"}, + {".eps", "application/postscript"}, + {".etl", "application/etl"}, + {".etx", "text/x-setext"}, + {".evy", "application/envoy"}, + {".exe", "application/octet-stream"}, + {".exe.config", "text/xml"}, + {".fdf", "application/vnd.fdf"}, + {".fif", "application/fractals"}, + {".filters", "Application/xml"}, + {".fla", "application/octet-stream"}, + {".flr", "x-world/x-vrml"}, + {".flv", "video/x-flv"}, + {".fsscript", "application/fsharp-script"}, + {".fsx", "application/fsharp-script"}, + {".generictest", "application/xml"}, + {".gif", "image/gif"}, + {".group", "text/x-ms-group"}, + {".gsm", "audio/x-gsm"}, + {".gtar", "application/x-gtar"}, + {".gz", "application/x-gzip"}, + {".h", "text/plain"}, + {".hdf", "application/x-hdf"}, + {".hdml", "text/x-hdml"}, + {".hhc", "application/x-oleobject"}, + {".hhk", "application/octet-stream"}, + {".hhp", "application/octet-stream"}, + {".hlp", "application/winhlp"}, + {".hpp", "text/plain"}, + {".hqx", "application/mac-binhex40"}, + {".hta", "application/hta"}, + {".htc", "text/x-component"}, + {".htm", "text/html"}, + {".html", "text/html"}, + {".htt", "text/webviewhtml"}, + {".hxa", "application/xml"}, + {".hxc", "application/xml"}, + {".hxd", "application/octet-stream"}, + {".hxe", "application/xml"}, + {".hxf", "application/xml"}, + {".hxh", "application/octet-stream"}, + {".hxi", "application/octet-stream"}, + {".hxk", "application/xml"}, + {".hxq", "application/octet-stream"}, + {".hxr", "application/octet-stream"}, + {".hxs", "application/octet-stream"}, + {".hxt", "text/html"}, + {".hxv", "application/xml"}, + {".hxw", "application/octet-stream"}, + {".hxx", "text/plain"}, + {".i", "text/plain"}, + {".ico", "image/x-icon"}, + {".ics", "application/octet-stream"}, + {".idl", "text/plain"}, + {".ief", "image/ief"}, + {".iii", "application/x-iphone"}, + {".inc", "text/plain"}, + {".inf", "application/octet-stream"}, + {".inl", "text/plain"}, + {".ins", "application/x-internet-signup"}, + {".ipa", "application/x-itunes-ipa"}, + {".ipg", "application/x-itunes-ipg"}, + {".ipproj", "text/plain"}, + {".ipsw", "application/x-itunes-ipsw"}, + {".iqy", "text/x-ms-iqy"}, + {".isp", "application/x-internet-signup"}, + {".ite", "application/x-itunes-ite"}, + {".itlp", "application/x-itunes-itlp"}, + {".itms", "application/x-itunes-itms"}, + {".itpc", "application/x-itunes-itpc"}, + {".IVF", "video/x-ivf"}, + {".jar", "application/java-archive"}, + {".java", "application/octet-stream"}, + {".jck", "application/liquidmotion"}, + {".jcz", "application/liquidmotion"}, + {".jfif", "image/pjpeg"}, + {".jnlp", "application/x-java-jnlp-file"}, + {".jpb", "application/octet-stream"}, + {".jpe", "image/jpeg"}, + {".jpeg", "image/jpeg"}, + {".jpg", "image/jpeg"}, + {".js", "application/x-javascript"}, + {".json", "application/json"}, + {".jsx", "text/jscript"}, + {".jsxbin", "text/plain"}, + {".latex", "application/x-latex"}, + {".library-ms", "application/windows-library+xml"}, + {".lit", "application/x-ms-reader"}, + {".loadtest", "application/xml"}, + {".lpk", "application/octet-stream"}, + {".lsf", "video/x-la-asf"}, + {".lst", "text/plain"}, + {".lsx", "video/x-la-asf"}, + {".lzh", "application/octet-stream"}, + {".m13", "application/x-msmediaview"}, + {".m14", "application/x-msmediaview"}, + {".m1v", "video/mpeg"}, + {".m2t", "video/vnd.dlna.mpeg-tts"}, + {".m2ts", "video/vnd.dlna.mpeg-tts"}, + {".m2v", "video/mpeg"}, + {".m3u", "audio/x-mpegurl"}, + {".m3u8", "audio/x-mpegurl"}, + {".m4a", "audio/m4a"}, + {".m4b", "audio/m4b"}, + {".m4p", "audio/m4p"}, + {".m4r", "audio/x-m4r"}, + {".m4v", "video/x-m4v"}, + {".mac", "image/x-macpaint"}, + {".mak", "text/plain"}, + {".man", "application/x-troff-man"}, + {".manifest", "application/x-ms-manifest"}, + {".map", "text/plain"}, + {".master", "application/xml"}, + {".mda", "application/msaccess"}, + {".mdb", "application/x-msaccess"}, + {".mde", "application/msaccess"}, + {".mdp", "application/octet-stream"}, + {".me", "application/x-troff-me"}, + {".mfp", "application/x-shockwave-flash"}, + {".mht", "message/rfc822"}, + {".mhtml", "message/rfc822"}, + {".mid", "audio/mid"}, + {".midi", "audio/mid"}, + {".mix", "application/octet-stream"}, + {".mk", "text/plain"}, + {".mmf", "application/x-smaf"}, + {".mno", "text/xml"}, + {".mny", "application/x-msmoney"}, + {".mod", "video/mpeg"}, + {".mov", "video/quicktime"}, + {".movie", "video/x-sgi-movie"}, + {".mp2", "video/mpeg"}, + {".mp2v", "video/mpeg"}, + {".mp3", "audio/mpeg"}, + {".mp4", "video/mp4"}, + {".mp4v", "video/mp4"}, + {".mpa", "video/mpeg"}, + {".mpe", "video/mpeg"}, + {".mpeg", "video/mpeg"}, + {".mpf", "application/vnd.ms-mediapackage"}, + {".mpg", "video/mpeg"}, + {".mpp", "application/vnd.ms-project"}, + {".mpv2", "video/mpeg"}, + {".mqv", "video/quicktime"}, + {".ms", "application/x-troff-ms"}, + {".msi", "application/octet-stream"}, + {".mso", "application/octet-stream"}, + {".mts", "video/vnd.dlna.mpeg-tts"}, + {".mtx", "application/xml"}, + {".mvb", "application/x-msmediaview"}, + {".mvc", "application/x-miva-compiled"}, + {".mxp", "application/x-mmxp"}, + {".nc", "application/x-netcdf"}, + {".nsc", "video/x-ms-asf"}, + {".nws", "message/rfc822"}, + {".ocx", "application/octet-stream"}, + {".oda", "application/oda"}, + {".odc", "text/x-ms-odc"}, + {".odh", "text/plain"}, + {".odl", "text/plain"}, + {".odp", "application/vnd.oasis.opendocument.presentation"}, + {".ods", "application/oleobject"}, + {".odt", "application/vnd.oasis.opendocument.text"}, + {".one", "application/onenote"}, + {".onea", "application/onenote"}, + {".onepkg", "application/onenote"}, + {".onetmp", "application/onenote"}, + {".onetoc", "application/onenote"}, + {".onetoc2", "application/onenote"}, + {".orderedtest", "application/xml"}, + {".osdx", "application/opensearchdescription+xml"}, + {".p10", "application/pkcs10"}, + {".p12", "application/x-pkcs12"}, + {".p7b", "application/x-pkcs7-certificates"}, + {".p7c", "application/pkcs7-mime"}, + {".p7m", "application/pkcs7-mime"}, + {".p7r", "application/x-pkcs7-certreqresp"}, + {".p7s", "application/pkcs7-signature"}, + {".pbm", "image/x-portable-bitmap"}, + {".pcast", "application/x-podcast"}, + {".pct", "image/pict"}, + {".pcx", "application/octet-stream"}, + {".pcz", "application/octet-stream"}, + {".pdf", "application/pdf"}, + {".pfb", "application/octet-stream"}, + {".pfm", "application/octet-stream"}, + {".pfx", "application/x-pkcs12"}, + {".pgm", "image/x-portable-graymap"}, + {".pic", "image/pict"}, + {".pict", "image/pict"}, + {".pkgdef", "text/plain"}, + {".pkgundef", "text/plain"}, + {".pko", "application/vnd.ms-pki.pko"}, + {".pls", "audio/scpls"}, + {".pma", "application/x-perfmon"}, + {".pmc", "application/x-perfmon"}, + {".pml", "application/x-perfmon"}, + {".pmr", "application/x-perfmon"}, + {".pmw", "application/x-perfmon"}, + {".png", "image/png"}, + {".pnm", "image/x-portable-anymap"}, + {".pnt", "image/x-macpaint"}, + {".pntg", "image/x-macpaint"}, + {".pnz", "image/png"}, + {".pot", "application/vnd.ms-powerpoint"}, + {".potm", "application/vnd.ms-powerpoint.template.macroEnabled.12"}, + {".potx", "application/vnd.openxmlformats-officedocument.presentationml.template"}, + {".ppa", "application/vnd.ms-powerpoint"}, + {".ppam", "application/vnd.ms-powerpoint.addin.macroEnabled.12"}, + {".ppm", "image/x-portable-pixmap"}, + {".pps", "application/vnd.ms-powerpoint"}, + {".ppsm", "application/vnd.ms-powerpoint.slideshow.macroEnabled.12"}, + {".ppsx", "application/vnd.openxmlformats-officedocument.presentationml.slideshow"}, + {".ppt", "application/vnd.ms-powerpoint"}, + {".pptm", "application/vnd.ms-powerpoint.presentation.macroEnabled.12"}, + {".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"}, + {".prf", "application/pics-rules"}, + {".prm", "application/octet-stream"}, + {".prx", "application/octet-stream"}, + {".ps", "application/postscript"}, + {".psc1", "application/PowerShell"}, + {".psd", "application/octet-stream"}, + {".psess", "application/xml"}, + {".psm", "application/octet-stream"}, + {".psp", "application/octet-stream"}, + {".pub", "application/x-mspublisher"}, + {".pwz", "application/vnd.ms-powerpoint"}, + {".qht", "text/x-html-insertion"}, + {".qhtm", "text/x-html-insertion"}, + {".qt", "video/quicktime"}, + {".qti", "image/x-quicktime"}, + {".qtif", "image/x-quicktime"}, + {".qtl", "application/x-quicktimeplayer"}, + {".qxd", "application/octet-stream"}, + {".ra", "audio/x-pn-realaudio"}, + {".ram", "audio/x-pn-realaudio"}, + {".rar", "application/octet-stream"}, + {".ras", "image/x-cmu-raster"}, + {".rat", "application/rat-file"}, + {".rc", "text/plain"}, + {".rc2", "text/plain"}, + {".rct", "text/plain"}, + {".rdlc", "application/xml"}, + {".resx", "application/xml"}, + {".rf", "image/vnd.rn-realflash"}, + {".rgb", "image/x-rgb"}, + {".rgs", "text/plain"}, + {".rm", "application/vnd.rn-realmedia"}, + {".rmi", "audio/mid"}, + {".rmp", "application/vnd.rn-rn_music_package"}, + {".roff", "application/x-troff"}, + {".rpm", "audio/x-pn-realaudio-plugin"}, + {".rqy", "text/x-ms-rqy"}, + {".rtf", "application/rtf"}, + {".rtx", "text/richtext"}, + {".ruleset", "application/xml"}, + {".s", "text/plain"}, + {".safariextz", "application/x-safari-safariextz"}, + {".scd", "application/x-msschedule"}, + {".sct", "text/scriptlet"}, + {".sd2", "audio/x-sd2"}, + {".sdp", "application/sdp"}, + {".sea", "application/octet-stream"}, + {".searchConnector-ms", "application/windows-search-connector+xml"}, + {".setpay", "application/set-payment-initiation"}, + {".setreg", "application/set-registration-initiation"}, + {".settings", "application/xml"}, + {".sgimb", "application/x-sgimb"}, + {".sgml", "text/sgml"}, + {".sh", "application/x-sh"}, + {".shar", "application/x-shar"}, + {".shtml", "text/html"}, + {".sit", "application/x-stuffit"}, + {".sitemap", "application/xml"}, + {".skin", "application/xml"}, + {".sldm", "application/vnd.ms-powerpoint.slide.macroEnabled.12"}, + {".sldx", "application/vnd.openxmlformats-officedocument.presentationml.slide"}, + {".slk", "application/vnd.ms-excel"}, + {".sln", "text/plain"}, + {".slupkg-ms", "application/x-ms-license"}, + {".smd", "audio/x-smd"}, + {".smi", "application/octet-stream"}, + {".smx", "audio/x-smd"}, + {".smz", "audio/x-smd"}, + {".snd", "audio/basic"}, + {".snippet", "application/xml"}, + {".snp", "application/octet-stream"}, + {".sol", "text/plain"}, + {".sor", "text/plain"}, + {".spc", "application/x-pkcs7-certificates"}, + {".spl", "application/futuresplash"}, + {".src", "application/x-wais-source"}, + {".srf", "text/plain"}, + {".SSISDeploymentManifest", "text/xml"}, + {".ssm", "application/streamingmedia"}, + {".sst", "application/vnd.ms-pki.certstore"}, + {".stl", "application/vnd.ms-pki.stl"}, + {".sv4cpio", "application/x-sv4cpio"}, + {".sv4crc", "application/x-sv4crc"}, + {".svc", "application/xml"}, + {".swf", "application/x-shockwave-flash"}, + {".t", "application/x-troff"}, + {".tar", "application/x-tar"}, + {".tcl", "application/x-tcl"}, + {".testrunconfig", "application/xml"}, + {".testsettings", "application/xml"}, + {".tex", "application/x-tex"}, + {".texi", "application/x-texinfo"}, + {".texinfo", "application/x-texinfo"}, + {".tgz", "application/x-compressed"}, + {".thmx", "application/vnd.ms-officetheme"}, + {".thn", "application/octet-stream"}, + {".tif", "image/tiff"}, + {".tiff", "image/tiff"}, + {".tlh", "text/plain"}, + {".tli", "text/plain"}, + {".toc", "application/octet-stream"}, + {".tr", "application/x-troff"}, + {".trm", "application/x-msterminal"}, + {".trx", "application/xml"}, + {".ts", "video/vnd.dlna.mpeg-tts"}, + {".tsv", "text/tab-separated-values"}, + {".ttf", "application/octet-stream"}, + {".tts", "video/vnd.dlna.mpeg-tts"}, + {".txt", "text/plain"}, + {".u32", "application/octet-stream"}, + {".uls", "text/iuls"}, + {".user", "text/plain"}, + {".ustar", "application/x-ustar"}, + {".vb", "text/plain"}, + {".vbdproj", "text/plain"}, + {".vbk", "video/mpeg"}, + {".vbproj", "text/plain"}, + {".vbs", "text/vbscript"}, + {".vcf", "text/x-vcard"}, + {".vcproj", "Application/xml"}, + {".vcs", "text/plain"}, + {".vcxproj", "Application/xml"}, + {".vddproj", "text/plain"}, + {".vdp", "text/plain"}, + {".vdproj", "text/plain"}, + {".vdx", "application/vnd.ms-visio.viewer"}, + {".vml", "text/xml"}, + {".vscontent", "application/xml"}, + {".vsct", "text/xml"}, + {".vsd", "application/vnd.visio"}, + {".vsi", "application/ms-vsi"}, + {".vsix", "application/vsix"}, + {".vsixlangpack", "text/xml"}, + {".vsixmanifest", "text/xml"}, + {".vsmdi", "application/xml"}, + {".vspscc", "text/plain"}, + {".vss", "application/vnd.visio"}, + {".vsscc", "text/plain"}, + {".vssettings", "text/xml"}, + {".vssscc", "text/plain"}, + {".vst", "application/vnd.visio"}, + {".vstemplate", "text/xml"}, + {".vsto", "application/x-ms-vsto"}, + {".vsw", "application/vnd.visio"}, + {".vsx", "application/vnd.visio"}, + {".vtx", "application/vnd.visio"}, + {".wav", "audio/wav"}, + {".wave", "audio/wav"}, + {".wax", "audio/x-ms-wax"}, + {".wbk", "application/msword"}, + {".wbmp", "image/vnd.wap.wbmp"}, + {".wcm", "application/vnd.ms-works"}, + {".wdb", "application/vnd.ms-works"}, + {".wdp", "image/vnd.ms-photo"}, + {".webarchive", "application/x-safari-webarchive"}, + {".webtest", "application/xml"}, + {".wiq", "application/xml"}, + {".wiz", "application/msword"}, + {".wks", "application/vnd.ms-works"}, + {".WLMP", "application/wlmoviemaker"}, + {".wlpginstall", "application/x-wlpg-detect"}, + {".wlpginstall3", "application/x-wlpg3-detect"}, + {".wm", "video/x-ms-wm"}, + {".wma", "audio/x-ms-wma"}, + {".wmd", "application/x-ms-wmd"}, + {".wmf", "application/x-msmetafile"}, + {".wml", "text/vnd.wap.wml"}, + {".wmlc", "application/vnd.wap.wmlc"}, + {".wmls", "text/vnd.wap.wmlscript"}, + {".wmlsc", "application/vnd.wap.wmlscriptc"}, + {".wmp", "video/x-ms-wmp"}, + {".wmv", "video/x-ms-wmv"}, + {".wmx", "video/x-ms-wmx"}, + {".wmz", "application/x-ms-wmz"}, + {".wpl", "application/vnd.ms-wpl"}, + {".wps", "application/vnd.ms-works"}, + {".wri", "application/x-mswrite"}, + {".wrl", "x-world/x-vrml"}, + {".wrz", "x-world/x-vrml"}, + {".wsc", "text/scriptlet"}, + {".wsdl", "text/xml"}, + {".wvx", "video/x-ms-wvx"}, + {".x", "application/directx"}, + {".xaf", "x-world/x-vrml"}, + {".xaml", "application/xaml+xml"}, + {".xap", "application/x-silverlight-app"}, + {".xbap", "application/x-ms-xbap"}, + {".xbm", "image/x-xbitmap"}, + {".xdr", "text/plain"}, + {".xht", "application/xhtml+xml"}, + {".xhtml", "application/xhtml+xml"}, + {".xla", "application/vnd.ms-excel"}, + {".xlam", "application/vnd.ms-excel.addin.macroEnabled.12"}, + {".xlc", "application/vnd.ms-excel"}, + {".xld", "application/vnd.ms-excel"}, + {".xlk", "application/vnd.ms-excel"}, + {".xll", "application/vnd.ms-excel"}, + {".xlm", "application/vnd.ms-excel"}, + {".xls", "application/vnd.ms-excel"}, + {".xlsb", "application/vnd.ms-excel.sheet.binary.macroEnabled.12"}, + {".xlsm", "application/vnd.ms-excel.sheet.macroEnabled.12"}, + {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, + {".xlt", "application/vnd.ms-excel"}, + {".xltm", "application/vnd.ms-excel.template.macroEnabled.12"}, + {".xltx", "application/vnd.openxmlformats-officedocument.spreadsheetml.template"}, + {".xlw", "application/vnd.ms-excel"}, + {".xml", "text/xml"}, + {".xmta", "application/xml"}, + {".xof", "x-world/x-vrml"}, + {".XOML", "text/plain"}, + {".xpm", "image/x-xpixmap"}, + {".xps", "application/vnd.ms-xpsdocument"}, + {".xrm-ms", "text/xml"}, + {".xsc", "application/xml"}, + {".xsd", "text/xml"}, + {".xsf", "text/xml"}, + {".xsl", "text/xml"}, + {".xslt", "text/xml"}, + {".xsn", "application/octet-stream"}, + {".xss", "application/xml"}, + {".xtp", "application/octet-stream"}, + {".xwd", "image/x-xwindowdump"}, + {".z", "application/x-compress"}, + {".zip", "application/x-zip-compressed"}, + #endregion + + }; + + public static string GetMimeTypeFromExtension(string extension) + { + if (extension == null) + { + throw new ArgumentNullException("extension"); + } + + if (!extension.StartsWith(".")) + { + extension = "." + extension; + } + + return _mappings.TryGetValue(extension, out var mime) ? mime : "application/octet-stream"; + } + + public static string GetMimeType(this string path) + { + return GetMimeTypeFromExtension(System.IO.Path.GetExtension(path)); + } + } +} \ No newline at end of file diff --git a/templates/dotnet/shared/ID.cs.twig b/templates/dotnet/shared/ID.cs.twig new file mode 100644 index 0000000000..1d59b3fe99 --- /dev/null +++ b/templates/dotnet/shared/ID.cs.twig @@ -0,0 +1,42 @@ +using System; + +namespace {{ spec.title | caseUcfirst }} +{ + public static class ID + { + // Generate an hex ID based on timestamp + // Recreated from https://www.php.net/manual/en/function.uniqid.php + private static string HexTimestamp() + { + var now = DateTime.UtcNow; + var epoch = (now - new DateTime(1970, 1, 1)); + var sec = (long)epoch.TotalSeconds; + var usec = (long)((epoch.TotalMilliseconds * 1000) % 1000); + + // Convert to hexadecimal + var hexTimestamp = sec.ToString("x") + usec.ToString("x").PadLeft(5, '0'); + return hexTimestamp; + } + + // Generate a unique ID with padding to have a longer ID + public static string Unique(int padding = 7) + { + var random = new Random(); + var baseId = HexTimestamp(); + var randomPadding = ""; + + for (int i = 0; i < padding; i++) + { + var randomHexDigit = random.Next(0, 16).ToString("x"); + randomPadding += randomHexDigit; + } + + return baseId + randomPadding; + } + + public static string Custom(string id) + { + return id; + } + } +} diff --git a/templates/dotnet/shared/Models/InputFile.cs.twig b/templates/dotnet/shared/Models/InputFile.cs.twig new file mode 100644 index 0000000000..4464608d08 --- /dev/null +++ b/templates/dotnet/shared/Models/InputFile.cs.twig @@ -0,0 +1,41 @@ +using System.IO; +using {{ spec.title | caseUcfirst }}.Extensions; + +namespace {{ spec.title | caseUcfirst }}.Models +{ + public class InputFile + { + public string Path { get; set; } = string.Empty; + public string Filename { get; set; } = string.Empty; + public string MimeType { get; set; } = string.Empty; + public string SourceType { get; set; } = string.Empty; + public object Data { get; set; } = new object(); + + public static InputFile FromPath(string path) => new InputFile + { + Path = path, + Filename = System.IO.Path.GetFileName(path), + MimeType = path.GetMimeType(), + SourceType = "path" + }; + + public static InputFile FromFileInfo(FileInfo fileInfo) => + InputFile.FromPath(fileInfo.FullName); + + public static InputFile FromStream(Stream stream, string filename, string mimeType) => new InputFile + { + Data = stream, + Filename = filename, + MimeType = mimeType, + SourceType = "stream" + }; + + public static InputFile FromBytes(byte[] bytes, string filename, string mimeType) => new InputFile + { + Data = bytes, + Filename = filename, + MimeType = mimeType, + SourceType = "bytes" + }; + } +} \ No newline at end of file diff --git a/templates/dotnet/shared/Models/Model.cs.twig b/templates/dotnet/shared/Models/Model.cs.twig new file mode 100644 index 0000000000..06f997744f --- /dev/null +++ b/templates/dotnet/shared/Models/Model.cs.twig @@ -0,0 +1,99 @@ +{% macro sub_schema(property) %}{% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | caseUcfirst | overrideIdentifier}}>{% else %}{{property.sub_schema | caseUcfirst | overrideIdentifier}}{% endif %}{% else %}{{property | typeName}}{% endif %}{% if not property.required %}?{% endif %}{% endmacro %} +{% macro property_name(definition, property) %}{{ property.name | caseUcfirst | removeDollarSign | escapeKeyword }}{% endmacro %} +using System; +using System.Linq; +using System.Collections.Generic; +using System.Text.Json; +using System.Text.Json.Serialization; + +namespace {{ spec.title | caseUcfirst }}.Models +{ + public class {{ definition.name | caseUcfirst | overrideIdentifier }} + { + {%~ for property in definition.properties %} + [JsonPropertyName("{{ property.name }}")] + public {{ _self.sub_schema(property) }} {{ _self.property_name(definition, property) | overrideProperty(definition.name) }} { get; private set; } + + {%~ endfor %} + {%~ if definition.additionalProperties %} + public Dictionary Data { get; private set; } + + {%~ endif %} + public {{ definition.name | caseUcfirst | overrideIdentifier }}( + {%~ for property in definition.properties %} + {{ _self.sub_schema(property) }} {{ property.name | caseCamel | escapeKeyword }}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %} + + {%~ endfor %} + {%~ if definition.additionalProperties %} + Dictionary data + {%~ endif %} + ) { + {%~ for property in definition.properties %} + {{ _self.property_name(definition, property) | overrideProperty(definition.name) }} = {{ property.name | caseCamel | escapeKeyword }}; + {%~ endfor %} + {%~ if definition.additionalProperties %} + Data = data; + {%~ endif %} + } + + public static {{ definition.name | caseUcfirst | overrideIdentifier }} From(Dictionary map) => new {{ definition.name | caseUcfirst | overrideIdentifier }}( + {%~ for property in definition.properties %} + {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}:{{' '}} + {%- if property.sub_schema %} + {%- if property.type == 'array' -%} + ((IEnumerable)map["{{ property.name }}"]).Select(it => {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: (Dictionary)it)).ToList() + {%- else -%} + {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: (Dictionary)map["{{ property.name }}"]) + {%- endif %} + {%- else %} + {%- if property.type == 'array' -%} + ((IEnumerable)map["{{ property.name }}"]).Select(x => {% if property.items.type == "string" %}x?.ToString(){% elseif property.items.type == "integer" %}{% if not property.required %}x == null ? (long?)null : {% endif %}Convert.ToInt64(x){% elseif property.items.type == "number" %}{% if not property.required %}x == null ? (double?)null : {% endif %}Convert.ToDouble(x){% elseif property.items.type == "boolean" %}{% if not property.required %}x == null ? (bool?)null : {% endif %}(bool)x{% else %}x{% endif %}).{% if property.items.type == "string" and property.required %}Where(x => x != null).{% endif %}ToList()! + {%- else %} + {%- if property.type == "integer" or property.type == "number" %} + {%- if not property.required -%}map["{{ property.name }}"] == null ? null : {% endif %}Convert.To{% if property.type == "integer" %}Int64{% else %}Double{% endif %}(map["{{ property.name }}"]) + {%- else %} + {%- if property.type == "boolean" -%} + ({{ property | typeName }}{% if not property.required %}?{% endif %})map["{{ property.name }}"] + {%- else -%} + map["{{ property.name }}"]{% if not property.required %}?{% endif %}.ToString() + {%- endif %} + {%~ endif %} + {%~ endif %} + {%~ endif %} + {%- if not loop.last or (loop.last and definition.additionalProperties) %}, + {%~ endif %} + {%~ endfor %} + {%- if definition.additionalProperties %} + data: map + {%- endif ~%} + ); + + public Dictionary ToMap() => new Dictionary() + { + {%~ for property in definition.properties %} + { "{{ property.name }}", {% if property.sub_schema %}{% if property.type == 'array' %}{{ _self.property_name(definition, property) | overrideProperty(definition.name) }}.Select(it => it.ToMap()){% else %}{{ _self.property_name(definition, property) | overrideProperty(definition.name) }}.ToMap(){% endif %}{% else %}{{ _self.property_name(definition, property) | overrideProperty(definition.name) }}{% endif %}{{ ' }' }}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %} + + {%~ endfor %} + {%~ if definition.additionalProperties %} + { "data", Data } + {%~ endif %} + }; + {%~ if definition.additionalProperties %} + + public T ConvertTo(Func, T> fromJson) => + fromJson.Invoke(Data); + {%~ endif %} + {%~ for property in definition.properties %} + {%~ if property.sub_schema %} + {%~ for def in spec.definitions %} + {%~ if def.name == property.sub_schema and def.additionalProperties and property.type == 'array' %} + + public T ConvertTo(Func, T> fromJson) => + (T){{ property.name | caseUcfirst | escapeKeyword }}.Select(it => it.ConvertTo(fromJson)); + + {%~ endif %} + {%~ endfor %} + {%~ endif %} + {%~ endfor %} + } +} \ No newline at end of file diff --git a/templates/dotnet/shared/Models/OrderType.cs.twig b/templates/dotnet/shared/Models/OrderType.cs.twig new file mode 100644 index 0000000000..12852880f6 --- /dev/null +++ b/templates/dotnet/shared/Models/OrderType.cs.twig @@ -0,0 +1,8 @@ +namespace {{ spec.title | caseUcfirst }} +{ + public enum OrderType + { + ASC, + DESC + } +} diff --git a/templates/dotnet/shared/Models/UploadProgress.cs.twig b/templates/dotnet/shared/Models/UploadProgress.cs.twig new file mode 100644 index 0000000000..47c78391ce --- /dev/null +++ b/templates/dotnet/shared/Models/UploadProgress.cs.twig @@ -0,0 +1,26 @@ +namespace {{ spec.title | caseUcfirst }} +{ + public class UploadProgress + { + public string Id { get; private set; } + public double Progress { get; private set; } + public long SizeUploaded { get; private set; } + public long ChunksTotal { get; private set; } + public long ChunksUploaded { get; private set; } + + public UploadProgress( + string id, + double progress, + long sizeUploaded, + long chunksTotal, + long chunksUploaded + ) + { + Id = id; + Progress = progress; + SizeUploaded = sizeUploaded; + ChunksTotal = chunksTotal; + ChunksUploaded = chunksUploaded; + } + } +} \ No newline at end of file diff --git a/templates/dotnet/shared/Permission.cs.twig b/templates/dotnet/shared/Permission.cs.twig new file mode 100644 index 0000000000..5bde420f15 --- /dev/null +++ b/templates/dotnet/shared/Permission.cs.twig @@ -0,0 +1,30 @@ +namespace {{ spec.title | caseUcfirst }} +{ + public static class Permission + { + public static string Read(string role) + { + return $"read(\"{role}\")"; + } + + public static string Write(string role) + { + return $"write(\"{role}\")"; + } + + public static string Create(string role) + { + return $"create(\"{role}\")"; + } + + public static string Update(string role) + { + return $"update(\"{role}\")"; + } + + public static string Delete(string role) + { + return $"delete(\"{role}\")"; + } + } +} diff --git a/templates/dotnet/shared/Query.cs.twig b/templates/dotnet/shared/Query.cs.twig new file mode 100644 index 0000000000..18359f30c2 --- /dev/null +++ b/templates/dotnet/shared/Query.cs.twig @@ -0,0 +1,161 @@ +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Text.Json; +using System.Text.Json.Serialization; + + +namespace {{ spec.title | caseUcfirst }} +{ + public class Query + { + [JsonPropertyName("method")] + public string Method { get; set; } = string.Empty; + + [JsonPropertyName("attribute")] + public string? Attribute { get; set; } + + [JsonPropertyName("values")] + public List? Values { get; set; } + + public Query() + { + } + + public Query(string method, string? attribute, object? values) + { + this.Method = method; + this.Attribute = attribute; + + if (values is IList valuesList) + { + this.Values = new List(); + foreach (var value in valuesList) + { + this.Values.Add(value); // Automatically boxes if value is a value type + } + } + else if (values != null) + { + this.Values = new List { values }; + } + } + + override public string ToString() + { + return JsonSerializer.Serialize(this, Client.SerializerOptions); + } + + public static string Equal(string attribute, object value) + { + return new Query("equal", attribute, value).ToString(); + } + + public static string NotEqual(string attribute, object value) + { + return new Query("notEqual", attribute, value).ToString(); + } + + public static string LessThan(string attribute, object value) + { + return new Query("lessThan", attribute, value).ToString(); + } + + public static string LessThanEqual(string attribute, object value) + { + return new Query("lessThanEqual", attribute, value).ToString(); + } + + public static string GreaterThan(string attribute, object value) + { + return new Query("greaterThan", attribute, value).ToString(); + } + + public static string GreaterThanEqual(string attribute, object value) + { + return new Query("greaterThanEqual", attribute, value).ToString(); + } + + public static string Search(string attribute, string value) + { + return new Query("search", attribute, value).ToString(); + } + + public static string IsNull(string attribute) + { + return new Query("isNull", attribute, null).ToString(); + } + + public static string IsNotNull(string attribute) + { + return new Query("isNotNull", attribute, null).ToString(); + } + + public static string StartsWith(string attribute, string value) + { + return new Query("startsWith", attribute, value).ToString(); + } + + public static string EndsWith(string attribute, string value) + { + return new Query("endsWith", attribute, value).ToString(); + } + + public static string Between(string attribute, string start, string end) + { + return new Query("between", attribute, new List { start, end }).ToString(); + } + + public static string Between(string attribute, int start, int end) + { + return new Query("between", attribute, new List { start, end }).ToString(); + } + + public static string Between(string attribute, double start, double end) + { + return new Query("between", attribute, new List { start, end }).ToString(); + } + + public static string Select(List attributes) + { + return new Query("select", null, attributes).ToString(); + } + + public static string CursorAfter(string documentId) + { + return new Query("cursorAfter", null, documentId).ToString(); + } + + public static string CursorBefore(string documentId) { + return new Query("cursorBefore", null, documentId).ToString(); + } + + public static string OrderAsc(string attribute) { + return new Query("orderAsc", attribute, null).ToString(); + } + + public static string OrderDesc(string attribute) { + return new Query("orderDesc", attribute, null).ToString(); + } + + public static string Limit(int limit) { + return new Query("limit", null, limit).ToString(); + } + + public static string Offset(int offset) { + return new Query("offset", null, offset).ToString(); + } + + public static string Contains(string attribute, object value) { + return new Query("contains", attribute, value).ToString(); + } + + public static string Or(List queries) { + return new Query("or", null, queries.Select(q => JsonSerializer.Deserialize(q, Client.DeserializerOptions)).ToList()).ToString(); + } + + public static string And(List queries) { + return new Query("and", null, queries.Select(q => JsonSerializer.Deserialize(q, Client.DeserializerOptions)).ToList()).ToString(); + } + } +} \ No newline at end of file diff --git a/templates/dotnet/shared/Role.cs.twig b/templates/dotnet/shared/Role.cs.twig new file mode 100644 index 0000000000..b3ecf2610b --- /dev/null +++ b/templates/dotnet/shared/Role.cs.twig @@ -0,0 +1,92 @@ +namespace Appwrite +{ + /// + /// Helper class to generate role strings for Permission. + /// + public static class Role + { + /// + /// Grants access to anyone. + /// + /// This includes authenticated and unauthenticated users. + /// + /// + public static string Any() + { + return "any"; + } + + /// + /// Grants access to a specific user by user ID. + /// + /// You can optionally pass verified or unverified for + /// status to target specific types of users. + /// + /// + public static string User(string id, string status = "") + { + return status == string.Empty + ? $"user:{id}" + : $"user:{id}/{status}"; + } + + /// + /// Grants access to any authenticated or anonymous user. + /// + /// You can optionally pass verified or unverified for + /// status to target specific types of users. + /// + /// + public static string Users(string status = "") + { + return status == string.Empty + ? "users" : + $"users/{status}"; + } + + /// + /// Grants access to any guest user without a session. + /// + /// Authenticated users don't have access to this role. + /// + /// + public static string Guests() + { + return "guests"; + } + + /// + /// Grants access to a team by team ID. + /// + /// You can optionally pass a role for role to target + /// team members with the specified role. + /// + /// + public static string Team(string id, string role = "") + { + return role == string.Empty + ? $"team:{id}" + : $"team:{id}/{role}"; + } + + /// + /// Grants access to a specific member of a team. + /// + /// When the member is removed from the team, they will + /// no longer have access. + /// + /// + public static string Member(string id) + { + return $"member:{id}"; + } + + /// + /// Grants access to a user with the specified label. + /// + public static string Label(string name) + { + return $"label:{name}"; + } + } +} \ No newline at end of file diff --git a/templates/dotnet/shared/Services/Service.cs.twig b/templates/dotnet/shared/Services/Service.cs.twig new file mode 100644 index 0000000000..c093d50c06 --- /dev/null +++ b/templates/dotnet/shared/Services/Service.cs.twig @@ -0,0 +1,12 @@ +namespace {{ spec.title | caseUcfirst }} +{ + public abstract class Service + { + protected readonly Client _client; + + public Service(Client client) + { + this._client = client; + } + } +} diff --git a/templates/dotnet/shared/Services/ServiceTemplate.cs.twig b/templates/dotnet/shared/Services/ServiceTemplate.cs.twig new file mode 100644 index 0000000000..5d07252559 --- /dev/null +++ b/templates/dotnet/shared/Services/ServiceTemplate.cs.twig @@ -0,0 +1,66 @@ +{% import 'dotnet/base/utils.twig' as utils %} +{% set taskType = taskType ?? 'Task' %} +{% set usingStatement = usingStatement ?? 'using System.Threading.Tasks;' %} +{% set commentFilter = commentFilter ?? 'dotnetComment' %} +using System; +using System.Collections.Generic; +using System.Linq; +{{ usingStatement }} +{% if spec.definitions is not empty %} +using {{ spec.title | caseUcfirst }}.Models; +{% endif %} +{% if spec.enums is not empty %} +using {{ spec.title | caseUcfirst }}.Enums; +{% endif %} + +namespace {{ spec.title | caseUcfirst }}.Services +{ + public class {{ service.name | caseUcfirst }} : Service + { + public {{ service.name | caseUcfirst }}(Client client) : base(client) + { + } + + {%~ for method in service.methods %} + {%~ if method.description %} + /// + {{~ method.description | dotnetComment }} + /// + {%~ endif %} + /// + {%~ if method.deprecated %} + {%~ if method.since and method.replaceWith %} + [Obsolete("This API has been deprecated since {{ method.since }}. Please use `{{ method.replaceWith | capitalizeFirst }}` instead.")] + {%~ else %} + [Obsolete("This API has been deprecated.")] + {%~ endif %} + {%~ endif %} + public {{ taskType }}{% if method.type == "webAuth" %}{% else %}<{{ utils.resultType(spec.title, method) }}>{% endif %} {{ method.name | caseUcfirst }}({{ utils.method_parameters(method.parameters, method.consumes) }}) + { + var apiPath = "{{ method.path }}"{% if method.parameters.path | length == 0 %};{% endif %} + + {{~ include('dotnet/base/params.twig') }} + + {%~ if method.responseModel %} + static {{ utils.resultType(spec.title, method) }} Convert(Dictionary it) => + {%~ if method.responseModel == 'any' %} + it; + {%~ else %} + {{ utils.resultType(spec.title, method) }}.From(map: it); + {%~ endif %} + {%~ endif %} + + {%~ if method.type == 'location' %} + {{~ include('dotnet/base/requests/location.twig') }} + {%~ elseif method.type == 'webAuth' %} + {{~ include('dotnet/base/requests/oauth.twig') }} + {%~ elseif 'multipart/form-data' in method.consumes %} + {{~ include('dotnet/base/requests/file.twig') }} + {%~ else %} + {{~ include('dotnet/base/requests/api.twig')}} + {%~ endif %} + } + + {%~ endfor %} + } +} diff --git a/templates/unity/Assets/Runtime/Core/Converters/ObjectToInferredTypesConverter.cs.twig b/templates/unity/Assets/Runtime/Core/Converters/ObjectToInferredTypesConverter.cs.twig index 563f92992a..758e6b659c 100644 --- a/templates/unity/Assets/Runtime/Core/Converters/ObjectToInferredTypesConverter.cs.twig +++ b/templates/unity/Assets/Runtime/Core/Converters/ObjectToInferredTypesConverter.cs.twig @@ -1,44 +1 @@ -using System; -using System.Collections.Generic; -using System.Text.Json; -using System.Text.Json.Serialization; - -namespace {{ spec.title | caseUcfirst }}.Converters -{ - public class ObjectToInferredTypesConverter : JsonConverter - { - public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - switch (reader.TokenType) - { - case JsonTokenType.True: - return true; - case JsonTokenType.False: - return false; - case JsonTokenType.Number: - if (reader.TryGetInt64(out long l)) - { - return l; - } - return reader.GetDouble(); - case JsonTokenType.String: - if (reader.TryGetDateTime(out DateTime datetime)) - { - return datetime; - } - return reader.GetString()!; - case JsonTokenType.StartObject: - return JsonSerializer.Deserialize>(ref reader, options)!; - case JsonTokenType.StartArray: - return JsonSerializer.Deserialize(ref reader, options)!; - default: - return JsonDocument.ParseValue(ref reader).RootElement.Clone(); - } - } - - public override void Write(Utf8JsonWriter writer, object objectToWrite, JsonSerializerOptions options) - { - JsonSerializer.Serialize(writer, objectToWrite, objectToWrite.GetType(), options); - } - } -} +{% include 'dotnet/shared/Converters/ObjectToInferredTypesConverter.cs.twig' %} \ No newline at end of file diff --git a/templates/unity/Assets/Runtime/Core/Converters/ValueClassConverter.cs.twig b/templates/unity/Assets/Runtime/Core/Converters/ValueClassConverter.cs.twig index 1b4fda3681..d14d96aaa6 100644 --- a/templates/unity/Assets/Runtime/Core/Converters/ValueClassConverter.cs.twig +++ b/templates/unity/Assets/Runtime/Core/Converters/ValueClassConverter.cs.twig @@ -1,39 +1 @@ -using System; -using System.Text.Json; -using System.Text.Json.Serialization; -using {{ spec.title | caseUcfirst }}.Enums; - -namespace {{ spec.title | caseUcfirst }}.Converters -{ - public class ValueClassConverter : JsonConverter - { - public override bool CanConvert(Type objectType) - { - return typeof(IEnum).IsAssignableFrom(objectType); - } - - public override object Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) - { - var value = reader.GetString(); - var constructor = typeToConvert.GetConstructor(new[] { typeof(string) }); - var obj = constructor?.Invoke(new object[] { value! }); - - return Convert.ChangeType(obj, typeToConvert)!; - } - - public override void Write(Utf8JsonWriter writer, object value, JsonSerializerOptions options) - { - var type = value.GetType(); - var property = type.GetProperty(nameof(IEnum.Value)); - var propertyValue = property?.GetValue(value); - - if (propertyValue == null) - { - writer.WriteNullValue(); - return; - } - - writer.WriteStringValue(propertyValue.ToString()); - } - } -} +{% include 'dotnet/shared/Converters/ValueClassConverter.cs.twig' %} \ No newline at end of file diff --git a/templates/unity/Assets/Runtime/Core/Enums/Enum.cs.twig b/templates/unity/Assets/Runtime/Core/Enums/Enum.cs.twig index 6720ce59bb..eaaf2d1998 100644 --- a/templates/unity/Assets/Runtime/Core/Enums/Enum.cs.twig +++ b/templates/unity/Assets/Runtime/Core/Enums/Enum.cs.twig @@ -1,19 +1 @@ -using System; - -namespace {{ spec.title | caseUcfirst }}.Enums -{ - public class {{ enum.name | caseUcfirst | overrideIdentifier }} : IEnum - { - public string Value { get; private set; } - - public {{ enum.name | caseUcfirst | overrideIdentifier }}(string value) - { - Value = value; - } - - {%~ for value in enum.enum %} - {%~ set key = enum.keys is empty ? value : enum.keys[loop.index0] %} - public static {{ enum.name | caseUcfirst | overrideIdentifier }} {{ key | caseEnumKey }} => new {{ enum.name | caseUcfirst | overrideIdentifier }}("{{ value }}"); - {%~ endfor %} - } -} +{% include 'dotnet/shared/Enums/Enum.cs.twig' %} \ No newline at end of file diff --git a/templates/unity/Assets/Runtime/Core/Enums/IEnum.cs.twig b/templates/unity/Assets/Runtime/Core/Enums/IEnum.cs.twig index 5d7744d128..7e249d14bd 100644 --- a/templates/unity/Assets/Runtime/Core/Enums/IEnum.cs.twig +++ b/templates/unity/Assets/Runtime/Core/Enums/IEnum.cs.twig @@ -1,9 +1 @@ -using System; - -namespace {{ spec.title | caseUcfirst }}.Enums -{ - public interface IEnum - { - public string Value { get; } - } -} +{% include 'dotnet/shared/Enums/IEnum.cs.twig' %} \ No newline at end of file diff --git a/templates/unity/Assets/Runtime/Core/Exception.cs.twig b/templates/unity/Assets/Runtime/Core/Exception.cs.twig index 47148276ca..e34d16077b 100644 --- a/templates/unity/Assets/Runtime/Core/Exception.cs.twig +++ b/templates/unity/Assets/Runtime/Core/Exception.cs.twig @@ -1,27 +1 @@ -using System; - -namespace {{spec.title | caseUcfirst}} -{ - public class {{spec.title | caseUcfirst}}Exception : Exception - { - public int? Code { get; set; } - public string? Type { get; set; } = null; - public string? Response { get; set; } = null; - - public {{spec.title | caseUcfirst}}Exception( - string? message = null, - int? code = null, - string? type = null, - string? response = null) : base(message) - { - this.Code = code; - this.Type = type; - this.Response = response; - } - - public {{spec.title | caseUcfirst}}Exception(string message, Exception inner) - : base(message, inner) - { - } - } -} +{% include 'dotnet/shared/Exception.cs.twig' %} \ No newline at end of file diff --git a/templates/unity/Assets/Runtime/Core/Extensions/Extensions.cs.twig b/templates/unity/Assets/Runtime/Core/Extensions/Extensions.cs.twig index d57318077e..45a30f4f24 100644 --- a/templates/unity/Assets/Runtime/Core/Extensions/Extensions.cs.twig +++ b/templates/unity/Assets/Runtime/Core/Extensions/Extensions.cs.twig @@ -1,627 +1 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Text.Json; - -namespace {{ spec.title | caseUcfirst }}.Extensions -{ - public static class Extensions - { - public static string ToJson(this Dictionary dict) - { - return JsonSerializer.Serialize(dict, Client.SerializerOptions); - } - - public static string ToQueryString(this Dictionary parameters) - { - var query = new List(); - - foreach (var kvp in parameters) - { - switch (kvp.Value) - { - case null: - continue; - case IList list: - foreach (var item in list) - { - query.Add($"{kvp.Key}[]={item}"); - } - break; - default: - query.Add($"{kvp.Key}={kvp.Value.ToString()}"); - break; - } - } - - return Uri.EscapeUriString(string.Join("&", query)); - } - - private static IDictionary _mappings = new Dictionary(StringComparer.InvariantCultureIgnoreCase) { - - #region Mime Types - {".323", "text/h323"}, - {".3g2", "video/3gpp2"}, - {".3gp", "video/3gpp"}, - {".3gp2", "video/3gpp2"}, - {".3gpp", "video/3gpp"}, - {".7z", "application/x-7z-compressed"}, - {".aa", "audio/audible"}, - {".AAC", "audio/aac"}, - {".aaf", "application/octet-stream"}, - {".aax", "audio/vnd.audible.aax"}, - {".ac3", "audio/ac3"}, - {".aca", "application/octet-stream"}, - {".accda", "application/msaccess.addin"}, - {".accdb", "application/msaccess"}, - {".accdc", "application/msaccess.cab"}, - {".accde", "application/msaccess"}, - {".accdr", "application/msaccess.runtime"}, - {".accdt", "application/msaccess"}, - {".accdw", "application/msaccess.webapplication"}, - {".accft", "application/msaccess.ftemplate"}, - {".acx", "application/internet-property-stream"}, - {".AddIn", "text/xml"}, - {".ade", "application/msaccess"}, - {".adobebridge", "application/x-bridge-url"}, - {".adp", "application/msaccess"}, - {".ADT", "audio/vnd.dlna.adts"}, - {".ADTS", "audio/aac"}, - {".afm", "application/octet-stream"}, - {".ai", "application/postscript"}, - {".aif", "audio/x-aiff"}, - {".aifc", "audio/aiff"}, - {".aiff", "audio/aiff"}, - {".air", "application/vnd.adobe.air-application-installer-package+zip"}, - {".amc", "application/x-mpeg"}, - {".application", "application/x-ms-application"}, - {".art", "image/x-jg"}, - {".asa", "application/xml"}, - {".asax", "application/xml"}, - {".ascx", "application/xml"}, - {".asd", "application/octet-stream"}, - {".asf", "video/x-ms-asf"}, - {".ashx", "application/xml"}, - {".asi", "application/octet-stream"}, - {".asm", "text/plain"}, - {".asmx", "application/xml"}, - {".aspx", "application/xml"}, - {".asr", "video/x-ms-asf"}, - {".asx", "video/x-ms-asf"}, - {".atom", "application/atom+xml"}, - {".au", "audio/basic"}, - {".avi", "video/x-msvideo"}, - {".axs", "application/olescript"}, - {".bas", "text/plain"}, - {".bcpio", "application/x-bcpio"}, - {".bin", "application/octet-stream"}, - {".bmp", "image/bmp"}, - {".c", "text/plain"}, - {".cab", "application/octet-stream"}, - {".caf", "audio/x-caf"}, - {".calx", "application/vnd.ms-office.calx"}, - {".cat", "application/vnd.ms-pki.seccat"}, - {".cc", "text/plain"}, - {".cd", "text/plain"}, - {".cdda", "audio/aiff"}, - {".cdf", "application/x-cdf"}, - {".cer", "application/x-x509-ca-cert"}, - {".chm", "application/octet-stream"}, - {".class", "application/x-java-applet"}, - {".clp", "application/x-msclip"}, - {".cmx", "image/x-cmx"}, - {".cnf", "text/plain"}, - {".cod", "image/cis-cod"}, - {".config", "application/xml"}, - {".contact", "text/x-ms-contact"}, - {".coverage", "application/xml"}, - {".cpio", "application/x-cpio"}, - {".cpp", "text/plain"}, - {".crd", "application/x-mscardfile"}, - {".crl", "application/pkix-crl"}, - {".crt", "application/x-x509-ca-cert"}, - {".cs", "text/plain"}, - {".csdproj", "text/plain"}, - {".csh", "application/x-csh"}, - {".csproj", "text/plain"}, - {".css", "text/css"}, - {".csv", "text/csv"}, - {".cur", "application/octet-stream"}, - {".cxx", "text/plain"}, - {".dat", "application/octet-stream"}, - {".datasource", "application/xml"}, - {".dbproj", "text/plain"}, - {".dcr", "application/x-director"}, - {".def", "text/plain"}, - {".deploy", "application/octet-stream"}, - {".der", "application/x-x509-ca-cert"}, - {".dgml", "application/xml"}, - {".dib", "image/bmp"}, - {".dif", "video/x-dv"}, - {".dir", "application/x-director"}, - {".disco", "text/xml"}, - {".dll", "application/x-msdownload"}, - {".dll.config", "text/xml"}, - {".dlm", "text/dlm"}, - {".doc", "application/msword"}, - {".docm", "application/vnd.ms-word.document.macroEnabled.12"}, - {".docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"}, - {".dot", "application/msword"}, - {".dotm", "application/vnd.ms-word.template.macroEnabled.12"}, - {".dotx", "application/vnd.openxmlformats-officedocument.wordprocessingml.template"}, - {".dsp", "application/octet-stream"}, - {".dsw", "text/plain"}, - {".dtd", "text/xml"}, - {".dtsConfig", "text/xml"}, - {".dv", "video/x-dv"}, - {".dvi", "application/x-dvi"}, - {".dwf", "drawing/x-dwf"}, - {".dwp", "application/octet-stream"}, - {".dxr", "application/x-director"}, - {".eml", "message/rfc822"}, - {".emz", "application/octet-stream"}, - {".eot", "application/octet-stream"}, - {".eps", "application/postscript"}, - {".etl", "application/etl"}, - {".etx", "text/x-setext"}, - {".evy", "application/envoy"}, - {".exe", "application/octet-stream"}, - {".exe.config", "text/xml"}, - {".fdf", "application/vnd.fdf"}, - {".fif", "application/fractals"}, - {".filters", "Application/xml"}, - {".fla", "application/octet-stream"}, - {".flr", "x-world/x-vrml"}, - {".flv", "video/x-flv"}, - {".fsscript", "application/fsharp-script"}, - {".fsx", "application/fsharp-script"}, - {".generictest", "application/xml"}, - {".gif", "image/gif"}, - {".group", "text/x-ms-group"}, - {".gsm", "audio/x-gsm"}, - {".gtar", "application/x-gtar"}, - {".gz", "application/x-gzip"}, - {".h", "text/plain"}, - {".hdf", "application/x-hdf"}, - {".hdml", "text/x-hdml"}, - {".hhc", "application/x-oleobject"}, - {".hhk", "application/octet-stream"}, - {".hhp", "application/octet-stream"}, - {".hlp", "application/winhlp"}, - {".hpp", "text/plain"}, - {".hqx", "application/mac-binhex40"}, - {".hta", "application/hta"}, - {".htc", "text/x-component"}, - {".htm", "text/html"}, - {".html", "text/html"}, - {".htt", "text/webviewhtml"}, - {".hxa", "application/xml"}, - {".hxc", "application/xml"}, - {".hxd", "application/octet-stream"}, - {".hxe", "application/xml"}, - {".hxf", "application/xml"}, - {".hxh", "application/octet-stream"}, - {".hxi", "application/octet-stream"}, - {".hxk", "application/xml"}, - {".hxq", "application/octet-stream"}, - {".hxr", "application/octet-stream"}, - {".hxs", "application/octet-stream"}, - {".hxt", "text/html"}, - {".hxv", "application/xml"}, - {".hxw", "application/octet-stream"}, - {".hxx", "text/plain"}, - {".i", "text/plain"}, - {".ico", "image/x-icon"}, - {".ics", "application/octet-stream"}, - {".idl", "text/plain"}, - {".ief", "image/ief"}, - {".iii", "application/x-iphone"}, - {".inc", "text/plain"}, - {".inf", "application/octet-stream"}, - {".inl", "text/plain"}, - {".ins", "application/x-internet-signup"}, - {".ipa", "application/x-itunes-ipa"}, - {".ipg", "application/x-itunes-ipg"}, - {".ipproj", "text/plain"}, - {".ipsw", "application/x-itunes-ipsw"}, - {".iqy", "text/x-ms-iqy"}, - {".isp", "application/x-internet-signup"}, - {".ite", "application/x-itunes-ite"}, - {".itlp", "application/x-itunes-itlp"}, - {".itms", "application/x-itunes-itms"}, - {".itpc", "application/x-itunes-itpc"}, - {".IVF", "video/x-ivf"}, - {".jar", "application/java-archive"}, - {".java", "application/octet-stream"}, - {".jck", "application/liquidmotion"}, - {".jcz", "application/liquidmotion"}, - {".jfif", "image/pjpeg"}, - {".jnlp", "application/x-java-jnlp-file"}, - {".jpb", "application/octet-stream"}, - {".jpe", "image/jpeg"}, - {".jpeg", "image/jpeg"}, - {".jpg", "image/jpeg"}, - {".js", "application/x-javascript"}, - {".json", "application/json"}, - {".jsx", "text/jscript"}, - {".jsxbin", "text/plain"}, - {".latex", "application/x-latex"}, - {".library-ms", "application/windows-library+xml"}, - {".lit", "application/x-ms-reader"}, - {".loadtest", "application/xml"}, - {".lpk", "application/octet-stream"}, - {".lsf", "video/x-la-asf"}, - {".lst", "text/plain"}, - {".lsx", "video/x-la-asf"}, - {".lzh", "application/octet-stream"}, - {".m13", "application/x-msmediaview"}, - {".m14", "application/x-msmediaview"}, - {".m1v", "video/mpeg"}, - {".m2t", "video/vnd.dlna.mpeg-tts"}, - {".m2ts", "video/vnd.dlna.mpeg-tts"}, - {".m2v", "video/mpeg"}, - {".m3u", "audio/x-mpegurl"}, - {".m3u8", "audio/x-mpegurl"}, - {".m4a", "audio/m4a"}, - {".m4b", "audio/m4b"}, - {".m4p", "audio/m4p"}, - {".m4r", "audio/x-m4r"}, - {".m4v", "video/x-m4v"}, - {".mac", "image/x-macpaint"}, - {".mak", "text/plain"}, - {".man", "application/x-troff-man"}, - {".manifest", "application/x-ms-manifest"}, - {".map", "text/plain"}, - {".master", "application/xml"}, - {".mda", "application/msaccess"}, - {".mdb", "application/x-msaccess"}, - {".mde", "application/msaccess"}, - {".mdp", "application/octet-stream"}, - {".me", "application/x-troff-me"}, - {".mfp", "application/x-shockwave-flash"}, - {".mht", "message/rfc822"}, - {".mhtml", "message/rfc822"}, - {".mid", "audio/mid"}, - {".midi", "audio/mid"}, - {".mix", "application/octet-stream"}, - {".mk", "text/plain"}, - {".mmf", "application/x-smaf"}, - {".mno", "text/xml"}, - {".mny", "application/x-msmoney"}, - {".mod", "video/mpeg"}, - {".mov", "video/quicktime"}, - {".movie", "video/x-sgi-movie"}, - {".mp2", "video/mpeg"}, - {".mp2v", "video/mpeg"}, - {".mp3", "audio/mpeg"}, - {".mp4", "video/mp4"}, - {".mp4v", "video/mp4"}, - {".mpa", "video/mpeg"}, - {".mpe", "video/mpeg"}, - {".mpeg", "video/mpeg"}, - {".mpf", "application/vnd.ms-mediapackage"}, - {".mpg", "video/mpeg"}, - {".mpp", "application/vnd.ms-project"}, - {".mpv2", "video/mpeg"}, - {".mqv", "video/quicktime"}, - {".ms", "application/x-troff-ms"}, - {".msi", "application/octet-stream"}, - {".mso", "application/octet-stream"}, - {".mts", "video/vnd.dlna.mpeg-tts"}, - {".mtx", "application/xml"}, - {".mvb", "application/x-msmediaview"}, - {".mvc", "application/x-miva-compiled"}, - {".mxp", "application/x-mmxp"}, - {".nc", "application/x-netcdf"}, - {".nsc", "video/x-ms-asf"}, - {".nws", "message/rfc822"}, - {".ocx", "application/octet-stream"}, - {".oda", "application/oda"}, - {".odc", "text/x-ms-odc"}, - {".odh", "text/plain"}, - {".odl", "text/plain"}, - {".odp", "application/vnd.oasis.opendocument.presentation"}, - {".ods", "application/oleobject"}, - {".odt", "application/vnd.oasis.opendocument.text"}, - {".one", "application/onenote"}, - {".onea", "application/onenote"}, - {".onepkg", "application/onenote"}, - {".onetmp", "application/onenote"}, - {".onetoc", "application/onenote"}, - {".onetoc2", "application/onenote"}, - {".orderedtest", "application/xml"}, - {".osdx", "application/opensearchdescription+xml"}, - {".p10", "application/pkcs10"}, - {".p12", "application/x-pkcs12"}, - {".p7b", "application/x-pkcs7-certificates"}, - {".p7c", "application/pkcs7-mime"}, - {".p7m", "application/pkcs7-mime"}, - {".p7r", "application/x-pkcs7-certreqresp"}, - {".p7s", "application/pkcs7-signature"}, - {".pbm", "image/x-portable-bitmap"}, - {".pcast", "application/x-podcast"}, - {".pct", "image/pict"}, - {".pcx", "application/octet-stream"}, - {".pcz", "application/octet-stream"}, - {".pdf", "application/pdf"}, - {".pfb", "application/octet-stream"}, - {".pfm", "application/octet-stream"}, - {".pfx", "application/x-pkcs12"}, - {".pgm", "image/x-portable-graymap"}, - {".pic", "image/pict"}, - {".pict", "image/pict"}, - {".pkgdef", "text/plain"}, - {".pkgundef", "text/plain"}, - {".pko", "application/vnd.ms-pki.pko"}, - {".pls", "audio/scpls"}, - {".pma", "application/x-perfmon"}, - {".pmc", "application/x-perfmon"}, - {".pml", "application/x-perfmon"}, - {".pmr", "application/x-perfmon"}, - {".pmw", "application/x-perfmon"}, - {".png", "image/png"}, - {".pnm", "image/x-portable-anymap"}, - {".pnt", "image/x-macpaint"}, - {".pntg", "image/x-macpaint"}, - {".pnz", "image/png"}, - {".pot", "application/vnd.ms-powerpoint"}, - {".potm", "application/vnd.ms-powerpoint.template.macroEnabled.12"}, - {".potx", "application/vnd.openxmlformats-officedocument.presentationml.template"}, - {".ppa", "application/vnd.ms-powerpoint"}, - {".ppam", "application/vnd.ms-powerpoint.addin.macroEnabled.12"}, - {".ppm", "image/x-portable-pixmap"}, - {".pps", "application/vnd.ms-powerpoint"}, - {".ppsm", "application/vnd.ms-powerpoint.slideshow.macroEnabled.12"}, - {".ppsx", "application/vnd.openxmlformats-officedocument.presentationml.slideshow"}, - {".ppt", "application/vnd.ms-powerpoint"}, - {".pptm", "application/vnd.ms-powerpoint.presentation.macroEnabled.12"}, - {".pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation"}, - {".prf", "application/pics-rules"}, - {".prm", "application/octet-stream"}, - {".prx", "application/octet-stream"}, - {".ps", "application/postscript"}, - {".psc1", "application/PowerShell"}, - {".psd", "application/octet-stream"}, - {".psess", "application/xml"}, - {".psm", "application/octet-stream"}, - {".psp", "application/octet-stream"}, - {".pub", "application/x-mspublisher"}, - {".pwz", "application/vnd.ms-powerpoint"}, - {".qht", "text/x-html-insertion"}, - {".qhtm", "text/x-html-insertion"}, - {".qt", "video/quicktime"}, - {".qti", "image/x-quicktime"}, - {".qtif", "image/x-quicktime"}, - {".qtl", "application/x-quicktimeplayer"}, - {".qxd", "application/octet-stream"}, - {".ra", "audio/x-pn-realaudio"}, - {".ram", "audio/x-pn-realaudio"}, - {".rar", "application/octet-stream"}, - {".ras", "image/x-cmu-raster"}, - {".rat", "application/rat-file"}, - {".rc", "text/plain"}, - {".rc2", "text/plain"}, - {".rct", "text/plain"}, - {".rdlc", "application/xml"}, - {".resx", "application/xml"}, - {".rf", "image/vnd.rn-realflash"}, - {".rgb", "image/x-rgb"}, - {".rgs", "text/plain"}, - {".rm", "application/vnd.rn-realmedia"}, - {".rmi", "audio/mid"}, - {".rmp", "application/vnd.rn-rn_music_package"}, - {".roff", "application/x-troff"}, - {".rpm", "audio/x-pn-realaudio-plugin"}, - {".rqy", "text/x-ms-rqy"}, - {".rtf", "application/rtf"}, - {".rtx", "text/richtext"}, - {".ruleset", "application/xml"}, - {".s", "text/plain"}, - {".safariextz", "application/x-safari-safariextz"}, - {".scd", "application/x-msschedule"}, - {".sct", "text/scriptlet"}, - {".sd2", "audio/x-sd2"}, - {".sdp", "application/sdp"}, - {".sea", "application/octet-stream"}, - {".searchConnector-ms", "application/windows-search-connector+xml"}, - {".setpay", "application/set-payment-initiation"}, - {".setreg", "application/set-registration-initiation"}, - {".settings", "application/xml"}, - {".sgimb", "application/x-sgimb"}, - {".sgml", "text/sgml"}, - {".sh", "application/x-sh"}, - {".shar", "application/x-shar"}, - {".shtml", "text/html"}, - {".sit", "application/x-stuffit"}, - {".sitemap", "application/xml"}, - {".skin", "application/xml"}, - {".sldm", "application/vnd.ms-powerpoint.slide.macroEnabled.12"}, - {".sldx", "application/vnd.openxmlformats-officedocument.presentationml.slide"}, - {".slk", "application/vnd.ms-excel"}, - {".sln", "text/plain"}, - {".slupkg-ms", "application/x-ms-license"}, - {".smd", "audio/x-smd"}, - {".smi", "application/octet-stream"}, - {".smx", "audio/x-smd"}, - {".smz", "audio/x-smd"}, - {".snd", "audio/basic"}, - {".snippet", "application/xml"}, - {".snp", "application/octet-stream"}, - {".sol", "text/plain"}, - {".sor", "text/plain"}, - {".spc", "application/x-pkcs7-certificates"}, - {".spl", "application/futuresplash"}, - {".src", "application/x-wais-source"}, - {".srf", "text/plain"}, - {".SSISDeploymentManifest", "text/xml"}, - {".ssm", "application/streamingmedia"}, - {".sst", "application/vnd.ms-pki.certstore"}, - {".stl", "application/vnd.ms-pki.stl"}, - {".sv4cpio", "application/x-sv4cpio"}, - {".sv4crc", "application/x-sv4crc"}, - {".svc", "application/xml"}, - {".swf", "application/x-shockwave-flash"}, - {".t", "application/x-troff"}, - {".tar", "application/x-tar"}, - {".tcl", "application/x-tcl"}, - {".testrunconfig", "application/xml"}, - {".testsettings", "application/xml"}, - {".tex", "application/x-tex"}, - {".texi", "application/x-texinfo"}, - {".texinfo", "application/x-texinfo"}, - {".tgz", "application/x-compressed"}, - {".thmx", "application/vnd.ms-officetheme"}, - {".thn", "application/octet-stream"}, - {".tif", "image/tiff"}, - {".tiff", "image/tiff"}, - {".tlh", "text/plain"}, - {".tli", "text/plain"}, - {".toc", "application/octet-stream"}, - {".tr", "application/x-troff"}, - {".trm", "application/x-msterminal"}, - {".trx", "application/xml"}, - {".ts", "video/vnd.dlna.mpeg-tts"}, - {".tsv", "text/tab-separated-values"}, - {".ttf", "application/octet-stream"}, - {".tts", "video/vnd.dlna.mpeg-tts"}, - {".txt", "text/plain"}, - {".u32", "application/octet-stream"}, - {".uls", "text/iuls"}, - {".user", "text/plain"}, - {".ustar", "application/x-ustar"}, - {".vb", "text/plain"}, - {".vbdproj", "text/plain"}, - {".vbk", "video/mpeg"}, - {".vbproj", "text/plain"}, - {".vbs", "text/vbscript"}, - {".vcf", "text/x-vcard"}, - {".vcproj", "Application/xml"}, - {".vcs", "text/plain"}, - {".vcxproj", "Application/xml"}, - {".vddproj", "text/plain"}, - {".vdp", "text/plain"}, - {".vdproj", "text/plain"}, - {".vdx", "application/vnd.ms-visio.viewer"}, - {".vml", "text/xml"}, - {".vscontent", "application/xml"}, - {".vsct", "text/xml"}, - {".vsd", "application/vnd.visio"}, - {".vsi", "application/ms-vsi"}, - {".vsix", "application/vsix"}, - {".vsixlangpack", "text/xml"}, - {".vsixmanifest", "text/xml"}, - {".vsmdi", "application/xml"}, - {".vspscc", "text/plain"}, - {".vss", "application/vnd.visio"}, - {".vsscc", "text/plain"}, - {".vssettings", "text/xml"}, - {".vssscc", "text/plain"}, - {".vst", "application/vnd.visio"}, - {".vstemplate", "text/xml"}, - {".vsto", "application/x-ms-vsto"}, - {".vsw", "application/vnd.visio"}, - {".vsx", "application/vnd.visio"}, - {".vtx", "application/vnd.visio"}, - {".wav", "audio/wav"}, - {".wave", "audio/wav"}, - {".wax", "audio/x-ms-wax"}, - {".wbk", "application/msword"}, - {".wbmp", "image/vnd.wap.wbmp"}, - {".wcm", "application/vnd.ms-works"}, - {".wdb", "application/vnd.ms-works"}, - {".wdp", "image/vnd.ms-photo"}, - {".webarchive", "application/x-safari-webarchive"}, - {".webtest", "application/xml"}, - {".wiq", "application/xml"}, - {".wiz", "application/msword"}, - {".wks", "application/vnd.ms-works"}, - {".WLMP", "application/wlmoviemaker"}, - {".wlpginstall", "application/x-wlpg-detect"}, - {".wlpginstall3", "application/x-wlpg3-detect"}, - {".wm", "video/x-ms-wm"}, - {".wma", "audio/x-ms-wma"}, - {".wmd", "application/x-ms-wmd"}, - {".wmf", "application/x-msmetafile"}, - {".wml", "text/vnd.wap.wml"}, - {".wmlc", "application/vnd.wap.wmlc"}, - {".wmls", "text/vnd.wap.wmlscript"}, - {".wmlsc", "application/vnd.wap.wmlscriptc"}, - {".wmp", "video/x-ms-wmp"}, - {".wmv", "video/x-ms-wmv"}, - {".wmx", "video/x-ms-wmx"}, - {".wmz", "application/x-ms-wmz"}, - {".wpl", "application/vnd.ms-wpl"}, - {".wps", "application/vnd.ms-works"}, - {".wri", "application/x-mswrite"}, - {".wrl", "x-world/x-vrml"}, - {".wrz", "x-world/x-vrml"}, - {".wsc", "text/scriptlet"}, - {".wsdl", "text/xml"}, - {".wvx", "video/x-ms-wvx"}, - {".x", "application/directx"}, - {".xaf", "x-world/x-vrml"}, - {".xaml", "application/xaml+xml"}, - {".xap", "application/x-silverlight-app"}, - {".xbap", "application/x-ms-xbap"}, - {".xbm", "image/x-xbitmap"}, - {".xdr", "text/plain"}, - {".xht", "application/xhtml+xml"}, - {".xhtml", "application/xhtml+xml"}, - {".xla", "application/vnd.ms-excel"}, - {".xlam", "application/vnd.ms-excel.addin.macroEnabled.12"}, - {".xlc", "application/vnd.ms-excel"}, - {".xld", "application/vnd.ms-excel"}, - {".xlk", "application/vnd.ms-excel"}, - {".xll", "application/vnd.ms-excel"}, - {".xlm", "application/vnd.ms-excel"}, - {".xls", "application/vnd.ms-excel"}, - {".xlsb", "application/vnd.ms-excel.sheet.binary.macroEnabled.12"}, - {".xlsm", "application/vnd.ms-excel.sheet.macroEnabled.12"}, - {".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}, - {".xlt", "application/vnd.ms-excel"}, - {".xltm", "application/vnd.ms-excel.template.macroEnabled.12"}, - {".xltx", "application/vnd.openxmlformats-officedocument.spreadsheetml.template"}, - {".xlw", "application/vnd.ms-excel"}, - {".xml", "text/xml"}, - {".xmta", "application/xml"}, - {".xof", "x-world/x-vrml"}, - {".XOML", "text/plain"}, - {".xpm", "image/x-xpixmap"}, - {".xps", "application/vnd.ms-xpsdocument"}, - {".xrm-ms", "text/xml"}, - {".xsc", "application/xml"}, - {".xsd", "text/xml"}, - {".xsf", "text/xml"}, - {".xsl", "text/xml"}, - {".xslt", "text/xml"}, - {".xsn", "application/octet-stream"}, - {".xss", "application/xml"}, - {".xtp", "application/octet-stream"}, - {".xwd", "image/x-xwindowdump"}, - {".z", "application/x-compress"}, - {".zip", "application/x-zip-compressed"}, - #endregion - - }; - - public static string GetMimeTypeFromExtension(string extension) - { - if (extension == null) - { - throw new ArgumentNullException("extension"); - } - - if (!extension.StartsWith(".")) - { - extension = "." + extension; - } - - return _mappings.TryGetValue(extension, out var mime) ? mime : "application/octet-stream"; - } - - public static string GetMimeType(this string path) - { - return GetMimeTypeFromExtension(System.IO.Path.GetExtension(path)); - } - } -} \ No newline at end of file +{% include 'dotnet/shared/Extensions/Extensions.cs.twig' %} \ No newline at end of file diff --git a/templates/unity/Assets/Runtime/Core/ID.cs.twig b/templates/unity/Assets/Runtime/Core/ID.cs.twig index 1d59b3fe99..02a1d4cbcd 100644 --- a/templates/unity/Assets/Runtime/Core/ID.cs.twig +++ b/templates/unity/Assets/Runtime/Core/ID.cs.twig @@ -1,42 +1 @@ -using System; - -namespace {{ spec.title | caseUcfirst }} -{ - public static class ID - { - // Generate an hex ID based on timestamp - // Recreated from https://www.php.net/manual/en/function.uniqid.php - private static string HexTimestamp() - { - var now = DateTime.UtcNow; - var epoch = (now - new DateTime(1970, 1, 1)); - var sec = (long)epoch.TotalSeconds; - var usec = (long)((epoch.TotalMilliseconds * 1000) % 1000); - - // Convert to hexadecimal - var hexTimestamp = sec.ToString("x") + usec.ToString("x").PadLeft(5, '0'); - return hexTimestamp; - } - - // Generate a unique ID with padding to have a longer ID - public static string Unique(int padding = 7) - { - var random = new Random(); - var baseId = HexTimestamp(); - var randomPadding = ""; - - for (int i = 0; i < padding; i++) - { - var randomHexDigit = random.Next(0, 16).ToString("x"); - randomPadding += randomHexDigit; - } - - return baseId + randomPadding; - } - - public static string Custom(string id) - { - return id; - } - } -} +{% include 'dotnet/shared/ID.cs.twig' %} \ No newline at end of file diff --git a/templates/unity/Assets/Runtime/Core/Models/InputFile.cs.twig b/templates/unity/Assets/Runtime/Core/Models/InputFile.cs.twig index 4464608d08..84355821f6 100644 --- a/templates/unity/Assets/Runtime/Core/Models/InputFile.cs.twig +++ b/templates/unity/Assets/Runtime/Core/Models/InputFile.cs.twig @@ -1,41 +1 @@ -using System.IO; -using {{ spec.title | caseUcfirst }}.Extensions; - -namespace {{ spec.title | caseUcfirst }}.Models -{ - public class InputFile - { - public string Path { get; set; } = string.Empty; - public string Filename { get; set; } = string.Empty; - public string MimeType { get; set; } = string.Empty; - public string SourceType { get; set; } = string.Empty; - public object Data { get; set; } = new object(); - - public static InputFile FromPath(string path) => new InputFile - { - Path = path, - Filename = System.IO.Path.GetFileName(path), - MimeType = path.GetMimeType(), - SourceType = "path" - }; - - public static InputFile FromFileInfo(FileInfo fileInfo) => - InputFile.FromPath(fileInfo.FullName); - - public static InputFile FromStream(Stream stream, string filename, string mimeType) => new InputFile - { - Data = stream, - Filename = filename, - MimeType = mimeType, - SourceType = "stream" - }; - - public static InputFile FromBytes(byte[] bytes, string filename, string mimeType) => new InputFile - { - Data = bytes, - Filename = filename, - MimeType = mimeType, - SourceType = "bytes" - }; - } -} \ No newline at end of file +{% include 'dotnet/shared/Models/InputFile.cs.twig' %} \ No newline at end of file diff --git a/templates/unity/Assets/Runtime/Core/Models/Model.cs.twig b/templates/unity/Assets/Runtime/Core/Models/Model.cs.twig index b542f17f0c..aed099061e 100644 --- a/templates/unity/Assets/Runtime/Core/Models/Model.cs.twig +++ b/templates/unity/Assets/Runtime/Core/Models/Model.cs.twig @@ -1,103 +1 @@ -{% macro sub_schema(property) %}{% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | caseUcfirst | overrideIdentifier}}>{% else %}{{property.sub_schema | caseUcfirst | overrideIdentifier}}{% endif %}{% else %}{{property | typeName}}{% endif %}{% if not property.required %}?{% endif %}{% endmacro %} -{% macro property_name(definition, property) %}{{ property.name | caseUcfirst | removeDollarSign | escapeKeyword }}{% endmacro %} -using System; -using System.Linq; -using System.Collections.Generic; -using System.Text.Json; -using System.Text.Json.Serialization; - -namespace {{ spec.title | caseUcfirst }}.Models -{ - public class {{ definition.name | caseUcfirst | overrideIdentifier }} - { - {%~ for property in definition.properties %} - [JsonPropertyName("{{ property.name }}")] - public {{ _self.sub_schema(property) }} {{ _self.property_name(definition, property) | overrideProperty(definition.name) }} { get; private set; } - - {%~ endfor %} - {%~ if definition.additionalProperties %} - public Dictionary Data { get; private set; } - - {%~ endif %} - public {{ definition.name | caseUcfirst | overrideIdentifier }}( - {%~ for property in definition.properties %} - {{ _self.sub_schema(property) }} {{ property.name | caseCamel | escapeKeyword }}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %} - - {%~ endfor %} - {%~ if definition.additionalProperties %} - Dictionary data - {%~ endif %} - ) { - {%~ for property in definition.properties %} - {{ _self.property_name(definition, property) | overrideProperty(definition.name) }} = {{ property.name | caseCamel | escapeKeyword }}; - {%~ endfor %} - {%~ if definition.additionalProperties %} - Data = data; - {%~ endif %} - } - - public static {{ definition.name | caseUcfirst | overrideIdentifier }} From(Dictionary map) => new {{ definition.name | caseUcfirst | overrideIdentifier }}( - {%~ for property in definition.properties %} - {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}:{{' '}} - {%- if property.sub_schema %} - {%- if property.type == 'array' -%} - map["{{ property.name }}"] is JsonElement jsonArray{{ loop.index }} ? jsonArray{{ loop.index }}.Deserialize>>()!.Select(it => {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: it)).ToList() : ((IEnumerable>)map["{{ property.name }}"]).Select(it => {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: it)).ToList() - {%- else -%} - {{ property.sub_schema | caseUcfirst | overrideIdentifier }}.From(map: map["{{ property.name }}"] is JsonElement jsonObj{{ loop.index }} ? jsonObj{{ loop.index }}.Deserialize>()! : (Dictionary)map["{{ property.name }}"]) - {%- endif %} - {%- else %} - {%- if property.type == 'array' -%} - map["{{ property.name }}"] is JsonElement jsonArrayProp{{ loop.index }} ? jsonArrayProp{{ loop.index }}.Deserialize<{{ property | typeName }}>()! : ({{ property | typeName }})map["{{ property.name }}"] - {%- else %} - {%- if property.type == "integer" or property.type == "number" %} - {%- if not property.required -%}map["{{ property.name }}"] == null ? null :{% endif %}Convert.To{% if property.type == "integer" %}Int64{% else %}Double{% endif %}(map["{{ property.name }}"]) - {%- else %} - {%- if property.type == "boolean" -%} - ({{ property | typeName }}{% if not property.required %}?{% endif %})map["{{ property.name }}"] - {%- else %} - {%- if not property.required -%} - map.TryGetValue("{{ property.name }}", out var {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}) ? {{ property.name | caseCamel | escapeKeyword | removeDollarSign }}?.ToString() : null - {%- else -%} - map["{{ property.name }}"].ToString() - {%- endif %} - {%- endif %} - {%~ endif %} - {%~ endif %} - {%~ endif %} - {%- if not loop.last or (loop.last and definition.additionalProperties) %}, - {%~ endif %} - {%~ endfor %} - {%- if definition.additionalProperties %} - data: map - {%- endif ~%} - ); - - public Dictionary ToMap() => new Dictionary() - { - {%~ for property in definition.properties %} - { "{{ property.name }}", {% if property.sub_schema %}{% if property.type == 'array' %}{{ _self.property_name(definition, property) | overrideProperty(definition.name) }}.Select(it => it.ToMap()){% else %}{{ _self.property_name(definition, property) | overrideProperty(definition.name) }}.ToMap(){% endif %}{% else %}{{ _self.property_name(definition, property) | overrideProperty(definition.name) }}{% endif %}{{ ' }' }}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %} - - {%~ endfor %} - {%~ if definition.additionalProperties %} - { "data", Data } - {%~ endif %} - }; - {%~ if definition.additionalProperties %} - - public T ConvertTo(Func, T> fromJson) => - fromJson.Invoke(Data); - {%~ endif %} - {%~ for property in definition.properties %} - {%~ if property.sub_schema %} - {%~ for def in spec.definitions %} - {%~ if def.name == property.sub_schema and def.additionalProperties and property.type == 'array' %} - - public T ConvertTo(Func, T> fromJson) => - (T){{ property.name | caseUcfirst | escapeKeyword }}.Select(it => it.ConvertTo(fromJson)); - - {%~ endif %} - {%~ endfor %} - {%~ endif %} - {%~ endfor %} - } -} +{% include 'dotnet/shared/Models/Model.cs.twig' %} \ No newline at end of file diff --git a/templates/unity/Assets/Runtime/Core/Models/OrderType.cs.twig b/templates/unity/Assets/Runtime/Core/Models/OrderType.cs.twig index 12852880f6..e70a6497bb 100644 --- a/templates/unity/Assets/Runtime/Core/Models/OrderType.cs.twig +++ b/templates/unity/Assets/Runtime/Core/Models/OrderType.cs.twig @@ -1,8 +1 @@ -namespace {{ spec.title | caseUcfirst }} -{ - public enum OrderType - { - ASC, - DESC - } -} +{% include 'dotnet/shared/Models/OrderType.cs.twig' %} \ No newline at end of file diff --git a/templates/unity/Assets/Runtime/Core/Models/UploadProgress.cs.twig b/templates/unity/Assets/Runtime/Core/Models/UploadProgress.cs.twig index 47c78391ce..ebc7be9d76 100644 --- a/templates/unity/Assets/Runtime/Core/Models/UploadProgress.cs.twig +++ b/templates/unity/Assets/Runtime/Core/Models/UploadProgress.cs.twig @@ -1,26 +1 @@ -namespace {{ spec.title | caseUcfirst }} -{ - public class UploadProgress - { - public string Id { get; private set; } - public double Progress { get; private set; } - public long SizeUploaded { get; private set; } - public long ChunksTotal { get; private set; } - public long ChunksUploaded { get; private set; } - - public UploadProgress( - string id, - double progress, - long sizeUploaded, - long chunksTotal, - long chunksUploaded - ) - { - Id = id; - Progress = progress; - SizeUploaded = sizeUploaded; - ChunksTotal = chunksTotal; - ChunksUploaded = chunksUploaded; - } - } -} \ No newline at end of file +{% include 'dotnet/shared/Models/UploadProgress.cs.twig' %} \ No newline at end of file diff --git a/templates/unity/Assets/Runtime/Core/Permission.cs.twig b/templates/unity/Assets/Runtime/Core/Permission.cs.twig index 5bde420f15..8a1c97604d 100644 --- a/templates/unity/Assets/Runtime/Core/Permission.cs.twig +++ b/templates/unity/Assets/Runtime/Core/Permission.cs.twig @@ -1,30 +1 @@ -namespace {{ spec.title | caseUcfirst }} -{ - public static class Permission - { - public static string Read(string role) - { - return $"read(\"{role}\")"; - } - - public static string Write(string role) - { - return $"write(\"{role}\")"; - } - - public static string Create(string role) - { - return $"create(\"{role}\")"; - } - - public static string Update(string role) - { - return $"update(\"{role}\")"; - } - - public static string Delete(string role) - { - return $"delete(\"{role}\")"; - } - } -} +{% include 'dotnet/shared/Permission.cs.twig' %} \ No newline at end of file diff --git a/templates/unity/Assets/Runtime/Core/Query.cs.twig b/templates/unity/Assets/Runtime/Core/Query.cs.twig index 18359f30c2..9ac20c20f9 100644 --- a/templates/unity/Assets/Runtime/Core/Query.cs.twig +++ b/templates/unity/Assets/Runtime/Core/Query.cs.twig @@ -1,161 +1 @@ -using System.Collections; -using System.Collections.Generic; -using System.Linq; -using System.Text.Json; -using System.Text.Json.Serialization; - - -namespace {{ spec.title | caseUcfirst }} -{ - public class Query - { - [JsonPropertyName("method")] - public string Method { get; set; } = string.Empty; - - [JsonPropertyName("attribute")] - public string? Attribute { get; set; } - - [JsonPropertyName("values")] - public List? Values { get; set; } - - public Query() - { - } - - public Query(string method, string? attribute, object? values) - { - this.Method = method; - this.Attribute = attribute; - - if (values is IList valuesList) - { - this.Values = new List(); - foreach (var value in valuesList) - { - this.Values.Add(value); // Automatically boxes if value is a value type - } - } - else if (values != null) - { - this.Values = new List { values }; - } - } - - override public string ToString() - { - return JsonSerializer.Serialize(this, Client.SerializerOptions); - } - - public static string Equal(string attribute, object value) - { - return new Query("equal", attribute, value).ToString(); - } - - public static string NotEqual(string attribute, object value) - { - return new Query("notEqual", attribute, value).ToString(); - } - - public static string LessThan(string attribute, object value) - { - return new Query("lessThan", attribute, value).ToString(); - } - - public static string LessThanEqual(string attribute, object value) - { - return new Query("lessThanEqual", attribute, value).ToString(); - } - - public static string GreaterThan(string attribute, object value) - { - return new Query("greaterThan", attribute, value).ToString(); - } - - public static string GreaterThanEqual(string attribute, object value) - { - return new Query("greaterThanEqual", attribute, value).ToString(); - } - - public static string Search(string attribute, string value) - { - return new Query("search", attribute, value).ToString(); - } - - public static string IsNull(string attribute) - { - return new Query("isNull", attribute, null).ToString(); - } - - public static string IsNotNull(string attribute) - { - return new Query("isNotNull", attribute, null).ToString(); - } - - public static string StartsWith(string attribute, string value) - { - return new Query("startsWith", attribute, value).ToString(); - } - - public static string EndsWith(string attribute, string value) - { - return new Query("endsWith", attribute, value).ToString(); - } - - public static string Between(string attribute, string start, string end) - { - return new Query("between", attribute, new List { start, end }).ToString(); - } - - public static string Between(string attribute, int start, int end) - { - return new Query("between", attribute, new List { start, end }).ToString(); - } - - public static string Between(string attribute, double start, double end) - { - return new Query("between", attribute, new List { start, end }).ToString(); - } - - public static string Select(List attributes) - { - return new Query("select", null, attributes).ToString(); - } - - public static string CursorAfter(string documentId) - { - return new Query("cursorAfter", null, documentId).ToString(); - } - - public static string CursorBefore(string documentId) { - return new Query("cursorBefore", null, documentId).ToString(); - } - - public static string OrderAsc(string attribute) { - return new Query("orderAsc", attribute, null).ToString(); - } - - public static string OrderDesc(string attribute) { - return new Query("orderDesc", attribute, null).ToString(); - } - - public static string Limit(int limit) { - return new Query("limit", null, limit).ToString(); - } - - public static string Offset(int offset) { - return new Query("offset", null, offset).ToString(); - } - - public static string Contains(string attribute, object value) { - return new Query("contains", attribute, value).ToString(); - } - - public static string Or(List queries) { - return new Query("or", null, queries.Select(q => JsonSerializer.Deserialize(q, Client.DeserializerOptions)).ToList()).ToString(); - } - - public static string And(List queries) { - return new Query("and", null, queries.Select(q => JsonSerializer.Deserialize(q, Client.DeserializerOptions)).ToList()).ToString(); - } - } -} \ No newline at end of file +{% include 'dotnet/shared/Query.cs.twig' %} \ No newline at end of file diff --git a/templates/unity/Assets/Runtime/Core/Role.cs.twig b/templates/unity/Assets/Runtime/Core/Role.cs.twig index 4dc45dcb74..abc2773ab2 100644 --- a/templates/unity/Assets/Runtime/Core/Role.cs.twig +++ b/templates/unity/Assets/Runtime/Core/Role.cs.twig @@ -1,92 +1 @@ -namespace {{ spec.title | caseUcfirst }} -{ - /// - /// Helper class to generate role strings for Permission. - /// - public static class Role - { - /// - /// Grants access to anyone. - /// - /// This includes authenticated and unauthenticated users. - /// - /// - public static string Any() - { - return "any"; - } - - /// - /// Grants access to a specific user by user ID. - /// - /// You can optionally pass verified or unverified for - /// status to target specific types of users. - /// - /// - public static string User(string id, string status = "") - { - return status == string.Empty - ? $"user:{id}" - : $"user:{id}/{status}"; - } - - /// - /// Grants access to any authenticated or anonymous user. - /// - /// You can optionally pass verified or unverified for - /// status to target specific types of users. - /// - /// - public static string Users(string status = "") - { - return status == string.Empty - ? "users" : - $"users/{status}"; - } - - /// - /// Grants access to any guest user without a session. - /// - /// Authenticated users don't have access to this role. - /// - /// - public static string Guests() - { - return "guests"; - } - - /// - /// Grants access to a team by team ID. - /// - /// You can optionally pass a role for role to target - /// team members with the specified role. - /// - /// - public static string Team(string id, string role = "") - { - return role == string.Empty - ? $"team:{id}" - : $"team:{id}/{role}"; - } - - /// - /// Grants access to a specific member of a team. - /// - /// When the member is removed from the team, they will - /// no longer have access. - /// - /// - public static string Member(string id) - { - return $"member:{id}"; - } - - /// - /// Grants access to a user with the specified label. - /// - public static string Label(string name) - { - return $"label:{name}"; - } - } -} \ No newline at end of file +{% include 'dotnet/shared/Role.cs.twig' %} \ No newline at end of file diff --git a/templates/unity/Assets/Runtime/Core/Services/Service.cs.twig b/templates/unity/Assets/Runtime/Core/Services/Service.cs.twig index 4df0635dc2..7ca1cbd13b 100644 --- a/templates/unity/Assets/Runtime/Core/Services/Service.cs.twig +++ b/templates/unity/Assets/Runtime/Core/Services/Service.cs.twig @@ -1,12 +1 @@ -namespace {{ spec.title | caseUcfirst }} -{ - public abstract class Service - { - protected readonly Client _client; - - public Service(Client client) - { - _client = client; - } - } -} +{% include 'dotnet/shared/Services/Service.cs.twig' %} \ No newline at end of file diff --git a/templates/unity/Assets/Runtime/Core/Services/ServiceTemplate.cs.twig b/templates/unity/Assets/Runtime/Core/Services/ServiceTemplate.cs.twig index 6a12ba1d1a..6babfb8089 100644 --- a/templates/unity/Assets/Runtime/Core/Services/ServiceTemplate.cs.twig +++ b/templates/unity/Assets/Runtime/Core/Services/ServiceTemplate.cs.twig @@ -1,66 +1,5 @@ -{% import 'unity/base/utils.twig' as utils %} #if UNI_TASK -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -using Cysharp.Threading.Tasks; -{% if spec.definitions is not empty %} -using {{ spec.title | caseUcfirst }}.Models; -{% endif %} -{% if spec.enums is not empty %} -using {{ spec.title | caseUcfirst }}.Enums; -{% endif %} - -namespace {{ spec.title | caseUcfirst }}.Services -{ - public class {{ service.name | caseUcfirst }} : Service - { - public {{ service.name | caseUcfirst }}(Client client) : base(client) - { - } - - {%~ for method in service.methods %} - {%~ if method.description %} - /// - {{~ method.description | unityComment }} - /// - {%~ endif %} - /// - {%~ if method.deprecated %} - {%~ if method.since and method.replaceWith %} - [Obsolete("This API has been deprecated since {{ method.since }}. Please use `{{ method.replaceWith | capitalizeFirst }}` instead.")] - {%~ else %} - [Obsolete("This API has been deprecated.")] - {%~ endif %} - {%~ endif %} - public UniTask{% if method.type == "webAuth" %}{% else %}<{{ utils.resultType(spec.title, method) }}>{% endif %} {{ method.name | caseUcfirst }}({{ utils.method_parameters(method.parameters, method.consumes) }}) - { - var apiPath = "{{ method.path }}"{% if method.parameters.path | length == 0 %};{% endif %} - - {{~ include('unity/base/params.twig') }} - - {%~ if method.responseModel %} - static {{ utils.resultType(spec.title, method) }} Convert(Dictionary it) => - {%~ if method.responseModel == 'any' %} - it; - {%~ else %} - {{ utils.resultType(spec.title, method) }}.From(map: it); - {%~ endif %} - {%~ endif %} - - {%~ if method.type == 'location' %} - {{~ include('unity/base/requests/location.twig') }} - {%~ elseif method.type == 'webAuth' %} - {{~ include('unity/base/requests/oauth.twig') }} - {%~ elseif 'multipart/form-data' in method.consumes %} - {{~ include('unity/base/requests/file.twig') }} - {%~ else %} - {{~ include('unity/base/requests/api.twig')}} - {%~ endif %} - } - - {%~ endfor %} - } -} +{% set taskType = 'UniTask' %} +{% set usingStatement = 'using Cysharp.Threading.Tasks;' %} +{% include 'dotnet/shared/Services/ServiceTemplate.cs.twig' %} #endif \ No newline at end of file diff --git a/templates/unity/base/params.twig b/templates/unity/base/params.twig deleted file mode 100644 index 40ad39df01..0000000000 --- a/templates/unity/base/params.twig +++ /dev/null @@ -1,21 +0,0 @@ -{% import 'unity/base/utils.twig' as utils %} - {%~ for parameter in method.parameters.path %} - .Replace("{{ '{' ~ parameter.name | caseCamel ~ '}' }}", {{ parameter.name | caseCamel | escapeKeyword }}{% if parameter.enumValues is not empty %}.Value{% endif %}){% if loop.last %};{% endif %} - - {%~ endfor %} - - var apiParameters = new Dictionary() - { - {%~ for parameter in method.parameters.query | merge(method.parameters.body) %} - { "{{ parameter.name }}", {{ utils.map_parameter(parameter) }} }{% if not loop.last %},{% endif %} - - {%~ endfor %} - }; - - var apiHeaders = new Dictionary() - { - {%~ for key, header in method.headers %} - { "{{ key }}", "{{ header }}" }{% if not loop.last %},{% endif %} - - {%~ endfor %} - }; diff --git a/templates/unity/base/requests/api.twig b/templates/unity/base/requests/api.twig deleted file mode 100644 index c6058476cd..0000000000 --- a/templates/unity/base/requests/api.twig +++ /dev/null @@ -1,11 +0,0 @@ -{% import 'unity/base/utils.twig' as utils %} - return _client.Call<{{ utils.resultType(spec.title, method) }}>( - method: "{{ method.method | caseUpper }}", - path: apiPath, - headers: apiHeaders, - {%~ if not method.responseModel %} - parameters: apiParameters.Where(it => it.Value != null).ToDictionary(it => it.Key, it => it.Value)!); - {%~ else %} - parameters: apiParameters.Where(it => it.Value != null).ToDictionary(it => it.Key, it => it.Value)!, - convert: Convert); - {%~ endif %} \ No newline at end of file diff --git a/templates/unity/base/requests/file.twig b/templates/unity/base/requests/file.twig deleted file mode 100644 index 0403d342c8..0000000000 --- a/templates/unity/base/requests/file.twig +++ /dev/null @@ -1,18 +0,0 @@ -string? idParamName = {% if method.parameters.all | filter(p => p.isUploadID) | length > 0 %}{% for parameter in method.parameters.all | filter(parameter => parameter.isUploadID) %}"{{ parameter.name }}"{% endfor %}{% else %}null{% endif %}; - -{%~ for parameter in method.parameters.all %} - {%~ if parameter.type == 'file' %} - var paramName = "{{ parameter.name }}"; - {%~ endif %} -{%~ endfor %} - -return _client.ChunkedUpload( -apiPath, -apiHeaders, -apiParameters, -{%~ if method.responseModel %} - Convert, -{%~ endif %} -paramName, -idParamName, -onProgress); \ No newline at end of file diff --git a/templates/unity/base/requests/location.twig b/templates/unity/base/requests/location.twig deleted file mode 100644 index d9f25ea1c2..0000000000 --- a/templates/unity/base/requests/location.twig +++ /dev/null @@ -1,5 +0,0 @@ - return _client.Call( - method: "{{ method.method | caseUpper }}", - path: apiPath, - headers: apiHeaders, - parameters: apiParameters.Where(it => it.Value != null).ToDictionary(it => it.Key, it => it.Value)!); diff --git a/templates/unity/base/requests/oauth.twig b/templates/unity/base/requests/oauth.twig deleted file mode 100644 index a257ef6a22..0000000000 --- a/templates/unity/base/requests/oauth.twig +++ /dev/null @@ -1,5 +0,0 @@ - return _client.Redirect( - method: "{{ method.method | caseUpper }}", - path: apiPath, - headers: apiHeaders, - parameters: apiParameters.Where(it => it.Value != null).ToDictionary(it => it.Key, it => it.Value)!); diff --git a/templates/unity/base/utils.twig b/templates/unity/base/utils.twig deleted file mode 100644 index 19ea870059..0000000000 --- a/templates/unity/base/utils.twig +++ /dev/null @@ -1,16 +0,0 @@ -{% macro parameter(parameter) %} -{% if parameter.name == 'orderType' %}{{ 'OrderType orderType = OrderType.ASC' }}{% else %} -{{ parameter | typeName }}{% if not parameter.required %}?{% endif %} {{ parameter.name | caseCamel | escapeKeyword }}{% if not parameter.required %} = null{% endif %}{% endif %} -{% endmacro %} -{% macro method_parameters(parameters, consumes) %} -{% if parameters.all|length > 0 %}{% for parameter in parameters.all | filter((param) => not param.isGlobal) %}{{ _self.parameter(parameter) }}{% if not loop.last %}{{ ', ' }}{% endif %}{% endfor %}{% if 'multipart/form-data' in consumes %},{% endif %}{% endif %}{% if 'multipart/form-data' in consumes %} Action? onProgress = null{% endif %} -{% endmacro %} -{% macro map_parameter(parameter) %} -{% if parameter.name == 'orderType' %}{{ parameter.name | caseCamel ~ '.ToString()'}}{% elseif parameter.isGlobal %}{{ parameter.name | caseUcfirst | escapeKeyword }}{% elseif parameter.enumValues is not empty %}{{ parameter.name | caseCamel | escapeKeyword }}?.Value{% else %}{{ parameter.name | caseCamel | escapeKeyword }}{% endif %} -{% endmacro %} -{% macro methodNeedsSecurityParameters(method) %} -{% if (method.type == "webAuth" or method.type == "location") and method.auth|length > 0 %}{{ true }}{% else %}{{false}}{% endif %} -{% endmacro %} -{% macro resultType(namespace, method) %} -{% if method.type == "webAuth" %}bool{% elseif method.type == "location" %}byte[]{% elseif not method.responseModel or method.responseModel == 'any' %}object{% else %}Models.{{method.responseModel | caseUcfirst | overrideIdentifier }}{% endif %} -{% endmacro %} \ No newline at end of file