This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
models.go
69 lines (56 loc) · 1.65 KB
/
models.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
package basicmessage
import (
"errors"
"strings"
"time"
"github.com/findy-network/findy-agent/std/decorator"
"github.com/lainio/err2"
"github.com/lainio/err2/try"
)
// todo: move this to right place later!
type AriesTime struct {
time.Time
}
// use generate errors with ACAPy when sending basic messages
// const ISO8601 = "2006-01-02 15:04:05.999999999Z"
const ISO8601 = "2006-01-02 15:04:05.999999Z"
type Basicmessage struct {
Type string `json:"@type,omitempty"`
ID string `json:"@id,omitempty"`
Thread *decorator.Thread `json:"~thread,omitempty"`
Content string `json:"content"`
SentTime AriesTime `json:"sent_time"`
}
func validateTimestamp(timeStr string) (t time.Time, err error) {
acceptedFormats := []string{ISO8601, time.RFC3339}
for _, fmt := range acceptedFormats {
if t, err = time.Parse(fmt, timeStr); err == nil {
break
}
}
return
}
func (at *AriesTime) UnmarshalJSON(b []byte) (err error) {
defer err2.Handle(&err)
t := try.To1(validateTimestamp(strings.Trim(string(b), "\"")))
*at = AriesTime{Time: t}
return
}
func (at AriesTime) MarshalJSON() ([]byte, error) {
// return (time.Time(t)).MarshalJSON()
// below taken from Go standard lib
t := at.Time
if y := t.Year(); y < 0 || y >= 10000 {
// RFC 3339 is clear that years are 4 digits exactly.
// See golang.org/issue/4556#c15 for more discussion.
return nil, errors.New("Time.MarshalJSON: year outside of range [0,9999]")
}
b := make([]byte, 0, len(ISO8601)+2)
b = append(b, '"')
b = t.AppendFormat(b, ISO8601)
b = append(b, '"')
return b, nil
}
func (at AriesTime) String() string {
return at.Time.String()
}