palkan / anyway_config

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

Type cast precedence over describe_options #68

Closed HenriqueMorato closed 4 years ago

HenriqueMorato commented 4 years ago

What did you do?

SOME_CONFIG_CLIENT_ID="123456.123456"

class SomeConfig < ApplicationConfig
  attr_config :client_id

  describe_options(
    # In this case, you should specify a hash with `type`
    # and (optionally) `desc` keys
    client_id: {
       desc: "client identificator",
       type: String
     }
  )

  required :client_id
end
$ SomeConfig.new.client_id
$ => 123456.12

# =>SomeConfig.new.client_id.class
# => Float

What did you expect to happen?

I was expecting the type to be string with full value

Environment

Ruby Version: 2.7

Framework Version (Rails, whatever): Rails 6.1.0.alpha

Anyway Config Version: 2.0.6

Am i doing something wrong or is it a bug?

palkan commented 4 years ago

describe_options is only used with parse_options!, it has no effect for the default data loading mechanism.

There is no built-in support for type coercion. If you want to enforce a string value, you can use on_load hook:

on_load { self.client_id = client_it.to_s if client_id }

I know, this is not ideal. There is a plan to add something skip_auto_serialization to config classes to disable any automatic type-casting; probably, will be a part of 2.1 release.