-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtypes.go
104 lines (83 loc) · 2.34 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package gearman // import "github.com/nathanaelle/gearman/v2"
import (
"bytes"
"encoding/base64"
"errors"
"github.com/nathanaelle/gearman/v2/protocol"
)
type (
// TaskID is used as the ID of a gearman Task
TaskID []byte
TaskMapID [64]byte
// Function is used as the Function Name of a gearman Task
Function []byte
// ClientID is used as the ID of a client in a gearman Task
ClientID []byte
)
// Cast is UnmarshalerGearman.Cast
func (tid *TaskID) Cast(o protocol.Opaque) error {
return tid.UnmarshalGearman(o.Bytes())
}
// UnmarshalGearman is UnmarshalerGearman.UnmarshalGearman
func (tid *TaskID) UnmarshalGearman(d []byte) error {
if len(d) > 64 {
return errors.New("too Long TaskID")
}
*tid = TaskID(d)
return nil
}
// MarshalGearman is MarshalerGearman.MarshalGearman
func (tid TaskID) MarshalGearman() ([]byte, error) {
if len([]byte(tid)) > 64 {
return nil, errors.New("too Long TaskID")
}
return []byte(tid), nil
}
// Len is MarshalerGearman.Len
func (tid TaskID) Len() int {
return len([]byte(tid))
}
func (tid TaskID) String() string {
return base64.RawURLEncoding.EncodeToString([]byte(tid))
}
// UnmarshalGearman is UnmarshalerGearman.UnmarshalGearman
func (fn *Function) UnmarshalGearman(d []byte) error {
*fn = Function(d)
return nil
}
// Cast is UnmarshalerGearman.Cast
func (fn *Function) Cast(o protocol.Opaque) error {
return fn.UnmarshalGearman(o.Bytes())
}
// MarshalGearman is MarshalerGearman.MarshalGearman
func (fn Function) MarshalGearman() ([]byte, error) {
return []byte(fn), nil
}
// Len is MarshalerGearman.Len
func (fn Function) Len() int {
return len([]byte(fn))
}
func (fn Function) String() string {
return base64.RawURLEncoding.EncodeToString([]byte(fn))
}
// IsEqual compare two Function and return true if they have the same name
func (fn Function) IsEqual(f2 Function) bool {
return bytes.Equal(fn, f2)
}
// UnmarshalGearman is UnmarshalerGearman.UnmarshalGearman
func (clid *ClientID) UnmarshalGearman(d []byte) error {
*clid = d
return nil
}
// Cast is UnmarshalerGearman.Cast
func (clid *ClientID) Cast(o protocol.Opaque) error {
return clid.UnmarshalGearman(o.Bytes())
}
// MarshalGearman is MarshalerGearman.MarshalGearman
func (clid ClientID) MarshalGearman() ([]byte, error) {
return []byte(clid), nil
}
// Len is MarshalerGearman.Len
func (clid ClientID) Len() int {
return len([]byte(clid))
}