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

Add configurable access for generated clients (#6374) #6416

Merged
merged 5 commits into from
Aug 21, 2023
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 @@ -5,13 +5,20 @@ namespace StrawberryShake.CodeGeneration.CSharp.Builders;

public class EnumBuilder : ITypeBuilder
{
private AccessModifier _accessModifier;
private readonly List<(string, long?, XmlCommentBuilder?)> _elements = new();
private string? _name;
private string? _underlyingType;
private XmlCommentBuilder? _xmlComment;

public static EnumBuilder New() => new();

public EnumBuilder SetAccessModifier(AccessModifier value)
{
_accessModifier = value;
return this;
}

public EnumBuilder SetName(string value)
{
_name = value;
Expand Down Expand Up @@ -61,14 +68,16 @@ public void Build(CodeWriter writer)
_xmlComment?.Build(writer);

writer.WriteGeneratedAttribute();

var modifier = _accessModifier.ToString().ToLowerInvariant();

if (_underlyingType is null)
{
writer.WriteIndentedLine($"public enum {_name}");
writer.WriteIndentedLine($"{modifier} enum {_name}");
}
else
{
writer.WriteIndentedLine($"public enum {_name} : {_underlyingType}");
writer.WriteIndentedLine($"{modifier} enum {_name} : {_underlyingType}");
}

writer.WriteIndentedLine("{");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ namespace StrawberryShake.CodeGeneration.CSharp.Builders;

public class InterfaceBuilder : AbstractTypeBuilder
{
private AccessModifier _accessModifier;
private readonly List<MethodBuilder> _methods = new();

private XmlCommentBuilder? _xmlComment;

public static InterfaceBuilder New() => new();

public InterfaceBuilder SetAccessModifier(AccessModifier value)
{
_accessModifier = value;
return this;
}

public new InterfaceBuilder SetName(string name)
{
base.SetName(name);
Expand Down Expand Up @@ -74,7 +81,9 @@ public override void Build(CodeWriter writer)

writer.WriteIndent();

writer.Write("public partial interface ");
var modifier = _accessModifier.ToString().ToLowerInvariant();

writer.Write($"{modifier} partial interface ");
writer.WriteLine(Name);

if (Implements.Count > 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ private static IReadOnlyList<GeneratorResult> GenerateCSharpDocuments(
CSharpGeneratorSettings settings)
{
var generatorSettings = new CSharpSyntaxGeneratorSettings(
settings.AccessModifier,
settings.NoStore,
settings.InputRecords,
settings.EntityRecords,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public class CSharpGeneratorSettings
/// The root namespace of the client.
/// </summary>
public string Namespace { get; set; } = "StrawberryShake.GraphQL";

/// <summary>
/// The access modifier of the client.
/// </summary>
public AccessModifier AccessModifier { get; set; } = AccessModifier.Internal;

/// <summary>
/// Defines if a schema needs to be fully valid.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using StrawberryShake.CodeGeneration.CSharp.Builders;
using StrawberryShake.CodeGeneration.Descriptors;
using StrawberryShake.CodeGeneration.Descriptors.Operations;
Expand All @@ -21,6 +22,7 @@ protected override void Generate(

var classBuilder = ClassBuilder
.New()
.SetAccessModifier(settings.AccessModifier)
.SetName(fileName)
.SetComment(descriptor.Documentation)
.AddImplements(descriptor.InterfaceType.ToString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ protected override void Generate(ClientDescriptor descriptor,

var interfaceBuilder = InterfaceBuilder
.New()
.SetAccessModifier(settings.AccessModifier)
.SetName(fileName)
.SetComment(descriptor.Documentation);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,23 @@ protected override CSharpSyntaxGeneratorResult Generate(
DataTypeDescriptor descriptor,
CSharpSyntaxGeneratorSettings settings)
{
var modifier = settings.AccessModifier == AccessModifier.Public
? SyntaxKind.PublicKeyword
: SyntaxKind.InternalKeyword;

return descriptor.IsInterface
? GenerateDataInterface(descriptor)
: GenerateDataClass(descriptor, settings);
? GenerateDataInterface(descriptor, modifier)
: GenerateDataClass(descriptor, modifier, settings.EntityRecords);
}

private CSharpSyntaxGeneratorResult GenerateDataInterface(
DataTypeDescriptor descriptor)
DataTypeDescriptor descriptor,
SyntaxKind accessModifier)
{
var interfaceDeclaration =
InterfaceDeclaration(descriptor.RuntimeType.Name)
.AddModifiers(
Token(SyntaxKind.PublicKeyword),
Token(accessModifier),
Token(SyntaxKind.PartialKeyword))
.AddGeneratedAttribute()
.AddSummary(descriptor.Documentation)
Expand All @@ -48,14 +53,15 @@ private CSharpSyntaxGeneratorResult GenerateDataInterface(

private CSharpSyntaxGeneratorResult GenerateDataClass(
DataTypeDescriptor descriptor,
CSharpSyntaxGeneratorSettings settings)
SyntaxKind accessModifier,
bool hasEntityRecords)
{
if (settings.EntityRecords)
if (hasEntityRecords)
{
var recordDeclarationSyntax =
RecordDeclaration(Token(SyntaxKind.RecordKeyword), descriptor.RuntimeType.Name)
.AddModifiers(
Token(SyntaxKind.PublicKeyword),
Token(accessModifier),
Token(SyntaxKind.PartialKeyword))
.AddGeneratedAttribute()
.AddSummary(descriptor.Documentation)
Expand Down Expand Up @@ -96,7 +102,7 @@ private CSharpSyntaxGeneratorResult GenerateDataClass(
var classDeclaration =
ClassDeclaration(descriptor.RuntimeType.Name)
.AddModifiers(
Token(SyntaxKind.PublicKeyword),
Token(accessModifier),
Token(SyntaxKind.PartialKeyword))
.AddGeneratedAttribute()
.AddSummary(descriptor.Documentation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected override void Generate(
var factory = ClassBuilder
.New(fileName)
.SetStatic()
.SetAccessModifier(AccessModifier.Public);
.SetAccessModifier(settings.AccessModifier);

var addClientMethod = factory
.AddMethod($"Add{descriptor.Name}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected override void Generate(EntityIdFactoryDescriptor descriptor,

var classBuilder = ClassBuilder
.New()
.SetAccessModifier(AccessModifier.Public)
.SetAccessModifier(settings.AccessModifier)
.AddImplements(TypeNames.IEntityIdSerializer)
.SetName(fileName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,14 @@ protected override CSharpSyntaxGeneratorResult Generate(
recordDeclarationSyntax);
}

var modifier = settings.AccessModifier == AccessModifier.Public
? SyntaxKind.PublicKeyword
: SyntaxKind.InternalKeyword;

var classDeclaration =
ClassDeclaration(descriptor.RuntimeType.Name)
.AddModifiers(
Token(SyntaxKind.PublicKeyword),
Token(modifier),
Token(SyntaxKind.PartialKeyword))
.AddGeneratedAttribute()
.AddSummary(descriptor.Documentation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ protected override void Generate(EnumTypeDescriptor descriptor,

var enumBuilder = EnumBuilder
.New()
.SetAccessModifier(settings.AccessModifier)
.SetComment(descriptor.Documentation)
.SetName(descriptor.RuntimeType.Name)
.SetUnderlyingType(descriptor.UnderlyingType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ protected override void Generate(EnumTypeDescriptor descriptor,

var classBuilder = ClassBuilder
.New(fileName)
.SetAccessModifier(settings.AccessModifier)
.AddImplements(IInputValueFormatter)
.AddImplements(ILeafValueParser.WithGeneric(String, descriptor.Name));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,30 @@ protected override CSharpSyntaxGeneratorResult Generate(
{
var stateNamespace = $"{descriptor.RuntimeType.Namespace}.{State}";
var infoInterfaceType = $"{stateNamespace}.{CreateInputValueInfo(descriptor.Name)}";

var modifier = settings.AccessModifier == AccessModifier.Public
? SyntaxKind.PublicKeyword
: SyntaxKind.InternalKeyword;

return new(
descriptor.Name,
null,
descriptor.RuntimeType.NamespaceWithoutGlobal,
settings.InputRecords
? GenerateRecord(descriptor, infoInterfaceType)
: GenerateClass(descriptor, infoInterfaceType));
? GenerateRecord(descriptor, modifier, infoInterfaceType)
: GenerateClass(descriptor, modifier, infoInterfaceType));
}

private BaseTypeDeclarationSyntax GenerateRecord(
InputObjectTypeDescriptor descriptor,
SyntaxKind accessModifier,
string infoInterfaceType)
{
var recordDeclaration =
RecordDeclaration(Token(SyntaxKind.RecordKeyword), descriptor.Name.ToEscapedName())
.AddImplements(infoInterfaceType)
.AddModifiers(
Token(SyntaxKind.PublicKeyword),
Token(accessModifier),
Token(SyntaxKind.PartialKeyword))
.AddGeneratedAttribute()
.AddEquality(descriptor.Name.ToEscapedName(), descriptor.Properties, true)
Expand All @@ -55,13 +60,14 @@ private BaseTypeDeclarationSyntax GenerateRecord(

private BaseTypeDeclarationSyntax GenerateClass(
InputObjectTypeDescriptor descriptor,
SyntaxKind accessModifier,
string infoInterfaceType)
{
var classDeclaration =
ClassDeclaration(descriptor.Name.ToEscapedName())
.AddImplements(infoInterfaceType)
.AddModifiers(
Token(SyntaxKind.PublicKeyword),
Token(accessModifier),
Token(SyntaxKind.PartialKeyword))
.AddGeneratedAttribute()
.AddEquality(descriptor.Name.ToEscapedName(), descriptor.Properties)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ protected override void Generate(

var classBuilder = ClassBuilder
.New()
.SetAccessModifier(settings.AccessModifier)
.SetName(fileName)
.AddImplements(TypeNames.IInputObjectFormatter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ resultBuilderDescriptor.ResultNamedType as InterfaceTypeDescriptor ??

var classBuilder = ClassBuilder
.New()
.SetAccessModifier(settings.AccessModifier)
.SetName(fileName);

var constructorBuilder = classBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ protected override void Generate(

var factory = ClassBuilder
.New(fileName)
.SetAccessModifier(AccessModifier.Public)
.SetAccessModifier(settings.AccessModifier)
.AddImplements(TypeNames.IStoreAccessor);

AddThrowNotValidWithoutStore(factory, "OperationStore", TypeNames.IOperationStore);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protected override void Generate(OperationDescriptor descriptor,

var classBuilder = ClassBuilder
.New()
.SetAccessModifier(settings.AccessModifier)
.SetName(fileName)
.AddImplements(TypeNames.IDocument)
.SetComment(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ protected override void Generate(

var classBuilder = ClassBuilder
.New()
.SetAccessModifier(settings.AccessModifier)
.SetComment(
XmlCommentBuilder
.New()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ protected override void Generate(OperationDescriptor descriptor,

var interfaceBuilder = InterfaceBuilder
.New()
.SetAccessModifier(settings.AccessModifier)
.SetComment(
XmlCommentBuilder
.New()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ protected override CSharpSyntaxGeneratorResult Generate(
{
var componentName = $"Use{descriptor.Name}";
var resultType = descriptor.ResultTypeReference.GetRuntimeType().ToString();

var modifier = settings.AccessModifier == AccessModifier.Public
? SyntaxKind.PublicKeyword
: SyntaxKind.InternalKeyword;

var classDeclaration =
ClassDeclaration(componentName)
.AddImplements(TypeNames.UseQuery.WithGeneric(resultType))
.AddModifiers(
Token(SyntaxKind.PublicKeyword),
Token(modifier),
Token(SyntaxKind.PartialKeyword))
.AddGeneratedAttribute()
.AddMembers(CreateOperationProperty(descriptor.RuntimeType.ToString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,16 @@ protected override CSharpSyntaxGeneratorResult Generate(
{
var componentName = $"Use{descriptor.Name}";
var resultType = descriptor.ResultTypeReference.GetRuntimeType().ToString();

var modifier = settings.AccessModifier == AccessModifier.Public
? SyntaxKind.PublicKeyword
: SyntaxKind.InternalKeyword;

var classDeclaration =
ClassDeclaration(componentName)
.AddImplements(TypeNames.UseSubscription.WithGeneric(resultType))
.AddModifiers(
Token(SyntaxKind.PublicKeyword),
Token(modifier),
Token(SyntaxKind.PartialKeyword))
.AddGeneratedAttribute()
.AddMembers(CreateOperationProperty(descriptor.RuntimeType.ToString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ typeDescriptor as ComplexTypeDescriptor ??
var classBuilder =
ClassBuilder
.New()
.SetAccessModifier(settings.AccessModifier)
.SetName(fileName)
.AddImplements(
TypeNames.IOperationResultDataFactory.WithGeneric(descriptor.RuntimeType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ protected override void Generate(

var classBuilder = ClassBuilder
.New()
.SetAccessModifier(settings.AccessModifier)
.AddImplements(
IEntityMapper.WithGeneric(
descriptor.ExtractType().ToString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typeDescriptor as ComplexTypeDescriptor ??

var classBuilder = ClassBuilder
.New()
.SetAccessModifier(settings.AccessModifier)
.AddImplements(TypeNames.IOperationResultDataInfo)
.SetName(fileName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ protected override void Generate(InterfaceTypeDescriptor descriptor,

var interfaceBuilder = InterfaceBuilder
.New()
.SetAccessModifier(settings.AccessModifier)
.SetComment(descriptor.Description)
.SetName(fileName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ protected override void Generate(

var classBuilder = ClassBuilder
.New()
.SetAccessModifier(settings.AccessModifier)
.SetComment(descriptor.Description)
.SetName(fileName)
.AddEquality(fileName, equalityProperties);
Expand Down
Loading