forked from criteo/haproxy-spoe-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.go
77 lines (59 loc) · 1.21 KB
/
actions.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 spoe
import (
"fmt"
"github.com/pkg/errors"
)
type varScope byte
const (
VarScopeProcess varScope = 0
VarScopeSession varScope = 1
VarScopeTransaction varScope = 2
VarScopeRequest varScope = 3
VarScopeResponse varScope = 4
)
const (
actionTypeSetVar byte = 1
actionTypeUnsetVar byte = 2
)
type Action interface {
encode([]byte) (int, error)
}
type ActionSetVar struct {
Name string
Scope varScope
Value interface{}
}
func (a ActionSetVar) encode(b []byte) (int, error) {
if len(b) < 3 {
return 0, fmt.Errorf("encode action: insufficient space in buffer")
}
b[0] = actionTypeSetVar
b[1] = 3
b[2] = byte(a.Scope)
off := 3
n, err := encodeKV(b[off:], a.Name, a.Value)
if err != nil {
return 0, errors.Wrap(err, "encode action")
}
off += n
return off, nil
}
type ActionUnsetVar struct {
Name string
Scope varScope
}
func (a ActionUnsetVar) encode(b []byte) (int, error) {
if len(b) < 3 {
return 0, fmt.Errorf("encode action: insufficient space in buffer")
}
b[0] = actionTypeUnsetVar
b[1] = 2
b[2] = byte(a.Scope)
off := 3
n, err := encodeString(b[off:], a.Name)
if err != nil {
return 0, errors.Wrap(err, "encode action")
}
off += n
return off, nil
}