Skip to content

YAML::Any#dig does not accept a string key #14305

Closed as not planned
Closed as not planned

Description

@syeopite

Bug Report

The dig methods of YAML::Any does not accept a string key so the following code:

require "yaml"

example_yaml = "---
test:
  example_1: 1
  example_2: 2
"

data = YAML.parse(example_yaml).as_h
puts data.dig("test", "example_1") # => Error: no overload matches 'Hash(YAML::Any, YAML::Any)#dig' with types String, String

Raises instead of the expected output of 1.

While I'm not entirely sure if this is a bug, it does feel incredibly awkward to have to wrap a YAML::Any around the arguments before the compiler accepts #dig

Output of crystal -v

Crystal 1.11.1 (2024-01-12)

LLVM: 16.0.6
Default target: x86_64-pc-linux-gnu

Activity

  1. added
    kind:bugA bug in the code. Does not apply to documentation, specs, etc.
    on Feb 19, 2024
  2. Blacksmoke16 commented on Feb 19, 2024

    @Blacksmoke16
    Member

    I think your problem is the .to_h call after YAML.parse, if you remove that it works as expected, otherwise you're using the Hash#dig version.

    EDIT: Looks like that works because the YAML::Any version is less restrictive in regards to what you can pass it? 🤷

  3. straight-shoota commented on Apr 4, 2024

    @straight-shoota
    Member

    Is there anything left to do here?

    @Blacksmoke16 could you clarify this comment:

    Looks like that works because the YAML::Any version is less restrictive in regards to what you can pass it? 🤷

  4. Blacksmoke16 commented on Apr 4, 2024

    @Blacksmoke16
    Member

    I think I meant I was thinking that Hash#dig is defined as def dig(key : K, *subkeys), notice the K type restriction. Whereas YAML::Any#dig is implemented as def dig(index_or_key, *subkeys) : YAML::Any without any type restrictions. But I know the latter will end up using the definition of the former internally so not sure sure how it avoids the compiler error when the type of the hash should be essentially the same?

  5. syeopite commented on Apr 4, 2024

    @syeopite
    ContributorAuthor

    So not a bug?

  6. straight-shoota commented on Apr 4, 2024

    @straight-shoota
    Member

    Yeah, not a bug. You're calling to_h so you get a raw Hash instead of a YAML::Any wrapper around it. That hash expects YAML::Any keys for its #dig method. You probably want to skip to_h and use YAML::Any#dig instead.

  7. straight-shoota commented on Apr 4, 2024

    @straight-shoota
    Member

    @Blacksmoke16 Regarding the type restriction, there are some general inconsistencies. Hash(K, V)#[](key) has no type restriction either, for example (ref #8893).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions