From 0a80110694fc1a3f44ec4cb5488c7529b8d2a2b9 Mon Sep 17 00:00:00 2001 From: maiha Date: Thu, 31 Aug 2017 20:05:08 +0900 Subject: [PATCH] mapping: array field should return empty array rather than error when the field is missing --- spec/example/facebook_adcreatives_spec.cr | 74 +++++++++++++++++++++++ spec/example/facebook_adimages_spec.cr | 38 ++++++++++++ src/jq/mapping.cr | 6 +- 3 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 spec/example/facebook_adcreatives_spec.cr create mode 100644 spec/example/facebook_adimages_spec.cr diff --git a/spec/example/facebook_adcreatives_spec.cr b/spec/example/facebook_adcreatives_spec.cr new file mode 100644 index 0000000..f146e36 --- /dev/null +++ b/spec/example/facebook_adcreatives_spec.cr @@ -0,0 +1,74 @@ +require "./spec_helper" + +module Example::Facebook::AdCreatives + # account.adcreatives(fields: %w( title object_story_id object_story_spec )) + STR = <<-EOF + [ + { + "title": "abc", + "object_story_spec": { + "page_id": "012345...", + "link_data": { + "link": "http://example.com/", + "message": "advertise message", + "name": "title", + "attachment_style": "link", + "image_hash": "c9e4c9...", + "call_to_action": { + "type": "LEARN_MORE" + }, + "child_attachments": [ + { + "link": "http://example.com/1", + "image_hash": "405bf...", + "name": "title of carousel1", + "call_to_action": { + "type": "LEARN_MORE" + } + }, + { + "link": "http://example.com/2", + "image_hash": "9cef4...", + "name": "title of carousel2", + "call_to_action": { + "type": "LEARN_MORE" + } + } + ] + } + } + } + ] + EOF + + class Attachment + Jq.mapping({ + link: String, + name: String, + }) + end + + class LinkData + Jq.mapping({ + link: String, + message: String, + name: String, + attachments: {Array(Attachment), ".child_attachments"}, + }) + end + + class AdCreative + Jq.mapping({ + title: String, + link_data: {LinkData, ".object_story_spec.link_data"}, + }) + end + + describe "Example::Facebook::AdCreatives" do + it "works" do + creatives = Array(AdCreative).from_json(STR) + link = creatives[0].link_data + link.attachments.map(&.link).should eq(["http://example.com/1","http://example.com/2"]) + end + end +end diff --git a/spec/example/facebook_adimages_spec.cr b/spec/example/facebook_adimages_spec.cr new file mode 100644 index 0000000..f9fb9fe --- /dev/null +++ b/spec/example/facebook_adimages_spec.cr @@ -0,0 +1,38 @@ +require "./spec_helper" + +module Example::Facebook::AdImages + # account.adimages(fields: ["name", "status", "creatives"]) + STR = <<-EOF + [ + { + "name": "abc", + "status": "ACTIVE", + "creatives": ["123456","789012"] + }, + { + "name": "xyz", + "status": "ACTIVE" + } + ] + EOF + + class AdImage + Jq.mapping({ + name: String, + status: String, + creatives: {Array(String), ".creatives"} + }) + end + + describe "Example::Facebook::AdImages" do + it "works with array field" do + images = Array(AdImage).from_json(STR) + images[0].creatives.should eq(["123456","789012"]) + end + + it "return empty array when array field is missing" do + images = Array(AdImage).from_json(STR) + images[1].creatives.should eq(Array(String).new) + end + end +end diff --git a/src/jq/mapping.cr b/src/jq/mapping.cr index dc8523d..773ce86 100644 --- a/src/jq/mapping.cr +++ b/src/jq/mapping.cr @@ -34,7 +34,11 @@ class Jq def {{key.id}} if @{{key.id}}.nil? - default_{{key.id}} + {% if tuple[0].stringify =~ /^Array\(/ %} + {{tuple[0]}}.new + {% else %} + default_{{key.id}} + {% end %} else @{{key.id}}.not_nil! end