65 lines
1.7 KiB
C#
65 lines
1.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
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)
|
|
{
|
|
return JTokenType.Null;
|
|
}
|
|
|
|
return value.Type;
|
|
}
|
|
|
|
public virtual AbstractSchemaType Resolve(SchemaNode node, JToken? value)
|
|
{
|
|
var type = TokenType(value);
|
|
|
|
if (type == JTokenType.Null)
|
|
{
|
|
throw new Exception("Value is null, cannot determine type");
|
|
}
|
|
|
|
return type switch
|
|
{
|
|
JTokenType.Array => new ArraySchemaType(node),
|
|
JTokenType.Object => new ObjectSchemaType(node),
|
|
_ => new StaticSchemaType(node, type)
|
|
};
|
|
}
|
|
|
|
}
|
|
}
|