This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
treatment.go
84 lines (69 loc) · 1.82 KB
/
treatment.go
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
82
83
84
package api
import (
"encoding/json"
)
// Treatment is a basic structure.
type Treatment struct {
PayloadName Payload `json:"Name" sql:"-"`
PayloadPhone Payload `json:"Phone" sql:"-"`
PayloadAddress Payload `json:"Address" sql:"-"`
// Validator specific fields
Name *Text `json:"-"`
Phone *Telephone `json:"-"`
Address *Location `json:"-"`
// Persister specific fields
ID int `json:"-"`
NameID int `json:"-" pg:", fk:Name"`
PhoneID int `json:"-" pg:", fk:Phone"`
AddressID int `json:"-" pg:", fk:Address"`
}
// Unmarshal bytes in to the entity properties.
func (entity *Treatment) Unmarshal(raw []byte) error {
err := json.Unmarshal(raw, entity)
if err != nil {
return err
}
name, err := entity.PayloadName.Entity()
if err != nil {
return err
}
entity.Name = name.(*Text)
phone, err := entity.PayloadPhone.Entity()
if err != nil {
return err
}
entity.Phone = phone.(*Telephone)
address, err := entity.PayloadAddress.Entity()
if err != nil {
return err
}
entity.Address = address.(*Location)
return err
}
// Marshal to payload structure
func (entity *Treatment) Marshal() Payload {
if entity.Name != nil {
entity.PayloadName = entity.Name.Marshal()
}
if entity.Phone != nil {
entity.PayloadPhone = entity.Phone.Marshal()
}
if entity.Address != nil {
entity.PayloadAddress = entity.Address.Marshal()
}
return MarshalPayloadEntity("psychological.treatment", entity)
}
// Valid checks the value(s) against an battery of tests.
func (entity *Treatment) Valid() (bool, error) {
var stack ErrorStack
if ok, err := entity.Name.Valid(); !ok {
stack.Append("Treatment", err)
}
if ok, err := entity.Phone.Valid(); !ok {
stack.Append("Treatment", err)
}
if ok, err := entity.Address.Valid(); !ok {
stack.Append("Treatment", err)
}
return !stack.HasErrors(), stack
}