Repository: amp-labs/connectors
Pull request #1320 (2025-01-31 23:12:27)

[CON-1036] fix(Klaviyo): ReadResult.Raw
# Description

Replaced the `flattenRecords` function with `common.RecordFlattener("attributes")`.

# Testing
## Accounts
![image](https://github.com/user-attachments/assets/c17bdad4-3203-4841-8e58-2071ea489256)
![image](https://github.com/user-attachments/assets/21b20ab5-4e74-4a0f-bd42-aff35023a5c0)



Cobalt0s: * **#1320** <a href="https://app.graphite.dev/github/pr/amp-labs/connectors/1320?utm_source=stack-comment-icon" target="_blank"><img src="https://static.graphite.dev/graphite-32x32-black.png" alt="Graphite" width="10px" height="10px"/></a> 👈 <a href="https://app.graphite.dev/github/pr/amp-labs/connectors/1320?utm_source=stack-comment-view-in-graphite" target="_blank">(View in Graphite)</a>
* `main`


<h2></h2>

This stack of pull requests is managed by <a href="https://graphite.dev?utm-source=stack-comment"><b>Graphite</b></a>. Learn more about <a href="https://stacking.dev/?utm_source=stack-comment">stacking</a>.
<!-- Current dependencies on/for this PR: -->

Patch:
diff --git a/providers/klaviyo/parse.go b/providers/klaviyo/parse.go
index d7d8f82a..5948ef90 100644
--- a/providers/klaviyo/parse.go
+++ b/providers/klaviyo/parse.go
@@ -5,67 +5,8 @@ import (
 	"github.com/spyzhov/ajson"
 )
 
-// Every object has special field attributes which holds all the object specific fields.
-// Therefore, nested "attributes" will be removed and fields will be moved to the top level of the object.
-//
-// Example accounts(shortened response):
-//
-//	 "data": [
-//	    {
-//	        "type": "",
-//	        "id": "",
-//	        "attributes": {
-//	            "test_account": false,
-//	            "contact_information": {},
-//	            "locale": ""
-//	        },
-//	        "links": {}
-//	    }
-//	],
-//
-// The resulting fields for the above will be: type, id, test_account, contact_information, locale, links.
-func getRecords(node *ajson.Node) ([]map[string]any, error) {
-	arr, err := jsonquery.New(node).ArrayOptional("data")
-	if err != nil {
-		return nil, err
-	}
-
-	return flattenRecords(arr)
-}
-
-func flattenRecords(arr []*ajson.Node) ([]map[string]any, error) {
-	result := make([]map[string]any, len(arr))
-
-	for index, element := range arr {
-		const keyAttributes = "attributes"
-
-		attributes, err := jsonquery.New(element).ObjectOptional(keyAttributes)
-		if err != nil {
-			return nil, err
-		}
-
-		original, err := jsonquery.Convertor.ObjectToMap(element)
-		if err != nil {
-			return nil, err
-		}
-
-		nested, err := jsonquery.Convertor.ObjectToMap(attributes)
-		if err != nil {
-			return nil, err
-		}
-
-		// Attributes object must be removed.
-		delete(original, keyAttributes)
-
-		// Fields from attributes are moved to the top level.
-		for key, value := range nested {
-			original[key] = value
-		}
-
-		result[index] = original
-	}
-
-	return result, nil
+func getRecords(node *ajson.Node) ([]*ajson.Node, error) {
+	return jsonquery.New(node).ArrayOptional("data")
 }
 
 func getNextRecordsURL(node *ajson.Node) (string, error) {
diff --git a/providers/klaviyo/read.go b/providers/klaviyo/read.go
index cdbad278..ad5bda70 100644
--- a/providers/klaviyo/read.go
+++ b/providers/klaviyo/read.go
@@ -31,7 +31,7 @@ func (c *Connector) Read(ctx context.Context, config common.ReadParams) (*common
 	return common.ParseResult(res,
 		getRecords,
 		getNextRecordsURL,
-		common.GetMarshaledData,
+		common.MakeMarshaledDataFunc(common.RecordFlattener("attributes")),
 		config.Fields,
 	)
 }


Test patch:
diff --git a/providers/klaviyo/read_test.go b/providers/klaviyo/read_test.go
index b7c0a34e..363cbd2e 100644
--- a/providers/klaviyo/read_test.go
+++ b/providers/klaviyo/read_test.go
@@ -75,9 +75,24 @@ func TestRead(t *testing.T) { //nolint:funlen,gocognit,cyclop,maintidx
 						"email": "jennifer@gmail.com",
 					},
 					Raw: map[string]any{
-						"id":      "01HSXWNWF52J5PJG45BW383RMV",
-						"created": "2024-03-26T17:24:42+00:00",
-						"updated": "2024-03-26T17:24:42+00:00",
+						"id": "01HSXWNWF52J5PJG45BW383RMV",
+						"attributes": map[string]any{
+							"email":           "jennifer@gmail.com",
+							"phone_number":    nil,
+							"external_id":     nil,
+							"anonymous_id":    nil,
+							"first_name":      nil,
+							"last_name":       nil,
+							"organization":    nil,
+							"locale":          nil,
+							"title":           nil,
+							"image":           nil,
+							"created":         "2024-03-26T17:24:42+00:00",
+							"updated":         "2024-03-26T17:24:42+00:00",
+							"last_event_date": "2024-03-26T17:24:41+00:00",
+							"location":        map[string]any{},
+							"properties":      map[string]any{},
+						},
 					},
 				}},
 				NextPage: "https://a.klaviyo.com/api/profiles?page%5Bsize%5D=1&page%5Bcursor%5D=bmV4dDo6aWQ6OjAxSFNYV05XRjUySjVQSkc0NUJXMzgzUk1W", // nolint:lll
@@ -110,9 +125,19 @@ func TestRead(t *testing.T) { //nolint:funlen,gocognit,cyclop,maintidx
 						"name": "Email Campaign - Nov 15, 2024, 1:18 AM",
 					},
 					Raw: map[string]any{
-						"status":       "Scheduled",
-						"created_at":   "2024-11-14T23:18:34.827140+00:00",
-						"scheduled_at": "2024-11-14T23:20:02.718919+00:00",
+						"attributes": map[string]any{
+							"name":             "Email Campaign - Nov 15, 2024, 1:18 AM",
+							"status":           "Scheduled",
+							"archived":         false,
+							"audiences":        map[string]any{},
+							"send_options":     map[string]any{},
+							"tracking_options": map[string]any{},
+							"send_strategy":    map[string]any{},
+							"created_at":       "2024-11-14T23:18:34.827140+00:00",
+							"scheduled_at":     "2024-11-14T23:20:02.718919+00:00",
+							"updated_at":       "2024-11-14T23:20:32.232276+00:00",
+							"send_time":        "2024-11-30T18:15:00+00:00",
+						},
 					},
 				}},
 				NextPage: "",
diff --git a/providers/klaviyo/test/read-campaigns.json b/providers/klaviyo/test/read-campaigns.json
index c955b047..976f037f 100644
--- a/providers/klaviyo/test/read-campaigns.json
+++ b/providers/klaviyo/test/read-campaigns.json
@@ -1,65 +1,49 @@
 {
-  "data" : [ {
-    "type" : "campaign",
-    "id" : "01JCPFHB29QZ1NDPR3GCGQS5G2",
-    "attributes" : {
-      "name" : "Email Campaign - Nov 15, 2024, 1:18 AM",
-      "status" : "Scheduled",
-      "archived" : false,
-      "audiences" : {
-        "included" : [ "XzKvNz" ],
-        "excluded" : [ ]
+  "data": [
+    {
+      "type": "campaign",
+      "id": "01JCPFHB29QZ1NDPR3GCGQS5G2",
+      "attributes": {
+        "name": "Email Campaign - Nov 15, 2024, 1:18 AM",
+        "status": "Scheduled",
+        "archived": false,
+        "audiences": {},
+        "send_options": {},
+        "tracking_options": {},
+        "send_strategy": {},
+        "created_at": "2024-11-14T23:18:34.827140+00:00",
+        "scheduled_at": "2024-11-14T23:20:02.718919+00:00",
+        "updated_at": "2024-11-14T23:20:32.232276+00:00",
+        "send_time": "2024-11-30T18:15:00+00:00"
       },
-      "send_options" : {
-        "use_smart_sending" : true,
-        "ignore_unsubscribes" : false
-      },
-      "tracking_options" : {
-        "add_tracking_params" : false,
-        "custom_tracking_params" : [ ],
-        "is_tracking_clicks" : true,
-        "is_tracking_opens" : true
-      },
-      "send_strategy" : {
-        "method" : "static",
-        "options_static" : {
-          "datetime" : "2024-11-30T18:15:00+00:00",
-          "is_local" : true,
-          "send_past_recipients_immediately" : true
+      "relationships": {
+        "campaign-messages": {
+          "data": [
+            {
+              "type": "campaign-message",
+              "id": "01JCPFHB2F7J0J2R2HC8GNNC3Z"
+            }
+          ],
+          "links": {
+            "self": "https://a.klaviyo.com/api/campaigns/01JCPFHB29QZ1NDPR3GCGQS5G2/relationships/campaign-messages/",
+            "related": "https://a.klaviyo.com/api/campaigns/01JCPFHB29QZ1NDPR3GCGQS5G2/campaign-messages/"
+          }
         },
-        "options_throttled" : null,
-        "options_sto" : null
-      },
-      "created_at" : "2024-11-14T23:18:34.827140+00:00",
-      "scheduled_at" : "2024-11-14T23:20:02.718919+00:00",
-      "updated_at" : "2024-11-14T23:20:32.232276+00:00",
-      "send_time" : "2024-11-30T18:15:00+00:00"
-    },
-    "relationships" : {
-      "campaign-messages" : {
-        "data" : [ {
-          "type" : "campaign-message",
-          "id" : "01JCPFHB2F7J0J2R2HC8GNNC3Z"
-        } ],
-        "links" : {
-          "self" : "https://a.klaviyo.com/api/campaigns/01JCPFHB29QZ1NDPR3GCGQS5G2/relationships/campaign-messages/",
-          "related" : "https://a.klaviyo.com/api/campaigns/01JCPFHB29QZ1NDPR3GCGQS5G2/campaign-messages/"
+        "tags": {
+          "links": {
+            "self": "https://a.klaviyo.com/api/campaigns/01JCPFHB29QZ1NDPR3GCGQS5G2/relationships/tags/",
+            "related": "https://a.klaviyo.com/api/campaigns/01JCPFHB29QZ1NDPR3GCGQS5G2/tags/"
+          }
         }
       },
-      "tags" : {
-        "links" : {
-          "self" : "https://a.klaviyo.com/api/campaigns/01JCPFHB29QZ1NDPR3GCGQS5G2/relationships/tags/",
-          "related" : "https://a.klaviyo.com/api/campaigns/01JCPFHB29QZ1NDPR3GCGQS5G2/tags/"
-        }
+      "links": {
+        "self": "https://a.klaviyo.com/api/campaigns/01JCPFHB29QZ1NDPR3GCGQS5G2/"
       }
-    },
-    "links" : {
-      "self" : "https://a.klaviyo.com/api/campaigns/01JCPFHB29QZ1NDPR3GCGQS5G2/"
     }
-  } ],
-  "links" : {
-    "self" : "https://a.klaviyo.com/api/campaigns?filter=greater-than%28updated_at%2C2024-11-14T22%3A27%3A51Z%29%2Cequals%28messages.channel%2C%27email%27%29",
-    "next" : null,
-    "prev" : null
+  ],
+  "links": {
+    "self": "https://a.klaviyo.com/api/campaigns?filter=greater-than%28updated_at%2C2024-11-14T22%3A27%3A51Z%29%2Cequals%28messages.channel%2C%27email%27%29",
+    "next": null,
+    "prev": null
   }
 }
diff --git a/providers/klaviyo/test/read-profiles-1-first-page.json b/providers/klaviyo/test/read-profiles-1-first-page.json
index 70697307..6134e344 100644
--- a/providers/klaviyo/test/read-profiles-1-first-page.json
+++ b/providers/klaviyo/test/read-profiles-1-first-page.json
@@ -17,18 +17,7 @@
         "created": "2024-03-26T17:24:42+00:00",
         "updated": "2024-03-26T17:24:42+00:00",
         "last_event_date": "2024-03-26T17:24:41+00:00",
-        "location": {
-          "city": null,
-          "zip": null,
-          "country": null,
-          "longitude": null,
-          "address2": null,
-          "address1": null,
-          "region": null,
-          "latitude": null,
-          "timezone": null,
-          "ip": null
-        },
+        "location": {},
         "properties": {}
       },
       "relationships": {