Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix YAML safe_load call arguments #26

Merged
merged 4 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby: [ 2.6, 2.7, 3.0 ]
ruby: [ 2.6, 2.7, 3 ]

steps:
- uses: actions/checkout@v2
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Fix `$delete` directive behavior, allowing it to work across any number of configuration tree levels
([#24](https://github.com/velocidi/frise/pull/24)).
- Fix symbol table when processing included files ([#25](https://github.com/velocidi/frise/pull/25)).
- Fix YAML.safe_load call arguments error in ruby 3.1 ([#25](https://github.com/velocidi/frise/pull/26)).

### 0.4.1 (July 7, 2020)

Expand Down
1 change: 1 addition & 0 deletions frise.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ Gem::Specification.new do |spec|
spec.add_development_dependency 'rubocop', '~> 1.10'
spec.add_development_dependency 'simplecov', '~> 0.18'
spec.add_development_dependency 'simplecov-lcov', '0.8.0'
spec.metadata['rubygems_mfa_required'] = 'true'
end
5 changes: 3 additions & 2 deletions lib/frise/defaults_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ def merge_defaults_obj(config, defaults)
elsif config.nil?
if defaults_class != 'Hash' then defaults
elsif defaults['$optional'] then nil
else merge_defaults_obj({}, defaults)
else
merge_defaults_obj({}, defaults)
end

elsif config == @delete_sym || defaults == @delete_sym
Expand All @@ -59,7 +60,7 @@ def merge_defaults_obj(config, defaults)

elsif defaults_class != config_class
raise "Cannot merge config #{config.inspect} (#{widened_class(config)}) " \
"with default #{defaults.inspect} (#{widened_class(defaults)})"
"with default #{defaults.inspect} (#{widened_class(defaults)})"

else
config
Expand Down
8 changes: 4 additions & 4 deletions lib/frise/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def process_includes(config, at_path, root_config, global_vars, include_confs_st
config, next_include_confs = extract_include(config, at_path)
include_confs = next_include_confs.map { |include| { _this: config, include: include } } + include_confs_stack
res = if include_confs.empty?
config.map { |k, v| [k, process_includes(v, at_path + [k], root_config, global_vars)] }.to_h
config.to_h { |k, v| [k, process_includes(v, at_path + [k], root_config, global_vars)] }
else
Lazy.new do
include_conf = include_confs.first
Expand All @@ -92,7 +92,7 @@ def process_schema_includes(schema, at_path, global_vars)

schema, included_schemas = extract_include(schema, at_path)
if included_schemas.empty?
schema.map { |k, v| [k, process_schema_includes(v, at_path + [k], global_vars)] }.to_h
schema.to_h { |k, v| [k, process_schema_includes(v, at_path + [k], global_vars)] }
else
included_schemas.each do |defaults_conf|
schema = Parser.parse(defaults_conf['file'], global_vars).merge(schema)
Expand All @@ -104,11 +104,11 @@ def process_schema_includes(schema, at_path, global_vars)
def process_schemas(config, at_path, global_vars)
return config unless config.instance_of?(Hash)

config = config.map do |k, v|
config = config.to_h do |k, v|
new_v = process_schemas(v, at_path + [k], global_vars)
return nil if !v.nil? && new_v.nil?
[k, new_v]
end.to_h
end

config, schema_files = extract_schema(config, at_path)
schema_files.each do |schema_file|
Expand Down
2 changes: 1 addition & 1 deletion lib/frise/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module Parser
class << self
def parse(file, symbol_table = nil)
return nil unless File.file? file
YAML.safe_load(parse_as_text(file, symbol_table), [], [], true) || {}
YAML.safe_load(parse_as_text(file, symbol_table), aliases: true) || {}
end

def parse_as_text(file, symbol_table = nil)
Expand Down
4 changes: 2 additions & 2 deletions lib/frise/validator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def validate_custom(full_schema, obj, path)
def validate_enum(full_schema, obj, path)
if full_schema[:enum] && !full_schema[:enum].include?(obj)
add_validation_error(path, "invalid value #{obj.inspect}. " \
"Accepted values are #{full_schema[:enum].map(&:inspect).join(', ')}")
"Accepted values are #{full_schema[:enum].map(&:inspect).join(', ')}")
return false
end
true
Expand Down Expand Up @@ -158,7 +158,7 @@ def validate_object(path, obj, schema)
def self.parse_symbols(obj)
case obj
when Array then obj.map { |e| parse_symbols(e) }
when Hash then obj.map { |k, v| [parse_symbols(k), parse_symbols(v)] }.to_h
when Hash then obj.to_h { |k, v| [parse_symbols(k), parse_symbols(v)] }
when String then obj.start_with?('$') ? obj[1..].to_sym : obj
else obj
end
Expand Down
2 changes: 1 addition & 1 deletion spec/frise/defaults_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@
conf = { '$content_include' => ['str.txt'] }
expect { DefaultsLoader.new.merge_defaults(conf, fixture_path('simple.yml')) }
.to raise_error 'Cannot merge config {"$content_include"=>["str.txt"]} (String) ' \
'with default {"str"=>"abc", "int"=>4, "bool"=>true} (Hash)'
'with default {"str"=>"abc", "int"=>4, "bool"=>true} (Hash)'
end

it 'should override defaults when value is $delete' do
Expand Down
4 changes: 2 additions & 2 deletions spec/frise/loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

validators = Object.new
def validators.short_string(_, str)
str.is_a?(String) && str.length < 5 || (raise "expected a short string, found #{str.inspect}")
(str.is_a?(String) && str.length < 5) || (raise "expected a short string, found #{str.inspect}")
end

RSpec.describe Loader do
Expand Down Expand Up @@ -42,7 +42,7 @@ def validators.short_string(_, str)
end

it 'should allow providing actions to be run just before loading defaults and schemas' do
on_load = ->(conf) { conf.map { |k, v| ["prefix_#{k}", v] }.to_h }
on_load = ->(conf) { conf.transform_keys { |k| "prefix_#{k}" } }

loader = Loader.new(pre_loaders: [on_load], exit_on_fail: false)

Expand Down
8 changes: 4 additions & 4 deletions spec/frise/validator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,11 @@ def validate_at(config, at_path, schema, opts = {})
it 'should allow custom validations in schemas' do
validators = Object.new
def validators.even_int(_, n)
n.is_a?(Integer) && (n % 2).zero? || (raise "expected an even number, found #{n}")
(n.is_a?(Integer) && (n % 2).zero?) || (raise "expected an even number, found #{n}")
end

def validators.length_len(root, str)
str.is_a?(String) && str.size == root['len'] ||
(str.is_a?(String) && str.size == root['len']) ||
(raise "expected a string of length #{root['len']}, found #{str.inspect}")
end

Expand Down Expand Up @@ -162,7 +162,7 @@ def validators.length_len(root, str)
it 'should allow validations on all keys of an object' do
validators = Object.new
def validators.short_string(_, str)
str.is_a?(String) && str.length < 5 || (raise "expected a short key, found #{str.inspect}")
(str.is_a?(String) && str.length < 5) || (raise "expected a short key, found #{str.inspect}")
end

schema = { 'obj' => { '$all_keys' => '$short_string', '$all' => 'String' } }
Expand Down Expand Up @@ -217,7 +217,7 @@ def validators.short_string(_, str)
it 'should be able to use complex schemas in their full form' do
validators = Object.new
def validators.short_string(_, str)
str.is_a?(String) && str.length < 5 || (raise "expected a short string, found #{str.inspect}")
(str.is_a?(String) && str.length < 5) || (raise "expected a short string, found #{str.inspect}")
end

schema = {
Expand Down