Skip to content

Commit

Permalink
Change properties key to _properties_ to reduce chance of conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
Sija committed Dec 5, 2017
1 parent 494fdcb commit bb71812
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
14 changes: 14 additions & 0 deletions spec/std/yaml/mapping_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ private class YAMLWithKey
})
end

private class YAMLWithPropertiesKey
YAML.mapping(
properties: Hash(String, String),
)
end

private class YAMLWithDefaults
YAML.mapping({
a: {type: Int32, default: 11},
Expand Down Expand Up @@ -244,6 +250,14 @@ describe "YAML mapping" do
yaml.pull.should eq(2)
end

it "outputs YAML with properties key" do
input = {
properties: {"foo" => "bar"},
}.to_yaml
yaml = YAMLWithPropertiesKey.from_yaml(input)
yaml.to_yaml.should eq(input)
end

it "allows small types of integer" do
yaml = YAMLWithSmallIntegers.from_yaml(%({"foo": 21, "bar": 7}))

Expand Down
24 changes: 12 additions & 12 deletions src/yaml/mapping.cr
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ module YAML
# it and initializes this type's instance variables.
#
# This macro also declares instance variables of the types given in the mapping.
macro mapping(properties, strict = false)
{% for key, value in properties %}
{% properties[key] = {type: value} unless value.is_a?(HashLiteral) || value.is_a?(NamedTupleLiteral) %}
macro mapping(_properties_, strict = false)
{% for key, value in _properties_ %}
{% _properties_[key] = {type: value} unless value.is_a?(HashLiteral) || value.is_a?(NamedTupleLiteral) %}
{% end %}

{% for key, value in properties %}
{% for key, value in _properties_ %}
@{{key.id}} : {{value[:type]}} {{ (value[:nilable] ? "?" : "").id }}

{% if value[:setter] == nil ? true : value[:setter] %}
Expand Down Expand Up @@ -116,7 +116,7 @@ module YAML
# FIXME: remove the dummy argument if we ever fix this.

def initialize(ctx : YAML::ParseContext, node : ::YAML::Nodes::Node, _dummy : Nil)
{% for key, value in properties %}
{% for key, value in _properties_ %}
%var{key.id} = nil
%found{key.id} = false
{% end %}
Expand All @@ -131,7 +131,7 @@ module YAML
key = key_node.value

case key
{% for key, value in properties %}
{% for key, value in _properties_ %}
when {{value[:key] || key.id.stringify}}
%found{key.id} = true

Expand Down Expand Up @@ -164,15 +164,15 @@ module YAML
node.raise "Expected mapping, not #{node.class}"
end

{% for key, value in properties %}
{% for key, value in _properties_ %}
{% unless value[:nilable] || value[:default] != nil %}
if %var{key.id}.nil? && !%found{key.id} && !::Union({{value[:type]}}).nilable?
node.raise "Missing yaml attribute: {{(value[:key] || key).id}}"
end
{% end %}
{% end %}

{% for key, value in properties %}
{% for key, value in _properties_ %}
{% if value[:nilable] %}
{% if value[:default] != nil %}
@{{key.id}} = %found{key.id} ? %var{key.id} : {{value[:default]}}
Expand All @@ -186,7 +186,7 @@ module YAML
{% end %}
{% end %}

{% for key, value in properties %}
{% for key, value in _properties_ %}
{% if value[:presence] %}
@{{key.id}}_present = %found{key.id}
{% end %}
Expand All @@ -195,7 +195,7 @@ module YAML

def to_yaml(%yaml : ::YAML::Nodes::Builder)
%yaml.mapping(reference: self) do
{% for key, value in properties %}
{% for key, value in _properties_ %}
_{{key.id}} = @{{key.id}}

unless _{{key.id}}.nil?
Expand All @@ -216,7 +216,7 @@ module YAML

# This is a convenience method to allow invoking `YAML.mapping`
# with named arguments instead of with a hash/named-tuple literal.
macro mapping(**properties)
::YAML.mapping({{properties}})
macro mapping(**_properties_)
::YAML.mapping({{_properties_}})
end
end

0 comments on commit bb71812

Please sign in to comment.