Repository navigation
Expand file tree
/
Copy pathcontact.go
More file actions
81 lines (66 loc) · 2.51 KB
/
Copy pathcontact.go
File metadata and controls
81 lines (66 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
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
package entities
import (
"database/sql/driver"
"encoding/json"
"time"
"github.com/NdoleStudio/stacktrace"
"github.com/google/uuid"
"github.com/lib/pq"
)
// EntityNameContact is the entitlement entity name for contacts.
const EntityNameContact = "Contact"
// ContactProperties is a free-form key/value map persisted as a jsonb column.
type ContactProperties map[string]string
var _ driver.Valuer = ContactProperties{}
// Value implements driver.Valuer, serializing the map to JSON bytes.
func (p ContactProperties) Value() (driver.Value, error) {
if p == nil {
return []byte("{}"), nil
}
data, err := json.Marshal(p)
if err != nil {
return nil, stacktrace.Propagatef(err, "cannot marshal ContactProperties")
}
return data, nil
}
// Scan implements sql.Scanner, deserializing jsonb bytes/string into the map.
func (p *ContactProperties) Scan(src any) error {
if src == nil {
*p = ContactProperties{}
return nil
}
var data []byte
switch value := src.(type) {
case []byte:
data = value
case string:
data = []byte(value)
default:
return stacktrace.NewErrorf("unsupported type [%T] for ContactProperties", src)
}
if len(data) == 0 {
*p = ContactProperties{}
return nil
}
result := ContactProperties{}
if err := json.Unmarshal(data, &result); err != nil {
return stacktrace.Propagatef(err, "cannot unmarshal ContactProperties")
}
*p = result
return nil
}
// Contact represents a saved contact belonging to a user.
type Contact struct {
ID uuid.UUID `json:"id" gorm:"primaryKey;type:uuid" bson:"_id" example:"32343a19-da5e-4b1b-a767-3298a73703cb"`
UserID UserID `json:"user_id" gorm:"index" bson:"user_id" example:"WB7DRDWrJZRGbYrv2CKGkqbzvqdC"`
Name string `json:"name" bson:"name" example:"Alice Smith"`
Emails pq.StringArray `json:"emails" gorm:"type:text[]" bson:"emails" swaggertype:"array,string" example:"alice@example.com"`
PhoneNumbers pq.StringArray `json:"phone_numbers" gorm:"type:text[]" bson:"phone_numbers" swaggertype:"array,string" example:"+18005550199,+18005550100"`
Properties ContactProperties `json:"properties" gorm:"type:jsonb" bson:"properties" swaggertype:"object,string"`
CreatedAt time.Time `json:"created_at" bson:"created_at" example:"2022-06-05T14:26:02.302718+03:00"`
UpdatedAt time.Time `json:"updated_at" bson:"updated_at" example:"2022-06-05T14:26:02.302718+03:00"`
}
// TableName overrides the table name used by Contact.
func (Contact) TableName() string {
return "contacts"
}