using System.Reflection; using System.Text; using System.Text.Json; using System.Text.Json.Serialization; using System.Text.Encodings; using System.Net.Http; // Enumerable namespace Teo { public struct Enumerable { OneOf _value; public OneOf Value { get => _value; set { _value = value; } } public static implicit operator Enumerable(OneOf value) => new Enumerable { Value = value }; public static implicit operator Enumerable(T t) => new Enumerable { Value = (OneOf)new Enumerable { Value = t } }; public static implicit operator Enumerable(T[] a) => new Enumerable { Value = (OneOf)new Enumerable { Value = a } }; public static explicit operator OneOf(Enumerable enumerable) { return enumerable.Value; } public override string ToString() { return Value.ToString()!; } } } // Filters namespace Teo { public class ComparableFilter where T : struct { public new T? Equals { get; set; } public T[]? In { get; set; } public T[]? NotIn { get; set; } public T? Lt { get; set; } public T? Lte { get; set; } public T? Gt { get; set; } public T? Gte { get; set; } public OneOf>? Not { get; set; } public ComparableFilter( T? equals = null, OneOf>? not = null, T[]? @in = null, T[]? notIn = null, T? lt = null, T? lte = null, T? gt = null, T? gte = null ) { Equals = equals; Not = not; In = @in; NotIn = notIn; Lt = lt; Lte = lte; Gt = gt; Gte = gte; } } public class ComparableNullableFilter where T : struct { public new Optional? Equals { get; set; } public T[]? In { get; set; } public T[]? NotIn { get; set; } public T? Lt { get; set; } public T? Lte { get; set; } public T? Gt { get; set; } public T? Gte { get; set; } public OneOf>? Not { get; set; } public ComparableNullableFilter( Optional? equals = null, OneOf>? not = null, T[]? @in = null, T[]? notIn = null, T? lt = null, T? lte = null, T? gt = null, T? gte = null ) { Equals = equals; Not = not; In = @in; NotIn = notIn; Lt = lt; Lte = lte; Gt = gt; Gte = gte; } } public class ComparableWithAggregatesFilter where T : struct { public new T? Equals { get; set; } public T[]? In { get; set; } public T[]? NotIn { get; set; } public T? Lt { get; set; } public T? Lte { get; set; } public T? Gt { get; set; } public T? Gte { get; set; } public OneOf>? Not { get; set; } public ComparableFilter? _Equals { get; set; } public ComparableFilter? _Min { get; set; } public ComparableFilter? _Max { get; set; } public ComparableWithAggregatesFilter( T? equals = null, OneOf>? not = null, T[]? @in = null, T[]? notIn = null, T? lt = null, T? lte = null, T? gt = null, T? gte = null, ComparableFilter? _equals = null, ComparableFilter? _min = null, ComparableFilter? _max = null ) { Equals = equals; Not = not; In = @in; NotIn = notIn; Lt = lt; Lte = lte; Gt = gt; Gte = gte; _Equals = _equals; _Min = _min; _Max = _max; } } public class ComparableNullableWithAggregatesFilter where T : struct { public new Optional? Equals { get; set; } public Optional[]? In { get; set; } public Optional[]? NotIn { get; set; } public T? Lt { get; set; } public T? Lte { get; set; } public T? Gt { get; set; } public T? Gte { get; set; } public OneOf>? Not { get; set; } public ComparableNullableFilter? _Equals { get; set; } public ComparableNullableFilter? _Min { get; set; } public ComparableNullableFilter? _Max { get; set; } public ComparableNullableWithAggregatesFilter( Optional? equals = null, OneOf>? not = null, Optional[]? @in = null, Optional[]? notIn = null, T? lt = null, T? lte = null, T? gt = null, T? gte = null, ComparableNullableFilter? _equals = null, ComparableNullableFilter? _min = null, ComparableNullableFilter? _max = null ) { Equals = equals; Not = not; In = @in; NotIn = notIn; Lt = lt; Lte = lte; Gt = gt; Gte = gte; _Equals = _equals; _Min = _min; _Max = _max; } } public class NumberWithAggregatesFilter : ComparableWithAggregatesFilter where T : struct { public new OneOf>? Not { get; set; } public ComparableFilter? _Sum { get; set; } public ComparableFilter? _Avg { get; set; } public NumberWithAggregatesFilter( T? equals = null, OneOf>? not = null, T[]? @in = null, T[]? notIn = null, T? lt = null, T? lte = null, T? gt = null, T? gte = null, ComparableFilter? _equals = null, ComparableFilter? _min = null, ComparableFilter? _max = null, ComparableFilter? _sum = null, ComparableFilter? _avg = null ) { Equals = equals; Not = not; In = @in; NotIn = notIn; Lt = lt; Lte = lte; Gt = gt; Gte = gte; _Equals = _equals; _Min = _min; _Max = _max; _Sum = _sum; _Avg = _avg; } } public class NumberNullableWithAggregatesFilter : ComparableNullableWithAggregatesFilter where T : struct { public new OneOf>? Not { get; set; } public ComparableNullableFilter? _Sum { get; set; } public ComparableNullableFilter? _Avg { get; set; } public NumberNullableWithAggregatesFilter( Optional? equals = null, OneOf>? not = null, Optional[]? @in = null, Optional[]? notIn = null, T? lt = null, T? lte = null, T? gt = null, T? gte = null, ComparableNullableFilter? _equals = null, ComparableNullableFilter? _min = null, ComparableNullableFilter? _max = null, ComparableNullableFilter? _sum = null, ComparableNullableFilter? _avg = null ) { Equals = equals; Not = not; In = @in; NotIn = notIn; Lt = lt; Lte = lte; Gt = gt; Gte = gte; _Equals = _equals; _Min = _min; _Max = _max; _Sum = _sum; _Avg = _avg; } } public class RefComparableFilter where T : class { public new T? Equals { get; set; } public T[]? In { get; set; } public T[]? NotIn { get; set; } public T? Lt { get; set; } public T? Lte { get; set; } public T? Gt { get; set; } public T? Gte { get; set; } public OneOf>? Not { get; set; } public RefComparableFilter( T? equals = null, OneOf>? not = null, T[]? @in = null, T[]? notIn = null, T? lt = null, T? lte = null, T? gt = null, T? gte = null ) { Equals = equals; Not = not; In = @in; NotIn = notIn; Lt = lt; Lte = lte; Gt = gt; Gte = gte; } } public class RefComparableNullableFilter where T : class { public new Optional? Equals { get; set; } public Optional[]? In { get; set; } public Optional[]? NotIn { get; set; } public T? Lt { get; set; } public T? Lte { get; set; } public T? Gt { get; set; } public T? Gte { get; set; } public OneOf, RefComparableNullableFilter>? Not { get; set; } public RefComparableNullableFilter( Optional? equals = null, OneOf, RefComparableNullableFilter>? not = null, Optional[]? @in = null, Optional[]? notIn = null, T? lt = null, T? lte = null, T? gt = null, T? gte = null ) { Equals = equals; Not = not; In = @in; NotIn = notIn; Lt = lt; Lte = lte; Gt = gt; Gte = gte; } } public class RefComparableWithAggregatesFilter where T : class { public new T? Equals { get; set; } public T[]? In { get; set; } public T[]? NotIn { get; set; } public T? Lt { get; set; } public T? Lte { get; set; } public T? Gt { get; set; } public T? Gte { get; set; } public OneOf>? Not { get; set; } public ComparableFilter? _Count { get; set; } public RefComparableFilter? _Min { get; set; } public RefComparableFilter? _Max { get; set; } public RefComparableWithAggregatesFilter( T? equals = null, OneOf>? not = null, T[]? @in = null, T[]? notIn = null, T? lt = null, T? lte = null, T? gt = null, T? gte = null, ComparableFilter? _count = null, RefComparableFilter? _min = null, RefComparableFilter? _max = null ) { Equals = equals; Not = not; In = @in; NotIn = notIn; Lt = lt; Lte = lte; Gt = gt; Gte = gte; _Count = _count; _Min = _min; _Max = _max; } } public class RefComparableNullableWithAggregatesFilter where T : class { public new Optional? Equals { get; set; } public Optional[]? In { get; set; } public Optional[]? NotIn { get; set; } public T? Lt { get; set; } public T? Lte { get; set; } public T? Gt { get; set; } public T? Gte { get; set; } public OneOf>? Not { get; set; } public ComparableNullableFilter? _Count { get; set; } public RefComparableNullableFilter? _Min { get; set; } public RefComparableNullableFilter? _Max { get; set; } public RefComparableNullableWithAggregatesFilter( Optional? equals = null, OneOf>? not = null, Optional[]? @in = null, Optional[]? notIn = null, T? lt = null, T? lte = null, T? gt = null, T? gte = null, ComparableNullableFilter? _count = null, RefComparableNullableFilter? _min = null, RefComparableNullableFilter? _max = null ) { Equals = equals; Not = not; In = @in; NotIn = notIn; Lt = lt; Lte = lte; Gt = gt; Gte = gte; _Count = _count; _Min = _min; _Max = _max; } } public class BoolFilter { public new bool? Equals { get; set; } public OneOf? Not { get; set; } public BoolFilter(bool? equals = null, OneOf? not = null) { Equals = equals; Not = not; } } public class BoolNullableFilter { public new Optional? Equals { get; set; } public OneOf, BoolNullableFilter>? Not { get; set; } public BoolNullableFilter(Optional? equals = null, OneOf, BoolNullableFilter>? not = null) { Equals = equals; Not = not; } } public class BoolWithAggregatesFilter { public new bool? Equals { get; set; } public OneOf? Not { get; set; } public ComparableFilter? _Count { get; set; } public BoolFilter? _Min { get; set; } public BoolFilter? _Max { get; set; } public BoolWithAggregatesFilter( bool? equals = null, OneOf? not = null, ComparableFilter? _count = null, BoolFilter? _min = null, BoolFilter? _max = null ) { Equals = equals; Not = not; _Count = _count; _Min = _min; _Max = _max; } } public class BoolNullableWithAggregatesFilter { public new Optional? Equals { get; set; } public OneOf? Not { get; set; } public ComparableNullableFilter? _Count { get; set; } public BoolNullableFilter? _Min { get; set; } public BoolNullableFilter? _Max { get; set; } public BoolNullableWithAggregatesFilter( bool? equals = null, OneOf? not = null, ComparableNullableFilter? _count = null, BoolNullableFilter? _min = null, BoolNullableFilter? _max = null ) { Equals = equals; Not = not; _Count = _count; _Min = _min; _Max = _max; } } public class StringFilter : RefComparableFilter { public string? Contains { get; set; } public string? StartsWith { get; set; } public string? EndsWith { get; set; } public string? Matches { get; set; } public new OneOf? Not { get; set; } public StringFilter( string? equals = null, string[]? @in = null, string[]? notIn = null, string? lt = null, string? lte = null, string? gt = null, string? gte = null, string? contains = null, string? startsWith = null, string? endsWith = null, string? matches = null, OneOf? not = null ) { Equals = equals; In = @in; NotIn = notIn; Lt = lt; Lte = lte; Gt = gt; Gte = gte; Contains = contains; StartsWith = startsWith; EndsWith = endsWith; Matches = matches; Not = not; } } public class StringNullableFilter : RefComparableNullableFilter { public string? Contains { get; set; } public string? StartsWith { get; set; } public string? EndsWith { get; set; } public string? Matches { get; set; } public new OneOf, StringNullableFilter>? Not { get; set; } public StringNullableFilter( Optional? equals = null, Optional[]? @in = null, Optional[]? notIn = null, string? lt = null, string? lte = null, string? gt = null, string? gte = null, string? contains = null, string? startsWith = null, string? endsWith = null, string? matches = null, OneOf, StringNullableFilter>? not = null ) { Equals = equals; In = @in; NotIn = notIn; Lt = lt; Lte = lte; Gt = gt; Gte = gte; Contains = contains; StartsWith = startsWith; EndsWith = endsWith; Matches = matches; Not = not; } } public class StringWithAggregatesFilter : RefComparableWithAggregatesFilter { public string? Contains { get; set; } public string? StartsWith { get; set; } public string? EndsWith { get; set; } public string? Matches { get; set; } public new OneOf? Not { get; set; } public new StringFilter? _Min { get; set; } public new StringFilter? _Max { get; set; } public StringWithAggregatesFilter( string? equals = null, string[]? @in = null, string[]? notIn = null, string? lt = null, string? lte = null, string? gt = null, string? gte = null, string? contains = null, string? startsWith = null, string? endsWith = null, string? matches = null, OneOf? not = null, ComparableFilter? _count = null, StringFilter? _min = null, StringFilter? _max = null ) { Equals = equals; In = @in; NotIn = notIn; Lt = lt; Lte = lte; Gt = gt; Gte = gte; Contains = contains; StartsWith = startsWith; EndsWith = endsWith; Matches = matches; Not = not; _Count = _count; _Min = _min; _Max = _max; } } public class StringNullableWithAggregatesFilter : RefComparableNullableWithAggregatesFilter { public string? Contains { get; set; } public string? StartsWith { get; set; } public string? EndsWith { get; set; } public string? Matches { get; set; } public new OneOf? Not { get; set; } public new StringNullableFilter? _Min { get; set; } public new StringNullableFilter? _Max { get; set; } public StringNullableWithAggregatesFilter( Optional? equals = null, Optional[]? @in = null, Optional[]? notIn = null, string? lt = null, string? lte = null, string? gt = null, string? gte = null, string? contains = null, string? startsWith = null, string? endsWith = null, string? matches = null, OneOf? not = null, ComparableNullableFilter? _count = null, StringNullableFilter? _min = null, StringNullableFilter? _max = null ) { Equals = equals; In = @in; NotIn = notIn; Lt = lt; Lte = lte; Gt = gt; Gte = gte; Contains = contains; StartsWith = startsWith; EndsWith = endsWith; Matches = matches; Not = not; _Count = _count; _Min = _min; _Max = _max; } } public class EnumFilter where T : struct { public new T? Equals { get; set; } public T[]? In { get; set; } public T[]? NotIn { get; set; } public OneOf>? Not { get; set; } public EnumFilter( T? equals, T[]? @in, T[]? notIn, OneOf>? not ) { Equals = equals; In = @in; NotIn = notIn; Not = not; } } public class EnumNullableFilter where T : struct { public new Optional? Equals { get; set; } public Optional[]? In { get; set; } public Optional[]? NotIn { get; set; } public OneOf, EnumNullableFilter>? Not { get; set; } public EnumNullableFilter( Optional? equals, Optional[]? @in, Optional[]? notIn, OneOf, EnumNullableFilter>? not ) { Equals = equals; In = @in; NotIn = notIn; Not = not; } } public class ValueArrayFilter where T : struct { public new T? Equals { set; get; } public T? Has { set; get; } public T[]? HasSome { set; get; } public T[]? HasEvery { set; get; } public bool? IsEmpty { get; set; } public int? Length { get; set; } public ValueArrayFilter( T? equals = null, T? has = null, T[]? hasSome = null, T[]? hasEvery = null, bool? isEmpty = null, int? length = null ) { Equals = equals; Has = has; HasSome = hasSome; HasEvery = hasEvery; IsEmpty = isEmpty; Length = length; } } public class ValueArrayNullableFilter where T : struct { public new Optional? Equals { set; get; } public T? Has { set; get; } public T[]? HasSome { set; get; } public T[]? HasEvery { set; get; } public bool? IsEmpty { get; set; } public int? Length { get; set; } public ValueArrayNullableFilter( Optional? equals = null, T? has = null, T[]? hasSome = null, T[]? hasEvery = null, bool? isEmpty = null, int? length = null ) { Equals = equals; Has = has; HasSome = hasSome; HasEvery = hasEvery; IsEmpty = isEmpty; Length = length; } } public class RefArrayFilter where T : class { public new T? Equals { set; get; } public T? Has { set; get; } public T[]? HasSome { set; get; } public T[]? HasEvery { set; get; } public bool? IsEmpty { get; set; } public int? Length { get; set; } public RefArrayFilter( T? equals = null, T? has = null, T[]? hasSome = null, T[]? hasEvery = null, bool? isEmpty = null, int? length = null ) { Equals = equals; Has = has; HasSome = hasSome; HasEvery = hasEvery; IsEmpty = isEmpty; Length = length; } } public class RefArrayNullableFilter where T : class { public new Optional? Equals { set; get; } public T? Has { set; get; } public T[]? HasSome { set; get; } public T[]? HasEvery { set; get; } public bool? IsEmpty { get; set; } public int? Length { get; set; } public RefArrayNullableFilter( Optional? equals = null, T? has = null, T[]? hasSome = null, T[]? hasEvery = null, bool? isEmpty = null, int? length = null ) { Equals = equals; Has = has; HasSome = hasSome; HasEvery = hasEvery; IsEmpty = isEmpty; Length = length; } } } // One of #nullable disable namespace Teo { public interface IOneOf { object Value { get; } int Index { get; } } internal static class Functions { internal static string FormatValue(T value) => $"{typeof(T).FullName}: {value?.ToString()}"; internal static string FormatValue(object @this, object @base, T value) => ReferenceEquals(@this, value) ? @base.ToString() : $"{typeof(T).FullName}: {value?.ToString()}"; } public struct OneOf : IOneOf { readonly T0 _value0; readonly T1 _value1; readonly int _index; OneOf(int index, T0 value0 = default, T1 value1 = default) { _index = index; _value0 = value0; _value1 = value1; } public object Value => _index switch { 0 => _value0, 1 => _value1, _ => throw new InvalidOperationException() }; public int Index => _index; public bool IsT0 => _index == 0; public bool IsT1 => _index == 1; public T0 AsT0 => _index == 0 ? _value0 : throw new InvalidOperationException($"Cannot return as T0 as result is T{_index}"); public T1 AsT1 => _index == 1 ? _value1 : throw new InvalidOperationException($"Cannot return as T1 as result is T{_index}"); public static implicit operator OneOf(T0 t) => new OneOf(0, value0: t); public static implicit operator OneOf(T1 t) => new OneOf(1, value1: t); public void Switch(Action f0, Action f1) { if (_index == 0 && f0 != null) { f0(_value0); return; } if (_index == 1 && f1 != null) { f1(_value1); return; } throw new InvalidOperationException(); } public TResult Match(Func f0, Func f1) { if (_index == 0 && f0 != null) { return f0(_value0); } if (_index == 1 && f1 != null) { return f1(_value1); } throw new InvalidOperationException(); } public static OneOf FromT0(T0 input) => input; public static OneOf FromT1(T1 input) => input; public OneOf MapT0(Func mapFunc) { if (mapFunc == null) { throw new ArgumentNullException(nameof(mapFunc)); } return _index switch { 0 => mapFunc(AsT0), 1 => AsT1, _ => throw new InvalidOperationException() }; } public OneOf MapT1(Func mapFunc) { if (mapFunc == null) { throw new ArgumentNullException(nameof(mapFunc)); } return _index switch { 0 => AsT0, 1 => mapFunc(AsT1), _ => throw new InvalidOperationException() }; } public bool TryPickT0(out T0 value, out T1 remainder) { value = IsT0 ? AsT0 : default; remainder = _index switch { 0 => default, 1 => AsT1, _ => throw new InvalidOperationException() }; return this.IsT0; } public bool TryPickT1(out T1 value, out T0 remainder) { value = IsT1 ? AsT1 : default; remainder = _index switch { 0 => AsT0, 1 => default, _ => throw new InvalidOperationException() }; return this.IsT1; } bool Equals(OneOf other) => _index == other._index && _index switch { 0 => Equals(_value0, other._value0), 1 => Equals(_value1, other._value1), _ => false }; public override bool Equals(object obj) { if (ReferenceEquals(null, obj)) { return false; } return obj is OneOf o && Equals(o); } public override string ToString() => _index switch { 0 => Functions.FormatValue(_value0), 1 => Functions.FormatValue(_value1), _ => throw new InvalidOperationException("Unexpected index, which indicates a problem in the OneOf codegen.") }; public override int GetHashCode() { unchecked { int hashCode = _index switch { 0 => _value0?.GetHashCode(), 1 => _value1?.GetHashCode(), _ => 0 } ?? 0; return (hashCode * 397) ^ _index; } } } } // Operations #nullable enable namespace Teo { public class ObjectIdFieldUpdateOperationsInput { public string? Set { get; set; } public ObjectIdFieldUpdateOperationsInput(string? set) { Set = set; } } public class NullableObjectIdFieldUpdateOperationsInput { public Optional? Set { get; set; } public NullableObjectIdFieldUpdateOperationsInput(Optional? set) { Set = set; } } public class StringFieldUpdateOperationsInput { public string? Set { get; set; } public StringFieldUpdateOperationsInput(string? set) { Set = set; } } public class NullableStringFieldUpdateOperationsInput { public Optional? Set { get; set; } public NullableStringFieldUpdateOperationsInput(Optional? set) { Set = set; } } public class NumberFieldUpdateOperationsInput where T: struct { public T? Set { get; set; } public T? Increment { get; set; } public T? Decrement { get; set; } public T? Multiply { get; set; } public T? Divide { get; set; } public NumberFieldUpdateOperationsInput( T? set = null, T? increment = null, T? decrement = null, T? multiply = null, T? divide = null ) { Set = set; Increment = increment; Decrement = decrement; Multiply = multiply; Divide = divide; } } public class NullableNumberFieldUpdateOperationsInput where T : struct { public Optional? Set { get; set; } public T? Increment { get; set; } public T? Decrement { get; set; } public T? Multiply { get; set; } public T? Divide { get; set; } public NullableNumberFieldUpdateOperationsInput( Optional? set = null, T? increment = null, T? decrement = null, T? multiply = null, T? divide = null ) { Set = set; Increment = increment; Decrement = decrement; Multiply = multiply; Divide = divide; } } public class BoolFieldUpdateOperationsInput { public bool? Set { get; set; } public BoolFieldUpdateOperationsInput(bool? set) { Set = set; } } public class NullableBoolFieldUpdateOperationsInput { public Optional? Set { get; set; } public NullableBoolFieldUpdateOperationsInput(Optional? set) { Set = set; } } public class DateOnlyFieldUpdateOperationsInput { public DateOnly? Set { get; set; } public DateOnlyFieldUpdateOperationsInput(DateOnly? set) { Set = set; } } public class NullableDateOnlyFieldUpdateOperationsInput { public Optional? Set { get; set; } public NullableDateOnlyFieldUpdateOperationsInput(Optional? set) { Set = set; } } public class DateTimeFieldUpdateOperationsInput { public DateTime? Set { get; set; } public DateTimeFieldUpdateOperationsInput(DateTime? set) { Set = set; } } public class NullableDateTimeFieldUpdateOperationsInput { public Optional? Set { get; set; } public NullableDateTimeFieldUpdateOperationsInput(Optional? set) { Set = set; } } public class EnumFieldUpdateOperationsInput where T: struct { public T? Set { get; set; } public EnumFieldUpdateOperationsInput(T? set) { Set = set; } } public class NullableEnumFieldUpdateOperationsInput where T : struct { public Optional? Set { get; set; } public NullableEnumFieldUpdateOperationsInput(Optional? set) { Set = set; } } public class ValueArrayFieldUpdateOperationsInput where T : struct { public T[]? Set { get; set; } public T? Push { get; set; } public ValueArrayFieldUpdateOperationsInput(T[]? set = null, T? push = null) { Set = set; Push = push; } } public class NullableValueArrayFieldUpdateOperationsInput where T : struct { public Optional? Set { get; set; } public T? Push { get; set; } public NullableValueArrayFieldUpdateOperationsInput(Optional? set = null, T? push = null) { Set = set; Push = push; } } public class RefArrayFieldUpdateOperationsInput where T : class { public T[]? Set { get; set; } public T? Push { get; set; } public RefArrayFieldUpdateOperationsInput(T[]? set = null, T? push = null) { Set = set; Push = push; } } public class NullableRefArrayFieldUpdateOperationsInput where T : class { public Optional? Set { get; set; } public T? Push { get; set; } public NullableRefArrayFieldUpdateOperationsInput(Optional? set = null, T? push = null) { Set = set; Push = push; } } } // Optional namespace Teo { public class Null { public Null() { } public static readonly Null NULL = new(); public override string ToString() => "Null"; } public struct Optional { OneOf _value; public OneOf Value { get => _value; set { _value = value; } } public static implicit operator Optional(OneOf value) => new Optional { Value = value }; public static implicit operator Optional(T t) => new Optional { Value = (OneOf)new Optional { Value = t } }; public static implicit operator Optional(Null n) => new Optional { Value = (OneOf)new Optional { Value = n } }; public static explicit operator OneOf(Optional optional) { return optional.Value; } public override string ToString() { return Value.ToString()!; } } } // Sort order namespace Teo { public enum SortOrder { Asc, Desc, } } // Resp namespace Teo { public struct Response { public D Data { get; set; } } public struct Response { public M Meta { get; set; } public D Data { get; set; } } public struct ErrorResponse { public ResponseError Error { get; set; } } public struct ResponseError { public string Type { get; set; } public string Message { get; set; } public Dictionary? Errors { get; set; } } public struct PagingInfo { public uint Count { get; set; } public uint? NumberOfPages { get; set; } } public struct TokenInfo { public string Token { get; set; } } public class TeoException : Exception { public string Type { get; } public Dictionary? Errors { get; } public TeoException(ResponseError responseError) : base(JSJsonSerializer.Serialize(responseError)) { Type = responseError.Type; Errors = responseError.Errors; } } } #nullable enable namespace Teo { {%- for class in outline.classes %} {%- if class.kind.is_any_kind_of_enum() %} public enum {{ class.name() }} { {%- for field in class.fields %} {{ field.name|pascalcase }} = {{ loop.index }}, {%- endfor %} } {%- else %} public class {{ class.model_name }}{{ class.name_suffix }} { {%- for field in class.fields_optional_at_last() %} public {{ field.field_type }}{%- if field.optional || class.kind.is_output() %}?{%- endif %} {{ field.name|capitalize_first }} { get; set; } {%- endfor %} public {{ class.model_name }}{{ class.name_suffix }}( {%- for field in class.fields_optional_at_last() %} {{ field.field_type }}{%- if field.optional || class.kind.is_output() %}?{%- endif %} {{ field.name|escape_csharp }}{%- if field.optional || class.kind.is_output() %} = null{%- endif %}{% if !loop.last %},{% endif %} {%- endfor %} ) { {%- for field in class.fields_optional_at_last() %} {{ field.name|capitalize_first }} = {{ field.name|escape_csharp }}; {%- endfor %} } } {%- endif %} {%- endfor %} // Serializers public class DateOnlyConverter : JsonConverter { public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { return DateOnly.Parse(reader.GetString() ?? string.Empty); } public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options) { writer.WriteStringValue(value.ToString("yyyy-MM-dd")); } } public class DateTimeConverter : JsonConverter { public override DateTime Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.StartObject) { throw new JsonException($"JsonTokenType was of type {reader.TokenType}, only objects are supported"); } DateTime? result = null; while (reader.Read()) { if (reader.TokenType == JsonTokenType.EndObject) { if (result == null) { throw new JsonException("DateTime value is not found"); } else { return result! ?? DateTime.UtcNow; } } if (reader.TokenType != JsonTokenType.PropertyName) { throw new JsonException("JsonTokenType was not PropertyName"); } var propertyName = reader.GetString(); if (propertyName != "$date") { throw new JsonException($"Expect `$date', PropertyName was {propertyName}"); } reader.Read(); result = DateTime.Parse(reader.GetString() ?? string.Empty); } throw new JsonException("DateTime value is not parsed"); } public override void Write(Utf8JsonWriter writer, DateTime value, JsonSerializerOptions options) { writer.WriteStringValue(value.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffK")); } } public class DateTimeOffsetConverter : JsonConverter { public override DateTimeOffset Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { return DateTimeOffset.Parse(reader.GetString() ?? string.Empty); } public override void Write(Utf8JsonWriter writer, DateTimeOffset value, JsonSerializerOptions options) { writer.WriteStringValue(value.UtcDateTime.ToString("yyyy-MM-ddTHH:mm:ss.fffK")); } } public class DecimalConverter : JsonConverter { public override decimal Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { if (reader.TokenType != JsonTokenType.StartObject) { throw new JsonException($"JsonTokenType was of type {reader.TokenType}, only objects are supported"); } decimal? result = null; while (reader.Read()) { if (reader.TokenType == JsonTokenType.EndObject) { if (result == null) { throw new JsonException("decimal value is not found"); } else { return result! ?? 0; } } if (reader.TokenType != JsonTokenType.PropertyName) { throw new JsonException("JsonTokenType was not PropertyName"); } var propertyName = reader.GetString(); if (propertyName != "$decimal") { throw new JsonException($"Expect `$decimal', PropertyName was {propertyName}"); } reader.Read(); result = decimal.Parse(reader.GetString() ?? string.Empty); } throw new JsonException("decimal value is not parsed"); } public override void Write(Utf8JsonWriter writer, decimal value, JsonSerializerOptions options) { writer.WriteStringValue(value.ToString()); } } public class OneOfJsonConverterFactory : JsonConverterFactory { public override bool CanConvert(Type typeToConvert) { if (!typeToConvert.IsGenericType) { return false; } if (typeToConvert.GetGenericTypeDefinition() != typeof(OneOf<,>)) { return false; } return true; } public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options) { Type t0 = typeToConvert.GetGenericArguments()[0]; Type t1 = typeToConvert.GetGenericArguments()[1]; JsonConverter converter = (JsonConverter)Activator.CreateInstance( typeof(OneOfJsonConverter<,>).MakeGenericType(new Type[] { t0, t1 }), BindingFlags.Instance | BindingFlags.Public, binder: null, args: null, culture: null)!; return converter; } } public class OneOfJsonConverter : JsonConverter> { public override OneOf Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { throw new NotImplementedException(); } public override void Write(Utf8JsonWriter writer, OneOf value, JsonSerializerOptions options) { value.Switch( t0 => { writer.WriteRawValue(JSJsonSerializer.Serialize(t0)); }, t1 => { writer.WriteRawValue(JSJsonSerializer.Serialize(t1)); }); } } public class OptionalJsonConverterFactory : JsonConverterFactory { public override bool CanConvert(Type typeToConvert) { if (!typeToConvert.IsGenericType) { return false; } if (typeToConvert.GetGenericTypeDefinition() != typeof(Optional<>)) { return false; } return true; } public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options) { Type t0 = typeToConvert.GetGenericArguments()[0]; JsonConverter converter = (JsonConverter)Activator.CreateInstance( typeof(OptionalJsonConverter<>).MakeGenericType(new Type[] { t0 }), BindingFlags.Instance | BindingFlags.Public, binder: null, args: null, culture: null)!; return converter; } } public class OptionalJsonConverter : JsonConverter> { public override Optional Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { throw new NotImplementedException(); } public override void Write(Utf8JsonWriter writer, Optional value, JsonSerializerOptions options) { value.Value.Switch( t0 => writer.WriteRawValue(JSJsonSerializer.Serialize(value.Value.AsT0)), t1 => writer.WriteRawValue("null")); } } public class EnumerableJsonConverterFactory : JsonConverterFactory { public override bool CanConvert(Type typeToConvert) { if (!typeToConvert.IsGenericType) { return false; } if (typeToConvert.GetGenericTypeDefinition() != typeof(Enumerable<>)) { return false; } return true; } public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options) { Type t0 = typeToConvert.GetGenericArguments()[0]; JsonConverter converter = (JsonConverter)Activator.CreateInstance( typeof(EnumerableJsonConverter<>).MakeGenericType(new Type[] { t0 }), BindingFlags.Instance | BindingFlags.Public, binder: null, args: null, culture: null)!; return converter; } } public class EnumerableJsonConverter : JsonConverter> { public override Enumerable Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { throw new NotImplementedException(); } public override void Write(Utf8JsonWriter writer, Enumerable value, JsonSerializerOptions options) { value.Value.Switch( t0 => writer.WriteRawValue(JSJsonSerializer.Serialize(value.Value.AsT0)), t1 => writer.WriteRawValue(JSJsonSerializer.Serialize(value.Value.AsT1))); } } public class SortOrderJsonConverter: JsonConverter { public override SortOrder Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { throw new NotImplementedException(); } public override void Write(Utf8JsonWriter writer, SortOrder value, JsonSerializerOptions options) { switch (value) { case SortOrder.Asc: { writer.WriteStringValue("asc"); return; } case SortOrder.Desc: { writer.WriteStringValue("desc"); return; } } } } {%- for class in outline.classes %} {%- if class.kind.is_any_kind_of_enum() %} public class {{ class.name() }}JsonConverter : JsonConverter<{{ class.name() }}> { public override {{ class.name() }} Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { switch (reader.GetString()) { {%- for field in class.fields %} case "{{ field.name }}": { return {{ class.name() }}.{{ field.name|pascalcase}}; } {%- endfor %} } throw new NotImplementedException(); } public override void Write(Utf8JsonWriter writer, {{ class.name() }} value, JsonSerializerOptions options) { switch (value) { {%- for field in class.fields %} case {{ class.name() }}.{{ field.name|pascalcase}}: { writer.WriteStringValue("{{ field.name }}"); return; } {%- endfor %} } } } {%- endif %} {%- endfor %} static public class JSJsonSerializer { static private JsonSerializerOptions options() { var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, }; options.Converters.Add(new DateOnlyConverter()); options.Converters.Add(new DateTimeConverter()); options.Converters.Add(new DateTimeOffsetConverter()); options.Converters.Add(new DecimalConverter()); options.Converters.Add(new OneOfJsonConverterFactory()); options.Converters.Add(new OptionalJsonConverterFactory()); options.Converters.Add(new EnumerableJsonConverterFactory()); options.Converters.Add(new SortOrderJsonConverter()); {%- for class in outline.classes %} {%- if class.kind.is_any_kind_of_enum() %} options.Converters.Add(new {{ class.name() }}JsonConverter()); {%- endif %} {%- endfor %} return options; } static public string Serialize(T value) { return JsonSerializer.Serialize(value, options()); } static public T? Deserialize(string value) { return JsonSerializer.Deserialize(value, options()); } } {%- for delegate in outline.delegates %} public class {{ delegate.model_name }}Delegate : Delegate { readonly string? _Token; protected internal {{ delegate.model_name }}Delegate(string? token = null) { _Token = token; } {%- for action in delegate.actions %} public async Task<{{ action.response }}> {{ action.name|capitalize_first }}({{ delegate.model_name }}{{ action.name|capitalize_first }}Args args) { return await Request<{{ action.response }}>("{{ delegate.model_name }}", "{{ action.name }}", args, _Token); } {%- endfor %} } {%- endfor %} public class Delegate { protected static readonly string HOST = "{{ conf.host }}"; protected async Task Request(string model, string action, object args, string? token = null) { // not handle our own errors yet var uri = new Uri(HOST + "/" + model + "/action/" + action); var client = new HttpClient(); if (token != null) { client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}"); } var jsonContent = JSJsonSerializer.Serialize(args)!; var content = new StringContent(jsonContent, Encoding.UTF8, "application/json"); var response = await client.PostAsync(uri, content); var httpResponseBody = await response.Content.ReadAsStringAsync(); if (((int)response.StatusCode) >= 400) { throw new TeoException(JSJsonSerializer.Deserialize(httpResponseBody)!.Error); } return JSJsonSerializer.Deserialize(httpResponseBody)!; } } public class {{ conf.object_name|capitalize_first }} { private string? _Token; public {{ conf.object_name|capitalize_first }}(string? token = null) { _Token = token; } {%- for delegate in outline.delegates %} public {{ delegate.model_name|capitalize_first }}Delegate {{ delegate.model_name|capitalize_first }} { get { return new {{ delegate.model_name|capitalize_first }}Delegate(_Token); } } {%- endfor %} } }