Skip to content
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

Parser update to handle in file variables #3309

Merged
merged 4 commits into from
Nov 10, 2023
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Microsoft.DotNet.Interactive.Http.Parsing.Parsing
{
internal class DeclaredVariable
{
public string Name { get; }
public string Value { get; }

public HttpBindingResult<string> HttpBindingResult { get; }
public DeclaredVariable(string name, string value, HttpBindingResult<string> httpBindingResult)
{
Name = name;
Value = value;
HttpBindingResult = httpBindingResult;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ public void Add(HttpCommentNode node)

public HttpBindingResult<HttpRequestMessage> TryGetHttpRequestMessage(HttpBindingDelegate bind)
{
var declaredVariables = SyntaxTree?.RootNode.GetDeclaredVariables();
if (declaredVariables?.Count > 0)
{
bind = node =>
{
if (declaredVariables.TryGetValue(node.Text, out var declaredValue))
{
return HttpBindingResult<object?>.Success(declaredValue.Value);
}
else { return bind(node); }
};
}
var request = new HttpRequestMessage();
var diagnostics = new List<Diagnostic>(base.GetDiagnostics());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#nullable enable

using Microsoft.DotNet.Interactive.Http.Parsing.Parsing;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,11 @@ private T ParseTrailingWhitespace<T>(T node, bool stopAfterNewLine = false, bool

ParseLeadingWhitespaceAndComments(node);
}
else if (IsAtStartOfEmbeddedExpression())
{
node = new HttpUrlNode(_sourceText, _syntaxTree);
node.Add(ParseEmbeddedExpression());
}
else
{
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#nullable enable

using Microsoft.CodeAnalysis.Text;
using Microsoft.DotNet.Interactive.Http.Parsing.Parsing;
using System.Collections.Generic;
using System.Linq;

namespace Microsoft.DotNet.Interactive.Http.Parsing;

Expand All @@ -27,4 +30,48 @@ public void Add(HttpRequestSeparatorNode separatorNode)
{
AddInternal(separatorNode);
}

public Dictionary<string, DeclaredVariable> GetDeclaredVariables()
{

var variableAndDeclarationNodes = ChildNodes.OfType<HttpVariableDeclarationAndAssignmentNode>();

var foundVariableValues = new Dictionary<string, string>();
var declaredVariables = new Dictionary<string, DeclaredVariable>();

foreach (var node in variableAndDeclarationNodes)
{
if (node.ValueNode is not null && node.DeclarationNode is not null)
{
var embeddedExpressionNodes = node.ValueNode.ChildNodes.OfType<HttpEmbeddedExpressionNode>();
if (!embeddedExpressionNodes.Any())
{
foundVariableValues.Add(node.DeclarationNode.VariableName, node.ValueNode.Text);
declaredVariables[node.DeclarationNode.VariableName] = new DeclaredVariable(node.DeclarationNode.VariableName, node.ValueNode.Text, HttpBindingResult<string>.Success(Text));
}
else
{
var value = node.ValueNode.TryGetValue(node =>
{
if (foundVariableValues.TryGetValue(node.Text, out string? strinValue))
{
return node.CreateBindingSuccess(strinValue);
}
else
{
return node.CreateBindingFailure(new HttpDiagnosticInfo("1", "invalid variable", CodeAnalysis.DiagnosticSeverity.Error));
}

});

if (value is not null && value.Value is not null)
{
declaredVariables[node.DeclarationNode.VariableName] = new DeclaredVariable(node.DeclarationNode.VariableName, value.Value, value);
}

}
}
}
return declaredVariables;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@ internal HttpVariableValueNode(SourceText sourceText, HttpSyntaxTree? syntaxTree

public void Add(HttpEmbeddedExpressionNode node) => AddInternal(node);

public HttpBindingResult<string> TryGetValue(HttpBindingDelegate bind) => BindByInterpolation(bind);

}
32 changes: 32 additions & 0 deletions src/Microsoft.DotNet.Interactive.Http.Tests/ParserTests.Request.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Linq;
using System.Net.Http;
using FluentAssertions;
Expand Down Expand Up @@ -127,5 +128,36 @@ public void error_is_reported_for_undefined_variable()
bindingResult.IsSuccessful.Should().BeFalse();
bindingResult.Diagnostics.Should().ContainSingle().Which.GetMessage().Should().Be(message);
}

[Fact]
public void binding_for_variable_using_another_variable_is_correct()
{
var result = Parse(
"""
@hostname = httpbin.org
@host = https://{{hostname}}

POST {{host}}/anything HTTP/1.1
content-type: application/json

{
"name": "sample1",
}


"""
);

var requestNode = result.SyntaxTree.RootNode.ChildNodes
.Should().ContainSingle<HttpRequestNode>().Which;

var bindingResult = requestNode.TryGetHttpRequestMessage(node =>
{
return node.CreateBindingFailure(CreateDiagnosticInfo(""));
});

bindingResult.IsSuccessful.Should().BeTrue();
bindingResult.Value.RequestUri.ToString().Should().Be("https://httpbin.org/anything");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Microsoft.DotNet.Interactive.Http.Parsing;
using Microsoft.DotNet.Interactive.Http.Parsing.Parsing;
using Microsoft.DotNet.Interactive.Http.Tests.Utility;
using Microsoft.DotNet.Interactive.Tests.Utility;
using Xunit;
Expand Down Expand Up @@ -166,5 +168,31 @@ public void multiple_variables_with_comments_are_parsed_correctly()
var variableDeclarationNode = declarationNodes.Select(v => v.VariableName).Should()
.BeEquivalentSequenceTo(new[] { "hostname", "host" });
}

[Fact]
public void declared_variables_can_be_used_for_binding()
{
var result = Parse(
"""
@hostname=httpbin.org
""");

var variables = result.SyntaxTree.RootNode.GetDeclaredVariables();
variables.Should().ContainSingle().Which.Value.Value.Should().Be("httpbin.org");
}

[Fact]
public void declared_variables_using_another_variable_can_be_resolved()
{
var result = Parse(
"""
@hostname=httpbin.org
@host=https://{{hostname}}
""");

var variables = result.SyntaxTree.RootNode.GetDeclaredVariables();
variables.Should().Contain(n => n.Key == "host").Which.Value.Should().BeOfType<DeclaredVariable>().Which.Value.Should().Be("https://httpbin.org");

}
}
}