Skip to content

Commit 3a668ef

Browse files
author
machibuse
committed
Refactor ListWrappers to introduce IListWithRangeAdd interface and update RepeatedFieldGuidWrapper to implement it. Update PropertyVisitor and ClassVisitor to support the changes.
1 parent f9d7ac9 commit 3a668ef

4 files changed

Lines changed: 124 additions & 171 deletions

File tree

Source/Porticle.Grpc.TypeMapper/ClassVisitor.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ public class ClassVisitor(TaskLoggingHelper log, bool wrapAllNonNullableStrings,
2222
var propertyVisitor = new PropertyVisitor(log, wrapAllNonNullableStrings, wrapAllNullableStringValues);
2323
node = (ClassDeclarationSyntax)propertyVisitor.Visit(node);
2424

25-
if (propertyVisitor.NeedGuidConverter) node = node.AddMembers(ClassFromSource(ListWrappers.RepeatedFieldGuidWrapper));
25+
if (propertyVisitor.NeedGuidConverter)
26+
{
27+
node = node.AddMembers(ClassFromSource(ListWrappers.RepeatedFieldGuidWrapper));
28+
node = node.AddMembers(InterfaceFromSource(ListWrappers.IListWithRangeAdd));
29+
}
2630

2731
var methodVisitor = new MethodVisitor(propertyVisitor.ReplaceProps);
2832
node = (ClassDeclarationSyntax)methodVisitor.Visit(node);
@@ -34,11 +38,15 @@ private static ClassDeclarationSyntax ClassFromSource(string classCode)
3438
{
3539
var syntaxTree = CSharpSyntaxTree.ParseText(classCode);
3640
var root = syntaxTree.GetRoot();
37-
var nestedClass = root.DescendantNodes()
38-
.OfType<ClassDeclarationSyntax>()
39-
.Single();
40-
return nestedClass
41-
.WithLeadingTrivia(SyntaxFactory.CarriageReturnLineFeed)
42-
.WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);
41+
var nestedClass = root.DescendantNodes().OfType<ClassDeclarationSyntax>().Single();
42+
return nestedClass.WithLeadingTrivia(SyntaxFactory.CarriageReturnLineFeed).WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);
43+
}
44+
45+
private static InterfaceDeclarationSyntax InterfaceFromSource(string classCode)
46+
{
47+
var syntaxTree = CSharpSyntaxTree.ParseText(classCode);
48+
var root = syntaxTree.GetRoot();
49+
var nestedClass = root.DescendantNodes().OfType<InterfaceDeclarationSyntax>().Single();
50+
return nestedClass.WithLeadingTrivia(SyntaxFactory.CarriageReturnLineFeed).WithTrailingTrivia(SyntaxFactory.CarriageReturnLineFeed);
4351
}
4452
}

Source/Porticle.Grpc.TypeMapper/ListWrappers.cs

Lines changed: 75 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,80 +2,91 @@
22

33
public static class ListWrappers
44
{
5-
public static string RepeatedFieldGuidWrapper =
6-
"""
7-
class RepeatedFieldGuidWrapper : System.Collections.Generic.IList<System.Guid>
8-
{
9-
private readonly Google.Protobuf.Collections.RepeatedField<string> _internList;
5+
public static string IListWithRangeAdd = """
6+
public interface IListWithRangeAdd<T> : System.Collections.Generic.IList<T>
7+
{
8+
void Add(IEnumerable<T> items);
9+
}
10+
""";
1011

11-
public RepeatedFieldGuidWrapper(Google.Protobuf.Collections.RepeatedField<string> internList)
12-
{
13-
this._internList = internList;
14-
}
12+
public static string RepeatedFieldGuidWrapper = """
13+
class RepeatedFieldGuidWrapper : IListWithRangeAdd<System.Guid>
14+
{
15+
private readonly Google.Protobuf.Collections.RepeatedField<string> _internList;
1516
16-
public System.Collections.Generic.IEnumerator<System.Guid> GetEnumerator()
17-
{
18-
using var enumerator = _internList.GetEnumerator();
19-
while (enumerator.MoveNext())
20-
{
21-
yield return System.Guid.Parse(enumerator.Current);
22-
}
23-
}
17+
public RepeatedFieldGuidWrapper(Google.Protobuf.Collections.RepeatedField<string> internList)
18+
{
19+
this._internList = internList;
20+
}
2421
25-
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
26-
{
27-
return GetEnumerator();
28-
}
22+
public System.Collections.Generic.IEnumerator<System.Guid> GetEnumerator()
23+
{
24+
using var enumerator = _internList.GetEnumerator();
25+
while (enumerator.MoveNext())
26+
{
27+
yield return System.Guid.Parse(enumerator.Current);
28+
}
29+
}
2930
30-
public void Add(System.Guid item)
31-
{
32-
_internList.Add(item.ToString("D"));
33-
}
31+
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
32+
{
33+
return GetEnumerator();
34+
}
3435
35-
public void Clear()
36-
{
37-
_internList.Clear();
38-
}
36+
public void Add(System.Guid item)
37+
{
38+
_internList.Add(item.ToString("D"));
39+
}
3940
40-
public bool Contains(System.Guid item)
41-
{
42-
return _internList.Contains(item.ToString("D"));
43-
}
41+
public void Add(IEnumerable<System.Guid> items)
42+
{
43+
_internList.AddRange(items.Select(i => i.ToString("D")));
44+
}
4445
45-
public void CopyTo(System.Guid[] array, int arrayIndex)
46-
{
47-
System.Linq.Enumerable.ToArray(System.Linq.Enumerable.Select(_internList, System.Guid.Parse)).CopyTo(array, arrayIndex);
48-
}
46+
public void Clear()
47+
{
48+
_internList.Clear();
49+
}
4950
50-
public bool Remove(System.Guid item)
51-
{
52-
return _internList.Remove(item.ToString("D"));
53-
}
51+
public bool Contains(System.Guid item)
52+
{
53+
return _internList.Contains(item.ToString("D"));
54+
}
5455
55-
public int Count => _internList.Count;
56-
57-
public bool IsReadOnly => _internList.IsReadOnly;
58-
59-
public int IndexOf(System.Guid item)
60-
{
61-
return _internList.IndexOf(item.ToString("D"));
62-
}
56+
public void CopyTo(System.Guid[] array, int arrayIndex)
57+
{
58+
System.Linq.Enumerable.ToArray(System.Linq.Enumerable.Select(_internList, System.Guid.Parse)).CopyTo(array, arrayIndex);
59+
}
6360
64-
public void Insert(int index, System.Guid item)
65-
{
66-
_internList.Insert(index, item.ToString("D"));
67-
}
61+
public bool Remove(System.Guid item)
62+
{
63+
return _internList.Remove(item.ToString("D"));
64+
}
6865
69-
public void RemoveAt(int index)
70-
{
71-
_internList.RemoveAt(index);
72-
}
66+
public int Count => _internList.Count;
67+
68+
public bool IsReadOnly => _internList.IsReadOnly;
69+
70+
public int IndexOf(System.Guid item)
71+
{
72+
return _internList.IndexOf(item.ToString("D"));
73+
}
7374
74-
public System.Guid this[int index]
75-
{
76-
get => System.Guid.Parse(_internList[index]);
77-
set => _internList[index] = value.ToString("D");
78-
}
79-
}
80-
""";
75+
public void Insert(int index, System.Guid item)
76+
{
77+
_internList.Insert(index, item.ToString("D"));
78+
}
79+
80+
public void RemoveAt(int index)
81+
{
82+
_internList.RemoveAt(index);
83+
}
84+
85+
public System.Guid this[int index]
86+
{
87+
get => System.Guid.Parse(_internList[index]);
88+
set => _internList[index] = value.ToString("D");
89+
}
90+
}
91+
""";
8192
}

0 commit comments

Comments
 (0)