Class: Nero::Parser

Inherits:
Object
  • Object
show all
Includes:
Rails::ParserExtension
Defined in:
lib/nero/parser.rb

Constant Summary collapse

TO_INT =
->(v) { Integer(v, 10) }
TO_FLOAT =

no base: Float() never reads "08" as octal

->(v) { Float(v) }
BOOL_YES =
%w[1 y yes true on].freeze
BOOL_NO =
%w[0 n no false off].freeze
TO_BOOL =
->(v) do
  val = v.downcase.strip
  return true if BOOL_YES.include?(val)
  return false if BOOL_NO.include?(val)

  raise ArgumentError, "Can't coerce #{v.inspect} to boolean. Acceptable values: #{(BOOL_YES + BOOL_NO).join("/")}"
end
TO_PATH =
->(v) { Pathname.new(v) }
TO_IP =
->(v) do
  IPAddr.new(v)
rescue IPAddr::Error
  raise ArgumentError, "not a valid IP address or CIDR range"
end
TO_LIST =

"a, b,,c" → ["a", "b", "c"]; a fallback sequence ([a, b]) is used as is.

->(v) { v.is_a?(Array) ? v.map(&:to_s) : v.split(",").map(&:strip).reject(&:empty?) }
PERMITTED_CLASSES =

Classes YAML may instantiate, as YAML.load allows by default.

%w[Symbol].freeze

Instance Method Summary collapse

Constructor Details

#initialize(environ: ENV, root: nil, permitted_classes: PERMITTED_CLASSES, symbolize_names: false, all_optional: nil, &block) ⇒ Parser

symbolize_names: as in YAML.load, string keys become symbols (other keys stay as they are). all_optional: every value that can be missing (env vars, Rails credentials) is optional:

missing resolves to the fallback or nil instead of an error. For tasks that load the
config without needing it, e.g. `rails assets:precompile` or `rails credentials:edit`.
Defaults to whether NERO_ALL_OPTIONAL is set (non-empty) in environ. The old name
NERO_ENV_ALL_OPTIONAL still works, with a deprecation warning, until 0.8.


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/nero/parser.rb', line 35

def initialize(environ: ENV, root: nil, permitted_classes: PERMITTED_CLASSES, symbolize_names: false,
  all_optional: nil, &block)
  @environ = environ
  if all_optional.nil?
    @deprecated_all_optional = set?("NERO_ENV_ALL_OPTIONAL") && !set?("NERO_ALL_OPTIONAL")
    all_optional = set?("NERO_ALL_OPTIONAL") || set?("NERO_ENV_ALL_OPTIONAL")
  end
  @all_optional = all_optional
  @root = root&.to_s
  @permitted_classes = permitted_classes
  @symbolize_names = symbolize_names
  @tags = {}
  add_tag("env", EnvTag.new)
  add_tag("env?", EnvTag.new(optional: true))
  add_tag("env/int", EnvTag.new(coerce: TO_INT))
  add_tag("env/int?", EnvTag.new(coerce: TO_INT, optional: true,
    deprecated: ["!env/int? is deprecated", use: "!env?/int", removed_in: "0.8"]))
  add_tag("env?/int", EnvTag.new(coerce: TO_INT, optional: true))
  add_tag("env/integer", EnvTag.new(coerce: TO_INT))
  add_tag("env/integer?", EnvTag.new(coerce: TO_INT, optional: true,
    deprecated: ["!env/integer? is deprecated", use: "!env?/integer", removed_in: "0.8"]))
  add_tag("env?/integer", EnvTag.new(coerce: TO_INT, optional: true))
  add_tag("env/bool", EnvTag.new(coerce: TO_BOOL))
  add_tag("env/bool?", EnvTag.new(coerce: TO_BOOL, optional: true,
    deprecated: ["!env/bool? is deprecated", use: "!env?/bool", removed_in: "0.8"]))
  add_tag("env?/bool", EnvTag.new(coerce: TO_BOOL, optional: true))
  add_tag("env/boolean", EnvTag.new(coerce: TO_BOOL))
  add_tag("env/boolean?", EnvTag.new(coerce: TO_BOOL, optional: true,
    deprecated: ["!env/boolean? is deprecated", use: "!env?/boolean", removed_in: "0.8"]))
  add_tag("env?/boolean", EnvTag.new(coerce: TO_BOOL, optional: true))
  add_tag("env/float", EnvTag.new(coerce: TO_FLOAT))
  add_tag("env/float?", EnvTag.new(coerce: TO_FLOAT, optional: true,
    deprecated: ["!env/float? is deprecated", use: "!env?/float", removed_in: "0.8"]))
  add_tag("env?/float", EnvTag.new(coerce: TO_FLOAT, optional: true))
  add_tag("env/path", EnvTag.new(coerce: TO_PATH))
  add_tag("env/path?", EnvTag.new(coerce: TO_PATH, optional: true,
    deprecated: ["!env/path? is deprecated", use: "!env?/path", removed_in: "0.8"]))
  add_tag("env?/path", EnvTag.new(coerce: TO_PATH, optional: true))
  add_tag("env/ip", EnvTag.new(coerce: TO_IP))
  add_tag("env?/ip", EnvTag.new(coerce: TO_IP, optional: true))
  add_tag("env/list", EnvTag.new(coerce: TO_LIST))
  add_tag("env?/list", EnvTag.new(coerce: TO_LIST, optional: true))
  add_tag("str/format", FormatTag.new)
  add_tag("format", FormatTag.new)
  add_tag("ref", RefTag.new)
  add_tag("ip", IpTag.new)
  add_tag("path/git_root", RootPathTag.new(containing: ".git"))
  block&.call(self)
end

Instance Method Details

#add_tag(name, handler) ⇒ Object



85
86
87
# File 'lib/nero/parser.rb', line 85

def add_tag(name, handler)
  @tags[name] = handler.is_a?(Proc) ? ProcTag.new(handler) : handler
end

#parse(yaml, dir: nil) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/nero/parser.rb', line 89

def parse(yaml, dir: nil)
  tree = ::Psych.parse_stream(yaml)
  ctx = Context.new(environ: @environ, dir:, all_optional: @all_optional)
  if @deprecated_all_optional
    ctx.deprecate("NERO_ENV_ALL_OPTIONAL is deprecated", use: "NERO_ALL_OPTIONAL", removed_in: "0.8")
  end

  if @root
    mark_inactive_roots(tree)
  end

  visitor = Visitor.build(@tags, ctx, permitted_classes: @permitted_classes, symbolize_names: @symbolize_names)
  begin
    result = visitor.accept(tree)
  rescue ::Psych::DisallowedClass => e
    ctx.add_error("#{e.message} (allow it with permitted_classes:)")
    return Result.new(nil, errors: ctx.errors.dup.freeze, warnings: ctx.warnings.dup.freeze)
  end
  value = result.first

  if @root
    unless value.is_a?(Hash) && value.key?(key(@root))
      ctx.add_error("root #{@root.inspect} not found in top-level keys")
      return Result.new(nil, errors: ctx.errors.dup.freeze, warnings: ctx.warnings.dup.freeze)
    end
    value = value[key(@root)]
  end

  value = resolve_refs(value, value, ctx) if contains_ref?(value)
  Result.new(value, errors: ctx.errors.dup.freeze, warnings: ctx.warnings.dup.freeze)
end

#parse_file(path) ⇒ Object



121
122
123
124
# File 'lib/nero/parser.rb', line 121

def parse_file(path)
  dir = File.dirname(File.realpath(path))
  parse(File.read(path), dir:)
end