Skip to content

Commit a68f318

Browse files
committed
Improve OBject generation
1 parent 8830ed2 commit a68f318

File tree

435 files changed

+6215
-2830
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

435 files changed

+6215
-2830
lines changed

Diff for: src/GqlPlus.Abstractions/GeneralHelpers.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Diagnostics.CodeAnalysis;
22
using System.Reflection;
33
using System.Runtime.CompilerServices;
4+
using System.Runtime.Serialization;
45
using GqlPlus.Structures;
56

67
namespace GqlPlus;
@@ -73,13 +74,19 @@ public static string Surround(
7374
string before,
7475
string after,
7576
string by = " ")
76-
=> before + items.Joined(by) + after;
77+
{
78+
string inner = items.Joined(by);
79+
return string.IsNullOrWhiteSpace(inner) ? "" : before + inner + after;
80+
}
7781

7882
public static string Surround<T>(
7983
this IEnumerable<T>? items,
8084
string before,
8185
string after,
8286
Func<T?, string> formatter,
8387
string by = " ")
84-
=> before + items.Joined(formatter, by) + after;
88+
{
89+
string inner = items.Joined(formatter, by);
90+
return string.IsNullOrWhiteSpace(inner) ? "" : before + inner + after;
91+
}
8592
}
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace GqlPlus.Generating.Objects;
22

33
internal class DualGenerator
4-
: GenerateForObject<IGqlpDualObject>
4+
: GenerateForObject<IGqlpDualObject, IGqlpDualBase, IGqlpDualField, IGqlpDualAlternate>
55
{
66
public override string TypePrefix => "Dual";
77
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
namespace GqlPlus.Generating.Objects;
1+
using GqlPlus.Ast;
22

3-
internal abstract class GenerateForObject<T>
4-
: GenerateForClass<T>
5-
where T : IGqlpObject
3+
namespace GqlPlus.Generating.Objects;
4+
5+
internal abstract class GenerateForObject<TObj, TBase, TField, TAlt>
6+
: GenerateForClass<TObj>
7+
where TObj : IGqlpObject<TBase, TField, TAlt>
8+
where TBase : IGqlpObjBase
9+
where TField : IGqlpObjField
10+
where TAlt : IGqlpObjAlternate
611
{
7-
internal override IEnumerable<MapPair<string>> TypeMembers(T ast, GeneratorContext context)
12+
internal override IEnumerable<MapPair<string>> TypeMembers(TObj ast, GeneratorContext context)
813
=> [.. ast.Fields.Select(FieldMember(context)), .. ast.Alternates.Select(AlternateMember(context))];
914

1015
private Func<IGqlpObjField, MapPair<string>> FieldMember(GeneratorContext context)
@@ -14,5 +19,30 @@ private Func<IGqlpObjAlternate, MapPair<string>> AlternateMember(GeneratorContex
1419
=> alternate => new("As" + alternate.Name, TypeString(alternate, context));
1520

1621
protected virtual string TypeString(IGqlpObjType type, GeneratorContext context)
17-
=> context.GetTypeAst<IGqlpType>(type.FullType)?.Name ?? type.FullType;
22+
=> context.GetTypeAst<IGqlpType>(type.Name)?.Name
23+
?? (type.IsTypeParam ? "T" : "") + type.Name;
24+
25+
protected override void TypeHeader(TObj ast, GeneratorContext context)
26+
{
27+
string typeParams = ast.TypeParams.Select(p => "T" + p.Name).Surround("<", ">", ",");
28+
29+
context.AppendLine($"public interface I{ast.Name}{typeParams}");
30+
if (ast.Parent is not null) {
31+
context.AppendLine(" : I" + ast.Parent.Name);
32+
}
33+
}
34+
35+
protected override void ClassHeader(TObj ast, GeneratorContext context)
36+
{
37+
string typeParams = ast.TypeParams.Select(p => "T" + p.Name).Surround("<", ">", ",");
38+
39+
context.AppendLine($"public class {TypePrefix}{ast.Name}{typeParams}");
40+
41+
if (ast.Parent is not null) {
42+
context.AppendLine(" : " + TypePrefix + ast.Parent.Name);
43+
context.AppendLine(" , I" + ast.Name + typeParams);
44+
} else {
45+
context.AppendLine(" : I" + ast.Name + typeParams);
46+
}
47+
}
1848
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace GqlPlus.Generating.Objects;
22

33
internal class InputGenerator
4-
: GenerateForObject<IGqlpInputObject>
4+
: GenerateForObject<IGqlpInputObject, IGqlpInputBase, IGqlpInputField, IGqlpInputAlternate>
55
{
66
public override string TypePrefix => "Input";
77
}
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace GqlPlus.Generating.Objects;
22

33
internal class OutputGenerator
4-
: GenerateForObject<IGqlpOutputObject>
4+
: GenerateForObject<IGqlpOutputObject, IGqlpOutputBase, IGqlpOutputField, IGqlpOutputAlternate>
55
{
66
public override string TypePrefix => "Output";
77
}

Diff for: src/GqlPlus.Generators/Generating/Simple/GenerateForSimple.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected override void TypeHeader(T ast, GeneratorContext context)
2121
base.TypeHeader(ast, context);
2222

2323
if (!string.IsNullOrWhiteSpace(ast.Parent)) {
24-
context.AppendLine(" : I" + ast.Name);
24+
context.AppendLine(" : I" + ast.Parent);
2525
}
2626
}
2727
}

Diff for: test/GqlPlus.Abstractions.ClassTests/GeneralHelpersTests.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,22 +87,22 @@ public void Quoted_NullInput_ReturnsEmptyString()
8787
}
8888

8989
[Fact]
90-
public void Surround_NullInput_ReturnsEmptyStringWithSurrounding()
90+
public void Surround_NullInput_ReturnsEmptyString()
9191
{
9292
IEnumerable<string>? input = null;
9393

9494
string result = input.Surround("[", "]");
9595

96-
result.ShouldBe("[]");
96+
result.ShouldBe("");
9797
}
9898

9999
[Fact]
100-
public void Surround_WithMapping_NullInput_ReturnsEmptyStringWithSurrounding()
100+
public void Surround_WithMapping_NullInput_ReturnsEmptyString()
101101
{
102102
IEnumerable<int>? input = null;
103103

104104
string result = input.Surround("[", "]", i => $"{i}");
105105

106-
result.ShouldBe("[]");
106+
result.ShouldBe("");
107107
}
108108
}

0 commit comments

Comments
 (0)