-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f99bbd6
commit 8247470
Showing
13 changed files
with
625 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,5 +23,6 @@ public enum StronglyType | |
Decimal, | ||
Double, | ||
Float, | ||
Short, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
src/Strongly/Templates/Short/Short_NewtonsoftJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
src/Strongly/Templates/Short/Short_SystemTextJsonConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.