public string GetDefaultExpression(TypeReference reference, bool required) { string typeElement = ResolveReference(reference, null); string nullableElement = ""; if (!required && !IsNullable(reference)) { nullableElement = "?"; } return($"default({typeElement}{nullableElement})"); }
public bool IsNullable(TypeReference type) { if ( type is TypeReference.ArrayTypeReference || type is TypeReference.DictionaryTypeReference || type is TypeReference.TypeModelReference || type == TypeReference.Any || type == TypeReference.File || type == TypeReference.String) { return(true); } return(false); }
public static bool IsNullableType(TypeReference reference) { if (reference == TypeReference.Boolean) { return(false); } if (reference == TypeReference.Int32) { return(false); } if (reference == TypeReference.Int64) { return(false); } if (reference == TypeReference.Float) { return(false); } if (reference == TypeReference.Double) { return(false); } if (reference == TypeReference.Date) { return(false); } if (reference == TypeReference.DateTime) { return(false); } if (reference == TypeReference.Uuid) { return(false); } return(true); }
protected override string ResolveReference(TypeReference reference, object[] args) { if (reference is TypeReference.ConstantTypeReference) { return("string"); } if (reference is TypeReference.TypeModelReference typeModelRef) { return(Helpers.PascalCase(typeModelRef.Model.Name.AsSpan())); } if (reference is TypeReference.ArrayTypeReference arrayTypeRef) { return($"IImmutableList<{ResolveReference(arrayTypeRef.BaseType, args)}>"); } if (reference is TypeReference.DictionaryTypeReference dictTypeRef) { return($"IImmutableDictionary<string, {ResolveReference(dictTypeRef.ValueType, args)}>"); } if (reference == TypeReference.Boolean) { return("bool"); } if (reference == TypeReference.Int32) { return("int"); } if (reference == TypeReference.Int64) { return("long"); } if (reference == TypeReference.Float) { return("float"); } if (reference == TypeReference.Double) { return("double"); } if (reference == TypeReference.String) { return("string"); } if (reference == TypeReference.Date) { return("DateTimeOffset"); } if (reference == TypeReference.DateTime) { return("DateTimeOffset"); } if (reference == TypeReference.Uuid) { return("Guid"); } if (reference == TypeReference.Void) { return("void"); } if (reference == TypeReference.Any) { return("Newtonsoft.Json.Linq.JToken"); } if (reference == TypeReference.File) { return("System.IO.Stream"); } if (reference == TypeReference.Byte) { // TODO: implement this } throw new NotSupportedException(reference.ToString()); }
public static void NotNullCheck(TextWriter output, object context, Action <TextWriter, object> template, TypeReference reference) { if (reference == TypeReference.String) { output.Write("!string.IsNullOrEmpty("); template(output, context); output.Write(")"); } else { template(output, context); output.Write(" != default"); } }
public void NotNullCheck(TextWriter output, object context, Action <TextWriter, object> template, TypeReference reference, bool required) { if (reference == TypeReference.String) { output.Write("!string.IsNullOrEmpty("); template(output, context); output.Write(")"); } else { template(output, context); output.WriteSafeString($" != {GetDefaultExpression(reference, required)}"); } }
public void NullCheck(TextWriter output, object context, Action <TextWriter, object> template, TypeReference reference) { if (reference == TypeReference.String) { output.Write("string.IsNullOrEmpty("); template(output, context); output.Write(")"); } else { template(output, context); output.WriteSafeString($" == default({ResolveReference(reference, null)})"); } }