Skip to content

Commit

Permalink
Merge pull request #29 from leonidcozlovschii/7_identifiers
Browse files Browse the repository at this point in the history
Added alias :as for field dsl
  • Loading branch information
mtkachenk0 committed Nov 5, 2020
2 parents a64f921 + c0289fb commit f97ca7a
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 37 deletions.
3 changes: 2 additions & 1 deletion lib/paradocs/extensions/payload_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ def build_simple_structure(struct, &block)
next if key.start_with?(Paradocs.config.meta_prefix) # skip all the meta fields
ex_value = restore_one(key, value, &block)
next if ex_value == @skip_word
[key, ex_value]
key = value[:alias] || key
[key.to_s, ex_value]
end.compact.to_h
end

Expand Down
9 changes: 6 additions & 3 deletions lib/paradocs/extensions/structure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ def nested(&block)
else
result[errors] += field.possible_errors
end
result[field.key] = meta unless ignore_transparent && field.transparent?
yield(field.key, meta) if block_given?

field_key = field.meta_data[:alias] || field.key
result[field_key] = meta unless ignore_transparent && field.transparent?
yield(field_key, meta) if block_given?

next unless field.mutates_schema?
schema.subschemes.each do |name, subschema|
Expand Down Expand Up @@ -103,7 +105,8 @@ def flatten(&block)
private

def collect_meta(field, root)
json_path = root.empty? ? "$.#{field.key}" : "#{root}.#{field.key}"
field_key = field.meta_data[:alias] || field.key
json_path = root.empty? ? "$.#{field_key}" : "#{root}.#{field_key}"
meta = field.meta_data.merge(json_path: json_path)
sc = meta.delete(:schema)
meta[:mutates_schema] = true if meta.delete(:mutates_schema)
Expand Down
4 changes: 4 additions & 0 deletions lib/paradocs/field_dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ def description(text)
meta description: text
end

def as(identifier)
meta alias: identifier
end

def example(value)
meta example: value
end
Expand Down
3 changes: 2 additions & 1 deletion lib/paradocs/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ def coerce_one(val, context, flds: fields)
invoke_subschemes!(val, context, flds: flds)
flds.each_with_object({}) do |(_, field), m|
r = field.resolve(val, context.sub(field.key))
m[field.key] = r.value if r.eligible?
key = field.meta_data[:alias] || field.key
m[key] = r.value if r.eligible?
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/paradocs/struct.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def new!(attrs = {}, environment = {})
# this hook is called after schema definition in DSL module
def paradocs_after_define_schema(schema)
schema.fields.keys.each do |key|
key = schema.fields[key].meta_data[:alias] || key
define_method key do
_graph[key]
end
Expand Down
2 changes: 1 addition & 1 deletion lib/paradocs/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Paradocs
VERSION = "1.1.3"
VERSION = "1.1.4"
end
6 changes: 3 additions & 3 deletions spec/extensions/payload_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
end
subschema(:fooschema) { }
subschema(:barschema) do
field(:barfield).present.type(:boolean)
field(:barfield).present.type(:boolean).as(:bar_field)
end
end
end
Expand All @@ -32,7 +32,7 @@
allow_any_instance_of(Array).to receive(:sample) { "bar" }
payloads = described_class.new(schema).build!
expect(payloads.keys.sort).to eq([:barschema, :fooschema, :subschema1, :subschema2_deep_schema, :subschema2_empty])
expect(payloads[:barschema]).to eq({"test" => nil, "foo" => {"bar" => "bar", "barfield" => nil}})
expect(payloads[:barschema]).to eq({"test" => nil, "foo" => {"bar" => "bar", "bar_field" => nil}})
expect(payloads[:fooschema]).to eq({"test" => nil, "foo" => {"bar" => "bar"}})
expect(payloads[:subschema1]).to eq({"test" => nil, "foo" => {"bar" => "bar"}, "subtest1" => nil})
expect(payloads[:subschema2_deep_schema]).to eq({
Expand All @@ -57,7 +57,7 @@
end

expect(payloads.keys.sort).to eq([:barschema, :fooschema, :subschema1, :subschema2_deep_schema, :subschema2_empty])
expect(payloads[:barschema]).to eq({"test" => nil, "foo" => {"bar" => nil, "barfield" => true}}) # barfield is change to true and bar is nil
expect(payloads[:barschema]).to eq({"test" => nil, "foo" => {"bar" => nil, "bar_field" => true}}) # barfield is change to true and bar is nil
expect(payloads[:fooschema]).to eq({"test" => nil, "foo" => {"bar" => nil}}) # bar is nil
expect(payloads[:subschema1]).to eq({"test" => nil, "foo" => {"bar" => nil}}) # subtest is missing, bar is nil
expect(payloads[:subschema2_deep_schema]).to eq({
Expand Down
30 changes: 16 additions & 14 deletions spec/extensions/structures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
subschema(:highest_level) { field(:test).present } # no mutations on this level -> subschema ignored

field(:data).type(:object).present.schema do
field(:id).type(:integer).present.policy(:policy_with_error)
field(:id).type(:integer).present.policy(:policy_with_error).as(:user_id)
field(:name).type(:string).meta(label: "very important staff").description("Example description").example("John")
field(:role).type(:string).declared.options(["admin", "user"]).default("user").mutates_schema! do |*|
:test_subschema
Expand Down Expand Up @@ -64,13 +64,14 @@
test_field: {required: true, present: true, json_path: "$.data.test_field", nested_name: "data.test_field"}
}
})
expect(data_structure[:id]).to eq({
expect(data_structure[:user_id]).to eq({
type: :integer,
required: true,
present: true,
policy_with_error: {errors: [ArgumentError]},
json_path: "$.data.id",
nested_name: "data.id"
alias: :user_id,
json_path: "$.data.user_id",
nested_name: "data.user_id"
})
expect(data_structure[:name]).to eq({
type: :string,
Expand Down Expand Up @@ -131,11 +132,12 @@
json_path: "$.data.extra[].extra",
policy_with_silent_error: {errors: []}
},
"data.id" => {
"data.user_id" => {
type: :integer,
required: true,
present: true,
json_path: "$.data.id",
alias: :user_id,
json_path: "$.data.user_id",
policy_with_error: {errors: [ArgumentError]}
},
"data.name" => {
Expand Down Expand Up @@ -180,7 +182,7 @@
subschema: {
_errors: [ArgumentError],
"data" => {type: :object, required: true, present: true, json_path: "$.data"},
"data.id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, json_path: "$.data.id"},
"data.user_id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, alias: :user_id, json_path: "$.data.user_id"},
"data.name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true, description: "Example description", example: "John"},
"data.role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", mutates_schema: true},
"data.extra" => {type: :array, required: true, json_path: "$.data.extra[]"},
Expand All @@ -190,7 +192,7 @@
test_subschema: {
_errors: [ArgumentError],
"data" => {type: :object, required: true, present: true, json_path: "$.data"},
"data.id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, json_path: "$.data.id"},
"data.user_id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, alias: :user_id, json_path: "$.data.user_id"},
"data.name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true, description: "Example description", example: "John"},
"data.role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", mutates_schema: true},
"data.extra" => {type: :array, required: true, json_path: "$.data.extra[]"},
Expand All @@ -215,7 +217,7 @@
structure: {
"role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", nested_name: "data.role", mutates_schema: true},
"test_field" => {required: true, present: true, json_path: "$.data.test_field", nested_name: "data.test_field"},
"id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, json_path: "$.data.id", nested_name: "data.id"},
"user_id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, alias: :user_id, json_path: "$.data.user_id", nested_name: "data.user_id"},
"name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true, nested_name: "data.name", description: "Example description", example: "John"},
"extra" => {
type: :array, required: true, json_path: "$.data.extra[]", nested_name: "data.extra",
Expand All @@ -233,11 +235,11 @@
json_path: "$.data",
nested_name: "data",
structure: {
"role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", nested_name: "data.role", mutates_schema: true},
"test1" => {required: true, present: true, json_path: "$.data.test1", nested_name: "data.test1"},
"id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, json_path: "$.data.id", nested_name: "data.id"},
"name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true, nested_name: "data.name", description: "Example description", example: "John"},
"extra" => {
"role" => {type: :string, options: ["admin", "user"], default: "user", json_path: "$.data.role", nested_name: "data.role", mutates_schema: true},
"test1" => {required: true, present: true, json_path: "$.data.test1", nested_name: "data.test1"},
"user_id" => {type: :integer, required: true, present: true, policy_with_error: {errors: [ArgumentError]}, alias: :user_id, json_path: "$.data.user_id", nested_name: "data.user_id"},
"name" => {type: :string, label: "very important staff", json_path: "$.data.name", mutates_schema: true, nested_name: "data.name", description: "Example description", example: "John"},
"extra" => {
type: :array, required: true, json_path: "$.data.extra[]", nested_name: "data.extra",
structure: {"extra" => {default: false, policy_with_silent_error: {errors: []}, json_path: "$.data.extra[].extra", nested_name: "data.extra.extra"}}}
}
Expand Down
12 changes: 6 additions & 6 deletions spec/schema_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

subject do
described_class.new do
field(:title).policy(:string).present
field(:title).policy(:string).present.as(:article_title)
field(:price).policy(:integer).meta(label: "A price")
field(:status).policy(:string).options(['visible', 'hidden'])
field(:tags).policy(:split).policy(:array)
Expand All @@ -33,8 +33,8 @@
describe "#structure" do
it "represents data structure and meta data" do
sc = subject.structure.nested
expect(sc[:title][:present]).to be true
expect(sc[:title][:type]).to eq :string
expect(sc[:article_title][:present]).to be true
expect(sc[:article_title][:type]).to eq :string
expect(sc[:price][:type]).to eq :integer
expect(sc[:price][:label]).to eq "A price"
expect(sc[:variants][:type]).to eq :array
Expand Down Expand Up @@ -76,7 +76,7 @@ def test_schema(schema, payload, result)

output = subject.resolve(payload).output
expect(output).to eq({
title: "title",
article_title: "title",
price: 100,
status: "visible",
tags: ["tag"],
Expand All @@ -101,7 +101,7 @@ def test_schema(schema, payload, result)
variants: [{name: 'v1', sku: 'ABC', stock: '10', available_if_no_stock: true}]
},
{
title: 'iPhone 6 Plus',
article_title: 'iPhone 6 Plus',
price: 100,
status: 'visible',
tags: ['tag1', 'tag2'],
Expand All @@ -114,7 +114,7 @@ def test_schema(schema, payload, result)
variants: [{name: 'v1', available_if_no_stock: '1'}]
},
{
title: 'iPhone 6 Plus',
article_title: 'iPhone 6 Plus',
variants: [{name: 'v1', stock: 1, available_if_no_stock: true}]
})

Expand Down
16 changes: 8 additions & 8 deletions spec/struct_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
include Paradocs::Struct

schema do
field(:title).type(:string).present
field(:title).type(:string).present.as(:example_title)
field(:friends).type(:array).default([]).schema friend_class
end
end

new_instance = klass.new
expect(new_instance.title).to eq ''
expect(new_instance.example_title).to eq ''
expect(new_instance.friends).to eq []
expect(new_instance.valid?).to be false
expect(new_instance.errors['$.title']).not_to be_nil
Expand All @@ -35,7 +35,7 @@
]
})

expect(instance.title).to eq 'foo'
expect(instance.example_title).to eq 'foo'
expect(instance.friends.size).to eq 2
expect(instance.friends.first.name).to eq 'Ismael'
expect(instance.friends.first).to be_a friend_class
Expand Down Expand Up @@ -154,7 +154,7 @@ def salutation
schema do
field(:title).type(:string).present
field(:friends).type(:array).schema do
field(:name).type(:string)
field(:name).type(:string).as(:person_name)
field(:age).type(:integer).default(20)
end
end
Expand All @@ -171,8 +171,8 @@ def salutation
expect(instance.to_h).to eq({
title: 'foo',
friends: [
{name: 'Jane', age: 20},
{name: 'Joe', age: 39},
{person_name: 'Jane', age: 20},
{person_name: 'Joe', age: 39},
]
})

Expand All @@ -190,7 +190,7 @@ def salutation
include Paradocs::Struct

schema do
field(:title).type(:string).present
field(:title).type(:string).present.as(:example_title)
field(:friends).type(:array).schema do
field(:name).type(:string)
field(:age).type(:integer).default(20)
Expand All @@ -213,7 +213,7 @@ def salutation
]
)

expect(instance.title).to eq 'foo'
expect(instance.example_title).to eq 'foo'
expect(instance.email).to eq 'email@me.com'
expect(instance.friends.size).to eq 2
end
Expand Down

0 comments on commit f97ca7a

Please sign in to comment.