From c3d04a54905bf44ce64491b52c3ea1a151667330 Mon Sep 17 00:00:00 2001 From: Kailash Nadh Date: Sun, 2 Oct 2022 22:53:38 +0530 Subject: [PATCH] Refactor `models.SubscriberAttribs` JSON wrapper to generic name `JSON`. --- cmd/subscribers.go | 2 +- internal/messenger/postback/postback.go | 2 +- .../messenger/postback/postback_easyjson.go | 2 +- internal/subimporter/importer.go | 2 +- models/models.go | 22 +++++++++---------- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/cmd/subscribers.go b/cmd/subscribers.go index 4ac9781..85afbcc 100644 --- a/cmd/subscribers.go +++ b/cmd/subscribers.go @@ -53,7 +53,7 @@ var ( Email: "demo@listmonk.app", Name: "Demo Subscriber", UUID: dummyUUID, - Attribs: models.SubscriberAttribs{"city": "Bengaluru"}, + Attribs: models.JSON{"city": "Bengaluru"}, } subQuerySortFields = []string{"email", "name", "created_at", "updated_at"} diff --git a/internal/messenger/postback/postback.go b/internal/messenger/postback/postback.go index ee10c76..b6ad26b 100644 --- a/internal/messenger/postback/postback.go +++ b/internal/messenger/postback/postback.go @@ -37,7 +37,7 @@ type recipient struct { UUID string `json:"uuid"` Email string `json:"email"` Name string `json:"name"` - Attribs models.SubscriberAttribs `json:"attribs"` + Attribs models.JSON `json:"attribs"` Status string `json:"status"` } diff --git a/internal/messenger/postback/postback_easyjson.go b/internal/messenger/postback/postback_easyjson.go index 78ac8d0..44c2b00 100644 --- a/internal/messenger/postback/postback_easyjson.go +++ b/internal/messenger/postback/postback_easyjson.go @@ -513,7 +513,7 @@ func easyjsonDf11841fDecodeGithubComKnadhListmonkInternalMessengerPostback1(in * in.Skip() } else { in.Delim('{') - out.Attribs = make(models.SubscriberAttribs) + out.Attribs = make(models.JSON) for !in.IsDelim('}') { key := string(in.String()) in.WantColon() diff --git a/internal/subimporter/importer.go b/internal/subimporter/importer.go index 3794f41..38837bf 100644 --- a/internal/subimporter/importer.go +++ b/internal/subimporter/importer.go @@ -536,7 +536,7 @@ func (s *Session) LoadCSV(srcPath string, delim rune) error { // JSON attributes. if len(row["attributes"]) > 0 { var ( - attribs models.SubscriberAttribs + attribs models.JSON b = []byte(row["attributes"]) ) if err := json.Unmarshal(b, &attribs); err != nil { diff --git a/models/models.go b/models/models.go index f3bc072..fc40b59 100644 --- a/models/models.go +++ b/models/models.go @@ -157,12 +157,12 @@ type User struct { type Subscriber struct { Base - UUID string `db:"uuid" json:"uuid"` - Email string `db:"email" json:"email" form:"email"` - Name string `db:"name" json:"name" form:"name"` - Attribs SubscriberAttribs `db:"attribs" json:"attribs"` - Status string `db:"status" json:"status"` - Lists types.JSONText `db:"lists" json:"lists"` + UUID string `db:"uuid" json:"uuid"` + Email string `db:"email" json:"email" form:"email"` + Name string `db:"name" json:"name" form:"name"` + Attribs JSON `db:"attribs" json:"attribs"` + Status string `db:"status" json:"status"` + Lists types.JSONText `db:"lists" json:"lists"` } type subLists struct { SubscriberID int `db:"subscriber_id"` @@ -178,8 +178,8 @@ type SubscriberExportProfile struct { LinkClicks json.RawMessage `db:"link_clicks" json:"link_clicks,omitempty"` } -// SubscriberAttribs is the map of key:value attributes of a subscriber. -type SubscriberAttribs map[string]interface{} +// JSON is is the wrapper for reading and writing arbitrary JSONB fields from the DB. +type JSON map[string]interface{} // StringIntMap is used to define DB Scan()s. type StringIntMap map[string]int @@ -398,14 +398,14 @@ func (subs Subscribers) LoadLists(stmt *sqlx.Stmt) error { } // Value returns the JSON marshalled SubscriberAttribs. -func (s SubscriberAttribs) Value() (driver.Value, error) { +func (s JSON) Value() (driver.Value, error) { return json.Marshal(s) } // Scan unmarshals JSONB from the DB. -func (s SubscriberAttribs) Scan(src interface{}) error { +func (s JSON) Scan(src interface{}) error { if src == nil { - s = make(SubscriberAttribs) + s = make(JSON) return nil }