palkan / anyway_config

Configuration library for Ruby gems and applications
MIT License
778 stars 52 forks source link

Question: correct syntax for `required` complex config attributes? #117

Closed antaflos closed 1 year ago

antaflos commented 1 year ago

What did you do?

Given a config class like this:

# frozen_string_literal: true

require 'anyway_config'

module LdapSync
  class Config < Anyway::Config
    config_name :ldap_sync
    env_prefix :ls
    attr_config ldap: {
                  base_dn: nil,
                  user_dn: nil,
                  bind_dn: nil,
                  bind_pw: nil,
                  host: nil,
                  port: 636,
                  tls: true,
                  tls_verify_cert: true,
                  tls_ca_cert_file: '/etc/ssl/certs/ca-certificates.crt',
                  tls_version: 'TLSv1_2'
                },
                database: {
                  host: nil,
                  name: nil,
                  user: nil,
                  password: nil,
                  port: 5432
                }

    coerce_types ldap: { bind_dn: :string }
    # required ...?
  end
end

What is the correct syntax to specify required config attributes like ldap.base_dn, ldap.host or database.host? I cannot seem to get it right.

I have tried:

required :ldap[:base_dn]

Error:

config.rb:31:in `[]': no implicit conversion of Symbol into Integer (TypeError)

Then:

required :ldap['base_dn']

Which doesn't anger Ruby but seems to resolve to nil:

The following config parameters for `LdapSync::Config(config_name: times_sync_ldap_group)` are missing or empty:

And lastly:

required ldap['base_dn']

Which Ruby doesn't like:

config.rb:31:in `<class:Config>': undefined local variable or method `ldap' for LdapSync::Config:Class (NameError)

I am out of ideas and would be thankful for some insight. Is this even possible?

I haven't used anyway config before but I really appreciate all the work it takes off my hands, since before I've been handling YAML-based config settings (loading, reading, validating, etc.) pretty much manually and not in an efficient way like anyway config does. Thanks for creating it!

Ruby Version: 2.7.6

Framework Version (Rails, whatever): none, pure Ruby

Anyway Config Version: 2.3.1

palkan commented 1 year ago

I am out of ideas and would be thankful for some insight. Is this even possible?

Not it is) Thanks for the suggestion!

Will be available in the next release (soon).