Skip to content

Commit

Permalink
style: Expose only OpenApiGenerator generated code.
Browse files Browse the repository at this point in the history
  • Loading branch information
HavenDV committed Jun 5, 2024
1 parent 29fc369 commit 3efe794
Show file tree
Hide file tree
Showing 72 changed files with 4,040 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,9 @@ FodyWeavers.xsd
# JetBrains Rider
*.sln.iml
/.idea/

# Expose generated OpenApiGenerator code
src/libs/Ollama/Generated/**/*.*
!src/libs/Ollama/Generated/OpenApiGenerator/**/*.*
src/libs/Ollama.Models/Generated/**/*.*
!src/libs/Ollama.Models/Generated/OpenApiGenerator/**/*.*
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
using System.Linq;

#nullable enable

namespace System
{
/// <summary>
///
/// </summary>
public readonly struct AnyOf<T1, T2> : global::System.IEquatable<AnyOf<T1, T2>>
{
/// <summary>
///
/// </summary>
#if NET6_0_OR_GREATER
public T1? Value1 { get; init; }
#else
public T1? Value1 { get; }
#endif

/// <summary>
///
/// </summary>
#if NET6_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.MemberNotNullWhen(true, nameof(Value1))]
#endif
public bool IsValue1 => Value1 != null;

/// <summary>
///
/// </summary>
public static implicit operator AnyOf<T1, T2>(T1 value) => new AnyOf<T1, T2>(value);

/// <summary>
///
/// </summary>
public static implicit operator T1?(AnyOf<T1, T2> @this) => @this.Value1;

/// <summary>
///
/// </summary>
public AnyOf(T1? value)
{
Value1 = value;
}

/// <summary>
///
/// </summary>
#if NET6_0_OR_GREATER
public T2? Value2 { get; init; }
#else
public T2? Value2 { get; }
#endif

/// <summary>
///
/// </summary>
#if NET6_0_OR_GREATER
[global::System.Diagnostics.CodeAnalysis.MemberNotNullWhen(true, nameof(Value2))]
#endif
public bool IsValue2 => Value2 != null;

/// <summary>
///
/// </summary>
public static implicit operator AnyOf<T1, T2>(T2 value) => new AnyOf<T1, T2>(value);

/// <summary>
///
/// </summary>
public static implicit operator T2?(AnyOf<T1, T2> @this) => @this.Value2;

/// <summary>
///
/// </summary>
public AnyOf(T2? value)
{
Value2 = value;
}

/// <summary>
///
/// </summary>
public AnyOf(
T1? value1,
T2? value2
)
{
Value1 = value1;
Value2 = value2;
}

/// <summary>
///
/// </summary>
public object? Object =>
Value2 as object ??
Value1 as object
;

/// <summary>
///
/// </summary>
public bool Validate()
{
return IsValue1 || IsValue2;
}

/// <summary>
///
/// </summary>
public override int GetHashCode()
{
var fields = new object?[]
{
Value1,
typeof(T1),
Value2,
typeof(T2),
};
const int offset = unchecked((int)2166136261);
const int prime = 16777619;
static int HashCodeAggregator(int hashCode, object? value) => value == null
? (hashCode ^ 0) * prime
: (hashCode ^ value.GetHashCode()) * prime;
return fields.Aggregate(offset, HashCodeAggregator);
}

/// <summary>
///
/// </summary>
public bool Equals(AnyOf<T1, T2> other)
{
return
global::System.Collections.Generic.EqualityComparer<T1?>.Default.Equals(Value1, other.Value1) &&
global::System.Collections.Generic.EqualityComparer<T2?>.Default.Equals(Value2, other.Value2)
;
}

/// <summary>
///
/// </summary>
public static bool operator ==(AnyOf<T1, T2> obj1, AnyOf<T1, T2> obj2)
{
return global::System.Collections.Generic.EqualityComparer<AnyOf<T1, T2>>.Default.Equals(obj1, obj2);
}

/// <summary>
///
/// </summary>
public static bool operator !=(AnyOf<T1, T2> obj1, AnyOf<T1, T2> obj2)
{
return !(obj1 == obj2);
}

/// <summary>
///
/// </summary>
public override bool Equals(object? obj)
{
return obj is AnyOf<T1, T2> o && Equals(o);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#nullable enable

namespace OpenApiGenerator.JsonConverters
{
/// <inheritdoc />
public class AnyOfJsonConverter<T1, T2> : global::System.Text.Json.Serialization.JsonConverter<global::System.AnyOf<T1, T2>>
{
/// <inheritdoc />
public override global::System.AnyOf<T1, T2> Read(
ref global::System.Text.Json.Utf8JsonReader reader,
global::System.Type typeToConvert,
global::System.Text.Json.JsonSerializerOptions options)
{
var
readerCopy = reader;
T1? value1 = default;
try
{
value1 = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref readerCopy, options);
}
catch (global::System.Text.Json.JsonException)
{
}

readerCopy = reader;
T2? value2 = default;
try
{
value2 = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref readerCopy, options);
}
catch (global::System.Text.Json.JsonException)
{
}
var result = new global::System.AnyOf<T1, T2>(
value1,

value2
);
if (!result.Validate())
{
throw new global::System.Text.Json.JsonException($"Invalid JSON format for AnyOf<{typeof(T1).Name}, {typeof(T2).Name}>");
}

if (value1 != null)
{
_ = global::System.Text.Json.JsonSerializer.Deserialize<T1>(ref reader, options);
}

else if (value2 != null)
{
_ = global::System.Text.Json.JsonSerializer.Deserialize<T2>(ref reader, options);
}
return result;
}

/// <inheritdoc />
public override void Write(
global::System.Text.Json.Utf8JsonWriter writer,
global::System.AnyOf<T1, T2> value,
global::System.Text.Json.JsonSerializerOptions options)
{
if (!value.Validate())
{
throw new global::System.Text.Json.JsonException($"Invalid AnyOf<{typeof(T1).Name}, {typeof(T2).Name}> object.");
}

if (value.IsValue1)
{
global::System.Text.Json.JsonSerializer.Serialize(writer, value.Value1, value.Value1!.GetType(), options);
}

else if (value.IsValue2)
{
global::System.Text.Json.JsonSerializer.Serialize(writer, value.Value2, value.Value2!.GetType(), options);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#nullable enable

namespace OpenApiGenerator.JsonConverters
{
/// <inheritdoc />
public sealed class AnyOfJsonConverterFactory2 : global::System.Text.Json.Serialization.JsonConverterFactory
{
/// <inheritdoc />
public override bool CanConvert(global::System.Type? typeToConvert)
{
return typeToConvert is { IsGenericType: true } && typeToConvert.GetGenericTypeDefinition() == typeof(global::System.AnyOf<,>);
}

/// <inheritdoc />
public override global::System.Text.Json.Serialization.JsonConverter CreateConverter(
global::System.Type typeToConvert,
global::System.Text.Json.JsonSerializerOptions options)
{
typeToConvert = typeToConvert ?? throw new global::System.ArgumentNullException(nameof(typeToConvert));

return (global::System.Text.Json.Serialization.JsonConverter)global::System.Activator.CreateInstance(
typeof(AnyOfJsonConverter<,>).MakeGenericType(typeToConvert.GenericTypeArguments))!;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#nullable enable

namespace OpenApiGenerator.JsonConverters
{
/// <inheritdoc />
public sealed class CreateModelResponseStatusJsonConverter : global::System.Text.Json.Serialization.JsonConverter<global::Ollama.CreateModelResponseStatus>
{
/// <inheritdoc />
public override global::Ollama.CreateModelResponseStatus Read(
ref global::System.Text.Json.Utf8JsonReader reader,
global::System.Type typeToConvert,
global::System.Text.Json.JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case global::System.Text.Json.JsonTokenType.String:
{
var stringValue = reader.GetString();
if (stringValue != null)
{
return global::Ollama.CreateModelResponseStatusExtensions.ToEnum(stringValue) ?? default;
}

break;
}
case global::System.Text.Json.JsonTokenType.Number:
{
var numValue = reader.GetInt32();
return (global::Ollama.CreateModelResponseStatus)numValue;
}
default:
throw new global::System.ArgumentOutOfRangeException(nameof(reader));
}

return default;
}

/// <inheritdoc />
public override void Write(
global::System.Text.Json.Utf8JsonWriter writer,
global::Ollama.CreateModelResponseStatus value,
global::System.Text.Json.JsonSerializerOptions options)
{
writer = writer ?? throw new global::System.ArgumentNullException(nameof(writer));

writer.WriteStringValue(global::Ollama.CreateModelResponseStatusExtensions.ToValueString(value));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#nullable enable

namespace OpenApiGenerator.JsonConverters
{
/// <inheritdoc />
public sealed class CreateModelResponseStatusNullableJsonConverter : global::System.Text.Json.Serialization.JsonConverter<global::Ollama.CreateModelResponseStatus?>
{
/// <inheritdoc />
public override global::Ollama.CreateModelResponseStatus? Read(
ref global::System.Text.Json.Utf8JsonReader reader,
global::System.Type typeToConvert,
global::System.Text.Json.JsonSerializerOptions options)
{
switch (reader.TokenType)
{
case global::System.Text.Json.JsonTokenType.String:
{
var stringValue = reader.GetString();
if (stringValue != null)
{
return global::Ollama.CreateModelResponseStatusExtensions.ToEnum(stringValue);
}

break;
}
case global::System.Text.Json.JsonTokenType.Number:
{
var numValue = reader.GetInt32();
return (global::Ollama.CreateModelResponseStatus)numValue;
}
default:
throw new global::System.ArgumentOutOfRangeException(nameof(reader));
}

return default;
}

/// <inheritdoc />
public override void Write(
global::System.Text.Json.Utf8JsonWriter writer,
global::Ollama.CreateModelResponseStatus? value,
global::System.Text.Json.JsonSerializerOptions options)
{
writer = writer ?? throw new global::System.ArgumentNullException(nameof(writer));

if (value == null)
{
writer.WriteNullValue();
}
else
{
writer.WriteStringValue(global::Ollama.CreateModelResponseStatusExtensions.ToValueString(value.Value));
}
}
}
}
Loading

0 comments on commit 3efe794

Please sign in to comment.