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

Add YAML::Nodes::Node#kind #10432

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion spec/std/yaml/serialization_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ describe "YAML serialization" do
]
YAML
end
ex.message.should eq("Expected Nil, not 1 at line 2, column 3")
ex.message.should eq(%(Expected Nil, not "1" at line 2, column 3))
ex.location.should eq({2, 3})
end

Expand Down
26 changes: 13 additions & 13 deletions src/yaml/from_yaml.cr
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ private def parse_scalar(ctx, node, type : T.class) forall T
ctx.record_anchor(node, value)
value
else
node.raise "Expected #{T}, not #{node.value}"
node.raise "Expected #{T}, not #{node.value.inspect}"
end
else
node.raise "Expected #{T}, not #{node.class.name}"
node.raise "Expected scalar, not #{node.type}"
end
end

Expand Down Expand Up @@ -72,7 +72,7 @@ def String.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
ctx.record_anchor(node, value)
value
else
node.raise "Expected String, not #{node.class.name}"
node.raise "Expected String, not #{node.type}"
end
end

Expand Down Expand Up @@ -105,7 +105,7 @@ end

def Array.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
unless node.is_a?(YAML::Nodes::Sequence)
node.raise "Expected sequence, not #{node.class}"
node.raise "Expected sequence, not #{node.type}"
end

node.each do |value|
Expand All @@ -130,7 +130,7 @@ end

def Set.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
unless node.is_a?(YAML::Nodes::Sequence)
node.raise "Expected sequence, not #{node.class}"
node.raise "Expected sequence, not #{node.type}"
end

node.each do |value|
Expand All @@ -155,7 +155,7 @@ end

def Hash.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
unless node.is_a?(YAML::Nodes::Mapping)
node.raise "Expected mapping, not #{node.class}"
node.raise "Expected mapping, not #{node.type}"
end

YAML::Schema::Core.each(node) do |key, value|
Expand All @@ -165,7 +165,7 @@ end

def Tuple.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
unless node.is_a?(YAML::Nodes::Sequence)
node.raise "Expected sequence, not #{node.class}"
node.raise "Expected sequence, not #{node.type}"
end

if node.nodes.size != {{T.size}}
Expand All @@ -183,7 +183,7 @@ end

def NamedTuple.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
unless node.is_a?(YAML::Nodes::Mapping)
node.raise "Expected mapping, not #{node.class}"
node.raise "Expected mapping, not #{node.type}"
end

{% begin %}
Expand Down Expand Up @@ -221,7 +221,7 @@ end

def Enum.new(ctx : YAML::ParseContext, node : YAML::Nodes::Node)
unless node.is_a?(YAML::Nodes::Scalar)
node.raise "Expected scalar, not #{node.class}"
node.raise "Expected scalar, not #{node.type}"
end

string = node.value
Expand Down Expand Up @@ -279,7 +279,7 @@ end
struct Time::Format
def from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : Time
unless node.is_a?(YAML::Nodes::Scalar)
node.raise "Expected scalar, not #{node.class}"
node.raise "Expected scalar, not #{node.type}"
end

parse(node.value, Time::Location::UTC)
Expand All @@ -289,7 +289,7 @@ end
module Time::EpochConverter
def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : Time
unless node.is_a?(YAML::Nodes::Scalar)
node.raise "Expected scalar, not #{node.class}"
node.raise "Expected scalar, not #{node.type}"
end

Time.unix(node.value.to_i)
Expand All @@ -299,7 +299,7 @@ end
module Time::EpochMillisConverter
def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : Time
unless node.is_a?(YAML::Nodes::Scalar)
node.raise "Expected scalar, not #{node.class}"
node.raise "Expected scalar, not #{node.type}"
end

Time.unix_ms(node.value.to_i64)
Expand All @@ -309,7 +309,7 @@ end
module YAML::ArrayConverter(Converter)
def self.from_yaml(ctx : YAML::ParseContext, node : YAML::Nodes::Node) : Array
unless node.is_a?(YAML::Nodes::Sequence)
node.raise "Expected sequence, not #{node.class}"
node.raise "Expected sequence, not #{node.type}"
end

ary = Array(typeof(Converter.from_yaml(ctx, node))).new
Expand Down
23 changes: 23 additions & 0 deletions src/yaml/nodes/nodes.cr
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ module YAML::Nodes
def raise(message)
::raise YAML::ParseException.new(message, *location)
end

# Returns the type of this node, which is equivalent to the class name.
abstract def type : String
end

# A YAML document.
Expand All @@ -51,6 +54,10 @@ module YAML::Nodes
def to_yaml(builder : YAML::Builder)
nodes.each &.to_yaml(builder)
end

def type : String
"document"
end
end

# A scalar value.
Expand All @@ -68,6 +75,10 @@ module YAML::Nodes
def to_yaml(builder : YAML::Builder)
builder.scalar(value, anchor, tag, style)
end

def type : String
"scalar"
end
end

# A sequence of nodes.
Expand Down Expand Up @@ -96,6 +107,10 @@ module YAML::Nodes
each &.to_yaml(builder)
end
end

def type : String
"sequence"
end
end

# A mapping of nodes.
Expand Down Expand Up @@ -131,6 +146,10 @@ module YAML::Nodes
end
end
end

def type : String
"mapping"
end
end

# An alias.
Expand All @@ -146,5 +165,9 @@ module YAML::Nodes
def to_yaml(builder : YAML::Builder)
builder.alias(anchor.not_nil!)
end

def type : String
"alias"
end
end
end