Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add implicit primitive type conversions #277

Merged
merged 2 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ public HeightRangeDouble(UInt128 value) : this(new BinaryJsonNumber(value))
{
}

/// <summary>
/// Conversion from JsonNumber.
/// </summary>
/// <param name = "value">The value from which to convert.</param>
public static implicit operator HeightRangeDouble(JsonNumber value)
{
if (value.HasDotnetBacking && value.ValueKind == JsonValueKind.Number)
{
return new(value.AsBinaryJsonNumber);
}

return new(value.AsJsonElement);
}

/// <summary>
/// Conversion to byte.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions Solutions/Corvus.Json.Benchmarking/PersonModel/OtherNames.Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ public static implicit operator OtherNames(ImmutableList<JsonAny> value)
return new(value);
}

/// <summary>
/// Conversion from JsonArray.
/// </summary>
/// <param name = "value">The value from which to convert.</param>
public static implicit operator OtherNames(JsonArray value)
{
if (value.HasDotnetBacking && value.ValueKind == JsonValueKind.Array)
{
return new(value.AsImmutableList());
}

return new(value.AsJsonElement);
}

/// <summary>
/// Initializes a new instance of the <see cref = "OtherNames"/> struct.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,6 @@ namespace Corvus.Json.Benchmarking.Models;
/// </summary>
public readonly partial struct OtherNames
{
/// <summary>
/// Conversion from <see cref = "Corvus.Json.Benchmarking.Models.PersonNameElement"/>.
/// </summary>
/// <param name = "value">The value from which to convert.</param>
public static implicit operator OtherNames(Corvus.Json.Benchmarking.Models.PersonNameElement value)
{
if (value.HasJsonElementBacking)
{
return new(value.AsJsonElement);
}

return value.ValueKind switch
{
JsonValueKind.String => new((string)value),
_ => Undefined
};
}

/// <summary>
/// Conversion to <see cref = "Corvus.Json.Benchmarking.Models.PersonNameElement"/>.
/// </summary>
Expand All @@ -56,10 +38,10 @@ public static explicit operator Corvus.Json.Benchmarking.Models.PersonNameElemen
}

/// <summary>
/// Conversion from <see cref = "Corvus.Json.Benchmarking.Models.PersonNameElementArray"/>.
/// Conversion from <see cref = "Corvus.Json.Benchmarking.Models.PersonNameElement"/>.
/// </summary>
/// <param name = "value">The value from which to convert.</param>
public static implicit operator OtherNames(Corvus.Json.Benchmarking.Models.PersonNameElementArray value)
public static implicit operator OtherNames(Corvus.Json.Benchmarking.Models.PersonNameElement value)
{
if (value.HasJsonElementBacking)
{
Expand All @@ -68,7 +50,7 @@ public static implicit operator OtherNames(Corvus.Json.Benchmarking.Models.Perso

return value.ValueKind switch
{
JsonValueKind.Array => new(value.AsImmutableList()),
JsonValueKind.String => new((string)value),
_ => Undefined
};
}
Expand All @@ -91,4 +73,22 @@ public static explicit operator Corvus.Json.Benchmarking.Models.PersonNameElemen

return Corvus.Json.Benchmarking.Models.PersonNameElementArray.Undefined;
}

/// <summary>
/// Conversion from <see cref = "Corvus.Json.Benchmarking.Models.PersonNameElementArray"/>.
/// </summary>
/// <param name = "value">The value from which to convert.</param>
public static implicit operator OtherNames(Corvus.Json.Benchmarking.Models.PersonNameElementArray value)
{
if (value.HasJsonElementBacking)
{
return new(value.AsJsonElement);
}

return value.ValueKind switch
{
JsonValueKind.Array => new(value.AsImmutableList()),
_ => Undefined
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ public static implicit operator OtherNames(string value)
return new(value);
}

/// <summary>
/// Conversion from JsonString.
/// </summary>
/// <param name = "value">The value from which to convert.</param>
public static implicit operator OtherNames(JsonString value)
{
if (value.HasDotnetBacking && value.ValueKind == JsonValueKind.String)
{
return new((string)value);
}

return new(value.AsJsonElement);
}

/// <summary>
/// Conversion to string.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions Solutions/Corvus.Json.Benchmarking/PersonModel/Person.Object.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ public Person(ImmutableList<JsonObjectProperty> value)
this.objectBacking = value;
}

/// <summary>
/// Conversion from JsonObject.
/// </summary>
/// <param name = "value">The value from which to convert.</param>
public static implicit operator Person(JsonObject value)
{
if (value.HasDotnetBacking && value.ValueKind == JsonValueKind.Object)
{
return new(value.AsPropertyBacking());
}

return new(value.AsJsonElement);
}

/// <inheritdoc/>
public ImmutableList<JsonObjectProperty> AsPropertyBacking()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ public static implicit operator PersonArray(ImmutableList<JsonAny> value)
return new(value);
}

/// <summary>
/// Conversion from JsonArray.
/// </summary>
/// <param name = "value">The value from which to convert.</param>
public static implicit operator PersonArray(JsonArray value)
{
if (value.HasDotnetBacking && value.ValueKind == JsonValueKind.Array)
{
return new(value.AsImmutableList());
}

return new(value.AsJsonElement);
}

/// <summary>
/// Initializes a new instance of the <see cref = "PersonArray"/> struct.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ public PersonName(ImmutableList<JsonObjectProperty> value)
this.objectBacking = value;
}

/// <summary>
/// Conversion from JsonObject.
/// </summary>
/// <param name = "value">The value from which to convert.</param>
public static implicit operator PersonName(JsonObject value)
{
if (value.HasDotnetBacking && value.ValueKind == JsonValueKind.Object)
{
return new(value.AsPropertyBacking());
}

return new(value.AsJsonElement);
}

/// <inheritdoc/>
public ImmutableList<JsonObjectProperty> AsPropertyBacking()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,20 @@ public static implicit operator PersonNameElement(string value)
return new(value);
}

/// <summary>
/// Conversion from JsonString.
/// </summary>
/// <param name = "value">The value from which to convert.</param>
public static implicit operator PersonNameElement(JsonString value)
{
if (value.HasDotnetBacking && value.ValueKind == JsonValueKind.String)
{
return new((string)value);
}

return new(value.AsJsonElement);
}

/// <summary>
/// Conversion to string.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ public static implicit operator PersonNameElementArray(ImmutableList<JsonAny> va
return new(value);
}

/// <summary>
/// Conversion from JsonArray.
/// </summary>
/// <param name = "value">The value from which to convert.</param>
public static implicit operator PersonNameElementArray(JsonArray value)
{
if (value.HasDotnetBacking && value.ValueKind == JsonValueKind.Array)
{
return new(value.AsImmutableList());
}

return new(value.AsJsonElement);
}

/// <summary>
/// Initializes a new instance of the <see cref = "PersonNameElementArray"/> struct.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2618,6 +2618,11 @@ public Conversion(TypeDeclaration typeDeclaration, bool isDirect)
/// </summary>
public bool IsBuiltInType => this.typeDeclaration.Schema().IsBuiltInType();

/// <summary>
/// Gets a value indicating whether this is a built-in primitive type.
/// </summary>
public bool IsBuiltInPrimitiveType => this.typeDeclaration.Schema().IsBuiltInPrimitiveType();

/// <summary>
/// Gets the fully qualified dotnet type name.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2618,6 +2618,11 @@ public Conversion(TypeDeclaration typeDeclaration, bool isDirect)
/// </summary>
public bool IsBuiltInType => this.typeDeclaration.Schema().IsBuiltInType();

/// <summary>
/// Gets a value indicating whether this is a built-in primitive type.
/// </summary>
public bool IsBuiltInPrimitiveType => this.typeDeclaration.Schema().IsBuiltInPrimitiveType();

/// <summary>
/// Gets the fully qualified dotnet type name.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2618,6 +2618,11 @@ public Conversion(TypeDeclaration typeDeclaration, bool isDirect)
/// </summary>
public bool IsBuiltInType => this.typeDeclaration.Schema().IsBuiltInType();

/// <summary>
/// Gets a value indicating whether this is a built-in primitive type.
/// </summary>
public bool IsBuiltInPrimitiveType => this.typeDeclaration.Schema().IsBuiltInPrimitiveType();

/// <summary>
/// Gets the fully qualified dotnet type name.
/// </summary>
Expand Down
Loading
Loading