This repository was archived by the owner on May 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreference.go
More file actions
64 lines (51 loc) · 1.38 KB
/
reference.go
File metadata and controls
64 lines (51 loc) · 1.38 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
package cap
import (
"crypto/sha1"
"encoding/hex"
"encoding/xml"
"fmt"
"strings"
)
type Reference struct {
Sender string `json:"sender"`
Identifier string `json:"identifier"`
Sent Time `json:"sent"`
}
type References []*Reference
func (reference *Reference) Id() string {
hash := sha1.New()
hash.Write([]byte(fmt.Sprintf("%s,%s,%s", reference.Sender, reference.Sent.FormatCAP(), reference.Identifier)))
return hex.EncodeToString(hash.Sum(nil))
}
func (m *References) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var str string
if err := d.DecodeElement(&str, &start); err != nil {
return err
}
// Ignore empty strings
if str == "" {
return nil
}
// Create the list if it doesn't already exist
references := strings.Split(str, " ")
for _, reference := range references {
components := strings.Split(reference, ",")
var sent Time
if err := sent.UnmarshalText([]byte(components[2])); err != nil {
return err
}
*m = append(*m, &Reference{
Sender: components[0],
Identifier: components[1],
Sent: sent,
})
}
return nil
}
func (m *References) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
values := make([]string, len(*m))
for i, ref := range *m {
values[i] = fmt.Sprintf("%s,%s,%s", ref.Sender, ref.Identifier, ref.Sent.FormatCAP())
}
return e.EncodeElement(strings.Join(values, " "), start)
}