Skip to content

Commit 30ebeb9

Browse files
chore(internal): codegen related update
1 parent 00653bc commit 30ebeb9

File tree

63 files changed

+1096
-426
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1096
-426
lines changed
Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System.Collections.Generic;
2-
using System.Linq;
3-
using System.Reflection;
42
using System.Text.Json.Serialization;
53
using Json = System.Text.Json;
64
using System = System;
@@ -56,69 +54,3 @@ public override void Write(
5654
Json::JsonSerializer.Serialize(writer, value.Raw(), options);
5755
}
5856
}
59-
60-
sealed class UnionConverter<T> : JsonConverter<T>
61-
where T : class
62-
{
63-
readonly List<System::Type> _variantTypes = Enumerable.ToList(
64-
Enumerable.Where(
65-
Assembly.GetExecutingAssembly().GetTypes(),
66-
type => type.BaseType == typeof(T)
67-
)
68-
);
69-
70-
public override T? Read(
71-
ref Json::Utf8JsonReader reader,
72-
System::Type _typeToConvert,
73-
Json::JsonSerializerOptions options
74-
)
75-
{
76-
List<Json::JsonException> exceptions = [];
77-
foreach (var variantType in _variantTypes)
78-
{
79-
try
80-
{
81-
return Json::JsonSerializer.Deserialize(ref reader, variantType, options) as T;
82-
}
83-
catch (Json::JsonException e)
84-
{
85-
exceptions.Add(e);
86-
}
87-
}
88-
throw new System::AggregateException(exceptions);
89-
}
90-
91-
public override void Write(
92-
Json::Utf8JsonWriter writer,
93-
T value,
94-
Json::JsonSerializerOptions options
95-
)
96-
{
97-
var variantType =
98-
_variantTypes.Find(type => type == value.GetType())
99-
?? throw new System::ArgumentOutOfRangeException(value.GetType().Name);
100-
Json::JsonSerializer.Serialize(writer, value, variantType, options);
101-
}
102-
}
103-
104-
sealed class VariantConverter<TVariant, TValue> : JsonConverter<TVariant>
105-
where TVariant : IVariant<TVariant, TValue>
106-
{
107-
public override TVariant Read(
108-
ref Json::Utf8JsonReader reader,
109-
System::Type _typeToConvert,
110-
Json::JsonSerializerOptions options
111-
)
112-
{
113-
return TVariant.From(Json::JsonSerializer.Deserialize<TValue>(ref reader, options)!);
114-
}
115-
116-
public override void Write(
117-
Json::Utf8JsonWriter writer,
118-
TVariant value,
119-
Json::JsonSerializerOptions options
120-
)
121-
{
122-
Json::JsonSerializer.Serialize(writer, value.Value, options);
123-
}
124-
}
Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
using System.Net.Http;
2+
using Scrapegraphai = Scrapegraphai;
23
using System = System;
34

45
namespace Scrapegraphai.Models.Crawl;
56

67
/// <summary>
78
/// Retrieve the status and results of a crawling job
89
/// </summary>
9-
public sealed record class CrawlRetrieveResultsParams : ParamsBase
10+
public sealed record class CrawlRetrieveResultsParams : Scrapegraphai::ParamsBase
1011
{
1112
public required string TaskID;
1213

13-
public override System::Uri Url(IScrapegraphaiClient client)
14+
public override System::Uri Url(Scrapegraphai::IScrapegraphaiClient client)
1415
{
1516
return new System::UriBuilder(
1617
client.BaseUrl.ToString().TrimEnd('/') + string.Format("/crawl/{0}", this.TaskID)
@@ -20,12 +21,15 @@ public sealed record class CrawlRetrieveResultsParams : ParamsBase
2021
}.Uri;
2122
}
2223

23-
public void AddHeadersToRequest(HttpRequestMessage request, IScrapegraphaiClient client)
24+
public void AddHeadersToRequest(
25+
HttpRequestMessage request,
26+
Scrapegraphai::IScrapegraphaiClient client
27+
)
2428
{
25-
ParamsBase.AddDefaultHeaders(request, client);
29+
Scrapegraphai::ParamsBase.AddDefaultHeaders(request, client);
2630
foreach (var item in this.HeaderProperties)
2731
{
28-
ParamsBase.AddHeaderElementToRequest(request, item.Key, item.Value);
32+
Scrapegraphai::ParamsBase.AddHeaderElementToRequest(request, item.Key, item.Value);
2933
}
3034
}
3135
}

src/Scrapegraphai/Models/Crawl/CrawlRetrieveResultsResponse.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
using System.Text.Json.Serialization;
44
using CrawlRetrieveResultsResponseProperties = Scrapegraphai.Models.Crawl.CrawlRetrieveResultsResponseProperties;
55
using Json = System.Text.Json;
6+
using Scrapegraphai = Scrapegraphai;
67

78
namespace Scrapegraphai.Models.Crawl;
89

9-
[JsonConverter(typeof(ModelConverter<CrawlRetrieveResultsResponse>))]
10+
[JsonConverter(typeof(Scrapegraphai::ModelConverter<CrawlRetrieveResultsResponse>))]
1011
public sealed record class CrawlRetrieveResultsResponse
11-
: ModelBase,
12-
IFromRaw<CrawlRetrieveResultsResponse>
12+
: Scrapegraphai::ModelBase,
13+
Scrapegraphai::IFromRaw<CrawlRetrieveResultsResponse>
1314
{
1415
/// <summary>
1516
/// Successful crawl results
@@ -23,7 +24,7 @@ public sealed record class CrawlRetrieveResultsResponse
2324

2425
return Json::JsonSerializer.Deserialize<CrawlRetrieveResultsResponseProperties::Result?>(
2526
element,
26-
ModelBase.SerializerOptions
27+
Scrapegraphai::ModelBase.SerializerOptions
2728
);
2829
}
2930
set { this.Properties["result"] = Json::JsonSerializer.SerializeToElement(value); }
@@ -38,7 +39,7 @@ public sealed record class CrawlRetrieveResultsResponse
3839

3940
return Json::JsonSerializer.Deserialize<CrawlRetrieveResultsResponseProperties::Status?>(
4041
element,
41-
ModelBase.SerializerOptions
42+
Scrapegraphai::ModelBase.SerializerOptions
4243
);
4344
}
4445
set { this.Properties["status"] = Json::JsonSerializer.SerializeToElement(value); }
@@ -51,7 +52,10 @@ public string? TaskID
5152
if (!this.Properties.TryGetValue("task_id", out Json::JsonElement element))
5253
return null;
5354

54-
return Json::JsonSerializer.Deserialize<string?>(element, ModelBase.SerializerOptions);
55+
return Json::JsonSerializer.Deserialize<string?>(
56+
element,
57+
Scrapegraphai::ModelBase.SerializerOptions
58+
);
5559
}
5660
set { this.Properties["task_id"] = Json::JsonSerializer.SerializeToElement(value); }
5761
}
@@ -66,7 +70,10 @@ public string? Traceback
6670
if (!this.Properties.TryGetValue("traceback", out Json::JsonElement element))
6771
return null;
6872

69-
return Json::JsonSerializer.Deserialize<string?>(element, ModelBase.SerializerOptions);
73+
return Json::JsonSerializer.Deserialize<string?>(
74+
element,
75+
Scrapegraphai::ModelBase.SerializerOptions
76+
);
7077
}
7178
set { this.Properties["traceback"] = Json::JsonSerializer.SerializeToElement(value); }
7279
}

src/Scrapegraphai/Models/Crawl/CrawlRetrieveResultsResponseProperties/Result.cs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
using System.Collections.Generic;
12
using System.Text.Json.Serialization;
23
using Json = System.Text.Json;
34
using ResultVariants = Scrapegraphai.Models.Crawl.CrawlRetrieveResultsResponseProperties.ResultVariants;
5+
using System = System;
46

57
namespace Scrapegraphai.Models.Crawl.CrawlRetrieveResultsResponseProperties;
68

79
/// <summary>
810
/// Successful crawl results
911
/// </summary>
10-
[JsonConverter(typeof(UnionConverter<Result>))]
12+
[JsonConverter(typeof(ResultConverter))]
1113
public abstract record class Result
1214
{
1315
internal Result() { }
@@ -19,3 +21,56 @@ public static implicit operator Result(Json::JsonElement value) =>
1921

2022
public abstract void Validate();
2123
}
24+
25+
sealed class ResultConverter : JsonConverter<Result>
26+
{
27+
public override Result? Read(
28+
ref Json::Utf8JsonReader reader,
29+
System::Type _typeToConvert,
30+
Json::JsonSerializerOptions options
31+
)
32+
{
33+
List<Json::JsonException> exceptions = [];
34+
35+
try
36+
{
37+
var deserialized = Json::JsonSerializer.Deserialize<string>(ref reader, options);
38+
if (deserialized != null)
39+
{
40+
return new ResultVariants::String(deserialized);
41+
}
42+
}
43+
catch (Json::JsonException e)
44+
{
45+
exceptions.Add(e);
46+
}
47+
48+
try
49+
{
50+
return new ResultVariants::JsonElement(
51+
Json::JsonSerializer.Deserialize<Json::JsonElement>(ref reader, options)
52+
);
53+
}
54+
catch (Json::JsonException e)
55+
{
56+
exceptions.Add(e);
57+
}
58+
59+
throw new System::AggregateException(exceptions);
60+
}
61+
62+
public override void Write(
63+
Json::Utf8JsonWriter writer,
64+
Result value,
65+
Json::JsonSerializerOptions options
66+
)
67+
{
68+
object variant = value switch
69+
{
70+
ResultVariants::JsonElement(var jsonElement) => jsonElement,
71+
ResultVariants::String(var string1) => string1,
72+
_ => throw new System::ArgumentOutOfRangeException(nameof(value)),
73+
};
74+
Json::JsonSerializer.Serialize(writer, variant, options);
75+
}
76+
}

src/Scrapegraphai/Models/Crawl/CrawlRetrieveResultsResponseProperties/ResultVariants/All.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
using System.Text.Json.Serialization;
21
using Json = System.Text.Json;
32

43
namespace Scrapegraphai.Models.Crawl.CrawlRetrieveResultsResponseProperties.ResultVariants;
54

65
/// <summary>
76
/// Successful crawl results
87
/// </summary>
9-
[JsonConverter(typeof(VariantConverter<JsonElement, Json::JsonElement>))]
108
public sealed record class JsonElement(Json::JsonElement Value)
119
: Result,
1210
IVariant<JsonElement, Json::JsonElement>
@@ -22,7 +20,6 @@ public override void Validate() { }
2220
/// <summary>
2321
/// Error message
2422
/// </summary>
25-
[JsonConverter(typeof(VariantConverter<String, string>))]
2623
public sealed record class String(string Value) : Result, IVariant<String, string>
2724
{
2825
public static String From(string value)

src/Scrapegraphai/Models/Crawl/CrawlRetrieveResultsResponseProperties/Status.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System.Text.Json.Serialization;
2+
using Scrapegraphai = Scrapegraphai;
23
using System = System;
34

45
namespace Scrapegraphai.Models.Crawl.CrawlRetrieveResultsResponseProperties;
56

6-
[JsonConverter(typeof(EnumConverter<Status, string>))]
7-
public sealed record class Status(string value) : IEnum<Status, string>
7+
[JsonConverter(typeof(Scrapegraphai::EnumConverter<Status, string>))]
8+
public sealed record class Status(string value) : Scrapegraphai::IEnum<Status, string>
89
{
910
public static readonly Status Pending = new("PENDING");
1011

0 commit comments

Comments
 (0)