Skip to content

Commit c10f744

Browse files
authored
Merge branch 'aaubry:master' into mergingparser_with_aliases_try2
2 parents 8ad36ef + 0d33b5f commit c10f744

File tree

3 files changed

+113
-5
lines changed

3 files changed

+113
-5
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// This file is part of YamlDotNet - A .NET library for YAML.
2+
// Copyright (c) Antoine Aubry and contributors
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy of
5+
// this software and associated documentation files (the "Software"), to deal in
6+
// the Software without restriction, including without limitation the rights to
7+
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8+
// of the Software, and to permit persons to whom the Software is furnished to do
9+
// so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in all
12+
// copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
// SOFTWARE.
21+
22+
using System;
23+
using System.Collections.Generic;
24+
using System.IO;
25+
using Xunit.Abstractions;
26+
using YamlDotNet.Samples.Helpers;
27+
using YamlDotNet.Serialization;
28+
using YamlDotNet.Serialization.NamingConventions;
29+
30+
namespace YamlDotNet.Samples
31+
{
32+
public class DeserializeGenericObject
33+
{
34+
private readonly ITestOutputHelper output;
35+
36+
public DeserializeGenericObject(ITestOutputHelper output)
37+
{
38+
this.output = output;
39+
}
40+
41+
[Sample(
42+
DisplayName = "Deserializing generic objects with basic types",
43+
Description = "Shows how to convert a YAML document to basic types such as String and Dictionary"
44+
)]
45+
public void Main()
46+
{
47+
var input = new StringReader(Document);
48+
var deserializer = new DeserializerBuilder()
49+
.WithNamingConvention(CamelCaseNamingConvention.Instance)
50+
.Build();
51+
var doc = deserializer.Deserialize<List<object>>(input);
52+
Console.WriteLine("## Document");
53+
foreach (Dictionary<object, object> item in doc)
54+
{
55+
Console.WriteLine("### Item");
56+
foreach (var kvp in item)
57+
{
58+
Console.WriteLine("{0}: {1}", kvp.Key, kvp.Value);
59+
}
60+
}
61+
}
62+
63+
private const string Document = @"---
64+
- name: Install certbot
65+
tags: setup
66+
community.general.snap:
67+
name: certbot
68+
classic: true
69+
- name: Create symlink for Certbot binary
70+
tags: setup
71+
ansible.builtin.file:
72+
src: /snap/bin/certbot
73+
dest: /usr/bin/certbot
74+
state: link
75+
become: true
76+
- name: Check if the certificate was already generated
77+
tags: generate_cert
78+
ansible.builtin.stat:
79+
path: ""/etc/letsencrypt/live/{{ domain }}/fullchain.pem""
80+
register: cert_fullchain_file
81+
- name: Generate certificate manually
82+
tags: generate_cert
83+
ansible.builtin.command:
84+
cmd: ""certbot certonly --standalone --preferred-challenges http --agree-tos --email {{ email }} -d {{ domain }}""
85+
creates: ""/etc/letsencrypt/live/{{ domain }}/fullchain.pem""
86+
when: not cert_fullchain_file.stat.exists
87+
";
88+
}
89+
}

YamlDotNet.Test/Core/EmitterTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,28 @@ public void CommentsAreEmittedCorrectly()
290290
.And.Contain("# Bottom comment");
291291
}
292292

293+
[Fact]
294+
public void CommentsBetweenMappingKeyAndValueAreEmittedCorrectly()
295+
{
296+
var events = MappingWith(
297+
Scalar("key").ImplicitPlain,
298+
InlineComment("inline comment"),
299+
StandaloneComment("standalone comment"),
300+
BlockSequenceStart,
301+
Scalar("value").ImplicitPlain,
302+
SequenceEnd
303+
);
304+
305+
var yaml = EmittedTextFrom(StreamedDocumentWith(events));
306+
307+
yaml.Should()
308+
.Contain(Lines(
309+
"key: # inline comment",
310+
"# standalone comment",
311+
" - value"
312+
));
313+
}
314+
293315
[Fact]
294316
public void ACommentAsTheFirstEventAddsANewLine()
295317
{

YamlDotNet/Core/Emitter.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,6 +1638,7 @@ private void EmitBlockMappingKey(ParsingEvent evt, bool isFirst)
16381638
{
16391639
states.Push(EmitterState.BlockMappingSimpleValue);
16401640
EmitNode(evt, true, true);
1641+
WriteIndicator(":", false, false, false);
16411642
}
16421643
else
16431644
{
@@ -1652,11 +1653,7 @@ private void EmitBlockMappingKey(ParsingEvent evt, bool isFirst)
16521653
/// </summary>
16531654
private void EmitBlockMappingValue(ParsingEvent evt, bool isSimple)
16541655
{
1655-
if (isSimple)
1656-
{
1657-
WriteIndicator(":", false, false, false);
1658-
}
1659-
else
1656+
if (!isSimple)
16601657
{
16611658
WriteIndent();
16621659
WriteIndicator(":", true, false, true);

0 commit comments

Comments
 (0)