module Multiwoven::Integrations::Destination::Airtable::SchemaHelper
def adjust_array_with_any(original_type, complex_type, exec_type, options)
def adjust_array_with_any(original_type, complex_type, exec_type, options) if original_type == "formula" && %w[number currency percent duration].include?(exec_type) complex_type = SCHEMA_TYPES[:NUMBER] elsif original_type == "formula" && ARRAY_FORMULAS.none? { |x| options.fetch("formula", "").start_with?(x) } complex_type = SCHEMA_TYPES[:STRING] elsif SIMPLE_AIRTABLE_TYPES.keys.include?(exec_type) complex_type["items"] = deep_copy(SIMPLE_AIRTABLE_TYPES[exec_type]) else complex_type["items"] = SCHEMA_TYPES[:STRING] end complex_type end
def adjust_complex_type(original_type, complex_type, options)
def adjust_complex_type(original_type, complex_type, options) exec_type = options.dig("result", "type") || "simpleText" if complex_type == SCHEMA_TYPES[:ARRAY_WITH_ANY] adjust_array_with_any(original_type, complex_type, exec_type, options) else complex_type end end
def build_schema(properties)
def build_schema(properties) { "$schema" => JSON_SCHEMA_URL, "type" => "object", "additionalProperties" => true, "properties" => properties } end
def clean_name(name_str)
def clean_name(name_str) name_str.strip.gsub(" ", "_") end
def deep_copy(object)
def deep_copy(object) Marshal.load(Marshal.dump(object)) end
def determine_schema(original_type, options)
def determine_schema(original_type, options) if COMPLEX_AIRTABLE_TYPES.keys.include?(original_type) complex_type = deep_copy(COMPLEX_AIRTABLE_TYPES[original_type]) adjust_complex_type(original_type, complex_type, options) elsif SIMPLE_AIRTABLE_TYPES.keys.include?(original_type) simple_type_schema(original_type, options) else SCHEMA_TYPES[:STRING] end end
def get_json_schema(table)
def get_json_schema(table) fields = table["fields"] || {} properties = fields.each_with_object({}) do |field, props| name, schema = process_field(field) props[name] = schema end build_schema(properties) end
def process_field(field)
def process_field(field) name = clean_name(field.fetch("name", "")) original_type = field.fetch("type", "") options = field.fetch("options", {}) schema = determine_schema(original_type, options) [name, schema] end
def simple_type_schema(original_type, options)
def simple_type_schema(original_type, options) exec_type = options.dig("result", "type") || original_type deep_copy(SIMPLE_AIRTABLE_TYPES[exec_type]) end