nero 0.7.0.rc5 → 0.7.0.rc6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 46d08e7f73abe4b12c92499ec3a92d243793b230340574a5a287d4442b704aea
4
- data.tar.gz: d17d6eeea4b14f25c167189ece0eabe18baaa8a5383b4422133ca5a81ec7f19a
3
+ metadata.gz: 62ecb94e14c94b7c1b29ee0bb129d97be5ecb39cad34c7493e778405ea8b9f04
4
+ data.tar.gz: 5f838bc003bbde162f560931afefbd3283fc9c61cdf2bd086396f1434eb92178
5
5
  SHA512:
6
- metadata.gz: daf3475313d0bc95c4df0d129b5cc34d3cca916db9170e47a33084d39e04467c387b0879b35646badc52f5e85cef3fee0f7dabbddd8b9c3d1e16db7eb0d32740
7
- data.tar.gz: fef0c09eb37bdb749b89034e9f08db58431de2bd960e3db50da9f98dfd72289ba1c4338aff75c6654c48876bf4934c608ca1e2987e428f17bd910cb8c8c27d20
6
+ metadata.gz: 37f2c53be5adadb1c14d6634c98d1b249f6df104b02813332761057c2ef90ce2f1016730bafef0558d15539f728dba586676a84508aeee5763d9230c19a47557
7
+ data.tar.gz: 8b9d702e980eaa58dabd81b943fc0ea31f69ff5ceafd90d08228109f7ed49212305b565dd46ab525debe072506c2dd609bc2e2b946c157df46b167051ab49470
data/CHANGELOG.md CHANGED
@@ -13,6 +13,7 @@ Total refactor and so quite some breaking changes. See [UPGRADING.md](UPGRADING.
13
13
  ## Breaking Changes
14
14
 
15
15
  - **`Nero.load` / `Nero.load_file` now return string keys**, like `YAML.load` (0.6: symbol keys). `Nero.load(...)[:foo]` silently gives `nil`: use `symbolize_names: true`, or `Nero.config_for` (symbol keys, like Rails). They raise `Nero::ParseError` with all errors at once; `Nero::Parser#parse`/`#parse_file` return a `Result` instead.
16
+ - **Unknown tags are a parse error.** A tag Nero doesn't know used to pass through silently: Psych treats an unknown local tag as no tag at all, so a typo (`!env/intger PORT` → `"PORT"`), a custom tag that isn't registered (`!str/upcsae foo` → `"foo"`) or Psych's shorthands (`!ruby/sym`, `!str:Foo`, `!binary`) gave a plain value and no error. Now every `!tag` must be built in or registered; YAML core tags (`!!str`, `!!binary`, `!!set`, ...) work as before. The error suggests similar names: `unknown tag !env/intger (maybe you meant: !env/integer, !env/int)`.
16
17
  - Nero.configure / Nero.configuration removed — custom tags are now registered via a block passed to parse/parse_file/config_for:
17
18
  ```ruby
18
19
  # Before
@@ -30,6 +31,7 @@ Total refactor and so quite some breaking changes. See [UPGRADING.md](UPGRADING.
30
31
  - !uri and !path tags removed — !uri is gone entirely; !path is no longer a built-in.
31
32
  - `Nero::PathRootTag` renamed to `Nero::RootPathTag` with a new constructor API.
32
33
  - `!path/rails_root` is only available with Rails (`nero/rails`, loaded by the Railtie); `!path/git_root` stays built in.
34
+ - `!path/git_root` / `!path/rails_root` stay below the root: a path leading outside it (`!path/rails_root ..`, `[config, ../..]`) is a parse error. `..` within the root is fine, and the result is cleaned (`config/../lib` → `<root>/lib`). For a path outside the project, use `!env/path` or a custom tag.
33
35
  - `!secret_key_base` (0.7.0 release candidates only) removed. Read it in Ruby instead (`Rails.application.secret_key_base`, or better `Rails.application.key_generator.generate_key("purpose")`), or register it as a one-line custom tag.
34
36
  - !str/format changed — now also available as !format; the map form (fmt: key) is replaced by a sequence with named-parameter hashes: !format ["http://%<host>s", host: !ref host].
35
37
  - load_config removed (was already deprecated).
data/README.md CHANGED
@@ -2,7 +2,12 @@
2
2
  <img src="assets/banner.svg" alt="Nero: declarative YAML-tags" width="100%">
3
3
  </p>
4
4
 
5
- [![Gem Version](https://badge.fury.io/rb/nero.svg)](https://badge.fury.io/rb/nero)
5
+ <p align="center">
6
+ <a href="https://badge.fury.io/rb/nero"><img src="https://badge.fury.io/rb/nero.svg" alt="Gem Version"></a>
7
+ <a href="https://eval.github.io/nero/"><img src="https://img.shields.io/badge/docs-API-ff9a3c" alt="API docs"></a>
8
+ <a href="https://rubygems.org/gems/nero"><img src="https://img.shields.io/gem/dt/nero" alt="Downloads"></a>
9
+ <a href="LICENSE.txt"><img src="https://img.shields.io/github/license/eval/nero" alt="License: MIT"></a>
10
+ </p>
6
11
 
7
12
  Nero is a RubyGem that offers declarative YAML-tags to simplify config files, e.g. for requiring and coercion of env-vars.
8
13
  Additionally, it allows you to create your own.
@@ -103,7 +108,7 @@ Nero's entry points behave like their namesakes:
103
108
  | `Nero.load(yaml)`, `Nero.load_file(path)` | `YAML.load`, `YAML.load_file` | strings (`symbolize_names: true` for symbols) | `Symbol` |
104
109
  | `Nero.config_for(:app, env:)` | `Rails.application.config_for` | symbols | `Symbol`, `Date`, `Time` |
105
110
 
106
- Differences from `YAML.load`: aliases (anchors) are allowed, and all problems are reported at once in a `Nero::ParseError` (see `#errors`). Allow more classes with `permitted_classes:` (this replaces the default list, as in Psych). Arbitrary objects (`!ruby/object:...`) are refused by default, also in `config_for` (Rails' `config_for` uses `YAML.unsafe_load`).
111
+ Differences from `YAML.load`: aliases (anchors) are allowed, all problems are reported at once in a `Nero::ParseError` (see `#errors`), and a tag Nero doesn't know is an error instead of being ignored (`unknown tag !env/intger (maybe you meant: !env/integer, !env/int)`). Allow more classes with `permitted_classes:` (this replaces the default list, as in Psych). Arbitrary objects (`!ruby/object:...`) are refused by default, also in `config_for` (Rails' `config_for` uses `YAML.unsafe_load`).
107
112
 
108
113
  ```ruby
109
114
  # config/app.yml, relative to the current directory (in Rails: Rails.root/config)
@@ -178,7 +183,7 @@ Deprecated usage (e.g. `!ref [a, b]`) still works and prints a warning on stderr
178
183
  # segments are joined, so other tags can provide them:
179
184
  uploads: !path/git_root [storage, !env [TENANT, default]]
180
185
  ```
181
- Segments are relative to the root: a missing (`nil`) or absolute segment is an error.
186
+ Segments are relative to the root: a missing (`nil`) or absolute segment is an error, and so is a path leading outside the root (`..`). The result is cleaned: `config/../lib` → `<root>/lib`.
182
187
  With Rails there's also `!path/rails_root` (see [Rails](#rails)).
183
188
  - `!format` (or `!str/format`)
184
189
  Using Ruby's [format specifications](https://docs.ruby-lang.org/en/master/language/format_specifications_rdoc.html):
@@ -276,6 +281,8 @@ config.x.app = Nero.config_for(:app)
276
281
 
277
282
  Pass a block to `Nero.load`, `Nero.load_file`, `Nero.config_for` or `Nero::Parser.new` and register tags with `add_tag(name, tag)`. Built-in tags with the same name are replaced.
278
283
 
284
+ Any other `!tag` is a parse error (`unknown tag !str/fromat`): a typo or a tag you forgot to register never silently passes through as a plain value. YAML's own `!!str`, `!!binary` etc. are fine; Psych's `!ruby/...` shorthands are not supported.
285
+
279
286
  A tag is anything that responds to `resolve(args, context:)`:
280
287
  - `args` is what follows the tag in YAML: an Array for a scalar or sequence, a Hash for a mapping. Nested tags (`!env` etc.) are already resolved.
281
288
  - `context` gives access to `environ` (the env-vars), `dir` (the folder of the yaml-config) and `add_error(message)`.
data/UPGRADING.md CHANGED
@@ -3,6 +3,9 @@
3
3
  0.7 is a rewrite. Most changes fail loudly (a `NoMethodError` or a `Nero::ParseError` at
4
4
  boot), but a few change values **silently**. Those come first.
5
5
 
6
+ One thing that was silent is now loud: a tag Nero doesn't know is a parse error (see
7
+ [Custom tags](#7-custom-tags)). In 0.6 a typo or an unregistered tag gave a plain value.
8
+
6
9
  Deprecated usage keeps working until 0.8 and prints a warning on stderr:
7
10
 
8
11
  ```
@@ -20,7 +23,7 @@ Run your app or test suite once after upgrading and fix every warning.
20
23
  - [ ] [Configuration](#4-neroconfigure-is-gone): no more `Nero.configure`
21
24
  - [ ] [`load_config`](#5-load_config-is-gone) → `config_for`
22
25
  - [ ] [Tags](#6-tags): `!ref`, `!str/format`, optional env tags, `!path`, `!uri`
23
- - [ ] [Custom tags](#7-custom-tags): new API
26
+ - [ ] [Custom tags](#7-custom-tags): new API; every `!tag` must be built in or registered
24
27
  - [ ] [Loading Ruby objects](#8-loading-ruby-objects): `extra_permitted_classes:` → `permitted_classes:`
25
28
  - [ ] [`NERO_ENV_ALL_OPTIONAL`](#9-nero_env_all_optional) → `NERO_ALL_OPTIONAL`
26
29
 
@@ -157,13 +160,21 @@ Nero.config_for(:app, env: Rails.env) # env: defaults to Rails.env in a Rails
157
160
  | `!str/format {fmt: "%<x>s", x: 1}` | `!format ["%<x>s", x: 1]` | The map form with `fmt:` is gone. Positional args (`!format ["%s/x", !ref url]`) still work. `!str/format` stays as an alias. |
158
161
  | `!path [a, b]` | `!path/git_root [a, b]`, `!env/path VAR` | `!path` is gone: it built a path relative to the current directory. Use a root-relative path, or a path from the environment. |
159
162
  | `!uri [smtps://, !env CREDS, @host]` | `!format ["smtps://%s@host", !env CREDS]` | `!uri` is gone: it concatenated and parsed without escaping. Build the string, and call `URI()` in Ruby if you need an object. |
160
- | `!path/rails_root` | `!path/rails_root` | Now only available in a Rails app. `!path/git_root` works everywhere. |
163
+ | `!path/rails_root` | `!path/rails_root` | Now only available in a Rails app. `!path/git_root` works everywhere. A path leading outside the root (`!path/rails_root ../shared`) is now an error: use `!env/path` for such a path. |
161
164
 
162
165
  New: `!env/int`, `!env/boolean` (aliases), `!env/path` and `!env?/path`, `!format`; with
163
166
  Rails also `!credentials`, `!duration` and `!str/inquirer`.
164
167
 
165
168
  ## 7. Custom tags
166
169
 
170
+ **Unregistered tags are errors.** 0.6 passed a tag it didn't know on to Psych, which
171
+ treats an unknown local tag as no tag: `!upcsae foo` quietly gave `"foo"`,
172
+ `!secret foo` the string `"foo"`. 0.7 reports `unknown tag !upcsae` instead, with a
173
+ suggestion when a built-in name is close (`unknown tag !env/intger (maybe you meant:
174
+ !env/integer, !env/int)`). So a custom tag you forget to register, or register on one code
175
+ path but not another (an initializer that reloads the config without the block), now fails
176
+ at boot instead of changing values.
177
+
167
178
  Tags no longer read `tag.args`/`coder`/`ctx`. A tag is anything that responds to
168
179
  `resolve(args, context:)`.
169
180
 
@@ -239,6 +250,10 @@ Nero.load(yaml, permitted_classes: %w[Symbol Date])
239
250
 
240
251
  Aliases (`&anchor`/`*alias`, `<<:` merge keys) work as before.
241
252
 
253
+ Psych's shorthand tags (`!ruby/sym`, `!ruby/range`, `!ruby/regexp`, `!str:Foo`, `!seq:Foo`,
254
+ `!binary`, `!float`, `!omap`, `!set`) are unknown tags in 0.7. Write symbols as `:foo`, and
255
+ use the YAML core form for the rest: `!!binary`, `!!float`, `!!omap`, `!!set`.
256
+
242
257
  ## 9. `NERO_ENV_ALL_OPTIONAL`
243
258
 
244
259
  Renamed to `NERO_ALL_OPTIONAL`, because it now also makes Rails `!credentials` optional. The
data/lib/nero/parser.rb CHANGED
@@ -179,12 +179,12 @@ module Nero
179
179
  key = value.path.join(".")
180
180
  if visited.include?(key)
181
181
  ctx.add_error("circular reference: #{key}")
182
- return nil
182
+ return
183
183
  end
184
184
  target = value.path.reduce(root) { |h, k| h.is_a?(Hash) ? h[key(k)] : nil }
185
185
  if target.nil?
186
186
  ctx.add_error("unknown ref #{key}")
187
- return nil
187
+ return
188
188
  end
189
189
  resolve_refs(target, root, ctx, visited | [key])
190
190
  when Deferred
@@ -17,7 +17,7 @@ module Nero
17
17
  keys = args.map(&:to_sym)
18
18
  value = credentials.dig(*keys)
19
19
  return value unless value.nil?
20
- return nil if context.all_optional?
20
+ return if context.all_optional?
21
21
 
22
22
  context.add_error("credential #{args.join(".")} not found")
23
23
  nil
@@ -11,12 +11,12 @@ module Nero
11
11
 
12
12
  unless amount.is_a?(Numeric)
13
13
  context.add_error("duration requires a numeric amount, got #{amount.inspect}")
14
- return nil
14
+ return
15
15
  end
16
16
 
17
17
  unless UNITS.include?(unit)
18
18
  context.add_error("duration unknown unit #{unit.inspect}, expected one of: #{UNITS.join(", ")}")
19
- return nil
19
+ return
20
20
  end
21
21
 
22
22
  amount.public_send(unit)
@@ -7,7 +7,7 @@ module Nero
7
7
  value = args[0]&.to_s
8
8
  if value.nil? || value.empty?
9
9
  context.add_error("str/inquirer requires a non-empty string argument")
10
- return nil
10
+ return
11
11
  end
12
12
  ActiveSupport::StringInquirer.new(value)
13
13
  end
@@ -9,7 +9,9 @@ module Nero
9
9
  # up: !path/git_root [storage, !env TENANT] # segments, joined
10
10
  #
11
11
  # Segments are strings or numbers. A nil segment (e.g. a missing `!env?`), an absolute
12
- # segment or a mapping is an error. The root must exist, the resulting path needn't.
12
+ # segment, a segment leading outside the root (`..`) or a mapping is an error. The
13
+ # result is cleaned (`config/../lib` → `lib`). The root must exist, the resulting path
14
+ # needn't.
13
15
  class RootPathTag < BaseTag
14
16
  def initialize(containing:)
15
17
  @containing = containing
@@ -22,7 +24,7 @@ module Nero
22
24
  segments = Array(args).map(&:to_s).reject(&:empty?)
23
25
  root = find_root(context) or return
24
26
 
25
- segments.empty? ? root : root.join(*segments)
27
+ segments.empty? ? root : root.join(*segments).cleanpath
26
28
  end
27
29
 
28
30
  private
@@ -30,12 +32,16 @@ module Nero
30
32
  def invalid_args(args)
31
33
  return "expected a path or a sequence of path segments, got a mapping" if args.is_a?(Hash)
32
34
 
35
+ below_root = Pathname.new(".")
33
36
  Array(args).each.with_index(1) do |segment, i|
34
37
  case segment
35
38
  when nil
36
39
  return "segment #{i} is nil (use a fallback, e.g. !env [VAR, default])"
37
40
  when String, Numeric
38
41
  return "segment #{i} (#{segment.inspect}) is absolute; segments are relative to the root" if File.absolute_path?(segment.to_s)
42
+
43
+ below_root = (below_root + segment.to_s).cleanpath
44
+ return "segment #{i} (#{segment.inspect}) leads outside the root" if below_root.each_filename.first == ".."
39
45
  else
40
46
  return "segment #{i} must be a string, got #{segment.class}"
41
47
  end
data/lib/nero/version.rb CHANGED
@@ -3,5 +3,5 @@
3
3
  module Nero
4
4
  # NOTE this is written upon release via:
5
5
  # $ rake gem:build[version=0.3.0]
6
- VERSION = "0.7.0.rc5"
6
+ VERSION = "0.7.0.rc6"
7
7
  end
data/lib/nero/visitor.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "did_you_mean"
4
+
3
5
  module Nero
4
6
  class Visitor < ::Psych::Visitors::ToRuby
5
7
  # Like YAML.load (safe_load): only `permitted_classes` (class names or classes) can be
@@ -47,8 +49,16 @@ module Nero
47
49
  private
48
50
 
49
51
  def find_nero_tag(tag)
50
- return nil unless tag&.start_with?("!")
51
- @nero_tags[tag[1..]]
52
+ return unless tag&.start_with?("!") # filter out untagged and core tags (e.g. !!str that arrive as tag:yaml.org,2002:str
53
+ tag_name = tag[1..]
54
+
55
+ @nero_tags.fetch(tag_name) do
56
+ dictionary = @nero_tags.reject { |_, t| t.respond_to?(:deprecated?) && t.deprecated? }.keys
57
+ suggestions = DidYouMean::SpellChecker.new(dictionary:).correct(tag_name)
58
+ hint = suggestions.empty? ? "" : " (maybe you meant: #{suggestions.map { "!#{_1}" }.join(", ")})"
59
+ @nero_ctx.add_error("unknown tag #{tag}#{hint}")
60
+ nil
61
+ end
52
62
  end
53
63
 
54
64
  def contains_ref?(value)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nero
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0.rc5
4
+ version: 0.7.0.rc6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gert Goet