-
Notifications
You must be signed in to change notification settings - Fork 22
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
Adde user defined attributes order support #83
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,36 +19,30 @@ def replace!(property, schema) | |
|
||
private | ||
|
||
def initialize_properties | ||
def initialize_default_properties(empty_items = {}) | ||
@schema[Json::TYPE] ||= Json::ARRAY | ||
@schema[Json::ITEMS] ||= polymorph_items({}) | ||
@schema[Json::ITEMS] ||= polymorph_items(empty_items) | ||
end | ||
|
||
def parse_schema(schema) | ||
@schema = {} | ||
initialize_properties | ||
|
||
if schema_of_any_kind?(schema) | ||
@schema[Json::ITEMS] = polymorph_items(schema) | ||
return | ||
end | ||
schema.is_a?(Array) ? parse_array(schema.dup) : parse_hash(schema.dup) | ||
end | ||
|
||
schema = schema.dup | ||
if schema.is_a?(Array) | ||
@schema[Json::ITEMS] = @parser.typed.parse(schema.shift) | ||
def parse_array(schema) | ||
initialize_default_properties([]) | ||
return if schema.empty? | ||
|
||
schema.each { |payload| @schema[Json::ITEMS].merge!(payload) } | ||
else | ||
@schema[Json::ITEMS] = @parser.typed.parse(schema.delete Json::ITEMS) if schema.key?(Json::ITEMS) | ||
@schema.merge!(schema) | ||
end | ||
@schema[Json::ITEMS] = @parser.typed.parse(schema.shift) | ||
schema.each { |payload| @schema[Json::ITEMS].merge!(payload) } | ||
end | ||
|
||
def schema_of_any_kind?(schema) | ||
return true if schema.empty? | ||
return false unless schema.respond_to?(:key?) && schema.key?(Json::ITEMS) | ||
def parse_hash(schema) | ||
@schema.merge!(schema) | ||
@schema[Json::ITEMS] = @parser.typed(polymorph_if_empty: true) | ||
.parse(schema.delete(Json::ITEMS) || schema) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Place the . on the previous line, together with the method call receiver. |
||
|
||
schema[Json::ITEMS].empty? | ||
initialize_default_properties | ||
end | ||
|
||
def polymorph_items(schema) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,27 +23,28 @@ def replace!(property, property_schema) | |
|
||
private | ||
|
||
def initialize_default_properties | ||
@schema[Json::DESCRIPTION] ||= '' | ||
@schema[Json::TYPE] ||= Json::OBJECT | ||
@schema[Json::ADDITIONAL_PROPERTIES] = !!@schema[Json::ADDITIONAL_PROPERTIES] | ||
@schema[Json::REQUIRED] ||= [] | ||
@schema[Json::PROPERTIES] ||= {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. хм. последний... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ага, он и так - последний |
||
end | ||
|
||
def parse_schema(schema) | ||
@schema = {} | ||
initialize_properties | ||
|
||
schema = schema.dup | ||
merge_required = schema.key?(Json::PROPERTIES) | ||
if schema.key?(Json::PROPERTIES) | ||
@schema.merge!(schema) | ||
end | ||
|
||
initialize_default_properties | ||
|
||
(schema.delete(Json::PROPERTIES) || schema).each do |property, property_schema| | ||
@schema[Json::PROPERTIES][property] = @parser.typed.parse_property( | ||
property, property_schema) | ||
end | ||
|
||
@schema.merge!(schema) if merge_required | ||
end | ||
|
||
def initialize_properties | ||
@schema[Json::DESCRIPTION] ||= '' | ||
@schema[Json::TYPE] ||= Json::OBJECT | ||
@schema[Json::ADDITIONAL_PROPERTIES] = !!@schema[Json::ADDITIONAL_PROPERTIES] | ||
@schema[Json::REQUIRED] ||= [] | ||
@schema[Json::PROPERTIES] ||= {} | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
require 'spec_helper' | ||
|
||
describe Lurker::Json::Attribute do | ||
let(:klass) { described_class } | ||
|
||
describe '#parse_schema' do | ||
context 'when default attributes order are broken by user' do | ||
let(:attribute) do | ||
klass.new( | ||
'example' => 'razum2um', | ||
'description' => '', | ||
'type' => 'string' | ||
) | ||
end | ||
let(:expected) do | ||
{ | ||
'example' => 'razum2um', | ||
'description' => '', | ||
'type' => 'string' | ||
}.to_json | ||
end | ||
|
||
it { expect(attribute.to_json).to eq expected } | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а можно чтобы по дефолту properties был последний а не первый? просто в большой схеме yaml потом трудно найти где внизу соответствие required & desc нужному объекту
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
В данном случае я все правильно сделал, дописал дефолт вниз, как и договаривались 😂
Так что жаловаться не на что. Все же мой вариант с заданным порядком в этом плане нравился больше
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Для новых схем все должно быть по порядку. Только для готовых сохраняем их порядок
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а. понял, норм. главное чтобы чистая схема генерилась правильно. я правда когда-то глаза ломал по отступам