Module: Etl::Integrations::Source::SalesforceConsumerGoodsCloud::SchemaHelper

Includes:
Core::Constants
Defined in:
lib/etl/integrations/source/salesforce_consumer_goods_cloud/schema_helper.rb

Constant Summary

Constants included from Core::Constants

Core::Constants::AIRTABLE_BASES_ENDPOINT, Core::Constants::AIRTABLE_GET_BASE_SCHEMA_ENDPOINT, Core::Constants::AIRTABLE_URL_BASE, Core::Constants::CATALOG_SPEC_PATH, Core::Constants::CONNECTOR_SPEC_PATH, Core::Constants::DATABRICKS_DRIVER_PATH, Core::Constants::DATABRICKS_MAC_DRIVER_PATH, Core::Constants::FACEBOOK_AUDIENCE_GET_ALL_ACCOUNTS, Core::Constants::GOOGLE_SHEETS_SCOPE, Core::Constants::GOOGLE_SPREADSHEET_ID_REGEX, Core::Constants::HTTP_DELETE, Core::Constants::HTTP_GET, Core::Constants::HTTP_POST, Core::Constants::HTTP_PUT, Core::Constants::JSON_SCHEMA_URL, Core::Constants::KLAVIYO_AUTH_ENDPOINT, Core::Constants::KLAVIYO_AUTH_PAYLOAD, Core::Constants::META_DATA_PATH, Core::Constants::SNOWFLAKE_DRIVER_PATH, Core::Constants::SNOWFLAKE_MAC_DRIVER_PATH

Class Method Summary collapse

Class Method Details

.create_json_schema_for_object(metadata) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/etl/integrations/source/salesforce_consumer_goods_cloud/schema_helper.rb', line 93

def create_json_schema_for_object()
  fields_schema = ["fields"].map do |field|
    {
      "#{field[:name]}": salesforce_field_to_json_schema_type(field)
    }
  end.reduce(:merge)

  json_schema = {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "title": ["name"],
    "type": "object",
    "additionalProperties": true,
    "properties": fields_schema
  }

  required = ["fields"].map do |field|
    field["name"] if field["nillable"] == false
  end.compact
  primary_key = ["fields"].map do |field|
    field["name"] if field["nillable"] == false && field["unique"] == true
  end.compact

  {
    "name": ["name"],
    "action": "create",
    "json_schema": json_schema,
    "required": required,
    "supported_sync_modes": %w[incremental],
    "source_defined_primary_key": [primary_key],
    "source_defined_cursor": false,
    "default_cursor_field": nil
  }
end

.salesforce_field_to_json_schema_type(sf_field) ⇒ Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/etl/integrations/source/salesforce_consumer_goods_cloud/schema_helper.rb', line 12

def salesforce_field_to_json_schema_type(sf_field) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
  case sf_field["type"]
  when "string", "Email", "Phone", "Text", "TextArea", "TextEncrypted", "URL", "Picklist (Single)"
    if sf_field["nillable"]
      { "type": %w[string null] }
    else
      { "type": "string" }
    end
  when "double", "Currency", "Percent"
    if sf_field["nillable"]
      { "type": %w[number null] }
    else
      { "type": "number" }
    end
  when "boolean", "Checkbox"
    if sf_field["nillable"]
      { "type": %w[boolean null] }
    else
      { "type": "boolean" }
    end
  when "int", "AutoNumber"
    if sf_field["nillable"]
      { "type": %w[integer null] }
    else
      { "type": "integer" }
    end
  when "date"
    if sf_field["nillable"]
      { "type": %w[string null], "format": "date" }
    else
      { "type": "string", "format": "date" }
    end
  when "datetime", "DateTime"
    if sf_field["nillable"]
      { "type": %w[string null], "format": "date-time" }
    else
      { "type": "string", "format": "date-time" }
    end
  when "time"
    if sf_field["nillable"]
      { "type": %w[string null], "format": "time" }
    else
      { "type": "string", "format": "time" }
    end
  when "textarea", "Text Area (Long)", "Text Area (Rich)"
    if sf_field["nillable"]
      { "type": %w[string null] }
    else
      { "type": "string" }
    end
  when "picklist", "multipicklist", "Picklist (Multi-select)"
    if sf_field[:picklistValues] && sf_field["nillable"]
      enum_values = sf_field[:picklistValues].map { |val| val["value"] }
      { "type": %w[string null], "items": { "type": "string" }, "enum": enum_values }
    elsif sf_field[:picklistValues]
      enum_values = sf_field[:picklistValues].map { |val| val["value"] }
      { "type": "string", "items": { "type": "string" }, "enum": enum_values }
    else
      { "type": "string", "items": { "type": "string" } }
    end
  when "reference", "Reference (Lookup & Master-Detail)"
    if sf_field["nillable"]
      { "type": %w[string null] }
    else
      { "type": "string" }
    end
  when "location", "Geolocation"
    if sf_field["nillable"]
      { "type": %w[object null], "properties": { "latitude": { "type": "number" }, "longitude": { "type": "number" } } }
    else
      { "type": "object", "properties": { "latitude": { "type": "number" }, "longitude": { "type": "number" } } }
    end
  else
    if sf_field["nillable"]
      { "type": %w[string null] }
    else
      { "type": "string" }
    end
  end
end