-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parameters.go
162 lines (134 loc) · 3.27 KB
/
parameters.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package moqt
import (
"bytes"
"errors"
"fmt"
"log/slog"
"reflect"
"time"
"github.com/OkutaniDaichi0106/gomoqt/internal/message"
"github.com/quic-go/quic-go/quicvarint"
)
type Parameters message.Parameters
func (p Parameters) Add(key uint64, value any) error {
encode := func(v uint64) []byte {
value := make([]byte, quicvarint.Len(uint64(v)))
return quicvarint.Append(value, uint64(v))
}
switch v := value.(type) {
case int64, int32, int16, int8:
p[key] = encode(uint64(reflect.ValueOf(v).Int()))
case uint64, uint32, uint16, uint8:
p[key] = encode(reflect.ValueOf(v).Uint())
case bool:
if v {
p[key] = encode(1)
} else if !v {
p[key] = encode(0)
}
case string:
p[key] = []byte(v)
case []byte:
p[key] = v
default:
return fmt.Errorf("invalid type: %T", value)
}
return nil
}
func (p Parameters) Remove(key uint64) {
delete(p, key)
}
func (p Parameters) ReadAsByteArray(key uint64) ([]byte, error) {
value, ok := p[key]
if !ok {
return nil, ErrParameterNotFound
}
return value, nil
}
func (p Parameters) ReadAsString(key uint64) (string, error) {
value, err := p.ReadAsByteArray(key)
if err != nil {
slog.Error("failed to read a parameter as byte array")
return "", err
}
return string(value), nil
}
func (p Parameters) ReadAsInt(key uint64) (int64, error) {
num, err := p.ReadAsUint(key)
if err != nil {
slog.Error("failed to read a parameter as uint", slog.String("error", err.Error()))
return 0, err
}
return int64(num), nil
}
func (p Parameters) ReadAsUint(key uint64) (uint64, error) {
value, ok := p[key]
if !ok {
return 0, ErrParameterNotFound
}
num, err := quicvarint.Read(quicvarint.NewReader(bytes.NewReader(value)))
if err != nil {
slog.Error("failed to read the bytes as uint64")
return 0, err
}
return num, nil
}
func (p Parameters) ReadAsBool(key uint64) (bool, error) {
num, err := p.ReadAsUint(key)
if err != nil {
slog.Error("failed to read a parameter as uint", slog.String("error", err.Error()))
return false, err
}
switch num {
case 0:
return false, nil
case 1:
return true, nil
default:
return false, errors.New("invalid value as bool")
}
}
var ErrParameterNotFound = errors.New("parameter not found")
const (
ROLE uint64 = 0x00
PATH uint64 = 0x01
MAX_SUBSCRIBE_ID uint64 = 0x02
AUTHORIZATION_INFO uint64 = 0x03
DELIVERY_TIMEOUT uint64 = 0x04
MAX_CACHE_DURATION uint64 = 0x05
)
func getPath(params Parameters) (string, bool) {
num, err := params.ReadAsString(PATH)
if err != nil {
return "", false
}
return num, true
}
func getMaxSubscribeID(params Parameters) (SubscribeID, bool) {
num, err := params.ReadAsUint(MAX_SUBSCRIBE_ID)
if err != nil {
return 0, false
}
return SubscribeID(num), true
}
func getMaxCacheDuration(params Parameters) (time.Duration, bool) {
num, err := params.ReadAsUint(MAX_CACHE_DURATION)
if err != nil {
return 0, false
}
return time.Duration(num), true
}
func getAuthorizationInfo(params Parameters) (string, bool) {
str, err := params.ReadAsString(AUTHORIZATION_INFO)
if err != nil {
return "", false
}
return str, true
}
func getDeliveryTimeout(params Parameters) (time.Duration, bool) {
num, err := params.ReadAsUint(DELIVERY_TIMEOUT)
if err != nil {
return 0, false
}
return time.Duration(num), true
}