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

Fixed Lower Case Type Names #4621

Merged
merged 3 commits into from
Jan 6, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ private BaseTypeDeclarationSyntax GenerateRecord(
string infoInterfaceType)
{
RecordDeclarationSyntax recordDeclaration =
RecordDeclaration(Token(SyntaxKind.RecordKeyword), descriptor.Name.Value)
RecordDeclaration(Token(SyntaxKind.RecordKeyword), descriptor.Name.ToEscapedName())
.AddImplements(infoInterfaceType)
.AddModifiers(
Token(SyntaxKind.PublicKeyword),
Token(SyntaxKind.PartialKeyword))
.AddGeneratedAttribute()
.AddEquality(descriptor.Name, descriptor.Properties, true)
.AddEquality(descriptor.Name.ToEscapedName(), descriptor.Properties, true)
.AddSummary(descriptor.Documentation)
.WithOpenBraceToken(Token(SyntaxKind.OpenBraceToken));

Expand All @@ -58,13 +58,13 @@ private BaseTypeDeclarationSyntax GenerateClass(
string infoInterfaceType)
{
ClassDeclarationSyntax classDeclaration =
ClassDeclaration(descriptor.Name.Value)
ClassDeclaration(descriptor.Name.ToEscapedName())
.AddImplements(infoInterfaceType)
.AddModifiers(
Token(SyntaxKind.PublicKeyword),
Token(SyntaxKind.PartialKeyword))
.AddGeneratedAttribute()
.AddEquality(descriptor.Name, descriptor.Properties)
.AddEquality(descriptor.Name.ToEscapedName(), descriptor.Properties)
.AddSummary(descriptor.Documentation);

classDeclaration = GenerateProperties(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using HotChocolate;
using StrawberryShake.CodeGeneration.Utilities;

namespace StrawberryShake.CodeGeneration.Descriptors.TypeDescriptors
{
Expand All @@ -12,7 +11,7 @@ public InputObjectTypeDescriptor(
RuntimeTypeInfo runtimeType,
string? documentation)
{
Name = NameUtils.GetClassName(name);
Name = name;
RuntimeType = runtimeType;
Documentation = documentation;
}
Expand Down
21 changes: 21 additions & 0 deletions src/StrawberryShake/CodeGeneration/src/CodeGeneration/Keywords.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using HotChocolate;

namespace StrawberryShake.CodeGeneration.CSharp
{
Expand Down Expand Up @@ -129,5 +130,25 @@ public static string ToSafeName(string name)

return name;
}

public static string ToEscapedName(this NameString name)
{
if (_keywords.Contains(name.Value))
{
return $"@{name}";
}

return name;
}

public static string ToEscapedName(this string name)
{
if (_keywords.Contains(name))
{
return $"@{name}";
}

return name;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ public static void Map(ClientModel model, IMapperContext context)
NameString typeName = arg.Type.TypeName();

INamedTypeDescriptor namedTypeDescriptor =
context.Types.Single(type =>
type.Name.Equals(Utilities.NameUtils.GetClassName(typeName)));
context.Types.Single(type => type.Name.Equals(typeName));

return new PropertyDescriptor(
arg.Name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private static void CollectInputTypes(
inputType,
new InputObjectTypeDescriptor(
inputType.Type.Name,
new (inputType.Name, context.Namespace),
new (inputType.Type.Name, context.Namespace),
inputType.Description));

typeDescriptors.Add(inputType.Name, descriptorModel);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Linq;
using StrawberryShake.CodeGeneration.CSharp;

namespace StrawberryShake.CodeGeneration
{
Expand Down Expand Up @@ -36,8 +37,8 @@ public RuntimeTypeInfo(string name, string @namespace, bool isValueType = false)

public string FullName =>
Namespace == "global::"
? Namespace + Name
: Namespace + "." + Name;
? Namespace + Name.ToEscapedName()
: Namespace + "." + Name.ToEscapedName();

public string NamespaceWithoutGlobal =>
Namespace.Replace("global::", string.Empty);
Expand Down Expand Up @@ -94,7 +95,7 @@ public override int GetHashCode()

public override string ToString()
{
return $"{Namespace}.{Name}";
return $"{Namespace}.{Name.ToEscapedName()}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -513,5 +513,22 @@ mutation insertPeople($people: [people_insert_input!]!) {
"extend schema @key(fields: \"id\")",
FileResource.Open("HasuraSchema.graphql"));
}

[Fact]
public void LowerCaseScalarArgument()
{
AssertResult(
@"
query GetPeopleByPk($id: uuid!) {
people_by_pk(id: $id) {
id
firstName
lastName
}
}
",
"extend schema @key(fields: \"id\")",
FileResource.Open("HasuraSchema.graphql"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,10 @@ namespace Foo.Bar

// StrawberryShake.CodeGeneration.CSharp.Generators.InputValueFormatterGenerator
[global::System.CodeDom.Compiler.GeneratedCode("StrawberryShake", "11.0.0")]
public partial class AbstractInputValueFormatter : global::StrawberryShake.Serialization.IInputObjectFormatter
public partial class abstractInputValueFormatter : global::StrawberryShake.Serialization.IInputObjectFormatter
{
private global::StrawberryShake.Serialization.IInputValueFormatter _stringFormatter = default !;
public global::System.String TypeName => "Abstract";
public global::System.String TypeName => "abstract";
public void Initialize(global::StrawberryShake.Serialization.ISerializerResolver serializerResolver)
{
_stringFormatter = serializerResolver.GetInputValueFormatter("String");
Expand All @@ -315,7 +315,7 @@ namespace Foo.Bar
return null;
}

var input = runtimeValue as global::Foo.Bar.Abstract;
var input = runtimeValue as global::Foo.Bar.@abstract;
var inputInfo = runtimeValue as global::Foo.Bar.State.IAbstractInfo;
if (input is null || inputInfo is null)
{
Expand Down Expand Up @@ -346,7 +346,7 @@ namespace Foo.Bar

// StrawberryShake.CodeGeneration.CSharp.Generators.InputTypeGenerator
[global::System.CodeDom.Compiler.GeneratedCode("StrawberryShake", "0.0.0.0")]
public partial class Abstract : global::Foo.Bar.State.IAbstractInfo, global::System.IEquatable<Abstract>
public partial class @abstract : global::Foo.Bar.State.IAbstractInfo, global::System.IEquatable<@abstract>
{
public override global::System.Boolean Equals(global::System.Object? obj)
{
Expand All @@ -365,10 +365,10 @@ namespace Foo.Bar
return false;
}

return Equals((Abstract)obj);
return Equals((@abstract)obj);
}

public virtual global::System.Boolean Equals(Abstract? other)
public virtual global::System.Boolean Equals(@abstract? other)
{
if (ReferenceEquals(null, other))
{
Expand Down Expand Up @@ -496,23 +496,23 @@ namespace Foo.Bar
public ReadonlyQuery(global::StrawberryShake.IOperationExecutor<IReadonlyResult> operationExecutor, global::StrawberryShake.Serialization.ISerializerResolver serializerResolver)
{
_operationExecutor = operationExecutor ?? throw new global::System.ArgumentNullException(nameof(operationExecutor));
_abstractFormatter = serializerResolver.GetInputValueFormatter("Abstract");
_abstractFormatter = serializerResolver.GetInputValueFormatter("abstract");
}

global::System.Type global::StrawberryShake.IOperationRequestFactory.ResultType => typeof(IReadonlyResult);
public async global::System.Threading.Tasks.Task<global::StrawberryShake.IOperationResult<IReadonlyResult>> ExecuteAsync(global::Foo.Bar.Abstract input, global::System.Threading.CancellationToken cancellationToken = default)
public async global::System.Threading.Tasks.Task<global::StrawberryShake.IOperationResult<IReadonlyResult>> ExecuteAsync(global::Foo.Bar.@abstract input, global::System.Threading.CancellationToken cancellationToken = default)
{
var request = CreateRequest(input);
return await _operationExecutor.ExecuteAsync(request, cancellationToken).ConfigureAwait(false);
}

public global::System.IObservable<global::StrawberryShake.IOperationResult<IReadonlyResult>> Watch(global::Foo.Bar.Abstract input, global::StrawberryShake.ExecutionStrategy? strategy = null)
public global::System.IObservable<global::StrawberryShake.IOperationResult<IReadonlyResult>> Watch(global::Foo.Bar.@abstract input, global::StrawberryShake.ExecutionStrategy? strategy = null)
{
var request = CreateRequest(input);
return _operationExecutor.Watch(request, strategy);
}

private global::StrawberryShake.OperationRequest CreateRequest(global::Foo.Bar.Abstract input)
private global::StrawberryShake.OperationRequest CreateRequest(global::Foo.Bar.@abstract input)
{
var variables = new global::System.Collections.Generic.Dictionary<global::System.String, global::System.Object?>();
variables.Add("input", FormatInput(input));
Expand All @@ -524,7 +524,7 @@ namespace Foo.Bar
return new global::StrawberryShake.OperationRequest(id: ReadonlyQueryDocument.Instance.Hash.Value, name: "Readonly", document: ReadonlyQueryDocument.Instance, strategy: global::StrawberryShake.RequestStrategy.Default, variables: variables);
}

private global::System.Object? FormatInput(global::Foo.Bar.Abstract value)
private global::System.Object? FormatInput(global::Foo.Bar.@abstract value)
{
if (value is null)
{
Expand Down Expand Up @@ -563,8 +563,8 @@ namespace Foo.Bar
[global::System.CodeDom.Compiler.GeneratedCode("StrawberryShake", "11.0.0")]
public partial interface IReadonlyQuery : global::StrawberryShake.IOperationRequestFactory
{
global::System.Threading.Tasks.Task<global::StrawberryShake.IOperationResult<IReadonlyResult>> ExecuteAsync(global::Foo.Bar.Abstract input, global::System.Threading.CancellationToken cancellationToken = default);
global::System.IObservable<global::StrawberryShake.IOperationResult<IReadonlyResult>> Watch(global::Foo.Bar.Abstract input, global::StrawberryShake.ExecutionStrategy? strategy = null);
global::System.Threading.Tasks.Task<global::StrawberryShake.IOperationResult<IReadonlyResult>> ExecuteAsync(global::Foo.Bar.@abstract input, global::System.Threading.CancellationToken cancellationToken = default);
global::System.IObservable<global::StrawberryShake.IOperationResult<IReadonlyResult>> Watch(global::Foo.Bar.@abstract input, global::StrawberryShake.ExecutionStrategy? strategy = null);
}

// StrawberryShake.CodeGeneration.CSharp.Generators.ClientGenerator
Expand Down Expand Up @@ -993,7 +993,7 @@ namespace Microsoft.Extensions.DependencyInjection
global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton<global::StrawberryShake.Serialization.ISerializer, global::StrawberryShake.Serialization.ByteArraySerializer>(services);
global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton<global::StrawberryShake.Serialization.ISerializer, global::StrawberryShake.Serialization.TimeSpanSerializer>(services);
global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton<global::StrawberryShake.Serialization.ISerializer, global::StrawberryShake.Serialization.JsonSerializer>(services);
global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton<global::StrawberryShake.Serialization.ISerializer, global::Foo.Bar.AbstractInputValueFormatter>(services);
global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton<global::StrawberryShake.Serialization.ISerializer, global::Foo.Bar.abstractInputValueFormatter>(services);
global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton<global::StrawberryShake.Serialization.ISerializerResolver>(services, sp => new global::StrawberryShake.Serialization.SerializerResolver(global::System.Linq.Enumerable.Concat(global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService<global::System.Collections.Generic.IEnumerable<global::StrawberryShake.Serialization.ISerializer>>(parentServices), global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService<global::System.Collections.Generic.IEnumerable<global::StrawberryShake.Serialization.ISerializer>>(sp))));
global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton<global::StrawberryShake.IOperationResultDataFactory<global::Foo.Bar.IReadonlyResult>, global::Foo.Bar.State.ReadonlyResultFactory>(services);
global::Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton<global::StrawberryShake.IOperationResultDataFactory>(services, sp => global::Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService<global::StrawberryShake.IOperationResultDataFactory<global::Foo.Bar.IReadonlyResult>>(sp));
Expand Down
Loading