Skip to content

Commit

Permalink
Improve Schema Parse and Merge adding Option merges
Browse files Browse the repository at this point in the history
  • Loading branch information
Struan Judd committed Jan 2, 2024
1 parent da564ea commit 4c18d3b
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 12 deletions.
6 changes: 6 additions & 0 deletions src/GqlPlus.Verifier/Merging/MergeSchemas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace GqlPlus.Verifier.Merging;
internal class MergeSchemas(
IMerge<CategoryDeclAst> categoryMerger,
IMerge<DirectiveDeclAst> directiveMerger,
IMerge<OptionDeclAst> optionMerger,
IMerge<AstType> astTypeMerger
) : GroupsMerger<SchemaAst>
{
Expand All @@ -15,14 +16,17 @@ protected override bool CanMergeGroup(IGrouping<string, SchemaAst> group)
{
var categories = Just<CategoryDeclAst>(group);
var directives = Just<DirectiveDeclAst>(group);
var options = Just<OptionDeclAst>(group);
var astTypes = Just<AstType>(group);

var categoriesCanMerge = categories.Any() || categoryMerger.CanMerge(categories);
var directivesCanMerge = directives.Any() || directiveMerger.CanMerge(directives);
var optionsCanMerge = options.Any() || optionMerger.CanMerge(options);
var astTypesCanMerge = astTypes.Any() || astTypeMerger.CanMerge(astTypes);

return categoriesCanMerge
&& directivesCanMerge
&& optionsCanMerge
&& (astTypesCanMerge || true);
}

Expand All @@ -33,10 +37,12 @@ protected override SchemaAst MergeGroup(IEnumerable<SchemaAst> group)
{
var categories = Just<CategoryDeclAst>(group);
var directives = Just<DirectiveDeclAst>(group);
var options = Just<OptionDeclAst>(group);
var astTypes = Just<AstType>(group);

var declarations = categoryMerger.Merge(categories).Cast<AstDeclaration>()
.Concat(directiveMerger.Merge(directives))
.Concat(optionMerger.Merge(options))
.Concat(astTypeMerger.Merge(astTypes));

return group.First() with { Declarations = [.. declarations] };
Expand Down
2 changes: 2 additions & 0 deletions src/GqlPlus.Verifier/Parse/Schema/ParseSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ private static Parser MakeParser<T>(Parser<T>.L parser)
public ParseSchema(
Parser<CategoryDeclAst>.D category,
Parser<DirectiveDeclAst>.D directive,
Parser<OptionDeclAst>.D optionParser,
Parser<EnumDeclAst>.D enumParser,
Parser<InputDeclAst>.D input,
Parser<OutputDeclAst>.D output,
Parser<ScalarDeclAst>.D scalar)
{
_parsers.Add("category", MakeParser<CategoryDeclAst>(category));
_parsers.Add("directive", MakeParser<DirectiveDeclAst>(directive));
_parsers.Add("option", MakeParser<OptionDeclAst>(optionParser));
_parsers.Add("enum", MakeParser<EnumDeclAst>(enumParser));
_parsers.Add("input", MakeParser<InputDeclAst>(input));
_parsers.Add("output", MakeParser<OutputDeclAst>(output));
Expand Down
30 changes: 19 additions & 11 deletions test/GqlPlus.Verifier.ClassTests/Merging/MergeSchemasTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,44 +8,52 @@ public class MergeSchemasTests
: TestGroups<SchemaAst>
{
[Theory, RepeatData(Repeats)]
public void CanMerge_TwoItemsDifferentDeclarations_ReturnsTrue(string name, string category, string directive)
public void CanMerge_TwoItemsDifferentDeclarations_ReturnsTrue(string name, string category, string directive, string option)
=> CanMerge_True([
new SchemaAst(AstNulls.At, name) with { Declarations = [new CategoryDeclAst(AstNulls.At, category), new OutputDeclAst(AstNulls.At, category)] },
new SchemaAst(AstNulls.At, name) with { Declarations = [new DirectiveDeclAst(AstNulls.At, directive)] }]);
new SchemaAst(AstNulls.At, name) with { Declarations = CategoryDeclarations(category) },
new SchemaAst(AstNulls.At, name) with { Declarations = OtherDeclarations(directive, option) }]);

[Theory, RepeatData(Repeats)]
public void Merge_TwoItemsDifferentDeclarations_ReturnsExpected(string name, string category, string directive)
public void Merge_TwoItemsDifferentDeclarations_ReturnsExpected(string name, string category, string directive, string option)
{
var categoryDecl = new CategoryDeclAst(AstNulls.At, category);
var outputDecl = new OutputDeclAst(AstNulls.At, category);
var directiveDecl = new DirectiveDeclAst(AstNulls.At, directive);
var categoryDecls = CategoryDeclarations(category);
var otherDecls = OtherDeclarations(directive, option);

Merge_Expected([
new SchemaAst(AstNulls.At, name) with { Declarations = [categoryDecl, outputDecl] },
new SchemaAst(AstNulls.At, name) with { Declarations = [directiveDecl] }],
new SchemaAst(AstNulls.At, name) with { Declarations = [categoryDecl, outputDecl, directiveDecl] });
new SchemaAst(AstNulls.At, name) with { Declarations = categoryDecls },
new SchemaAst(AstNulls.At, name) with { Declarations = otherDecls }],
new SchemaAst(AstNulls.At, name) with { Declarations = [.. categoryDecls, .. otherDecls] });

_categories.ReceivedWithAnyArgs(1).Merge([]);
_directives.ReceivedWithAnyArgs(1).Merge([]);
_options.ReceivedWithAnyArgs(1).Merge([]);
_astTypes.ReceivedWithAnyArgs(1).Merge([]);
}

private readonly MergeSchemas _merger;
private readonly IMerge<CategoryDeclAst> _categories;
private readonly IMerge<DirectiveDeclAst> _directives;
private readonly IMerge<OptionDeclAst> _options;
private readonly IMerge<AstType> _astTypes;

public MergeSchemasTests()
{
_categories = Merger<CategoryDeclAst>();
_directives = Merger<DirectiveDeclAst>();
_options = Merger<OptionDeclAst>();
_astTypes = Merger<AstType>();

_merger = new(_categories, _directives, _astTypes);
_merger = new(_categories, _directives, _options, _astTypes);
}

protected override GroupsMerger<SchemaAst> MergerGroups => _merger;

protected override SchemaAst MakeDistinct(string name)
=> new(AstNulls.At, name);

private static AstDeclaration[] CategoryDeclarations(string category)
=> [new CategoryDeclAst(AstNulls.At, category), new OutputDeclAst(AstNulls.At, category)];

private static AstDeclaration[] OtherDeclarations(string directive, string option)
=> [new DirectiveDeclAst(AstNulls.At, directive), new OptionDeclAst(AstNulls.At, option)];
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace GqlPlus.Verifier.Parse.Schema;

public class SchemaParserTests(Parser<SchemaAst>.D parser)
public class ParseSchemaTests(Parser<SchemaAst>.D parser)
{
private readonly Parser<SchemaAst>.L _parser = parser;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,27 @@ Success
(Unique)
All
}
{
!O I@8/1
Option
}
{
!O I@8/1
OptAlias
[
Opt1
Opt2
]
}
{
!O I@8/1
OptSetting
{
!OS I@21/1
setting
=( !c P@64/1 [ !k I@29/1 Boolean.true !k N@65/1 0 ] )
}
}
{
!E I@60/1
InFieldEnum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ await WhenAll(
["directive"] = "directive @Dir { inline } directive @Dir { spread }",
["directive-alias"] = "directive @DirAlias [Dir1] { variable } directive @DirAlias [Dir2] { field }",
["directive-params"] = "directive @DirParams(DirParamsIn) { operation } directive @DirParams { fragment } input DirParamsIn { }",
["option"] = "option Option { } option Option { }",
["option-alias"] = "option OptAlias [Opt1] { } option OptAlias [Opt2] { }",
["option-setting"] = "option OptSetting { setting=true } option OptSetting { setting=[0] }",
["enum-alias"] = "enum EnAlias [En1] { alias } enum EnAlias [En2] { alias }",
["enum-diff"] = "enum EnDiff { one } enum EnDiff { two }",
["enum-same"] = "enum EnSame { same } enum EnSame { same }",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
!G I@1/1
Success
{
!O I@8/1
OptAlias
[
Opt1
Opt2
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
!G I@1/1
Success
{
!O I@8/1
Option
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[
!G I@1/1
Success
{
!O I@8/1
OptSetting
{
!OS I@21/1
setting
=( !c P@64/1 [ !k I@29/1 Boolean.true !k N@65/1 0 ] )
}
}
]

0 comments on commit 4c18d3b

Please sign in to comment.