Add To Body Schema

This commit is contained in:
Ben C 2022-08-29 08:04:09 -04:00 committed by GitHub
parent 135aae9974
commit 60d2682229
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,96 +1,97 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using NewHorizons.External.Configs; using NewHorizons.External.Configs;
using NJsonSchema; using NJsonSchema;
using NJsonSchema.Generation; using NJsonSchema.Generation;
namespace SchemaExporter; namespace SchemaExporter;
public static class SchemaExporter public static class SchemaExporter
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
const string folderName = "NewHorizons/Schemas"; const string folderName = "NewHorizons/Schemas";
Directory.CreateDirectory(folderName); Directory.CreateDirectory(folderName);
Console.WriteLine("Schema Generator: We're winning!"); Console.WriteLine("Schema Generator: We're winning!");
var settings = new JsonSchemaGeneratorSettings var settings = new JsonSchemaGeneratorSettings
{ {
IgnoreObsoleteProperties = true, IgnoreObsoleteProperties = true,
DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.NotNull, DefaultReferenceTypeNullHandling = ReferenceTypeNullHandling.NotNull,
FlattenInheritanceHierarchy = true, FlattenInheritanceHierarchy = true,
AllowReferencesWithProperties = true AllowReferencesWithProperties = true
}; };
var bodySchema = new Schema<PlanetConfig>("Celestial Body Schema", "Schema for a celestial body in New Horizons", $"{folderName}/body_schema", settings); var bodySchema = new Schema<PlanetConfig>("Celestial Body Schema", "Schema for a celestial body in New Horizons", $"{folderName}/body_schema", settings);
bodySchema.Output(); bodySchema.Output();
var systemSchema = var systemSchema =
new Schema<StarSystemConfig>("Star System Schema", "Schema for a star system in New Horizons", $"{folderName}/star_system_schema", settings); new Schema<StarSystemConfig>("Star System Schema", "Schema for a star system in New Horizons", $"{folderName}/star_system_schema", settings);
systemSchema.Output(); systemSchema.Output();
var addonSchema = new Schema<AddonConfig>("Addon Manifest Schema", var addonSchema = new Schema<AddonConfig>("Addon Manifest Schema",
"Schema for an addon manifest in New Horizons", $"{folderName}/addon_manifest_schema", settings); "Schema for an addon manifest in New Horizons", $"{folderName}/addon_manifest_schema", settings);
addonSchema.Output(); addonSchema.Output();
var translationSchema = var translationSchema =
new Schema<TranslationConfig>("Translation Schema", "Schema for a translation file in New Horizons", $"{folderName}/translation_schema", settings); new Schema<TranslationConfig>("Translation Schema", "Schema for a translation file in New Horizons", $"{folderName}/translation_schema", settings);
translationSchema.Output(); translationSchema.Output();
Console.WriteLine("Done!"); Console.WriteLine("Done!");
} }
private readonly struct Schema<T> private readonly struct Schema<T>
{ {
private readonly JsonSchemaGeneratorSettings _generatorSettings; private readonly JsonSchemaGeneratorSettings _generatorSettings;
private readonly string _title, _description; private readonly string _title, _description;
private readonly string _outFileName; private readonly string _outFileName;
public Schema(string schemaTitle, string schemaDescription, string fileName, JsonSchemaGeneratorSettings settings) public Schema(string schemaTitle, string schemaDescription, string fileName, JsonSchemaGeneratorSettings settings)
{ {
_title = schemaTitle; _title = schemaTitle;
_description = schemaDescription; _description = schemaDescription;
_outFileName = fileName; _outFileName = fileName;
_generatorSettings = settings; _generatorSettings = settings;
} }
public void Output() public void Output()
{ {
Console.WriteLine($"Outputting {_title}"); Console.WriteLine($"Outputting {_title}");
File.WriteAllText($"{_outFileName}.json", ToString()); File.WriteAllText($"{_outFileName}.json", ToString());
} }
public override string ToString() public override string ToString()
{ {
return GetJsonSchema().ToJson(); return GetJsonSchema().ToJson();
} }
private JsonSchema GetJsonSchema() private JsonSchema GetJsonSchema()
{ {
var schema = JsonSchema.FromType<T>(_generatorSettings); var schema = JsonSchema.FromType<T>(_generatorSettings);
schema.Title = _title; schema.Title = _title;
var schemaLinkProp = new JsonSchemaProperty var schemaLinkProp = new JsonSchemaProperty
{ {
Type = JsonObjectType.String, Type = JsonObjectType.String,
Description = "The schema to validate with" Description = "The schema to validate with"
}; };
schema.Properties.Add("$schema", schemaLinkProp); schema.Properties.Add("$schema", schemaLinkProp);
schema.ExtensionData ??= new Dictionary<string, object>(); schema.ExtensionData ??= new Dictionary<string, object>();
schema.ExtensionData.Add("$docs", new Dictionary<string, object> schema.ExtensionData.Add("$docs", new Dictionary<string, object>
{ {
{"title", _title}, {"title", _title},
{"description", _description} {"description", _description}
}); });
if (_title == "Celestial Body Schema") if (_title == "Celestial Body Schema")
{ {
schema.Definitions["OrbitModule"].Properties["semiMajorAxis"].Default = 5000f; schema.Definitions["OrbitModule"].Properties["semiMajorAxis"].Default = 5000f;
} schema.Properties.Add("extras", new Dictionary<string, object>());
}
if (_title == "Star System Schema")
{ if (_title == "Star System Schema")
schema.Definitions["NomaiCoordinates"].Properties["x"].UniqueItems = true; {
schema.Definitions["NomaiCoordinates"].Properties["y"].UniqueItems = true; schema.Definitions["NomaiCoordinates"].Properties["x"].UniqueItems = true;
schema.Definitions["NomaiCoordinates"].Properties["z"].UniqueItems = true; schema.Definitions["NomaiCoordinates"].Properties["y"].UniqueItems = true;
} schema.Definitions["NomaiCoordinates"].Properties["z"].UniqueItems = true;
}
return schema;
} return schema;
} }
}
} }