-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactored out System.Text.Json from ElementDataProvider.cs using Sou…
…rceGeneration
- Loading branch information
1 parent
9dd75d6
commit 1a27287
Showing
10 changed files
with
189 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
Generators/ChemSharp.Molecules.Generator/ElementProviderGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
using System.Collections.ObjectModel; | ||
using System.Text; | ||
using System.Text.Json; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Text; | ||
|
||
namespace ChemSharp.Molecules.Generator; | ||
|
||
[Generator] | ||
public class ElementProviderGenerator : ISourceGenerator | ||
{ | ||
private const string Indent = " "; | ||
|
||
/// <summary> | ||
/// Where the colorData is located | ||
/// </summary> | ||
private const string ColorSource = "ChemSharp.Molecules.Generator.Resources.colorData.txt"; | ||
|
||
/// <summary> | ||
/// Where the element data is located | ||
/// </summary> | ||
private const string ApiSource = "ChemSharp.Molecules.Generator.Resources.elements.json"; | ||
|
||
public void Initialize(GeneratorInitializationContext context) | ||
{ | ||
_elementData = EnsureApiData(); | ||
_colorData = EnsureColorData(); | ||
} | ||
public void Execute(GeneratorExecutionContext context) | ||
{ | ||
var sb = new StringBuilder(); | ||
sb.AppendLine("using System.Collections.Generic;"); | ||
sb.Append(@$"namespace ChemSharp.Molecules{{ | ||
public static class ElementDataProvider{{ | ||
public static Dictionary<string, string> ColorData => _colorData; | ||
public static Element[] ElementData => _elementData; | ||
private static Element[] _elementData = new Element[]{{ | ||
"); | ||
foreach (var jsonElement in _elementData) | ||
{ | ||
sb.AppendLine(Space(3) + "new Element(){"); | ||
sb.AppendLine(Space(4) + $"Name = \"{jsonElement.TryGetString("Name")}\","); | ||
sb.AppendLine(Space(4) + $"Symbol = \"{jsonElement.TryGetString("Symbol")}\","); | ||
sb.AppendLine(Space(4) + $"Appearance = \"{jsonElement.TryGetString("Appearance")}\","); | ||
sb.AppendLine(Space(4) + $"AtomicWeight = {jsonElement.TryGetDouble("AtomicWeight")},"); | ||
sb.AppendLine(Space(4) + $"AtomicNumber = {jsonElement.TryGetInt("AtomicNumber")},"); | ||
sb.AppendLine(Space(4) + $"Group = {jsonElement.TryGetInt("Group")},"); | ||
sb.AppendLine(Space(4) + $"Period = {jsonElement.TryGetInt("Period")},"); | ||
sb.AppendLine(Space(4) + $"Block = \"{jsonElement.TryGetString("Block")}\","); | ||
sb.AppendLine(Space(4) + $"Category = \"{jsonElement.TryGetString("Category")}\","); | ||
sb.AppendLine( | ||
Space(4) + $"ElectronConfiguration = \"{jsonElement.TryGetString("ElectronConfiguration")}\","); | ||
sb.AppendLine(Space(4) + $"Electronegativity = {jsonElement.TryGetDouble("Electronegativity")},"); | ||
sb.AppendLine(Space(4) + $"CovalentRadius = {jsonElement.TryGetInt("CovalentRadius")},"); | ||
sb.AppendLine(Space(4) + $"AtomicRadius = {jsonElement.TryGetInt("AtomicRadius")},"); | ||
sb.AppendLine(Space(4) + $"VdWRadius = {jsonElement.TryGetInt("VdWRadius")},"); | ||
sb.AppendLine(Space(4) + $"CAS = \"{jsonElement.TryGetString("CAS")}\","); | ||
sb.AppendLine(Space(3) + "},"); | ||
} | ||
|
||
sb.Append(@$" | ||
}}; | ||
private static Dictionary<string, string> _colorData = new Dictionary<string,string>(){{ | ||
"); | ||
foreach (var kvp in _colorData) sb.AppendLine(Space(3) + $"{{\"{kvp.Key}\",\"{kvp.Value}\"}},"); | ||
sb.Append(@$" | ||
}}; | ||
}} | ||
}}"); | ||
context.AddSource($"ElementDataProvider_generated", SourceText.From(sb.ToString(), Encoding.UTF8)); | ||
} | ||
|
||
private static JsonElement[] EnsureApiData() | ||
{ | ||
var raw = ResourceUtil.ReadResourceString(ApiSource); | ||
return JsonSerializer.Deserialize<JsonElement[]>(raw)!; | ||
} | ||
/// <summary> | ||
/// Loads Color Data from ColorSource | ||
/// </summary> | ||
/// <returns></returns> | ||
private static ReadOnlyDictionary<string, string> EnsureColorData() | ||
{ | ||
var data = ResourceUtil.ReadResourceString(ColorSource); | ||
var dic = data.Split(new[] {"\n", "\r\n", "\r"}, StringSplitOptions.RemoveEmptyEntries) | ||
.Select(line => line.Split(',')) | ||
.ToDictionary(columns => columns[0], columns => columns[1]); | ||
return new ReadOnlyDictionary<string, string>(dic); | ||
} | ||
|
||
private static string Space(int spacers) | ||
{ | ||
var res = new StringBuilder(); | ||
for (var i = 0; i < spacers; i++) | ||
res.Append(Indent); | ||
return res.ToString(); | ||
} | ||
|
||
#pragma warning disable CS8618 // Ein Non-Nullable-Feld muss beim Beenden des Konstruktors einen Wert ungleich NULL enthalten. Erwägen Sie die Deklaration als Nullable. | ||
private JsonElement[] _elementData; | ||
private ReadOnlyDictionary<string, string> _colorData; | ||
#pragma warning restore CS8618 // Ein Non-Nullable-Feld muss beim Beenden des Konstruktors einen Wert ungleich NULL enthalten. Erwägen Sie die Deklaration als Nullable. | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System.Globalization; | ||
using System.Text.Json; | ||
|
||
namespace ChemSharp.Molecules.Generator; | ||
|
||
public static class JsonTools | ||
{ | ||
public static string TryGetString(this JsonElement jsonObject, string propertyName) | ||
{ | ||
if (!jsonObject.TryGetProperty(propertyName, out var value)) | ||
return ""; | ||
return value.GetString() ?? ""; | ||
} | ||
|
||
public static string TryGetInt(this JsonElement jsonObject, string propertyName) | ||
{ | ||
if (!jsonObject.TryGetProperty(propertyName, out var value)) | ||
return 0.ToString(CultureInfo.InvariantCulture); | ||
if (value.ValueKind != JsonValueKind.Number) | ||
return 0.ToString(CultureInfo.InvariantCulture); | ||
if (!value.TryGetInt32(out var @int)) | ||
return 0.ToString(CultureInfo.InvariantCulture); | ||
return @int.ToString(CultureInfo.InvariantCulture); | ||
} | ||
|
||
public static string TryGetDouble(this JsonElement jsonObject, string propertyName) | ||
{ | ||
if (!jsonObject.TryGetProperty(propertyName, out var value)) | ||
return 0d.ToString(CultureInfo.InvariantCulture); | ||
if (value.ValueKind != JsonValueKind.Number) | ||
return 0d.ToString(CultureInfo.InvariantCulture); | ||
if (!value.TryGetDouble(out var @double)) | ||
return 0d.ToString(CultureInfo.InvariantCulture); | ||
return @double.ToString(CultureInfo.InvariantCulture); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System.Reflection; | ||
|
||
namespace ChemSharp.Molecules.Generator; | ||
|
||
public static class ResourceUtil | ||
{ | ||
/// <summary> | ||
/// Loads ResourceStream | ||
/// </summary> | ||
/// <param name="resourceName"></param> | ||
/// <returns></returns> | ||
public static Stream LoadResource(string resourceName) => Assembly.GetAssembly(typeof(ResourceUtil)) | ||
?.GetManifestResourceStream(resourceName); | ||
|
||
/// <summary> | ||
/// Reads a String from Resource | ||
/// </summary> | ||
/// <param name="path"></param> | ||
/// <returns></returns> | ||
public static string ReadResourceString(string path) | ||
{ | ||
StreamReader sr; | ||
if (path.Contains("ChemSharp.Molecules.Generator.Resources")) //resource loading | ||
{ | ||
var stream = LoadResource(path); | ||
sr = new StreamReader(stream); | ||
} | ||
else | ||
sr = new StreamReader(path); | ||
|
||
var data = sr.ReadToEnd(); | ||
sr.Close(); | ||
return data; | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters