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

Source generator tweaks #262

Merged
merged 7 commits into from
Mar 22, 2021
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ public void Override(GeneratorSchemaClass c)
throw new ArgumentNullException(nameof(c));
}

var property = new GeneratorSchemaProperty(c, "query-input", "QueryInput")
{
Description = "Gets or sets the query input search parameter.",
};
var property = new GeneratorSchemaProperty(c, "query-input", "QueryInput", "Gets or sets the query input search parameter.");
property.Types.AddRange(
new List<GeneratorSchemaPropertyType>()
{
Expand Down
3 changes: 1 addition & 2 deletions Tools/Schema.NET.Tool/CustomOverrides/RenameEventProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ public void Override(GeneratorSchemaClass c)
.First(x => string.Equals(x.Name, "Event", StringComparison.OrdinalIgnoreCase));
c.Properties.Remove(eventProperty);

var updatedProperty = new GeneratorSchemaProperty(c, eventProperty.JsonName, "Events")
var updatedProperty = new GeneratorSchemaProperty(c, eventProperty.JsonName, "Events", eventProperty.Description)
{
Description = eventProperty.Description,
Order = eventProperty.Order,
};
updatedProperty.Types.AddRange(eventProperty.Types);
Expand Down
18 changes: 10 additions & 8 deletions Tools/Schema.NET.Tool/GeneratorModels/GeneratorSchemaClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ namespace Schema.NET.Tool.GeneratorModels
using Schema.NET.Tool.Constants;

[DebuggerDisplay("{Name}")]
#pragma warning disable CA1716 // Identifiers should not match keywords
public class GeneratorSchemaClass : GeneratorSchemaObject
#pragma warning restore CA1716 // Identifiers should not match keywords
{
public GeneratorSchemaClass(Uri id)
: base(string.Empty, string.Empty) => this.Id = id;
: this(layer: string.Empty, id, name: string.Empty, description: string.Empty)
{
}

public GeneratorSchemaClass(Uri id, string layer, string name)
: base(layer, name) => this.Id = id;
public GeneratorSchemaClass(string layer, Uri id, string name, string description, bool isCombined = false)
: base(layer, name, description)
{
this.Id = id;
this.IsCombined = isCombined;
}

public IEnumerable<GeneratorSchemaClass> Ancestors => EnumerableExtensions
.Traverse(this, x => x.Parents)
Expand All @@ -29,15 +33,13 @@ public GeneratorSchemaClass(Uri id, string layer, string name)
.Traverse(this, x => x.Children)
.Where(x => x != this);

public string? Description { get; set; }

public Uri Id { get; }

public bool IsArchived => EnumerableExtensions
.Traverse(this, x => x.Parents)
.Any(x => string.Equals(x.Layer, LayerName.Archived, StringComparison.Ordinal));

public bool IsCombined { get; set; }
public bool IsCombined { get; }

public IEnumerable<GeneratorSchemaProperty> DeclaredProperties
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ namespace Schema.NET.Tool.GeneratorModels
using System.Diagnostics;

[DebuggerDisplay("{Name}")]
#pragma warning disable CA1724 // Identifiers should conflict with namespaces
public class GeneratorSchemaEnumeration : GeneratorSchemaObject
#pragma warning restore CA1724 // Identifiers should conflict with namespaces
{
public GeneratorSchemaEnumeration(string layer, string name)
: base(layer, name)
public GeneratorSchemaEnumeration(string layer, string name, string description)
: base(layer, name, description)
{
}

public string? Description { get; set; }

public List<GeneratorSchemaEnumerationValue> Values { get; } = new List<GeneratorSchemaEnumerationValue>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ namespace Schema.NET.Tool.GeneratorModels
[DebuggerDisplay("{Name}")]
public class GeneratorSchemaEnumerationValue
{
public GeneratorSchemaEnumerationValue(string name, Uri uri)
public GeneratorSchemaEnumerationValue(string name, Uri uri, string description)
{
this.Name = name;
this.Uri = uri;
this.Description = description;
}

public string? Description { get; set; }
public string Description { get; }

public string Name { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ namespace Schema.NET.Tool.GeneratorModels
{
public class GeneratorSchemaObject
{
public GeneratorSchemaObject(string layer, string name)
public GeneratorSchemaObject(string layer, string name, string description)
{
this.Layer = layer;
this.Name = name;
this.Description = description;
}

public string Description { get; }

public string Layer { get; }

public string Name { get; }
Expand Down
10 changes: 4 additions & 6 deletions Tools/Schema.NET.Tool/GeneratorModels/GeneratorSchemaProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,19 @@ namespace Schema.NET.Tool.GeneratorModels
using System.Linq;

[DebuggerDisplay("{Name}")]
#pragma warning disable CA1716 // Identifiers should not match keywords
public class GeneratorSchemaProperty
#pragma warning restore CA1716 // Identifiers should not match keywords
{
public GeneratorSchemaProperty(GeneratorSchemaClass @class, string jsonName, string name)
public GeneratorSchemaProperty(GeneratorSchemaClass @class, string jsonName, string name, string description)
{
this.Class = @class;
this.JsonName = jsonName;
this.Name = name;
this.Description = description;
}

public GeneratorSchemaClass Class { get; }

public string? Description { get; set; }
public string Description { get; }

public string JsonName { get; }

Expand Down Expand Up @@ -66,9 +65,8 @@ public string JsonConverterType

public GeneratorSchemaProperty Clone(GeneratorSchemaClass context)
{
var property = new GeneratorSchemaProperty(context, this.JsonName, this.Name)
var property = new GeneratorSchemaProperty(context, this.JsonName, this.Name, this.Description)
{
Description = this.Description,
Order = this.Order,
};
property.Types.AddRange(this.Types.Select(x => x.Clone()));
Expand Down
4 changes: 2 additions & 2 deletions Tools/Schema.NET.Tool/Models/SchemaClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ public class SchemaClass : SchemaObject
{
private static readonly Uri EnumerationId = new("https://schema.org/Enumeration");

public SchemaClass(Uri id, string label, string layer)
: base(id, label, layer)
public SchemaClass(string layer, Uri id, string label, string comment)
: base(layer, id, label, comment)
{
}

Expand Down
4 changes: 2 additions & 2 deletions Tools/Schema.NET.Tool/Models/SchemaEnumerationValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace Schema.NET.Tool.Models

public class SchemaEnumerationValue : SchemaObject
{
public SchemaEnumerationValue(Uri id, string label, string layer)
: base(id, label, layer)
public SchemaEnumerationValue(string layer, Uri id, string label, string comment)
: base(layer, id, label, comment)
{
}
}
Expand Down
9 changes: 5 additions & 4 deletions Tools/Schema.NET.Tool/Models/SchemaObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,22 @@ public abstract class SchemaObject
"PronounceableText",
};

public SchemaObject(Uri id, string label, string layer)
public SchemaObject(string layer, Uri id, string label, string comment)
{
this.Layer = layer;
this.Id = id;
this.Label = label;
this.Layer = layer;
this.Comment = comment;
}

public string Comment { get; }

public Uri Id { get; }

public string Label { get; }

public string Layer { get; }

public string? Comment { get; set; }

public List<string> Types { get; } = new List<string>();

public virtual bool IsArchived => string.Equals(this.Layer, LayerName.Archived, StringComparison.OrdinalIgnoreCase);
Expand Down
4 changes: 2 additions & 2 deletions Tools/Schema.NET.Tool/Models/SchemaProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace Schema.NET.Tool.Models

public class SchemaProperty : SchemaObject
{
public SchemaProperty(Uri id, string label, string layer)
: base(id, label, layer)
public SchemaProperty(string layer, Uri id, string label, string comment)
: base(layer, id, label, comment)
{
}

Expand Down
34 changes: 15 additions & 19 deletions Tools/Schema.NET.Tool/Repositories/SchemaPropertyJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,17 @@ public override void Write(Utf8JsonWriter writer, List<SchemaObject> value, Json
var id = SchemaOrgUrl(idToken.GetString()!);
var types = GetTokenValues(token, "@type").ToArray();

string? comment;
string comment;
if (commentToken.ValueKind == JsonValueKind.Object && commentToken.TryGetProperty("@value", out var commentValueToken))
{
comment = commentValueToken.GetString();
comment = commentValueToken.GetString()!;
}
else
{
comment = commentToken.GetString();
comment = commentToken.GetString()!;
}

var label = GetLabel(token);
var domainIncludes = GetTokenValues(token, "schema:domainIncludes", "@id").Select(SchemaOrgUrl).ToArray();
var rangeIncludes = GetTokenValues(token, "schema:rangeIncludes", "@id").Select(SchemaOrgUrl).ToArray();
var subClassOf = GetTokenValues(token, "rdfs:subClassOf", "@id").Select(SchemaOrgUrl).ToArray();
var isPartOf = GetTokenValues(token, "schema:isPartOf", "@id").Select(s => new Uri(s)).FirstOrDefault();

var layer = LayerName.Core;
Expand All @@ -75,32 +72,31 @@ public override void Write(Utf8JsonWriter writer, List<SchemaObject> value, Json

if (types.Any(type => string.Equals(type, "rdfs:Class", StringComparison.Ordinal)))
{
var schemaClass = new SchemaClass(id, label, layer)
{
Comment = comment,
};
var schemaClass = new SchemaClass(layer, id, label, comment);

var subClassOf = GetTokenValues(token, "rdfs:subClassOf", "@id").Select(SchemaOrgUrl);
schemaClass.SubClassOfIds.AddRange(subClassOf);

schemaClass.Types.AddRange(types);
return schemaClass;
}
else if (types.Any(type => string.Equals(type, "rdf:Property", StringComparison.Ordinal)))
{
var schemaProperty = new SchemaProperty(id, label, layer)
{
Comment = comment,
};
var schemaProperty = new SchemaProperty(layer, id, label, comment);

var domainIncludes = GetTokenValues(token, "schema:domainIncludes", "@id").Select(SchemaOrgUrl);
schemaProperty.DomainIncludes.AddRange(domainIncludes);

var rangeIncludes = GetTokenValues(token, "schema:rangeIncludes", "@id").Select(SchemaOrgUrl);
schemaProperty.RangeIncludes.AddRange(rangeIncludes);

schemaProperty.Types.AddRange(types);
return schemaProperty;
}
else
{
var schemaEnumerationValue = new SchemaEnumerationValue(id, label, layer)
{
Comment = comment,
};
schemaEnumerationValue.Types.AddRange(types.Select(SchemaOrgUrl).Select(u => u.ToString()).ToArray());
var schemaEnumerationValue = new SchemaEnumerationValue(layer, id, label, comment);
schemaEnumerationValue.Types.AddRange(types.Select(SchemaOrgUrl).Select(u => u.ToString()));
return schemaEnumerationValue;
}
}
Expand Down
11 changes: 6 additions & 5 deletions Tools/Schema.NET.Tool/Repositories/SchemaRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace Schema.NET.Tool.Repositories
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text.Json;
Expand Down Expand Up @@ -35,23 +36,23 @@ public class SchemaRepository : ISchemaRepository
public async Task<IEnumerable<SchemaObject>?> GetSchemaObjectsAsync()
{
using (var response = await this.httpClient
.GetAsync(new Uri("/version/latest/schemaorg-all-https.jsonld", UriKind.Relative))
.GetAsync(new Uri("/version/latest/schemaorg-all-https.jsonld", UriKind.Relative), HttpCompletionOption.ResponseHeadersRead)
.ConfigureAwait(false))
{
response.EnsureSuccessStatusCode();
var json = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
return Deserialize<List<SchemaObject>>(json, new SchemaPropertyJsonConverter());
var jsonStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
return await DeserializeAsync<List<SchemaObject>>(jsonStream, new SchemaPropertyJsonConverter()).ConfigureAwait(false);
}
}

private static T? Deserialize<T>(string json, JsonConverter converter)
private static async Task<T?> DeserializeAsync<T>(Stream jsonStream, JsonConverter converter)
{
var options = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
options.Converters.Add(converter);
return JsonSerializer.Deserialize<T>(json, options);
return await JsonSerializer.DeserializeAsync<T>(jsonStream, options).ConfigureAwait(false);
}
}
}
51 changes: 28 additions & 23 deletions Tools/Schema.NET.Tool/SchemaSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace Schema.NET.Tool
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Schema.NET.Tool.CustomOverrides;
using Schema.NET.Tool.GeneratorModels;
Expand All @@ -14,41 +13,47 @@ namespace Schema.NET.Tool
[Generator]
public class SchemaSourceGenerator : ISourceGenerator
{
private IEnumerable<GeneratorSchemaObject>? SchemaObjects { get; set; }
private static readonly object SchemaLock = new();

private static IEnumerable<GeneratorSchemaObject>? SchemaObjects { get; set; }

public void Initialize(GeneratorInitializationContext context)
{
#pragma warning disable IDE0022 // Use expression body for methods
Task.Run(async () =>
var schemaRepository = new SchemaRepository(new HttpClient()
{
BaseAddress = new Uri("https://schema.org"),
});
var schemaService = new SchemaService(
new IClassOverride[]
{
new AddQueryInputPropertyToSearchAction(),
new AddTextTypeToActionTarget(),
new AddNumberTypeToMediaObjectHeightAndWidth(),
new RenameEventProperty(),
},
Array.Empty<IEnumerationOverride>(),
schemaRepository,
false);

if (SchemaObjects is null)
{
var schemaRepository = new SchemaRepository(new HttpClient()
lock (SchemaLock)
{
BaseAddress = new Uri("https://schema.org"),
});
var schemaService = new SchemaService(
new IClassOverride[]
if (SchemaObjects is null)
{
new AddQueryInputPropertyToSearchAction(),
new AddTextTypeToActionTarget(),
new AddNumberTypeToMediaObjectHeightAndWidth(),
new RenameEventProperty(),
},
Array.Empty<IEnumerationOverride>(),
schemaRepository,
false);

this.SchemaObjects = await schemaService.GetObjectsAsync().ConfigureAwait(false);
#pragma warning disable VSTHRD002 // Avoid problematic synchronous waits
}).GetAwaiter().GetResult();
#pragma warning restore IDE0022 // Use expression body for methods
SchemaObjects = schemaService.GetObjectsAsync().GetAwaiter().GetResult();
#pragma warning restore VSTHRD002 // Avoid problematic synchronous waits
}
}
}
}

public void Execute(GeneratorExecutionContext context)
{
if (this.SchemaObjects is not null)
if (SchemaObjects is not null)
{
foreach (var schemaObject in this.SchemaObjects)
foreach (var schemaObject in SchemaObjects)
{
var source = string.Empty;
if (schemaObject is GeneratorSchemaClass schemaClass)
Expand Down
Loading