Compare commits

...
2 Commits
10 changed files with 90 additions and 46 deletions
@@ -144,6 +144,8 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.Compendium
using var writer = new StreamWriter(path);
schema.Beautify();
writer.WriteLine(name);
writer.WriteLine($"Different values: {schema.Values}");
writer.WriteLine("=====================================");
@@ -5,20 +5,17 @@ using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Pf2eDataScrapper.SchemaBuilder;
using Pf2eDataScrapper.SchemaBuilder.Types;
namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.Compendium
{
internal class CompendiumSchemaTypeResolver : TypeResolver
{
public class FoundryRulesSchemaType(SchemaNode parent) : ArraySchemaType(parent)
public class FoundryRulesSchemaType(SchemaNode parent) : IgnoreSchemaType(parent)
{
public override string GetTypeName()
{
return "###FoundryRulesObject###";
}
public override string ToString()
{
return GetTypeName();
return "ignored (Foundry rules object)";
}
}
@@ -34,32 +31,19 @@ namespace Pf2eDataScrapper.Importers.Implementations.Pf2eFoundry.Compendium
"Item.spell.system.heightening.damage"
];
protected bool ShouldBeDictionary(SchemaNode parent, JToken? value)
public override AbstractSchemaType Resolve(SchemaNode node, JToken? value)
{
//if (value is not JValue || value.Parent is not JProperty)
//{
// return false;
//}
//var p = (JProperty)value.Parent;
//return DictionaryPaths.Any(path => path == $"{parent.Path}.{p.Name}");
return DictionaryPaths.Any(path => path == parent.Path);
}
public override AbstractSchemaType Resolve(SchemaNode parent, JToken? value)
{
if (ShouldBeDictionary(parent, value))
if (CheckPath(node, DictionaryPaths))
{
return new DictionarySchemaType(parent);
return new DictionarySchemaType(node);
}
if (node.Path.EndsWith(".rules"))
{
return new FoundryRulesSchemaType(node);
}
if (parent.Path.EndsWith(".rules"))
{
return new FoundryRulesSchemaType(parent);
}
return base.Resolve(parent, value);
return base.Resolve(node, value);
}
}
}
+5 -1
View File
@@ -5,6 +5,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Pf2eDataScrapper.SchemaBuilder.Types;
namespace Pf2eDataScrapper.SchemaBuilder
{
@@ -98,7 +99,10 @@ namespace Pf2eDataScrapper.SchemaBuilder
public void Beautify()
{
foreach (var type in Types)
{
type.Beautify();
}
}
}
}
+28 -4
View File
@@ -4,11 +4,35 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Pf2eDataScrapper.SchemaBuilder.Types;
namespace Pf2eDataScrapper.SchemaBuilder
{
internal class TypeResolver
{
public bool CheckPath(SchemaNode node, string path)
{
if (!path.Contains("*"))
{
return path == node.Path;
}
var pathParts = path.Split('.');
var nodeParts = node.Path.Split('.');
if (pathParts.Length > nodeParts.Length)
{
return false;
}
return !pathParts.Where((t, i) => t != "*" && t != nodeParts[i]).Any();
}
public bool CheckPath(SchemaNode node, ICollection<string> paths)
{
return paths.Any(path => CheckPath(node, path));
}
public JTokenType TokenType(JToken? value)
{
if (value is null || value.Type is JTokenType.Null or JTokenType.Undefined or JTokenType.None)
@@ -19,7 +43,7 @@ namespace Pf2eDataScrapper.SchemaBuilder
return value.Type;
}
public virtual AbstractSchemaType Resolve(SchemaNode parent, JToken? value)
public virtual AbstractSchemaType Resolve(SchemaNode node, JToken? value)
{
var type = TokenType(value);
@@ -30,9 +54,9 @@ namespace Pf2eDataScrapper.SchemaBuilder
return type switch
{
JTokenType.Array => new ArraySchemaType(parent),
JTokenType.Object => new ObjectSchemaType(parent),
_ => new StaticSchemaType(parent, type)
JTokenType.Array => new ArraySchemaType(node),
JTokenType.Object => new ObjectSchemaType(node),
_ => new StaticSchemaType(node, type)
};
}
@@ -1,7 +1,7 @@
using System.ComponentModel.Design.Serialization;
using Newtonsoft.Json.Linq;
namespace Pf2eDataScrapper.SchemaBuilder
namespace Pf2eDataScrapper.SchemaBuilder.Types
{
internal abstract class AbstractSchemaType(SchemaNode parent)
{
@@ -32,7 +32,10 @@ namespace Pf2eDataScrapper.SchemaBuilder
/// <returns></returns>
public abstract string GetTypeName();
public abstract override string ToString();
public override string ToString()
{
return $"{GetTypeName()} {Parent.Path}";
}
/// <summary>
@@ -3,9 +3,10 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;
using Newtonsoft.Json.Linq;
namespace Pf2eDataScrapper.SchemaBuilder
namespace Pf2eDataScrapper.SchemaBuilder.Types
{
internal class ArraySchemaType(SchemaNode parent) : AbstractSchemaType(parent)
{
@@ -53,7 +54,7 @@ namespace Pf2eDataScrapper.SchemaBuilder
public override string ToString()
{
return string.Join("\n" + Tab, [
GetTypeName() + $" {{{Parent?.Path ?? ""}}}",
base.ToString(),
$"Length: min={MinCount} max={MaxCount}",
"Value type: " + Indent(ElementSchema.ToString())
]);
@@ -5,7 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace Pf2eDataScrapper.SchemaBuilder
namespace Pf2eDataScrapper.SchemaBuilder.Types
{
internal class DictionarySchemaType(SchemaNode parent) : AbstractSchemaType(parent)
{
@@ -42,7 +42,7 @@ namespace Pf2eDataScrapper.SchemaBuilder
public override string ToString()
{
return string.Join("\n" + Tab, [
GetTypeName() + $" {{{Parent?.Path ?? ""}}}",
base.ToString(),
"Key:" + Indent(KeyNode.ToString()),
"Value:" + Indent(ItemNode.ToString())
]);
@@ -0,0 +1,27 @@
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Pf2eDataScrapper.SchemaBuilder.Types
{
internal class IgnoreSchemaType(SchemaNode parent) : AbstractSchemaType(parent)
{
public override bool IsCompatibleValue(JToken value)
{
return true;
}
public override void AddValue(JToken value)
{
}
public override void Beautify()
{
}
public override string GetTypeName()
{
return "ignore";
}
}
}
@@ -5,7 +5,7 @@ using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Pf2eDataScrapper.SchemaBuilder
namespace Pf2eDataScrapper.SchemaBuilder.Types
{
internal class ObjectSchemaType(SchemaNode parent) : AbstractSchemaType(parent)
{
@@ -34,7 +34,7 @@ namespace Pf2eDataScrapper.SchemaBuilder
if (field is null)
{
field = new SchemaNode(this.Parent, pair.Key);
field = new SchemaNode(Parent, pair.Key);
Fields.Add(field);
}
@@ -67,11 +67,11 @@ namespace Pf2eDataScrapper.SchemaBuilder
public override string ToString()
{
List<string> rows = [$"object {{{Parent?.Path ?? ""}}}"];
List<string> rows = [base.ToString()];
foreach (var field in Fields)
{
string s = field.Name;
var s = field.Name;
if (field.IsNullable)
{
@@ -1,6 +1,6 @@
using Newtonsoft.Json.Linq;
namespace Pf2eDataScrapper.SchemaBuilder
namespace Pf2eDataScrapper.SchemaBuilder.Types
{
internal class StaticSchemaType(SchemaNode parent, JTokenType type) : AbstractSchemaType(parent)
{
@@ -46,7 +46,7 @@ namespace Pf2eDataScrapper.SchemaBuilder
public override string ToString()
{
var result = GetTypeName() + $" {{{Parent?.Path ?? ""}}}";
var result = base.ToString();
var count = Values.Count;
if (count == Root.Values)
@@ -68,7 +68,6 @@ namespace Pf2eDataScrapper.SchemaBuilder
return result.Trim();
}
result += "\n";
result = Values.Aggregate(result, (current, value) => current + $"{Tab}{value.Key} (x{value.Value})\n");