Skip to content

Commit ffb62cc

Browse files
committed
improve PluginFactory.CreatePlugin to be able to accept IPluginGraphAttributes instead of PluginGraphAttributes
1 parent 9ed0e1f commit ffb62cc

File tree

5 files changed

+137
-37
lines changed

5 files changed

+137
-37
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// SPDX-FileCopyrightText: 2023 smdn <smdn@smdn.jp>
2+
// SPDX-License-Identifier: MIT
3+
4+
using System;
5+
using System.Collections.Generic;
6+
7+
namespace Smdn.Net.MuninPlugin;
8+
9+
#pragma warning disable IDE0040
10+
partial class PluginFactory {
11+
#pragma warning restore IDE0040
12+
private const string ObsoleteMessageUseOverloadWithIPluginGraphAttributes = "Use overloads that accept IPluginGraphAttributes instead.";
13+
14+
/// <summary>Create a plugin with one field which fetches the value from a delegate.</summary>
15+
[Obsolete(ObsoleteMessageUseOverloadWithIPluginGraphAttributes)]
16+
public static IPlugin CreatePlugin(
17+
string name,
18+
string fieldLabel,
19+
Func<double?> fetchFieldValue,
20+
PluginGraphAttributes graphAttributes
21+
)
22+
=> CreatePlugin(
23+
name: name,
24+
fieldLabel: fieldLabel,
25+
fetchFieldValue: fetchFieldValue,
26+
graphAttributes: (IPluginGraphAttributes)graphAttributes ?? throw new ArgumentNullException(nameof(graphAttributes))
27+
);
28+
29+
/// <summary>Create a plugin with one field which fetches the value from a delegate.</summary>
30+
[Obsolete(ObsoleteMessageUseOverloadWithIPluginGraphAttributes)]
31+
public static IPlugin CreatePlugin(
32+
string name,
33+
string fieldLabel,
34+
PluginFieldGraphStyle fieldGraphStyle,
35+
Func<double?> fetchFieldValue,
36+
PluginGraphAttributes graphAttributes
37+
)
38+
=> CreatePlugin(
39+
name: name,
40+
fieldLabel: fieldLabel,
41+
fieldGraphStyle: fieldGraphStyle,
42+
fetchFieldValue: fetchFieldValue,
43+
graphAttributes: (IPluginGraphAttributes)graphAttributes ?? throw new ArgumentNullException(nameof(graphAttributes))
44+
);
45+
46+
/// <summary>Create a plugin which has one field.</summary>
47+
[Obsolete(ObsoleteMessageUseOverloadWithIPluginGraphAttributes)]
48+
public static IPlugin CreatePlugin(
49+
string name,
50+
PluginGraphAttributes graphAttributes,
51+
PluginFieldBase field
52+
)
53+
=> CreatePlugin(
54+
name: name,
55+
graphAttributes: (IPluginGraphAttributes)graphAttributes ?? throw new ArgumentNullException(nameof(graphAttributes)),
56+
field: field
57+
);
58+
59+
/// <summary>Create a plugin which has multiple fields.</summary>
60+
[Obsolete(ObsoleteMessageUseOverloadWithIPluginGraphAttributes)]
61+
public static IPlugin CreatePlugin(
62+
string name,
63+
PluginGraphAttributes graphAttributes,
64+
IReadOnlyCollection<PluginFieldBase> fields
65+
)
66+
=> CreatePlugin(
67+
name: name,
68+
graphAttributes: (IPluginGraphAttributes)graphAttributes ?? throw new ArgumentNullException(nameof(graphAttributes)),
69+
fields: fields
70+
);
71+
72+
/// <summary>Create a plugin which has multiple fields.</summary>
73+
[Obsolete(ObsoleteMessageUseOverloadWithIPluginGraphAttributes)]
74+
public static IPlugin CreatePlugin(
75+
string name,
76+
PluginGraphAttributes graphAttributes,
77+
IReadOnlyCollection<IPluginField> fields
78+
)
79+
=> CreatePlugin(
80+
name: name,
81+
graphAttributes: (IPluginGraphAttributes)graphAttributes ?? throw new ArgumentNullException(nameof(graphAttributes)),
82+
fields: fields
83+
);
84+
}

src/Smdn.Net.MuninNode/Smdn.Net.MuninPlugin/PluginFactory.CreatePlugin.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static IPlugin CreatePlugin(
1414
string name,
1515
string fieldLabel,
1616
Func<double?> fetchFieldValue,
17-
PluginGraphAttributes graphAttributes
17+
IPluginGraphAttributes graphAttributes
1818
)
1919
=> CreatePlugin(
2020
name: name,
@@ -30,7 +30,7 @@ public static IPlugin CreatePlugin(
3030
string fieldLabel,
3131
PluginFieldGraphStyle fieldGraphStyle,
3232
Func<double?> fetchFieldValue,
33-
PluginGraphAttributes graphAttributes
33+
IPluginGraphAttributes graphAttributes
3434
)
3535
=> CreatePlugin(
3636
name: name,
@@ -49,7 +49,7 @@ PluginGraphAttributes graphAttributes
4949
/// <summary>Create a plugin which has one field.</summary>
5050
public static IPlugin CreatePlugin(
5151
string name,
52-
PluginGraphAttributes graphAttributes,
52+
IPluginGraphAttributes graphAttributes,
5353
PluginFieldBase field
5454
)
5555
=> CreatePlugin(
@@ -61,10 +61,10 @@ PluginFieldBase field
6161
/// <summary>Create a plugin which has multiple fields.</summary>
6262
public static IPlugin CreatePlugin(
6363
string name,
64-
PluginGraphAttributes graphAttributes,
64+
IPluginGraphAttributes graphAttributes,
6565
IReadOnlyCollection<PluginFieldBase> fields
6666
)
67-
=> new Plugin(
67+
=> new DefaultPlugin(
6868
name: name,
6969
graphAttributes: graphAttributes,
7070
fields: fields ?? throw new ArgumentNullException(nameof(fields))
@@ -73,10 +73,10 @@ IReadOnlyCollection<PluginFieldBase> fields
7373
/// <summary>Create a plugin which has multiple fields.</summary>
7474
public static IPlugin CreatePlugin(
7575
string name,
76-
PluginGraphAttributes graphAttributes,
76+
IPluginGraphAttributes graphAttributes,
7777
IReadOnlyCollection<IPluginField> fields
7878
)
79-
=> new Plugin(
79+
=> new DefaultPlugin(
8080
name: name,
8181
graphAttributes: graphAttributes,
8282
fields: fields ?? throw new ArgumentNullException(nameof(fields))
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// SPDX-FileCopyrightText: 2025 smdn <smdn@smdn.jp>
2+
// SPDX-License-Identifier: MIT
3+
4+
using System;
5+
using System.Collections.Generic;
6+
7+
namespace Smdn.Net.MuninPlugin;
8+
9+
#pragma warning disable IDE0040
10+
partial class PluginFactory {
11+
#pragma warning restore IDE0040
12+
private sealed class DefaultPlugin : IPlugin, IPluginDataSource {
13+
public string Name { get; }
14+
15+
public IPluginGraphAttributes GraphAttributes { get; }
16+
public IReadOnlyCollection<IPluginField> Fields { get; }
17+
18+
public IPluginDataSource DataSource => this;
19+
public INodeSessionCallback? SessionCallback => null;
20+
21+
public DefaultPlugin(
22+
string name,
23+
IPluginGraphAttributes graphAttributes,
24+
IReadOnlyCollection<IPluginField> fields
25+
)
26+
{
27+
ArgumentExceptionShim.ThrowIfNullOrEmpty(name, nameof(name));
28+
29+
Name = name;
30+
GraphAttributes = graphAttributes ?? throw new ArgumentNullException(nameof(graphAttributes));
31+
Fields = fields ?? throw new ArgumentNullException(nameof(fields));
32+
}
33+
}
34+
}

tests/Smdn.Net.MuninNode/Smdn.Net.MuninNode.Protocol/MuninProtocolHandler.Command.Config.cs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private static System.Collections.IEnumerable YieldTestCases_HandleCommandAsync_
6060
var plugins = new[] {
6161
PluginFactory.CreatePlugin(
6262
"plugin1",
63-
graphAttrs,
63+
(IPluginGraphAttributes)graphAttrs,
6464
new[] {
6565
PluginFactory.CreateField("plugin1field1", static () => 1.1),
6666
PluginFactory.CreateField("plugin1field2", PluginFieldGraphStyle.LineWidth3, static () => 1.2)
@@ -71,7 +71,7 @@ private static System.Collections.IEnumerable YieldTestCases_HandleCommandAsync_
7171
"plugin2field1",
7272
PluginFieldGraphStyle.Area,
7373
static () => 2.1,
74-
graphAttrs
74+
(IPluginGraphAttributes)graphAttrs
7575
),
7676
};
7777

@@ -159,7 +159,7 @@ private static System.Collections.IEnumerable YieldTestCases_HandleCommandAsync_
159159

160160
var plugin = PluginFactory.CreatePlugin(
161161
"plugin1",
162-
graphAttrs,
162+
(IPluginGraphAttributes)graphAttrs,
163163
new[] {
164164
PluginFactory.CreateField("plugin1field1", static () => 1.1),
165165
}
@@ -203,7 +203,7 @@ private static System.Collections.IEnumerable YieldTestCases_HandleCommandAsync_
203203

204204
var plugin = PluginFactory.CreatePlugin(
205205
"plugin2",
206-
graphAttrs,
206+
(IPluginGraphAttributes)graphAttrs,
207207
new[] {
208208
PluginFactory.CreateField("plugin1field1", static () => 2.1),
209209
}
@@ -259,7 +259,7 @@ private static System.Collections.IEnumerable YieldTestCases_HandleCommandAsync_
259259

260260
var plugin = PluginFactory.CreatePlugin(
261261
"plugin1",
262-
graphAttrs,
262+
(IPluginGraphAttributes)graphAttrs,
263263
new[] {
264264
PluginFactory.CreateField("plugin1field1", static () => 1.1),
265265
PluginFactory.CreateField("plugin1field2", PluginFieldGraphStyle.LineWidth3, static () => 1.2)
@@ -315,7 +315,7 @@ private static System.Collections.IEnumerable YieldTestCases_HandleCommandAsync_
315315

316316
var plugin = PluginFactory.CreatePlugin(
317317
"plugin1",
318-
graphAttrs,
318+
(IPluginGraphAttributes)graphAttrs,
319319
new[] {
320320
PluginFactory.CreateField("plugin1field1", static () => 1.1),
321321
PluginFactory.CreateField("plugin1field2", PluginFieldGraphStyle.LineWidth3, static () => 1.2)
@@ -385,7 +385,7 @@ CancellationToken cancellationToken
385385
"field",
386386
style,
387387
static () => 0.0,
388-
graphAttrs
388+
(IPluginGraphAttributes)graphAttrs
389389
),
390390
};
391391

@@ -465,7 +465,7 @@ public async Task HandleCommandAsync_ConfigCommand_WarningAndCriticalField(
465465
var plugins = new[] {
466466
PluginFactory.CreatePlugin(
467467
"plugin",
468-
graphAttrs,
468+
(IPluginGraphAttributes)graphAttrs,
469469
new[] { field }
470470
),
471471
};
@@ -506,7 +506,7 @@ public async Task HandleCommandAsync_ConfigCommand_NegativeField(CancellationTok
506506
var plugins = new[] {
507507
PluginFactory.CreatePlugin(
508508
"plugin",
509-
graphAttrs,
509+
(IPluginGraphAttributes)graphAttrs,
510510
new[] {
511511
PluginFactory.CreateField(
512512
name: PositiveFieldName,
@@ -568,13 +568,7 @@ [Values] bool enableDirtyConfig
568568
name: PluginName,
569569
fieldLabel: "field",
570570
fetchFieldValue: static () => 0.0,
571-
graphAttributes: new PluginGraphAttributes(
572-
title: GraphTitle,
573-
category: "test",
574-
verticalLabel: "test",
575-
scale: false,
576-
arguments: "--args"
577-
)
571+
graphAttributes: new PluginGraphAttributesBuilder(title: GraphTitle).Build()
578572
)
579573
])
580574
}

tests/Smdn.Net.MuninNode/Smdn.Net.MuninNode.Protocol/MuninProtocolHandler.Command.Fetch.cs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@ partial class MuninProtocolHandlerTests {
1616
#pragma warning restore IDE0040
1717
private static System.Collections.IEnumerable YieldTestCases_HandleCommandAsync_FetchCommand()
1818
{
19-
var graphAttrs = new PluginGraphAttributes(
20-
title: "title",
21-
category: "test",
22-
verticalLabel: "test",
23-
scale: false,
24-
arguments: "--args"
25-
);
19+
var graphAttrs = new PluginGraphAttributesBuilder(title: "title").Build();
2620

2721
var plugins = new[] {
2822
PluginFactory.CreatePlugin(
@@ -144,13 +138,7 @@ [Values] bool enableDirtyConfig
144138
name: PluginName,
145139
fieldLabel: "field",
146140
fetchFieldValue: static () => 0.0,
147-
graphAttributes: new PluginGraphAttributes(
148-
title: "title",
149-
category: "test",
150-
verticalLabel: "test",
151-
scale: false,
152-
arguments: "--args"
153-
)
141+
graphAttributes: new PluginGraphAttributesBuilder(title: "title").Build()
154142
)
155143
])
156144
}

0 commit comments

Comments
 (0)