Skip to content

Commit

Permalink
Revert Yaml.deserialize to allow for anchors/alises (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
Blacksmoke16 authored Aug 5, 2019
1 parent 77658af commit a9601a5
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 46 deletions.
2 changes: 1 addition & 1 deletion spec/converters/yaml_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ describe OQ::Converters::Yaml do
end
end

pending "with anchors" do
describe "with anchors" do
it "should output correctly" do
run_binary(ANCHORS, args: ["-i", "yaml", "-c", "."]) do |output|
output.should eq %({"base":{"name":"Everyone has same name"},"foo":{"name":"Everyone has same name","age":10},"bar":{"name":"Everyone has same name","age":20}}\n)
Expand Down
47 changes: 2 additions & 45 deletions src/converters/yaml.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,9 @@ class JSON::Builder
end

module OQ::Converters::Yaml
# ameba:disable Metrics/CyclomaticComplexity
# OPTIMIZE: Figure out a way to handle aliases/anchors while streaming.
def self.deserialize(input : IO, output : IO, **args) : Nil
yaml = YAML::PullParser.new(input)
json = JSON::Builder.new(output)

yaml.read_stream do
loop do
case yaml.kind
when .document_start?
json.start_document
when .document_end?
json.end_document
yaml.read_next
break
when .scalar?
string = yaml.value

if json.next_is_object_key?
json.scalar(string)
else
scalar = YAML::Schema::Core.parse_scalar(yaml)
case scalar
when Nil
json.scalar(scalar)
when Bool
json.scalar(scalar)
when Int64
json.scalar(scalar)
when Float64
json.scalar(scalar)
else
json.scalar(string)
end
end
when .sequence_start?
json.start_array
when .sequence_end?
json.end_array
when .mapping_start?
json.start_object
when .mapping_end?
json.end_object
end
yaml.read_next
end
end
YAML.parse(input).to_json output
end

# ameba:disable Metrics/CyclomaticComplexity
Expand Down

0 comments on commit a9601a5

Please sign in to comment.