Skip to content

Commit

Permalink
add short base type
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasteles committed Oct 20, 2023
1 parent f99bbd6 commit 8247470
Show file tree
Hide file tree
Showing 13 changed files with 625 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Strongly.Attributes/StronglyType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ public enum StronglyType
Decimal,
Double,
Float,
Short,
}
}
22 changes: 22 additions & 0 deletions src/Strongly/EmbeddedSources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,28 @@ static class EmbeddedSources
},
};

internal static readonly ResourceCollection ShortResources = new(
"short",
AutoGeneratedHeader,
LoadEmbeddedResource("Short.Short_Base"),
LoadEmbeddedResource("Short.Short_NewtonsoftJsonConverter"),
LoadEmbeddedResource("Short.Short_SystemTextJsonConverter"),
LoadEmbeddedResource("Short.Short_TypeConverter"),
DefaultEfValueConverter,
LoadEmbeddedResource("Short.Short_DapperTypeHandler"),
DefaultIComparable,
LoadEmbeddedResource("Short.Short_SwaggerSchemaFilter"),
DefaultParsableNumber,
DefaultIFormattable
)
{
IsNumeric = true,
TemplateVars =
{
[NumberStyleKey] = nameof(NumberStyles.Integer),
},
};

internal static readonly ResourceCollection BigIntegerResources = new(
"System.Numerics.BigInteger",
AutoGeneratedHeader,
Expand Down
1 change: 1 addition & 0 deletions src/Strongly/SourceGenerationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public static string CreateStrongValue(StronglyContext ctx, StringBuilder? sb)
},
StronglyType.Int => EmbeddedSources.IntResources,
StronglyType.Long => EmbeddedSources.LongResources,
StronglyType.Short => EmbeddedSources.ShortResources,
StronglyType.Decimal => EmbeddedSources.DecimalResources,
StronglyType.Double => EmbeddedSources.DoubleResources,
StronglyType.Float => EmbeddedSources.FloatResources,
Expand Down
3 changes: 3 additions & 0 deletions src/Strongly/Templates/Short/Short_Base.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
 public bool Equals(TYPENAME other) => this.Value.Equals(other.Value);

public static readonly TYPENAME Empty = new TYPENAME(0);
20 changes: 20 additions & 0 deletions src/Strongly/Templates/Short/Short_DapperTypeHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler<TYPENAME>
{
public override void SetValue(System.Data.IDbDataParameter parameter, TYPENAME value)
{
parameter.Value = value.Value;
}

public override TYPENAME Parse(object value)
{
return value switch
{
short shortValue => new TYPENAME(shortValue),
int intValue when intValue < short.MaxValue => new TYPENAME((short)intValue),
long longValue when longValue < short.MaxValue => new TYPENAME((short)longValue),
string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new TYPENAME(result),
_ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TYPENAME"),
};
}
}
20 changes: 20 additions & 0 deletions src/Strongly/Templates/Short/Short_NewtonsoftJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

class TYPENAMENewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter
{
public override bool CanConvert(System.Type objectType)
{
return objectType == typeof(TYPENAME);
}

public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object? value, Newtonsoft.Json.JsonSerializer serializer)
{
var id = value as TYPENAME?;
serializer.Serialize(writer, id?.Value);
}

public override object? ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object? existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
var result = serializer.Deserialize<short?>(reader);
return result.HasValue ? new TYPENAME(result.Value) : null;
}
}
15 changes: 15 additions & 0 deletions src/Strongly/Templates/Short/Short_SwaggerSchemaFilter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

class TYPENAMESchemaFilter : Swashbuckle.AspNetCore.SwaggerGen.ISchemaFilter
{
public void Apply(Microsoft.OpenApi.Models.OpenApiSchema schema, Swashbuckle.AspNetCore.SwaggerGen.SchemaFilterContext context)
{
var idSchema = new Microsoft.OpenApi.Models.OpenApiSchema { Type = "integer", Format = "" };
schema.Type = idSchema.Type;
schema.Minimum = short.MinValue;
schema.Maximum = short.MaxValue;
schema.Format = idSchema.Format;
schema.Example = idSchema.Example;
schema.Default = idSchema.Default;
schema.Properties = idSchema.Properties;
}
}
13 changes: 13 additions & 0 deletions src/Strongly/Templates/Short/Short_SystemTextJsonConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

class TYPENAMESystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter<TYPENAME>
{
public override TYPENAME Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
{
return new TYPENAME(reader.GetInt16());
}

public override void Write(System.Text.Json.Utf8JsonWriter writer, TYPENAME value, System.Text.Json.JsonSerializerOptions options)
{
writer.WriteNumberValue(value.Value);
}
}
41 changes: 41 additions & 0 deletions src/Strongly/Templates/Short/Short_TypeConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

class TYPENAMETypeConverter : System.ComponentModel.TypeConverter
{
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Type sourceType)
{
return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}

public override object? ConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object value)
{
return value switch
{
short shortValue => new TYPENAME(shortValue),
string stringValue when !string.IsNullOrEmpty(stringValue) && short.TryParse(stringValue, out var result) => new TYPENAME(result),
_ => base.ConvertFrom(context, culture, value),
};
}

public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Type? sourceType)
{
return sourceType == typeof(short) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType);
}

public override object? ConvertTo(System.ComponentModel.ITypeDescriptorContext? context, System.Globalization.CultureInfo? culture, object? value, System.Type destinationType)
{
if (value is TYPENAME idValue)
{
if (destinationType == typeof(short))
{
return idValue.Value;
}

if (destinationType == typeof(string))
{
return idValue.Value.ToString();
}
}

return base.ConvertTo(context, culture, value, destinationType);
}
}
1 change: 1 addition & 0 deletions test/Strongly.Tests/DapperTypeHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ public static void AddHandlers()
SqlMapper.AddTypeHandler(new DapperGuidCombId.DapperTypeHandler());
SqlMapper.AddTypeHandler(new DapperDoubleId.DapperTypeHandler());
SqlMapper.AddTypeHandler(new DapperFloatId.DapperTypeHandler());
SqlMapper.AddTypeHandler(new DapperShortId.DapperTypeHandler());
}
}
Loading

0 comments on commit 8247470

Please sign in to comment.