@@ -16,6 +16,8 @@ public static InterfaceData PrepareData(
1616
1717 var isStrict = attributeData . NamedArguments . FirstOrDefault ( x => x . Key == "Strict" ) . Value . Value is bool strict &&
1818 strict ;
19+ var generateGoogleFunctionTool = attributeData . NamedArguments . FirstOrDefault ( x => x . Key == "GoogleFunctionTool" ) . Value . Value is bool googleFunctionTool &&
20+ googleFunctionTool ;
1921 var methods = interfaceSymbol
2022 . GetMembers ( )
2123 . OfType < IMethodSymbol > ( )
@@ -43,42 +45,93 @@ public static InterfaceData PrepareData(
4345 return new InterfaceData (
4446 Namespace : interfaceSymbol . ContainingNamespace . ToDisplayString ( ) ,
4547 Name : interfaceSymbol . ToDisplayString ( SymbolDisplayFormat . MinimallyQualifiedFormat ) ,
48+ GoogleFunctionTool : generateGoogleFunctionTool ,
4649 Methods : methods ) ;
4750 }
4851
4952 public static InterfaceData PrepareMethodData (
50- this IMethodSymbol interfaceSymbol ,
51- AttributeData attributeData )
53+ List < ( IMethodSymbol , AttributeData ) > list )
5254 {
53- interfaceSymbol = interfaceSymbol ?? throw new ArgumentNullException ( nameof ( interfaceSymbol ) ) ;
54- attributeData = attributeData ?? throw new ArgumentNullException ( nameof ( attributeData ) ) ;
55+ //interfaceSymbol = interfaceSymbol ?? throw new ArgumentNullException(nameof(interfaceSymbol));
56+ list = list ?? throw new ArgumentNullException ( nameof ( list ) ) ;
57+
58+ var namespaceName = "CSharpToJsonSchema" ;
59+ var className = "Tools" ;
60+ List < MethodData > methodList = new ( ) ;
61+ List < string > namespaces = new ( ) ;
62+ bool generateGoogleFunctionTools = false ;
63+ foreach ( var l in list )
64+ {
65+ var ( interfaceSymbol , attributeData ) = l ;
66+ var isStrict = attributeData . NamedArguments . FirstOrDefault ( x => x . Key == "Strict" ) . Value . Value is bool strict &&
67+ strict ;
68+ var ggft = attributeData . NamedArguments . FirstOrDefault ( x => x . Key == "GoogleFunctionTool" ) . Value . Value is bool googleFunctionTool &&
69+ googleFunctionTool ;
70+ if ( ggft )
71+ generateGoogleFunctionTools = true ;
72+
73+ var x = interfaceSymbol ;
74+ var parameters = x . Parameters
75+ //.Where(static x => x.Type.MetadataName != "CancellationToken")
76+ . ToArray ( ) ;
77+
78+ var methodData = new MethodData (
79+ Name : x . Name ,
80+ Description : GetDescription ( x ) ,
81+ IsAsync : x . IsAsync || x . ReturnType . Name == "Task" ,
82+ IsVoid : x . ReturnsVoid || x . ReturnType . MetadataName == "Task" ,
83+ IsStrict : isStrict ,
84+ Parameters : parameters . Select ( static y => y ) . ToArray ( ) ,
85+ Descriptions : parameters . Select ( static l => GetParameterDescriptions ( l ) ) . SelectMany ( s => s )
86+ . ToDictionary ( s => s . Key , s => s . Value ) ,
87+ ReturnType : x . ReturnType
88+ ) ;
89+ methodList . Add ( methodData ) ;
90+ namespaces . Add ( interfaceSymbol . ContainingNamespace . ToDisplayString ( ) ) ;
91+ }
5592
56- var isStrict = attributeData . NamedArguments . FirstOrDefault ( x => x . Key == "Strict" ) . Value . Value is bool strict &&
57- strict ;
58- var x = interfaceSymbol ;
59- var parameters = x . Parameters
60- //.Where(static x => x.Type.MetadataName != "CancellationToken")
61- . ToArray ( ) ;
93+ return new InterfaceData (
94+ Namespace : GetCommonRootNamespace ( namespaces ) ?? namespaceName ,
95+ Name : className ,
96+ GoogleFunctionTool : generateGoogleFunctionTools ,
97+ Methods : methodList . ToArray ( ) ) ;
98+ }
99+ public static string ? GetCommonRootNamespace ( IEnumerable < string > namespaces )
100+ {
101+ // Convert the list of namespaces to a list of arrays split by "."
102+ var splitNamespaces = namespaces
103+ . Select ( ns => ns . Split ( '.' ) )
104+ . ToList ( ) ;
105+
106+ if ( ! splitNamespaces . Any ( ) || ! splitNamespaces [ 0 ] . Any ( ) )
107+ {
108+ return null ;
109+ }
62110
63- var methodData = new MethodData (
64- Name : x . Name ,
65- Description : GetDescription ( x ) ,
66- IsAsync : x . IsAsync || x . ReturnType . Name == "Task" ,
67- IsVoid : x . ReturnsVoid || x . ReturnType . MetadataName == "Task" ,
68- IsStrict : isStrict ,
69- Parameters : parameters . Select ( static y => y ) . ToArray ( ) ,
70- Descriptions : parameters . Select ( static l => GetParameterDescriptions ( l ) ) . SelectMany ( s => s )
71- . ToDictionary ( s => s . Key , s => s . Value ) ,
72- ReturnType : x . ReturnType
73- ) ;
111+ // Start with the first namespace parts
112+ var firstNsParts = splitNamespaces [ 0 ] ;
113+ var commonParts = new List < string > ( ) ;
74114
115+ // For each index in the first namespace
116+ for ( int i = 0 ; i < firstNsParts . Length ; i ++ )
117+ {
118+ // Check if every other namespace has the same part at this index
119+ string currentPart = firstNsParts [ i ] ;
120+ if ( splitNamespaces . All ( nsArr => nsArr . Length > i && nsArr [ i ] == currentPart ) )
121+ {
122+ commonParts . Add ( currentPart ) ;
123+ }
124+ else
125+ {
126+ // Stop the moment there is a mismatch
127+ break ;
128+ }
129+ }
75130
76- return new InterfaceData (
77- Namespace : interfaceSymbol . ContainingNamespace . ToDisplayString ( ) ,
78- Name : "I" + interfaceSymbol . Name ,
79- Methods : [ methodData ] ) ;
131+ return string . Join ( "." , commonParts ) ;
80132 }
81133
134+
82135 // private static Dictionary<string, bool> GetIsRequired(IParameterSymbol[] parameters, Dictionary<string, bool>? dics = null)
83136 // {
84137 // dics ??= new Dictionary<string, bool>();
0 commit comments