1+ using FlowSynx . PluginCore ;
2+ using FlowSynx . PluginCore . Extensions ;
3+ using FlowSynx . PluginCore . Helpers ;
4+ using FlowSynx . Plugins . Template . Models ;
5+
6+ namespace FlowSynx . Plugins . Template ;
7+
8+ public class MathPlugin : IPlugin
9+ {
10+ private IPluginLogger ? _logger ;
11+ private bool _isInitialized ;
12+
13+ public PluginMetadata Metadata => new ( )
14+ {
15+ Id = Guid . Parse ( "5cb7390d-d578-4cd0-8368-b73d8bae1b0a" ) ,
16+ Name = "MyPlugin" ,
17+ CompanyName = "MyCompany" ,
18+ Description = "This is the test plugin for FlowSynx workflow engine." ,
19+ Version = new Version ( 1 , 0 , 0 ) ,
20+ Category = PluginCategory . Data ,
21+ Authors = new List < string > { "MyCompany" } ,
22+ Copyright = "© MyCompany. All rights reserved." ,
23+ Icon = "flowsynx.png" ,
24+ ReadMe = "README.md" ,
25+ ProjectUrl = "https://example.com" ,
26+ Tags = new List < string > ( ) { "mycompany" , "test-plugin" , "data" , "data-platform" , "plugin" , "flowsynx" } ,
27+ MinimumFlowSynxVersion = new Version ( 1 , 0 , 0 ) ,
28+ } ;
29+
30+ public PluginSpecifications ? Specifications { get ; set ; }
31+
32+ public Type SpecificationsType => typeof ( MathPluginSpecifications ) ;
33+
34+ private Dictionary < string , Func < InputParameter , CancellationToken , Task < object ? > > > OperationMap
35+ => new ( StringComparer . OrdinalIgnoreCase )
36+ {
37+ [ "plus" ] = ExecuteAddAsync ,
38+ [ "subtract" ] = ExecuteSubtractAsync ,
39+ [ "multiply" ] = ExecuteMultiplyAsync ,
40+ [ "divide" ] = ExecuteDivideAsync
41+ } ;
42+
43+ public IReadOnlyCollection < string > SupportedOperations => OperationMap . Keys ;
44+
45+ public Task Initialize ( IPluginLogger logger )
46+ {
47+ if ( ReflectionHelper . IsCalledViaReflection ( ) )
48+ throw new InvalidOperationException ( "Reflection-based access is not allowed." ) ;
49+
50+ ArgumentNullException . ThrowIfNull ( logger ) ;
51+ _logger = logger ;
52+ _isInitialized = true ;
53+ return Task . CompletedTask ;
54+ }
55+
56+ public async Task < object ? > ExecuteAsync ( PluginParameters parameters , CancellationToken cancellationToken )
57+ {
58+ cancellationToken . ThrowIfCancellationRequested ( ) ;
59+
60+ if ( ReflectionHelper . IsCalledViaReflection ( ) )
61+ throw new InvalidOperationException ( "Reflection-based access is not allowed." ) ;
62+
63+ if ( ! _isInitialized )
64+ throw new InvalidOperationException ( $ "Plugin '{ Metadata . Name } ' v{ Metadata . Version } is not initialized.") ;
65+
66+ var inputParameter = parameters . ToObject < InputParameter > ( ) ;
67+ if ( ! OperationMap . TryGetValue ( inputParameter . Operation , out var handler ) )
68+ throw new NotSupportedException ( $ "Operation '{ inputParameter . Operation } ' is not supported.") ;
69+
70+ return await handler ( inputParameter , cancellationToken ) ;
71+ }
72+
73+ #region private methods
74+ private Task < object ? > ExecuteAddAsync ( InputParameter parameters , CancellationToken cancellationToken )
75+ {
76+ double result = parameters . Number1 + parameters . Number2 ;
77+ _logger ? . LogInfo ( $ "Adding { parameters . Number1 } + { parameters . Number2 } = { result } ") ;
78+ return Task . FromResult < object ? > ( result ) ;
79+ }
80+
81+ private Task < object ? > ExecuteSubtractAsync ( InputParameter parameters , CancellationToken cancellationToken )
82+ {
83+ double result = parameters . Number1 - parameters . Number2 ;
84+ _logger ? . LogInfo ( $ "Subtracting { parameters . Number1 } - { parameters . Number2 } = { result } ") ;
85+ return Task . FromResult < object ? > ( result ) ;
86+ }
87+
88+ private Task < object ? > ExecuteMultiplyAsync ( InputParameter parameters , CancellationToken cancellationToken )
89+ {
90+ double result = parameters . Number1 * parameters . Number2 ;
91+ _logger ? . LogInfo ( $ "Multiplying { parameters . Number1 } * { parameters . Number2 } = { result } ") ;
92+ return Task . FromResult < object ? > ( result ) ;
93+ }
94+
95+ private Task < object ? > ExecuteDivideAsync ( InputParameter parameters , CancellationToken cancellationToken )
96+ {
97+ if ( parameters . Number2 == 0 )
98+ {
99+ _logger ? . LogError ( "Division by zero attempted." ) ;
100+ throw new DivideByZeroException ( "Cannot divide by zero." ) ;
101+ }
102+ double result = parameters . Number1 / parameters . Number2 ;
103+ _logger ? . LogInfo ( $ "Dividing { parameters . Number1 } / { parameters . Number2 } = { result } ") ;
104+ return Task . FromResult < object ? > ( result ) ;
105+ }
106+ #endregion
107+ }
0 commit comments