Revert "Removed unnecessary YamlService."

This reverts commit fef3fbfbe7.
This commit is contained in:
Sipke Schoorstra 2016-04-12 21:46:19 +02:00
parent fef3fbfbe7
commit c05b2546f1
9 changed files with 124 additions and 17 deletions

View File

@ -320,6 +320,7 @@
<Compile Include="Localization\DateTimePartsTests.cs" />
<Compile Include="Localization\DefaultDateLocalizationServicesTests.cs" />
<Compile Include="Localization\DefaultDateFormatterTests.cs" />
<Compile Include="Services\YamlParserTests.cs" />
<Compile Include="Stubs\StubApplicationEnvironment.cs" />
<Compile Include="Stubs\StubCultureSelector.cs" />
<Compile Include="Localization\TestHelpers.cs" />

View File

@ -0,0 +1,70 @@
using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using Newtonsoft.Json.Linq;
using Orchard.Services;
namespace Orchard.Tests.Services {
[TestFixture]
public class YamlParserTests {
[Test]
public void ShouldConvertYamlToWellknowType() {
var parser = new YamlParser();
var yaml = SampleYamlDocument;
var order = parser.Deserialize<Order>(yaml);
Assert.AreEqual("Nikola", order.Customer.FirstName);
Assert.AreEqual(2, order.Items.Count);
}
[Test]
public void ShouldConvertYamlToDynamic()
{
var parser = new YamlParser();
var yaml = SampleYamlDocument;
var order = parser.Deserialize(yaml);
Assert.AreEqual("Nikola", (string)order.Customer.FirstName);
Assert.AreEqual(2, (int)order.Items.Count);
}
public class Order {
public DateTime Date { get; set; }
public Customer Customer { get; set; }
public IList<OrderItem> Items { get; set; }
}
public class OrderItem {
public string Product { get; set; }
public int Quantity { get; set; }
public decimal Price { get; set; }
}
public class Customer {
public string FirstName { get; set; }
public string LastName { get; set; }
}
private const string SampleYamlDocument =
@"
Date: 1916-04-01
Customer:
FirstName: Nikola
LastName: Tesla
Items:
- Product: Bulb
Quantity: 1
Price: 1.46
- Product: Wire
Quantity: 1
Price: 0.32
";
}
}

View File

@ -102,14 +102,6 @@
<Reference Include="System.Xml" />
<Reference Include="System.Configuration" />
<Reference Include="System.Xml.Linq" />
<Reference Include="YamlDotNet, Version=3.8.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\YamlDotNet.3.8.0\lib\net35\YamlDotNet.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="YamlDotNet.Dynamic, Version=3.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\YamlDotNet.Dynamic.3.2.3\lib\net40\YamlDotNet.Dynamic.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Content Include="Assets\JavaScript\Models\RecycleBin.js" />

View File

@ -17,10 +17,10 @@ using Orchard.Layouts.Services;
using Orchard.Layouts.Shapes;
using Orchard.Layouts.ViewModels;
using Orchard.Localization;
using Orchard.Services;
using Orchard.Themes.Services;
using Orchard.Tokens;
using Orchard.Utility.Extensions;
using YamlDotNet.Dynamic;
namespace Orchard.Layouts.Providers {
[OrchardFeature("Orchard.Layouts.Snippets")]
@ -34,6 +34,7 @@ namespace Orchard.Layouts.Providers {
private readonly Work<ICurrentThemeShapeBindingResolver> _currentThemeShapeBindingResolver;
private readonly Work<ITokenizer> _tokenizer;
private readonly IWorkContextAccessor _wca;
private readonly Work<IYamlParser> _yamlParser;
public SnippetElementHarvester(
IWorkContextAccessor workContextAccessor,
@ -43,7 +44,8 @@ namespace Orchard.Layouts.Providers {
Work<IElementFactory> elementFactory,
Work<IShapeDisplay> shapeDisplay,
Work<ITokenizer> tokenizer,
Work<ICurrentThemeShapeBindingResolver> currentThemeShapeBindingResolver) {
Work<ICurrentThemeShapeBindingResolver> currentThemeShapeBindingResolver,
Work<IYamlParser> yamlParser) {
_shapeFactory = shapeFactory;
_siteThemeService = siteThemeService;
@ -52,6 +54,7 @@ namespace Orchard.Layouts.Providers {
_shapeDisplay = shapeDisplay;
_tokenizer = tokenizer;
_currentThemeShapeBindingResolver = currentThemeShapeBindingResolver;
_yamlParser = yamlParser;
_wca = workContextAccessor;
}
@ -146,7 +149,7 @@ namespace Orchard.Layouts.Providers {
return null;
var yaml = File.ReadAllText(paramsFileName);
var snippetConfig = Deserialize(yaml);
var snippetConfig = _yamlParser.Value.Deserialize(yaml);
var fieldsConfig = snippetConfig.Fields != null ? snippetConfig.Fields.Children : new dynamic[0];
var descriptor = new SnippetDescriptor();
@ -199,9 +202,5 @@ namespace Orchard.Layouts.Providers {
var markup = File.ReadAllText(localFileName);
return markup.Contains("@Html.SnippetField");
}
private dynamic Deserialize(string yaml) {
return new DynamicYaml(yaml);
}
}
}

View File

@ -7,6 +7,4 @@
<package id="Microsoft.AspNet.WebPages" version="3.2.3" targetFramework="net452" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net452" />
<package id="YamlDotNet" version="3.8.0" targetFramework="net452" />
<package id="YamlDotNet.Dynamic" version="3.2.3" targetFramework="net452" />
</packages>

View File

@ -170,6 +170,10 @@
<HintPath>..\packages\YamlDotNet.3.8.0\lib\net35\YamlDotNet.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="YamlDotNet.Dynamic, Version=3.2.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\YamlDotNet.Dynamic.3.2.3\lib\net40\YamlDotNet.Dynamic.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ContentManagement\Extensions\DriverResultExtensions.cs" />
@ -423,7 +427,9 @@
<Compile Include="Services\ClientHostAddressAccessor.cs" />
<Compile Include="Services\DefaultJsonConverter.cs" />
<Compile Include="Services\IClientHostAddressAccessor.cs" />
<Compile Include="Services\IYamlParser.cs" />
<Compile Include="Services\IJsonConverter.cs" />
<Compile Include="Services\YamlParser.cs" />
<Compile Include="Settings\CurrentSiteWorkContext.cs" />
<Compile Include="Settings\ResourceDebugMode.cs" />
<Compile Include="Tasks\Locking\Services\DistributedLockSchemaBuilder.cs" />

View File

@ -0,0 +1,21 @@
namespace Orchard.Services {
/// <summary>
/// Provides methods to deserialize objects from YAML documents.
/// </summary>
public interface IYamlParser : IDependency {
/// <summary>
/// Deserializes a YAML document to a dynamic object.
/// </summary>
/// <param name="yaml">The YAML document to deserialize.</param>
/// <returns>The deserialized object.</returns>
dynamic Deserialize(string yaml);
/// <summary>
/// Deserializes a YAML document to a specific object.
/// </summary>
/// <typeparam name="T">The type of the object to deserialize.</typeparam>
/// <param name="yaml">The YAML document to deserialize.</param>
/// <returns>The deserialized object.</returns>
T Deserialize<T>(string yaml);
}
}

View File

@ -0,0 +1,19 @@
using System.IO;
using YamlDotNet.Dynamic;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace Orchard.Services {
public class YamlParser : IYamlParser {
public dynamic Deserialize(string yaml) {
return new DynamicYaml(yaml);
}
public T Deserialize<T>(string yaml) {
var deserializer = new Deserializer(namingConvention: new PascalCaseNamingConvention(), ignoreUnmatched: true);
using (var reader = new StringReader(yaml)) {
return deserializer.Deserialize<T>(reader);
}
}
}
}

View File

@ -18,4 +18,5 @@
<package id="NHibernate" version="4.0.1.4000" targetFramework="net452" />
<package id="Owin" version="1.0" targetFramework="net452" />
<package id="YamlDotNet" version="3.8.0" targetFramework="net452" />
<package id="YamlDotNet.Dynamic" version="3.2.3" targetFramework="net452" />
</packages>