Skip to content

Commit

Permalink
.Net: Rename SKFunction factory methods (#3371)
Browse files Browse the repository at this point in the history
The only difference between these two methods should be how the method
information is provided, whether it's a single delegate or a combination
of a MethodInfo and a target object. This PR thus renames both methods
to just be overloads of the same Create method, accepting the same
parameters other than the exact ones which specify the method. It also
cleans up the implementation in various ways, removing code duplication,
removing extraneous allocation, fixing XML comments, etc.

Fixes #3165

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [x] The code builds clean without any errors or warnings
- [x] The PR follows the [SK Contribution
Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
and the [pre-submission formatting
script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts)
raises no violations
- [x] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
  • Loading branch information
stephentoub committed Nov 10, 2023
1 parent e3ff3e3 commit 4f4a663
Show file tree
Hide file tree
Showing 14 changed files with 337 additions and 383 deletions.
Expand Up @@ -160,7 +160,7 @@ string MyFunctionAsync(SKContext context)

List<ISKFunction> functions = new()
{
SKFunction.FromNativeMethod(Method(MyFunctionAsync), this),
SKFunction.Create(Method(MyFunctionAsync), this),
};

Assert.NotNull(functions[0]);
Expand Down Expand Up @@ -190,7 +190,7 @@ string MyFunctionAsync(SKContext context)
return $"F({context.Variables.Input})";
}

var func = SKFunction.FromNativeMethod(Method(MyFunctionAsync), this);
var func = SKFunction.Create(Method(MyFunctionAsync), this);

Assert.NotNull(func);

Expand Down Expand Up @@ -223,7 +223,7 @@ public async Task ItRendersCodeUsingNamedVariablesAsync()
return $"[{dateStr}] {name} ({age}): \"{slogan}\"";
}

var func = SKFunction.FromNativeMethod(Method(MyFunctionAsync), this);
var func = SKFunction.Create(Method(MyFunctionAsync), this);

Assert.NotNull(func);

Expand Down Expand Up @@ -257,7 +257,7 @@ public async Task ItHandlesSyntaxErrorsAsync()
return $"[{dateStr}] {name} ({age}): \"{slogan}\"";
}

ISKFunction func = SKFunction.FromNativeMethod(Method(MyFunctionAsync), this);
ISKFunction func = SKFunction.Create(Method(MyFunctionAsync), this);
Assert.NotNull(func);

this._variables.Set("input", "Mario");
Expand Down Expand Up @@ -286,7 +286,7 @@ public async Task ItRendersCodeUsingImplicitInputAndNamedVariablesAsync()
return $"[{dateStr}] {name} ({age}): \"{slogan}\"";
}

ISKFunction func = SKFunction.FromNativeMethod(Method(MyFunctionAsync), this);
ISKFunction func = SKFunction.Create(Method(MyFunctionAsync), this);

Assert.NotNull(func);

Expand Down Expand Up @@ -338,9 +338,9 @@ string MyFunction3Async(SKContext context)

var functions = new List<ISKFunction>()
{
SKFunction.FromNativeMethod(Method(MyFunction1Async), this, "func1"),
SKFunction.FromNativeMethod(Method(MyFunction2Async), this, "func2"),
SKFunction.FromNativeMethod(Method(MyFunction3Async), this, "func3")
SKFunction.Create(Method(MyFunction1Async), this, "func1"),
SKFunction.Create(Method(MyFunction2Async), this, "func2"),
SKFunction.Create(Method(MyFunction3Async), this, "func3")
};

this.MockFunctionRunner(functions);
Expand All @@ -363,7 +363,7 @@ Task<string> MyFunctionAsync(SKContext context)
return Task.FromResult(context.Variables.Input);
}

ISKFunction func = SKFunction.FromNativeMethod(Method(MyFunctionAsync), this);
ISKFunction func = SKFunction.Create(Method(MyFunctionAsync), this);
Assert.NotNull(func);

this._variables.Set("myVar", "BAR");
Expand Down
Expand Up @@ -160,7 +160,7 @@ string MyFunctionAsync(SKContext context)

List<ISKFunction> functions = new()
{
SKFunction.FromNativeMethod(Method(MyFunctionAsync), this),
SKFunction.Create(Method(MyFunctionAsync), this),
};

Assert.NotNull(functions[0]);
Expand Down Expand Up @@ -189,7 +189,7 @@ string MyFunctionAsync(SKContext context)
return $"F({context.Variables.Input})";
}

var func = SKFunction.FromNativeMethod(Method(MyFunctionAsync), this);
var func = SKFunction.Create(Method(MyFunctionAsync), this);

Assert.NotNull(func);

Expand Down Expand Up @@ -221,7 +221,7 @@ public async Task ItRendersCodeUsingNamedVariablesAsync()
return $"[{dateStr}] {name} ({age}): \"{slogan}\"";
}

var func = SKFunction.FromNativeMethod(Method(MyFunctionAsync), this);
var func = SKFunction.Create(Method(MyFunctionAsync), this);

Assert.NotNull(func);

Expand Down Expand Up @@ -254,7 +254,7 @@ public async Task ItHandlesSyntaxErrorsAsync()
return $"[{dateStr}] {name} ({age}): \"{slogan}\"";
}

ISKFunction func = SKFunction.FromNativeMethod(Method(MyFunctionAsync), this);
ISKFunction func = SKFunction.Create(Method(MyFunctionAsync), this);
Assert.NotNull(func);

this._variables.Set("input", "Mario");
Expand Down Expand Up @@ -282,7 +282,7 @@ public async Task ItRendersCodeUsingImplicitInputAndNamedVariablesAsync()
return $"[{dateStr}] {name} ({age}): \"{slogan}\"";
}

ISKFunction func = SKFunction.FromNativeMethod(Method(MyFunctionAsync), this);
ISKFunction func = SKFunction.Create(Method(MyFunctionAsync), this);

Assert.NotNull(func);

Expand Down Expand Up @@ -332,9 +332,9 @@ string MyFunction3Async(SKContext context)

var functions = new List<ISKFunction>()
{
SKFunction.FromNativeMethod(Method(MyFunction1Async), this, "func1"),
SKFunction.FromNativeMethod(Method(MyFunction2Async), this, "func2"),
SKFunction.FromNativeMethod(Method(MyFunction3Async), this, "func3")
SKFunction.Create(Method(MyFunction1Async), this, "func1"),
SKFunction.Create(Method(MyFunction2Async), this, "func2"),
SKFunction.Create(Method(MyFunction3Async), this, "func3")
};

this.MockFunctionRunner(functions);
Expand All @@ -357,7 +357,7 @@ Task<string> MyFunctionAsync(SKContext context)
return Task.FromResult(context.Variables.Input);
}

ISKFunction func = SKFunction.FromNativeMethod(Method(MyFunctionAsync), this);
ISKFunction func = SKFunction.Create(Method(MyFunctionAsync), this);
Assert.NotNull(func);

this._variables.Set("myVar", "BAR");
Expand Down
Expand Up @@ -184,8 +184,8 @@ async Task<SKContext> ExecuteAsync(SKContext context)
return context;
}

var function = SKFunction.FromNativeFunction(
nativeFunction: ExecuteAsync,
var function = SKFunction.Create(
method: ExecuteAsync,
parameters: operationParameters.ToList(),
description: operation.Name,
pluginName: pluginName,
Expand Down
Expand Up @@ -265,8 +265,8 @@ async Task<RestApiOperationResponse> ExecuteAsync(SKContext context)
})
.ToList();

var function = SKFunction.FromNativeFunction(
nativeFunction: ExecuteAsync,
var function = SKFunction.Create(
method: ExecuteAsync,
parameters: parameters,
description: operation.Description,
pluginName: pluginName,
Expand Down

0 comments on commit 4f4a663

Please sign in to comment.