Skip to content

Refactor code base to remove dependency on 'Microsoft.Extensions.Logging' and merge 'Function' with 'FunctionInfo' #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/Azure.Functions.PowerShell.Worker.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
<PackageReference Include="Microsoft.PowerShell.SDK" Version="6.1.0-rc.1" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="CommandLineParser" Version="2.3.0" />
Expand Down
43 changes: 0 additions & 43 deletions src/Function/FunctionInfo.cs

This file was deleted.

39 changes: 0 additions & 39 deletions src/Function/FunctionLoader.cs

This file was deleted.

65 changes: 65 additions & 0 deletions src/FunctionLoader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

using System;
using Google.Protobuf.Collections;
using Microsoft.Azure.WebJobs.Script.Grpc.Messages;

namespace Microsoft.Azure.Functions.PowerShellWorker
{
internal class FunctionLoader
{
private readonly MapField<string, FunctionInfo> _loadedFunctions = new MapField<string, FunctionInfo>();

internal FunctionInfo GetFunctionInfo(string functionId)
{
if (_loadedFunctions.TryGetValue(functionId, out FunctionInfo funcInfo))
{
return funcInfo;
}

throw new InvalidOperationException($"Function with the ID '{functionId}' was not loaded.");
}

internal void Load(FunctionLoadRequest request)
{
// TODO: catch "load" issues at "func start" time.
// ex. Script doesn't exist, entry point doesn't exist
_loadedFunctions.Add(request.FunctionId, new FunctionInfo(request.Metadata));
}
}

internal class FunctionInfo
{
internal readonly string Directory;
internal readonly string EntryPoint;
internal readonly string FunctionName;
internal readonly string ScriptPath;
internal readonly MapField<string, BindingInfo> AllBindings;
internal readonly MapField<string, BindingInfo> OutputBindings;

public FunctionInfo(RpcFunctionMetadata metadata)
{
FunctionName = metadata.Name;
Directory = metadata.Directory;
EntryPoint = metadata.EntryPoint;
ScriptPath = metadata.ScriptFile;

AllBindings = new MapField<string, BindingInfo>();
OutputBindings = new MapField<string, BindingInfo>();

foreach (var binding in metadata.Bindings)
{
AllBindings.Add(binding.Key, binding.Value);

// PowerShell doesn't support the 'InOut' type binding
if (binding.Value.Direction == BindingInfo.Types.Direction.Out)
{
OutputBindings.Add(binding.Key, binding.Value);
}
}
}
}
}
33 changes: 15 additions & 18 deletions src/Http/HttpRequestContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,26 @@
//

using System;
using System.Collections.Generic;
using Google.Protobuf.Collections;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we shouldn't need this using anymore

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need it for the use of Dictionary<string, string> in HttpRequestContext. MapField<string, string> is case sensitive, we need case insensitive for powershell, so $req.Query.Name works for ?name=Joe.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I know. I meant remove Google.Protobuf.Collections since you removed the references to MapField

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. Will submit another PR to remove it.


namespace Microsoft.Azure.Functions.PowerShellWorker
{
/// <summary>
/// Custom type represent the context of the in-coming Http request.
/// </summary>
public class HttpRequestContext : IEquatable<HttpRequestContext>
public class HttpRequestContext
{
/// <summary>
/// Constructor for HttpRequestContext.
/// </summary>
public HttpRequestContext()
{
Headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Params = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Query = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
}

/// <summary>
/// Gets the Body of the Http request.
/// </summary>
Expand All @@ -21,7 +32,7 @@ public class HttpRequestContext : IEquatable<HttpRequestContext>
/// <summary>
/// Gets the Headers of the Http request.
/// </summary>
public MapField<string, string> Headers { get; internal set; }
public Dictionary<string, string> Headers { get; private set; }

/// <summary>
/// Gets the Method of the Http request.
Expand All @@ -36,30 +47,16 @@ public class HttpRequestContext : IEquatable<HttpRequestContext>
/// <summary>
/// Gets the Params of the Http request.
/// </summary>
public MapField<string, string> Params { get; internal set; }
public Dictionary<string, string> Params { get; private set; }

/// <summary>
/// Gets the Query of the Http request.
/// </summary>
public MapField<string, string> Query { get; internal set; }
public Dictionary<string, string> Query { get; private set; }

/// <summary>
/// Gets the RawBody of the Http request.
/// </summary>
public object RawBody { get; internal set; }

/// <summary>
/// Compare with another HttpRequestContext object.
/// </summary>
public bool Equals(HttpRequestContext other)
{
return Method == other.Method
&& Url == other.Url
&& Headers.Equals(other.Headers)
&& Params.Equals(other.Params)
&& Query.Equals(other.Query)
&& (Body == other.Body || Body.Equals(other.Body))
&& (RawBody == other.RawBody || RawBody.Equals(other.RawBody));
}
}
}
29 changes: 3 additions & 26 deletions src/Http/HttpResponseContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
//

using System;
using System.Collections;
using System.Collections.Generic;

namespace Microsoft.Azure.Functions.PowerShellWorker
{
/// <summary>
/// Custom type represent the context of the Http response.
/// </summary>
public class HttpResponseContext : IEquatable<HttpResponseContext>
public class HttpResponseContext
{
/// <summary>
/// Gets or sets the Body of the Http response.
Expand All @@ -31,34 +31,11 @@ public class HttpResponseContext : IEquatable<HttpResponseContext>
/// <summary>
/// Gets or sets the Headers of the Http response.
/// </summary>
public Hashtable Headers { get; set; } = new Hashtable();
public Dictionary<string, string> Headers { get; set; } = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

/// <summary>
/// Gets or sets the StatusCode of the Http response.
/// </summary>
public string StatusCode { get; set; } = "200";

/// <summary>
/// Compare with another HttpResponseContext object.
/// </summary>
public bool Equals(HttpResponseContext other)
{
bool sameHeaders = true;
foreach (DictionaryEntry dictionaryEntry in Headers)
{
if (!other.Headers.ContainsKey(dictionaryEntry.Key)
|| dictionaryEntry.Value != other.Headers[dictionaryEntry.Key])
{
sameHeaders = false;
break;
}
}

return ContentType == other.ContentType
&& EnableContentNegotiation == other.EnableContentNegotiation
&& StatusCode == other.StatusCode
&& sameHeaders
&& (Body == other.Body || Body.Equals(other.Body));
}
}
}
6 changes: 3 additions & 3 deletions src/Messaging/MessagingStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.Messaging
{
internal class MessagingStream : IDisposable
{
private SemaphoreSlim _writeStreamHandle = new SemaphoreSlim(1, 1);
private SemaphoreSlim _writeSemaphore = new SemaphoreSlim(1, 1);
private AsyncDuplexStreamingCall<StreamingMessage, StreamingMessage> _call;
private bool isDisposed;

Expand Down Expand Up @@ -45,14 +45,14 @@ public async Task WriteAsync(StreamingMessage message)

// Wait for the handle to be released because we can't have
// more than one message being sent at the same time
await _writeStreamHandle.WaitAsync();
await _writeSemaphore.WaitAsync();
try
{
await _call.RequestStream.WriteAsync(message);
}
finally
{
_writeStreamHandle.Release();
_writeSemaphore.Release();
}
}
}
Expand Down
Loading