-
Notifications
You must be signed in to change notification settings - Fork 14
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
Generate inline taxonomy mapping in taxonomy.json #123
Conversation
2781d35
to
8d934f1
Compare
@chesterbot01 can we start with new dist files for mappings only? Once those are made (and vetted), we can add this to |
We should also be preparing a fresh schema cue file to validate these for json. I'd also suggest thinking through what (if any) the appropriate |
@@ -70,5 +72,61 @@ def serialize_nested(connection) | |||
name: connection.name, | |||
} | |||
end | |||
|
|||
def build_mapping_blocks(mapping_rules) |
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.
Use the instance variable instead of passing this in? The other ones take a param because they're operating against a subset of the instance var vs its entirety
Integration.all.each do |integration| | ||
[true, false].each do |from_shopify| |
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.
Integration.all.each do |integration| | |
[true, false].each do |from_shopify| | |
mapping_rules = Integration.all.flat_map do |integration| | |
[true, false].filter_map do |from_shopify| | |
rules = mapping_rules.where(integration:, from_shopify:) | |
rules if rule.any? | |
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.
Mostly, if you decompose this it'll be easier to read with each piece being more focused. Also makes it simpler to use map methods, which are nice that each with manual tracking arrays 😁
mappings = [] | ||
Category.verticals.each do |vertical| | ||
mappings << MappingBuilder.build_one_to_one_mappings_for_vertical( | ||
mapping_rules: mapping_rule_block, | ||
vertical: vertical, | ||
) | ||
end | ||
processed_mappings = mappings.flatten.compact |
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.
AFAICT compact
isn't needed here
mappings = [] | |
Category.verticals.each do |vertical| | |
mappings << MappingBuilder.build_one_to_one_mappings_for_vertical( | |
mapping_rules: mapping_rule_block, | |
vertical: vertical, | |
) | |
end | |
processed_mappings = mappings.flatten.compact | |
mappings = Category.verticals.flat_map do |vertical| | |
MappingBuilder.build_one_to_one_mappings_for_vertical(mapping_rules:, vertical:) | |
end |
mapping[:input][:attributes] = mapping[:input][:attributes].map do |attribute| | ||
{ | ||
name: Property.find(attribute[:name]).gid, | ||
value: attribute[:value].nil? ? nil : PropertyValue.find(attribute[:value]).gid, |
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.
value: attribute[:value].nil? ? nil : PropertyValue.find(attribute[:value]).gid, | |
value: attribute[:value] || PropertyValue.find(attribute[:value]).gid, |
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.
I have to revert the change here because in some cases a nil attribute value will lead to a specific output that has been defined in YAML mapping rules.
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.
Ah fair. Kinda makes me want a NilPropertyValue
instead of magic nil, but this is fine
app/services/mapping_builder.rb
Outdated
relevant_rules = mapping_rules.select { |rule| rule.input.product_category_id.start_with?(vertical.id) } | ||
relevant_rules.map do |rule| | ||
{ | ||
input: rule.input.payload.delete_if { |_k, v| v.nil? }, | ||
output: rule.output.payload.delete_if { |_k, v| v.nil? }, | ||
} | ||
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.
relevant_rules = mapping_rules.select { |rule| rule.input.product_category_id.start_with?(vertical.id) } | |
relevant_rules.map do |rule| | |
{ | |
input: rule.input.payload.delete_if { |_k, v| v.nil? }, | |
output: rule.output.payload.delete_if { |_k, v| v.nil? }, | |
} | |
end | |
mapping_rules | |
.select { _1.input.product_category_id.start_with?(vertical.id) } | |
.map do | |
{ | |
input: _1.input.payload.delete_if { |_k, v| v.nil? }, | |
output: _1.output.payload.delete_if { |_k, v| v.nil? }, | |
} | |
end |
all_mapping_blocks = [] | ||
puts "Generating mappings ..." | ||
mapping_count = 0 | ||
Integration.all.each do |integration| | ||
[true, false].each do |from_shopify| | ||
mapping_rule_block = mapping_rules.where( | ||
integration_id: integration.id, | ||
from_shopify: from_shopify, | ||
) | ||
next if mapping_rule_block.count.zero? | ||
|
||
mappings = [] | ||
Category.verticals.each do |vertical| | ||
mappings << MappingBuilder.build_one_to_one_mappings_for_vertical( | ||
mapping_rules: mapping_rule_block, | ||
vertical: vertical, | ||
) | ||
end | ||
processed_mappings = mappings.flatten.compact | ||
mapping_count += processed_mappings.count | ||
all_mapping_blocks << { | ||
input_taxonomy: "shopify/v1", | ||
output_taxonomy: "#{integration.name}/v1", | ||
rules: processed_mappings, | ||
} | ||
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.
I had done this piece by piece, but assembling it:
all_mapping_blocks = [] | |
puts "Generating mappings ..." | |
mapping_count = 0 | |
Integration.all.each do |integration| | |
[true, false].each do |from_shopify| | |
mapping_rule_block = mapping_rules.where( | |
integration_id: integration.id, | |
from_shopify: from_shopify, | |
) | |
next if mapping_rule_block.count.zero? | |
mappings = [] | |
Category.verticals.each do |vertical| | |
mappings << MappingBuilder.build_one_to_one_mappings_for_vertical( | |
mapping_rules: mapping_rule_block, | |
vertical: vertical, | |
) | |
end | |
processed_mappings = mappings.flatten.compact | |
mapping_count += processed_mappings.count | |
all_mapping_blocks << { | |
input_taxonomy: "shopify/v1", | |
output_taxonomy: "#{integration.name}/v1", | |
rules: processed_mappings, | |
} | |
end | |
end | |
mapping_rules = Integration.all.flat_map do |integration| | |
[true, false].filter_map do |from_shopify| | |
rules = mapping_rules.where(integration:, from_shopify:) | |
rules if rule.any? | |
end | |
end | |
rules = Category.verticals.flat_map do |vertical| | |
MappingBuilder.build_one_to_one_mappings_for_vertical(mapping_rules:, vertical:) | |
end | |
mapping_rules.map do |mapping| | |
{ | |
input_taxonomy: "shopify/v1", # isn't this determined by the true/false technically? | |
output_taxonomy: "#{mapping.integration.name}/v1", | |
rules:, | |
} | |
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.
Thanks! Simplified the code based on the suggestion.
Sounds good, moved taxonomy mapping to a separate file for now. |
e99c901
to
a886c6a
Compare
@@ -2,9 +2,10 @@ | |||
|
|||
module Dist | |||
class JSONSerializer | |||
def initialize(verticals:, properties:, version:) | |||
def initialize(verticals:, properties:, mapping_rules: nil, version:) |
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.
Do we need default value here?
def initialize(verticals:, properties:, mapping_rules: nil, version:) | |
def initialize(verticals:, properties:, mapping_rules:, version:) |
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.
Removed.
@@ -70,5 +79,56 @@ def serialize_nested(connection) | |||
name: connection.name, | |||
} | |||
end | |||
|
|||
def build_mapping_blocks | |||
puts "Generating mappings ..." |
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.
Do we need this in our serializers? I think it's reasonable for serializers to not have console output 😅
a886c6a
to
c1a3983
Compare
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.
Looks good, though I'd like someone from multi-channel land to give this a check before merge as well (cc @jeanrick)
c1a3983
to
4bfb869
Compare
@@ -70,5 +79,54 @@ def serialize_nested(connection) | |||
name: connection.name, | |||
} | |||
end | |||
|
|||
def build_mapping_blocks | |||
mapping_count = 0 |
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.
We don't need this anymore, correct?
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.
Yea, good catch!
4ff15f0
to
16fad0a
Compare
@@ -52,6 +54,7 @@ LOCAL_DB = tmp/local.sqlite3 | |||
# CUE imports needed for schema validation | |||
ATTRIBUTES_DATA_CUE = schema/attributes_data.cue | |||
CATEGORIES_DATA_CUE = schema/categories_data.cue | |||
MAPPINGS_DATA_CUE = schema/mappings_data.cue |
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.
You only need this if you need the data generated as part of integrity checks (see validations.cue
)
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.
without schema/mappings_data.cue
, cue vet -c
will error out
mappings: field is required but not present:
./schema.cue:59:1
we have some validation logic for mappings defined in schema/schema.cue
582c246
to
1ee66df
Compare
input_taxonomy: "2024-04" | ||
output_taxonomy: "2021-09-21" |
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.
We should still prefix these versions with the platform e.g. shopify/2024-04
and google/2021-09-21
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.
Thanks! Done.
1ee66df
to
33cdb5d
Compare
33cdb5d
to
4819d48
Compare
Generate inline taxonomy mapping:
![Screenshot 2024-03-25 at 10 17 11 PM](https://private-user-images.githubusercontent.com/11143394/316705177-491412e1-2a28-4ac7-ba66-8ec7234b6d2c.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjE2MDI5MTQsIm5iZiI6MTcyMTYwMjYxNCwicGF0aCI6Ii8xMTE0MzM5NC8zMTY3MDUxNzctNDkxNDEyZTEtMmEyOC00YWM3LWJhNjYtOGVjNzIzNGI2ZDJjLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA3MjElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwNzIxVDIyNTY1NFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWUyYmFkZTdjZGJlNTUzZTAwZGVmZmIyNWNjMTFmMjZlN2JmZmFmOTUxMGJjZTNhOGNlMjI5YTYyYzQxZTBhMWEmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.T3ics293pBX9Y67Nhh54nYALsdJt-x7t2JboTigkMLo)