Refactor models.SubscriberAttribs JSON wrapper to generic name JSON.

This commit is contained in:
Kailash Nadh 2022-10-02 22:53:38 +05:30
parent db2fd9ad70
commit c3d04a5490
5 changed files with 15 additions and 15 deletions

View file

@ -53,7 +53,7 @@ var (
Email: "demo@listmonk.app", Email: "demo@listmonk.app",
Name: "Demo Subscriber", Name: "Demo Subscriber",
UUID: dummyUUID, UUID: dummyUUID,
Attribs: models.SubscriberAttribs{"city": "Bengaluru"}, Attribs: models.JSON{"city": "Bengaluru"},
} }
subQuerySortFields = []string{"email", "name", "created_at", "updated_at"} subQuerySortFields = []string{"email", "name", "created_at", "updated_at"}

View file

@ -37,7 +37,7 @@ type recipient struct {
UUID string `json:"uuid"` UUID string `json:"uuid"`
Email string `json:"email"` Email string `json:"email"`
Name string `json:"name"` Name string `json:"name"`
Attribs models.SubscriberAttribs `json:"attribs"` Attribs models.JSON `json:"attribs"`
Status string `json:"status"` Status string `json:"status"`
} }

View file

@ -513,7 +513,7 @@ func easyjsonDf11841fDecodeGithubComKnadhListmonkInternalMessengerPostback1(in *
in.Skip() in.Skip()
} else { } else {
in.Delim('{') in.Delim('{')
out.Attribs = make(models.SubscriberAttribs) out.Attribs = make(models.JSON)
for !in.IsDelim('}') { for !in.IsDelim('}') {
key := string(in.String()) key := string(in.String())
in.WantColon() in.WantColon()

View file

@ -536,7 +536,7 @@ func (s *Session) LoadCSV(srcPath string, delim rune) error {
// JSON attributes. // JSON attributes.
if len(row["attributes"]) > 0 { if len(row["attributes"]) > 0 {
var ( var (
attribs models.SubscriberAttribs attribs models.JSON
b = []byte(row["attributes"]) b = []byte(row["attributes"])
) )
if err := json.Unmarshal(b, &attribs); err != nil { if err := json.Unmarshal(b, &attribs); err != nil {

View file

@ -157,12 +157,12 @@ type User struct {
type Subscriber struct { type Subscriber struct {
Base Base
UUID string `db:"uuid" json:"uuid"` UUID string `db:"uuid" json:"uuid"`
Email string `db:"email" json:"email" form:"email"` Email string `db:"email" json:"email" form:"email"`
Name string `db:"name" json:"name" form:"name"` Name string `db:"name" json:"name" form:"name"`
Attribs SubscriberAttribs `db:"attribs" json:"attribs"` Attribs JSON `db:"attribs" json:"attribs"`
Status string `db:"status" json:"status"` Status string `db:"status" json:"status"`
Lists types.JSONText `db:"lists" json:"lists"` Lists types.JSONText `db:"lists" json:"lists"`
} }
type subLists struct { type subLists struct {
SubscriberID int `db:"subscriber_id"` SubscriberID int `db:"subscriber_id"`
@ -178,8 +178,8 @@ type SubscriberExportProfile struct {
LinkClicks json.RawMessage `db:"link_clicks" json:"link_clicks,omitempty"` LinkClicks json.RawMessage `db:"link_clicks" json:"link_clicks,omitempty"`
} }
// SubscriberAttribs is the map of key:value attributes of a subscriber. // JSON is is the wrapper for reading and writing arbitrary JSONB fields from the DB.
type SubscriberAttribs map[string]interface{} type JSON map[string]interface{}
// StringIntMap is used to define DB Scan()s. // StringIntMap is used to define DB Scan()s.
type StringIntMap map[string]int type StringIntMap map[string]int
@ -398,14 +398,14 @@ func (subs Subscribers) LoadLists(stmt *sqlx.Stmt) error {
} }
// Value returns the JSON marshalled SubscriberAttribs. // Value returns the JSON marshalled SubscriberAttribs.
func (s SubscriberAttribs) Value() (driver.Value, error) { func (s JSON) Value() (driver.Value, error) {
return json.Marshal(s) return json.Marshal(s)
} }
// Scan unmarshals JSONB from the DB. // Scan unmarshals JSONB from the DB.
func (s SubscriberAttribs) Scan(src interface{}) error { func (s JSON) Scan(src interface{}) error {
if src == nil { if src == nil {
s = make(SubscriberAttribs) s = make(JSON)
return nil return nil
} }