-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
72 lines (55 loc) · 1.92 KB
/
types.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
package bencode
// Value represents a bencoded value with one of the
// following concrete types: Dictionary, List, Integer, and
// Bytes
type Value interface {
Type() Type
Value() interface{}
ToDict() (*Dictionary, bool)
ToList() (List, bool)
ToInteger() (Integer, bool)
ToBytes() (Bytes, bool)
}
type Type int
const (
TypeDict Type = iota
TypeInteger
TypeList
TypeBytes
)
func IsDict(v Value) bool {
return v.Type() == TypeDict
}
func IsList(v Value) bool {
return v.Type() == TypeList
}
func IsInteger(v Value) bool {
return v.Type() == TypeInteger
}
func IsBytes(v Value) bool {
return v.Type() == TypeBytes
}
// Bytes represents a bencoded string of bytes
type Bytes []byte
func (b Bytes) Value() interface{} { return b }
func (b Bytes) Type() Type { return TypeBytes }
func (b Bytes) ToDict() (*Dictionary, bool) { return nil, false }
func (b Bytes) ToList() (List, bool) { return List{}, false }
func (b Bytes) ToInteger() (Integer, bool) { return Integer(0), false }
func (b Bytes) ToBytes() (Bytes, bool) { return b, true }
// List represents a bencoded list
type List []Value
func (l List) Value() interface{} { return l }
func (l List) Type() Type { return TypeList }
func (l List) ToDict() (*Dictionary, bool) { return &Dictionary{}, false }
func (l List) ToInteger() (Integer, bool) { return Integer(0), false }
func (l List) ToBytes() (Bytes, bool) { return Bytes{}, false }
func (l List) ToList() (List, bool) { return l, true }
// Integer represents a bencoded integer
type Integer int
func (i Integer) Value() interface{} { return i }
func (i Integer) Type() Type { return TypeInteger }
func (i Integer) ToBytes() (Bytes, bool) { return Bytes{}, false }
func (i Integer) ToDict() (*Dictionary, bool) { return &Dictionary{}, false }
func (i Integer) ToList() (List, bool) { return List{}, false }
func (i Integer) ToInteger() (Integer, bool) { return i, true }