-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpacket.go
77 lines (65 loc) · 2.07 KB
/
packet.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
package sentry
// A Packet is a JSON serializable object that will be sent to
// the Sentry server to describe an event. It provides convenience
// methods for setting options and handling the various types of
// option that can be added.
type Packet interface {
// SetOptions will set all non-nil options provided, intelligently
// merging values when supported by an option, or replacing existing
// values if not.
SetOptions(options ...Option) Packet
// Clone will create a copy of this packet which can then be modified
// independently. In most cases it is a better idea to create a new
// client with the options you wish to override, however there are
// situations where this is a cleaner solution.
Clone() Packet
}
type packet map[string]Option
// NewPacket creates a new packet which will be sent to the Sentry
// server after its various options have been set.
// You will not usually need to create a Packet yourself, instead
// you should use your `Client`'s `Capture()` method.
func NewPacket() Packet {
return &packet{}
}
func (p packet) Clone() Packet {
np := packet{}
for k, v := range p {
np[k] = v
}
return &np
}
func (p packet) SetOptions(options ...Option) Packet {
for _, opt := range options {
p.setOption(opt)
}
return &p
}
func (p packet) setOption(option Option) {
if option == nil {
return
}
// If the option implements Omit(), check to see whether
// it has elected to be omitted.
if omittable, ok := option.(OmitableOption); ok {
if omittable.Omit() {
return
}
}
// If the option implements Finalize(), call it to give the
// option the chance to prepare itself properly
if finalizable, ok := option.(FinalizeableOption); ok {
finalizable.Finalize()
}
if advanced, ok := option.(AdvancedOption); ok {
advanced.Apply(p)
} else if existing, ok := p[option.Class()]; ok {
if mergable, ok := option.(MergeableOption); ok {
p[option.Class()] = mergable.Merge(existing)
} else {
p[option.Class()] = option
}
} else {
p[option.Class()] = option
}
}