Skip to content

Commit

Permalink
Merge pull request #153 from Shopify/active-serializers
Browse files Browse the repository at this point in the history
Make serializers simple POROs
  • Loading branch information
elsom25 committed May 3, 2024
2 parents ef4dc84 + 3944e19 commit fa2177b
Show file tree
Hide file tree
Showing 31 changed files with 551 additions and 328 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ gem "rubocop-shopify", require: false

group :test do
gem "minitest", "~> 5.21"
gem "minitest-rails", "~> 7.1"
gem "minitest-hooks", "~> 1.5"
end
65 changes: 65 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
GEM
remote: https://rubygems.org/
specs:
actionpack (7.1.3.2)
actionview (= 7.1.3.2)
activesupport (= 7.1.3.2)
nokogiri (>= 1.8.5)
racc
rack (>= 2.2.4)
rack-session (>= 1.0.1)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
actionview (7.1.3.2)
activesupport (= 7.1.3.2)
builder (~> 3.1)
erubi (~> 1.11)
rails-dom-testing (~> 2.2)
rails-html-sanitizer (~> 1.6)
activemodel (7.1.3.2)
activesupport (= 7.1.3.2)
activerecord (7.1.3.2)
Expand All @@ -22,14 +38,17 @@ GEM
ast (2.4.2)
base64 (0.2.0)
bigdecimal (3.1.7)
builder (3.2.4)
byebug (11.1.3)
colorator (1.1.0)
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
crass (1.0.6)
drb (2.2.1)
em-websocket (0.5.3)
eventmachine (>= 0.12.9)
http_parser.rb (~> 0)
erubi (1.12.0)
eventmachine (1.2.7)
ffi (1.16.3)
forwardable-extended (2.6.0)
Expand All @@ -40,6 +59,10 @@ GEM
http_parser.rb (0.8.0)
i18n (1.14.4)
concurrent-ruby (~> 1.0)
io-console (0.7.2)
irb (1.13.0)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
jekyll (4.3.3)
addressable (~> 2.4)
colorator (~> 1.0)
Expand Down Expand Up @@ -70,25 +93,64 @@ GEM
listen (3.9.0)
rb-fsevent (~> 0.10, >= 0.10.3)
rb-inotify (~> 0.9, >= 0.9.10)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
mercenary (0.4.0)
minitest (5.22.3)
minitest-hooks (1.5.1)
minitest (> 5.3)
minitest-rails (7.1.0)
minitest (~> 5.20)
railties (~> 7.1.0)
mutex_m (0.2.0)
nokogiri (1.16.4-arm64-darwin)
racc (~> 1.4)
nokogiri (1.16.4-x86_64-linux)
racc (~> 1.4)
parallel (1.24.0)
parser (3.3.0.5)
ast (~> 2.4.1)
racc
pathutil (0.16.2)
forwardable-extended (~> 2.6)
psych (5.1.2)
stringio
public_suffix (5.0.5)
racc (1.7.3)
rack (3.0.10)
rack-session (2.0.0)
rack (>= 3.0.0)
rack-test (2.1.0)
rack (>= 1.3)
rackup (2.1.0)
rack (>= 3)
webrick (~> 1.8)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
railties (7.1.3.2)
actionpack (= 7.1.3.2)
activesupport (= 7.1.3.2)
irb
rackup (>= 1.0.0)
rake (>= 12.2)
thor (~> 1.0, >= 1.2.2)
zeitwerk (~> 2.6)
rainbow (3.1.1)
rake (13.2.1)
rb-fsevent (0.11.2)
rb-inotify (0.10.1)
ffi (~> 1.0)
rdoc (6.6.3.1)
psych (>= 4.0.0)
regexp_parser (2.9.0)
reline (0.5.5)
io-console (~> 0.5)
rexml (3.2.6)
rouge (4.2.1)
rubocop (1.63.2)
Expand All @@ -114,8 +176,10 @@ GEM
google-protobuf (>= 3.25, < 5.0)
sqlite3 (1.7.3-arm64-darwin)
sqlite3 (1.7.3-x86_64-linux)
stringio (3.1.0)
terminal-table (3.0.2)
unicode-display_width (>= 1.1.1, < 3)
thor (1.3.1)
timeout (0.4.1)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
Expand All @@ -134,6 +198,7 @@ DEPENDENCIES
jekyll (~> 4.3)
minitest (~> 5.21)
minitest-hooks (~> 1.5)
minitest-rails (~> 7.1)
rake (~> 13.1)
rubocop-shopify
sqlite3 (~> 1.7)
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ $(DOCS_GENERATED_SENTINEL): $(LOCAL_DB) $(CATEGORIES_DATA) $(ATTRIBUTES_DATA) $(
$(V)touch $@

$(DIST_GENERATED_SENTINEL): $(LOCAL_DB) $(CATEGORIES_DATA) $(ATTRIBUTES_DATA) $(VALUES_DATA) $(MAPPINGS_DATA)
@$(GENERATE) "Building Dist" "$(GENERATED_DIST_PATH)/*.[json|txt]"
@$(GENERATE) "Building Distribution" "$(GENERATED_DIST_PATH)/*.[json|txt]"
$(V)bin/generate_dist $(VARG)
$(V)touch $@

Expand All @@ -99,7 +99,7 @@ clean:
$(V)rm -f $(CATEGORIES_DATA_CUE)
@$(NUKE) "Cleaning mapping data cuefiles" $(MAPPINGS_DATA_CUE)
$(V)rm -f $(MAPPINGS_DATA_CUE)
@$(NUKE) "Cleaning Generated Dist Files" $(GENERATED_DIST_PATH)
@$(NUKE) "Cleaning Generated Distribution Files" $(GENERATED_DIST_PATH)
$(V)rm -f $(DIST_GENERATED_SENTINEL)
$(V)rm -rf $(GENERATED_DIST_PATH)/*.json
$(V)rm -rf $(GENERATED_DIST_PATH)/*.txt
Expand Down
4 changes: 4 additions & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class << self
def gid(id)
"gid://shopify/TaxonomyCategory/#{id}"
end

def parent_id_of(id)
id.split("-")[0...-1].join("-").presence
end
end

def gid
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module Dist
module Distribution
class JSONSerializer
def initialize(verticals:, properties:, mapping_rules:, version:)
@verticals = verticals
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# frozen_string_literal: true

module Dist
module Distribution
class TextSerializer
def initialize(verticals:, properties:, values:, version:)
@verticals = verticals
Expand Down
36 changes: 15 additions & 21 deletions app/serializers/docs/search_index_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,25 @@

module Docs
class SearchIndexSerializer
include Singleton

class << self
delegate :serialize, to: :instance
end

def serialize(category_dist_json)
return @serialized if @serialized
def unpack(hash)
{
"title" => hash["full_name"],
"url" => "?categoryId=#{CGI.escapeURIComponent(hash["id"])}",
"category" => {
"id" => hash["id"],
"name" => hash["name"],
"fully_qualified_type" => hash["full_name"],
"depth" => hash["level"],
},
}
end

search_index = category_dist_json.flat_map do |vertical|
vertical["categories"].map do |category|
{
"title" => category.fetch("full_name"),
"url" => "?categoryId=#{CGI.escapeURIComponent(category.fetch("id"))}",
"category" => {
"id" => category.fetch("id"),
"name" => category.fetch("name"),
"fully_qualified_type" => category.fetch("full_name"),
"depth" => category.fetch("level"),
},
}
def unpack_all(data_list)
data_list.flat_map do |vertical|
vertical["categories"].map { unpack(_1) }
end
end

@serialized = ::JSON.fast_generate(search_index)
end
end
end
54 changes: 23 additions & 31 deletions app/serializers/docs/sibling_groups_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,35 @@

module Docs
class SiblingGroupsSerializer
include Singleton

class << self
delegate :serialize, to: :instance
end

def serialize(category_dist_json)
return @serialized if @serialized

sibling_groups = {}
category_dist_json.each do |vertical|
vertical["categories"].each do |category|
parent_id = category.fetch("parent_id").presence || "root"
depth = category.fetch("level")
def unpack(hash)
{
"id" => hash["id"],
"name" => hash["name"],
"fully_qualified_type" => hash["full_name"],
"depth" => hash["level"],
"parent_id" => parent_id(hash),
"node_type" => hash["level"].zero? ? "root" : "leaf",
"ancestor_ids" => hash["ancestors"].map { _1["id"] }.join(","),
"attribute_ids" => hash["attributes"].map { _1["id"] }.join(","),
}
end

sibling_groups[depth] ||= {}
sibling_groups[depth][parent_id] ||= []
sibling_groups[depth][parent_id] << sibling_group(category, parent_id:, depth:)
def unpack_all(data_list)
data_list.each_with_object({}) do |vertical, groups|
vertical["categories"].each do |hash|
groups[hash["level"]] ||= {}
groups[hash["level"]][parent_id(hash)] ||= []
groups[hash["level"]][parent_id(hash)] << unpack(hash)
end
end
end

@serialized = sibling_groups.to_yaml(line_width: 1000)
end

private
private

def sibling_group(category, parent_id:, depth:)
{
"id" => category.fetch("id"),
"name" => category.fetch("name"),
"fully_qualified_type" => category.fetch("full_name"),
"depth" => depth,
"parent_id" => parent_id,
"node_type" => depth.zero? ? "root" : "leaf",
"ancestor_ids" => category.fetch("ancestors").map { _1.fetch("id") }.join(","),
"attribute_ids" => category.fetch("attributes").map { _1.fetch("id") }.join(","),
}
def parent_id(hash)
hash["parent_id"].presence || "root"
end
end
end
end
17 changes: 0 additions & 17 deletions app/serializers/object_serializer.rb

This file was deleted.

20 changes: 20 additions & 0 deletions app/serializers/source/categories_property_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

module Source
class CategoriesPropertySerializer
class << self
def unpack(hash)
hash["attributes"].map do |attribute|
{
"category_id" => hash["id"],
"property_friendly_id" => attribute,
}
end
end

def unpack_all(data_list)
data_list.flat_map { unpack(_1) }
end
end
end
end
32 changes: 32 additions & 0 deletions app/serializers/source/category_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

module Source
class CategorySerializer
class << self
def unpack(hash)
{
"id" => hash["id"],
"parent_id" => Category.parent_id_of(hash["id"]),
"name" => hash["name"],
}
end

def unpack_all(hash_list)
hash_list.map { unpack(_1) }
end

def pack(category)
{
"id" => category.id,
"name" => category.name,
"children" => category.children.map(&:id),
"attributes" => category.properties.reorder(:id).pluck(:friendly_id),
}
end

def pack_all(categories)
categories.map { pack(_1) }
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

module Source
class ExtendedPropertiesPropertyValueSerializer
class << self
def unpack(hash)
base_property = Property.find_by!(friendly_id: hash["base_friendly_id"])
base_property.property_values.map do |value|
{
"property_id" => hash["id"],
"property_value_friendly_id" => value.friendly_id,
}
end
end

def unpack_all(hash_list)
hash_list.flat_map { unpack(_1) }
end
end
end
end
Loading

0 comments on commit fa2177b

Please sign in to comment.