Skip to content

Commit

Permalink
add Set#from_yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
kostya committed Jul 2, 2018
1 parent ad56b14 commit 0583ce7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions spec/std/yaml/serialization_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ describe "YAML serialization" do
Array(Int32).from_yaml("---\n- 1\n- 2\n- 3\n").should eq([1, 2, 3])
end

it "does Set#from_yaml" do
Set(Int32).from_yaml("---\n- 1\n- 2\n- 2\n").should eq(Set.new([1, 2]))
end

it "does Array#from_yaml from IO" do
io = IO::Memory.new "---\n- 1\n- 2\n- 3\n"
Array(Int32).from_yaml(io).should eq([1, 2, 3])
Expand Down
16 changes: 16 additions & 0 deletions src/yaml/from_yaml.cr
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,22 @@ def Array.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
end
end

def Set.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
ctx.read_alias(node, self) do |obj|
return obj
end

unless node.is_a?(YAML::Nodes::Sequence)
node.raise "Expected sequence, not #{node.class}"
end

set = new
node.each do |value|
set << T.new(ctx, value)
end
set
end

def Hash.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
ctx.read_alias(node, self) do |obj|
return obj
Expand Down

0 comments on commit 0583ce7

Please sign in to comment.