Skip to content

Commit

Permalink
[.Net] Fix microsoft#2687 by adding global:: keyword in generated code (
Browse files Browse the repository at this point in the history
microsoft#2689)

* add tests

* remove approved file

* update

* update approve file
  • Loading branch information
LittleLittleCloud authored May 15, 2024
1 parent 23e8d27 commit 7214921
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</PropertyGroup>

<Import Project="$(RepoRoot)/nuget/nuget-package.props" />


<PropertyGroup>
<Title>AutoGen.SourceGenerator</Title>
Expand Down Expand Up @@ -50,6 +50,10 @@
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="AutoGen.SourceGenerator.Tests"/>
</ItemGroup>

<ItemGroup>
<Compile Update="Template\FunctionCallTemplate.cs">
<DesignTime>True</DesignTime>
Expand Down
10 changes: 5 additions & 5 deletions dotnet/src/AutoGen.SourceGenerator/FunctionCallGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

private class PartialClassOutput
{
public PartialClassOutput(string fullClassName, ClassDeclarationSyntax classDeclarationSyntax, IEnumerable<FunctionContract> functionContracts)
public PartialClassOutput(string fullClassName, ClassDeclarationSyntax classDeclarationSyntax, IEnumerable<SourceGeneratorFunctionContract> functionContracts)
{
FullClassName = fullClassName;
ClassDeclarationSyntax = classDeclarationSyntax;
Expand All @@ -155,10 +155,10 @@ public PartialClassOutput(string fullClassName, ClassDeclarationSyntax classDecl

public ClassDeclarationSyntax ClassDeclarationSyntax { get; }

public IEnumerable<FunctionContract> FunctionContracts { get; }
public IEnumerable<SourceGeneratorFunctionContract> FunctionContracts { get; }
}

private FunctionContract CreateFunctionContract(MethodDeclarationSyntax method, string? className, string? namespaceName)
private SourceGeneratorFunctionContract CreateFunctionContract(MethodDeclarationSyntax method, string? className, string? namespaceName)
{
// get function_call attribute
var functionCallAttribute = method.AttributeLists.SelectMany(attributeList => attributeList.Attributes)
Expand Down Expand Up @@ -208,7 +208,7 @@ private FunctionContract CreateFunctionContract(MethodDeclarationSyntax method,
description = System.Text.RegularExpressions.Regex.Replace(description, @"[^\S\r\n]+\/[\/]+\s*", string.Empty);
}
var jsonItemType = parameter.Type!.ToString().EndsWith("[]") ? parameter.Type!.ToString().Substring(0, parameter.Type!.ToString().Length - 2) : null;
return new ParameterContract
return new SourceGeneratorParameterContract
{
Name = parameter.Identifier.ToString(),
JsonType = parameter.Type!.ToString() switch
Expand All @@ -234,7 +234,7 @@ private FunctionContract CreateFunctionContract(MethodDeclarationSyntax method,
};
});

return new FunctionContract
return new SourceGeneratorFunctionContract
{
ClassName = className,
Namespace = namespaceName,
Expand Down
10 changes: 5 additions & 5 deletions dotnet/src/AutoGen.SourceGenerator/FunctionExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@

internal static class FunctionExtension
{
public static string GetFunctionName(this FunctionContract function)
public static string GetFunctionName(this SourceGeneratorFunctionContract function)
{
return function.Name ?? string.Empty;
}

public static string GetFunctionSchemaClassName(this FunctionContract function)
public static string GetFunctionSchemaClassName(this SourceGeneratorFunctionContract function)
{
return $"{function.GetFunctionName()}Schema";
}

public static string GetFunctionDefinitionName(this FunctionContract function)
public static string GetFunctionDefinitionName(this SourceGeneratorFunctionContract function)
{
return $"{function.GetFunctionName()}Function";
}

public static string GetFunctionWrapperName(this FunctionContract function)
public static string GetFunctionWrapperName(this SourceGeneratorFunctionContract function)
{
return $"{function.GetFunctionName()}Wrapper";
}

public static string GetFunctionContractName(this FunctionContract function)
public static string GetFunctionContractName(this SourceGeneratorFunctionContract function)
{
return $"{function.GetFunctionName()}FunctionContract";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace AutoGen.SourceGenerator
{
internal class FunctionContract
internal class SourceGeneratorFunctionContract
{
public string? Namespace { get; set; }

Expand All @@ -15,12 +15,12 @@ internal class FunctionContract

public string? ReturnDescription { get; set; }

public ParameterContract[]? Parameters { get; set; }
public SourceGeneratorParameterContract[]? Parameters { get; set; }

public string? ReturnType { get; set; }
}

internal class ParameterContract
internal class SourceGeneratorParameterContract
{
public string? Name { get; set; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public virtual string TransformText()
// This code was generated by a tool.
// </auto-generated>
//----------------------
using Azure.AI.OpenAI;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
Expand Down Expand Up @@ -152,7 +151,8 @@ public virtual string TransformText()
}
this.Write(" },\r\n");
}
this.Write(" };\r\n }\r\n\r\n public Azure.AI.OpenAI.FunctionDefinition ");
this.Write(" };\r\n }\r\n\r\n public global::Azure.AI.OpenAI.FunctionDefin" +
"ition ");
this.Write(this.ToStringHelper.ToStringWithCulture(functionContract.GetFunctionDefinitionName()));
this.Write("\r\n {\r\n get => this.");
this.Write(this.ToStringHelper.ToStringWithCulture(functionContract.GetFunctionContractName()));
Expand All @@ -168,7 +168,7 @@ public virtual string TransformText()

public string NameSpace {get; set;}
public string ClassName {get; set;}
public IEnumerable<FunctionContract> FunctionContracts {get; set;}
public IEnumerable<SourceGeneratorFunctionContract> FunctionContracts {get; set;}
public bool IsStatic {get; set;} = false;

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
// This code was generated by a tool.
// </auto-generated>
//----------------------
using Azure.AI.OpenAI;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
Expand Down Expand Up @@ -98,7 +97,7 @@ namespace <#=NameSpace#>
};
}

public Azure.AI.OpenAI.FunctionDefinition <#=functionContract.GetFunctionDefinitionName()#>
public global::Azure.AI.OpenAI.FunctionDefinition <#=functionContract.GetFunctionDefinitionName()#>
{
get => this.<#=functionContract.GetFunctionContractName()#>.ToOpenAIFunctionDefinition();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//----------------------
// <auto-generated>
// This code was generated by a tool.
// </auto-generated>
//----------------------
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using System;
using AutoGen.Core;
using AutoGen.OpenAI.Extension;

namespace AutoGen.SourceGenerator.Tests
{
public partial class FunctionExamples
{

private class AddAsyncSchema
{
[JsonPropertyName(@"a")]
public System.Int32 a {get; set;}
[JsonPropertyName(@"b")]
public System.Int32 b {get; set;}
}

public System.Threading.Tasks.Task`1[System.String] AddAsyncWrapper(string arguments)
{
var schema = JsonSerializer.Deserialize<AddAsyncSchema>(
arguments,
new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
});

return AddAsync(schema.a, schema.b);
}

public FunctionContract AddAsyncFunctionContract
{
get => new FunctionContract
{
Name = @"AddAsync",
Description = @"Add two numbers.",
ReturnType = typeof(System.Threading.Tasks.Task`1[System.String]),
Parameters = new []
{
new FunctionParameterContract
{
Name = @"a",
Description = @"The first number.",
ParameterType = typeof(System.Int32),
IsRequired = true,
},
new FunctionParameterContract
{
Name = @"b",
Description = @"The second number.",
ParameterType = typeof(System.Int32),
IsRequired = true,
},
},
};
}

public global::Azure.AI.OpenAI.FunctionDefinition AddAsyncFunction
{
get => this.AddAsyncFunctionContract.ToOpenAIFunctionDefinition();
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\AutoGen.SourceGenerator\AutoGen.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\..\src\AutoGen.SourceGenerator\AutoGen.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
<ProjectReference Include="..\..\src\AutoGen\AutoGen.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// FunctionCallTemplateTests.cs

using ApprovalTests;
using ApprovalTests.Namers;
using ApprovalTests.Reporters;
using AutoGen.SourceGenerator.Template;
using Xunit;

namespace AutoGen.SourceGenerator.Tests;

public class FunctionCallTemplateTests
{
[Fact]
[UseReporter(typeof(DiffReporter))]
[UseApprovalSubdirectory("ApprovalTests")]
public void TestFunctionCallTemplate()
{
var functionExample = new FunctionExamples();
var function = functionExample.AddAsyncFunctionContract;
var functionCallTemplate = new FunctionCallTemplate()
{
ClassName = function.ClassName,
NameSpace = function.Namespace,
FunctionContracts = [new SourceGeneratorFunctionContract()
{
Name = function.Name,
Description = function.Description,
ReturnType = function.ReturnType!.ToString(),
ReturnDescription = function.ReturnDescription,
Parameters = function.Parameters!.Select(p => new SourceGeneratorParameterContract()
{
Name = p.Name,
Description = p.Description,
Type = p.ParameterType!.ToString(),
IsOptional = !p.IsRequired,
JsonType = p.ParameterType!.ToString(),
}).ToArray()
}]
};

var actual = functionCallTemplate.TransformText();

Approvals.Verify(actual);
}
}

0 comments on commit 7214921

Please sign in to comment.