Skip to content

config_context_list and config_context_hash only work with .rb config files #58

@elyscape

Description

@elyscape

Context lists and context hashes only work with Ruby config files. Attempting to use a JSON, YAML, or TOML file or load from a hash results in an error:

irb(main):001:0> require 'mixlib/config'
=> true
irb(main):002:0> class Config
irb(main):003:1>   extend Mixlib::Config
irb(main):004:1>   config_context_list :tests, :test do
irb(main):005:2*     default :x, 5
irb(main):006:2>   end
irb(main):007:1> end
=> [#<Proc:0x00007ffb3b068d88@(irb):4>]
irb(main):008:0> puts File.read('conf.yaml')
---
tests:
  - x: 7
  - x: 14
=> nil
irb(main):009:0> Config.from_file 'conf.yaml'
ArgumentError: wrong number of arguments (given 1, expected 0)
[elided]
irb(main):010:0> Config.from_hash({ tests: [{x:7}, {x:14}] })
ArgumentError: wrong number of arguments (given 1, expected 0)
[elided]

The issue is in the from_hash method. It attempts to convert a hash into executable Ruby code and then executes it. Unfortunately, this doesn't work for context lists and hashes. Here's what it produces from the sample config above:

tests [{"x"=>7}, {"x"=>14}]

This seems like it might work but, unfortunately, config_context_list and config_context_hash do not create methods for the plural symbol that take arguments.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions