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

Fix/発生させる例外をせんようの型にする #32

Merged
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
5 changes: 0 additions & 5 deletions mooresmaster.Generator/CodeGenerate/CodeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ public static CodeFile[] Generate(Definition definition)
files[interfaceDefinition.FileName].Add(GenerateInterfaceCode(interfaceDefinition));
}

// $$$"""
// {{{string.Join("\n", definition.TypeDefinitions.Select(GenerateTypeDefinitionCode))}}}
// {{{string.Join("\n", definition.InterfaceDefinitions.Select(GenerateInterfaceCode))}}}
// """

return files
.Select(file =>
new CodeFile(
Expand Down
2 changes: 1 addition & 1 deletion mooresmaster.Generator/Definitions/Definition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public record TypeDefinition(string FileName, TypeName TypeName, TypeName[] Inhe
public TypeName TypeName = TypeName;
}

public record struct PropertyDefinition(Type Type, PropertyId? PropertyId, bool IsNullable, string[]? Enums);
public record struct PropertyDefinition(Type Type, string PropertyName, PropertyId? PropertyId, bool IsNullable, string[]? Enums);

public record struct InterfacePropertyDefinition(Type Type);
16 changes: 8 additions & 8 deletions mooresmaster.Generator/Definitions/DefinitionGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,19 @@ private static Dictionary<string, PropertyDefinition> GetProperties(NameTable na
switch (typeSemantics.Schema)
{
case ArraySchema arraySchema:
propertyTable["items"] = new PropertyDefinition(new ArrayType(Type.GetType(nameTable, semantics.SchemaTypeSemanticsTable[table.Table[arraySchema.Items]], table.Table[arraySchema.Items], semantics, table)), null, arraySchema.IsNullable, null);
propertyTable["items"] = new PropertyDefinition(new ArrayType(Type.GetType(nameTable, semantics.SchemaTypeSemanticsTable[table.Table[arraySchema.Items]], table.Table[arraySchema.Items], semantics, table)), "items", null, arraySchema.IsNullable, null);
break;
case BooleanSchema:
propertyTable["value"] = new PropertyDefinition(new BooleanType(), null, typeSemantics.Schema.IsNullable, null);
propertyTable["value"] = new PropertyDefinition(new BooleanType(), "value", null, typeSemantics.Schema.IsNullable, null);
break;
case IntegerSchema:
propertyTable["value"] = new PropertyDefinition(new IntType(), null, typeSemantics.Schema.IsNullable, null);
propertyTable["value"] = new PropertyDefinition(new IntType(), "value", null, typeSemantics.Schema.IsNullable, null);
break;
case NumberSchema:
propertyTable["value"] = new PropertyDefinition(new FloatType(), null, typeSemantics.Schema.IsNullable, null);
propertyTable["value"] = new PropertyDefinition(new FloatType(), "value", null, typeSemantics.Schema.IsNullable, null);
break;
case StringSchema stringSchema:
propertyTable["value"] = new PropertyDefinition(new StringType(), null, typeSemantics.Schema.IsNullable, stringSchema.Enums);
propertyTable["value"] = new PropertyDefinition(new StringType(), "value", null, typeSemantics.Schema.IsNullable, stringSchema.Enums);
break;
case ObjectSchema:
foreach (var propertyId in typeSemantics.Properties)
Expand All @@ -144,15 +144,15 @@ private static Dictionary<string, PropertyDefinition> GetProperties(NameTable na
string[]? enums = null;
if (schema is StringSchema stringSchema) enums = stringSchema.Enums;

propertyTable[name] = new PropertyDefinition(Type.GetType(nameTable, propertyTypeId, schema, semantics, table), propertyId, typeSemantics.Schema.IsNullable, enums);
propertyTable[name] = new PropertyDefinition(Type.GetType(nameTable, propertyTypeId, schema, semantics, table), name, propertyId, typeSemantics.Schema.IsNullable, enums);
}

break;
case SwitchSchema:
propertyTable["value"] = new PropertyDefinition(new CustomType(nameTable.TypeNames[classId]), null, typeSemantics.Schema.IsNullable, null);
propertyTable["value"] = new PropertyDefinition(new CustomType(nameTable.TypeNames[classId]), "value", null, typeSemantics.Schema.IsNullable, null);
break;
case RefSchema refSchema:
propertyTable["value"] = new PropertyDefinition(new CustomType(refSchema.GetRefName()), null, typeSemantics.Schema.IsNullable, null);
propertyTable["value"] = new PropertyDefinition(new CustomType(refSchema.GetRefName()), "value", null, typeSemantics.Schema.IsNullable, null);
break;
default:
throw new ArgumentOutOfRangeException(nameof(typeSemantics.Schema));
Expand Down
59 changes: 42 additions & 17 deletions mooresmaster.Generator/LoaderGenerate/LoaderGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,25 +144,18 @@

foreach (var propertyDefinition in typeDefinition.PropertyTable.Values.Where(v => v.PropertyId.HasValue).Where(v => !semantics.PropertySemanticsTable[v.PropertyId!.Value].IsNullable))
{
var propertyName = semantics.PropertySemanticsTable[propertyDefinition.PropertyId!.Value].PropertyName;
var jsonPropertyName = semantics.PropertySemanticsTable[propertyDefinition.PropertyId!.Value].PropertyName;
var typeName = typeDefinition.TypeName.GetModelName();

builder.AppendLine(
$$$"""
if (json["{{{propertyName}}}"] == null)
{
var errorMessage = $"SchemaLoadError\nErrorPath: {json.Path}\nTargetProperty: {{{propertyName}}}\n\n";

var parent = json.Parent;
while (parent != null)
{
// errorMessage += parent.ToString() + "\n";
parent = parent.Parent;
}

throw new global::System.Exception(errorMessage);
}

"""
$$$$"""
if (json["{{{{jsonPropertyName}}}}"] == null)
{
var propertyPath = json.Parent == null ? "{{{{jsonPropertyName}}}}" : $"{json.Path}.{{{{jsonPropertyName}}}}";
throw new global::Mooresmaster.Loader.MooresmasterLoaderException(propertyPath, typeof({{{{typeName}}}}).Name, "{{{{propertyDefinition.PropertyName}}}}");
}

"""
);
}

Expand Down Expand Up @@ -231,7 +224,7 @@
.Select(property => $"{property.Value.Type.GetName()} {property.Key} = {GeneratePropertyLoaderCode(
property.Value.Type,
$$$"""
json["{{{semantics.PropertySemanticsTable[property.Value.PropertyId.Value].PropertyName}}}"]

Check warning on line 227 in mooresmaster.Generator/LoaderGenerate/LoaderGenerator.cs

View workflow job for this annotation

GitHub Actions / build

Nullable value type may be null.
""")};"
)
);
Expand Down Expand Up @@ -372,6 +365,38 @@
""".GetPreprocessedCode();
}

public static string GenerateLoaderExceptionTypeCode()
{
return """"
namespace Mooresmaster.Loader
{
public class MooresmasterLoaderException : Exception
{
public string ErrorProperty;
public string ErrorType;
public string PropertyPath;

public MooresmasterLoaderException(string propertyPath, string errorType, string errorProperty)
{
PropertyPath = propertyPath;
ErrorType = errorType;
ErrorProperty = errorProperty;
}

public override string Message => ToString();

public override string ToString()
{
return $$$"""
PropertyPath: {{{PropertyPath}}}
ErrorProperty: {{{ErrorType}}}.{{{ErrorProperty}}}
""";
}
}
}
"""".GetPreprocessedCode();
}

private static string Indent(this string code, bool firstLine = false, int level = 1)
{
var indent = new string(' ', 4 * level);
Expand Down
1 change: 1 addition & 0 deletions mooresmaster.Generator/MooresmasterSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
private void Emit(SourceProductionContext context, (Compilation compilation, ImmutableArray<AdditionalText> additionalTexts) input)
{
context.AddSource("mooresmaster.loader.BuiltinLoader.g.cs", LoaderGenerator.GenerateBuiltinLoaderCode());
context.AddSource("mooresmaster.loader.exception.g.cs", LoaderGenerator.GenerateLoaderExceptionTypeCode());

var (schemas, schemaTable) = ParseAdditionalText(input.additionalTexts);
var semantics = SemanticsGenerator.Generate(schemas.Select(schema => schema.Schema).ToImmutableArray(), schemaTable);
Expand Down
36 changes: 1 addition & 35 deletions mooresmaster.SandBox/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,8 @@ internal static class Program
{
private static void Main(string[] args)
{
// var blocks = BlocksLoader.Load(GetJson("blocks"));
// var challenges = ChallengesLoader.Load(GetJson("challenges"));
// var craftRecipes = CraftRecipesLoader.Load(GetJson("craftRecipes"));
// var items = ItemsLoader.Load(GetJson("items"));
// var machineRecipes = MachineRecipesLoader.Load(GetJson("machineRecipes"));
// var mapObjects = MapObjectsLoader.Load(GetJson("mapObjects"));


// Console.WriteLine(blocks);
// Console.WriteLine(challenges);
// Console.WriteLine(craftRecipes);
// Console.WriteLine(items);
// Console.WriteLine(machineRecipes);
// Console.WriteLine(mapObjects);

// var testModJsonsDirectoryPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestMod");
// var jsonPaths = Directory.GetFiles(testModJsonsDirectoryPath);
// var jsons = jsonPaths.Select(path => (path, File.ReadAllText(path)));

// foreach (var (path, json) in jsons)
// {
// // Console.WriteLine(jsonFile);
// var jsonData = (JObject)JsonConvert.DeserializeObject(json);
//
// try
// {
// }
// catch (Exception e)
// {
// Console.ForegroundColor = ConsoleColor.Red;
// Console.WriteLine($"throw from {path}\n{e}");
// Console.ResetColor();
// }
// }
}

private static JToken GetJson(string name)
{
var blockJsonPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestMod", $"{name}.json");
Expand Down
1 change: 1 addition & 0 deletions mooresmaster.SandBox/mooresmaster.SandBox.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<AdditionalFiles Include="Schema\ymlSample\machineRecipes.yml"/>
<None Remove="Schema\ymlSample\mapObjects.yml"/>
<AdditionalFiles Include="Schema\ymlSample\mapObjects.yml"/>
<None Remove="schema\ymlSample\loaderTest.yml"/>
</ItemGroup>

<ItemGroup>
Expand Down
Loading