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

Undefined method 'first?' for YAML::Any when using YAML::Schema::FailSafe.parse #6786

Closed
SeedyROM opened this issue Sep 24, 2018 · 3 comments

Comments

@SeedyROM
Copy link

SeedyROM commented Sep 24, 2018

I'm trying to parse a YAML file with the FailSafe parser, however my perfectly valid .yml file somehow throws the error above.

YAML:

context:
    replace_me: "Yes please!"

Code:

require "yaml"

file = File.read "./config.yml"
yaml = YAML::Schema::FailSafe.parse file

Traceback:

in /usr/lib/crystal/yaml/schema/fail_safe.cr:7: instantiating 'YAML::Schema::FailSafe::Parser.class#new(String)'

    Parser.new data, &.parse
           ^~~

in /usr/lib/crystal/yaml/schema/fail_safe.cr:7: instantiating 'YAML::Schema::FailSafe::Parser.class#new(String)'

    Parser.new data, &.parse
           ^~~

in /usr/lib/crystal/yaml/schema/fail_safe.cr:7: instantiating 'YAML::Schema::FailSafe::Parser#parse()'

    Parser.new data, &.parse
                       ^~~~~

in /usr/lib/crystal/yaml/parser.cr:65: instantiating 'cast_document(YAML::Any)'

    cast_document(document)
    ^~~~~~~~~~~~~

in /usr/lib/crystal/yaml/schema/fail_safe.cr:38: undefined method 'first?' for YAML::Any

      doc.first? || Any.new(nil)
@wooster0
Copy link
Contributor

Here's a workaround for now:

module YAML::Schema::FailSafe
  class Parser < YAML::Parser
    def cast_document(doc)
      doc[0]? || Any.new(nil)
    end
  end
end

@SeedyROM
Copy link
Author

SeedyROM commented Sep 25, 2018

Interesting! I'll have to check that out. I achieved my goal by using doc.as_a.first?. But then again I wasn't parsing multiple documents, however I found the default YAML parser did exactly what I wanted anyway.

Although the presence of this bug is worth noting!

@SeedyROM
Copy link
Author

Maybe I was being too Rubyist about my style with that considering the conversion to an Array.

doc[0]? || ... will definitely work as intended!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants