-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutil.go
More file actions
75 lines (67 loc) · 1.47 KB
/
util.go
File metadata and controls
75 lines (67 loc) · 1.47 KB
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
package api
import (
"context"
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/google/uuid"
"github.com/livepeer/livepeer-data/pkg/data"
"github.com/livepeer/livepeer-data/pkg/jsse"
)
func toSSEEvent(evt data.Event) (jsse.Event, error) {
data, err := json.Marshal(evt)
if err != nil {
return jsse.Event{}, err
}
return jsse.Event{
ID: evt.ID().String(),
Event: "lp_event",
Data: data,
}, nil
}
func parseInputTimestamp(str string) (*time.Time, error) {
if str == "" {
return nil, nil
}
t, rfcErr := time.Parse(time.RFC3339Nano, str)
if rfcErr == nil {
return &t, nil
}
ts, unixErr := strconv.ParseInt(str, 10, 64)
if unixErr != nil {
return nil, fmt.Errorf("bad time %q. must be in RFC3339 or Unix Timestamp (millisecond) formats. rfcErr: %s; unixErr: %s", str, rfcErr, unixErr)
}
t = time.UnixMilli(ts)
return &t, nil
}
func parseInputUUID(str string) (*uuid.UUID, error) {
if str == "" {
return nil, nil
}
uuid, err := uuid.Parse(str)
if err != nil {
return nil, fmt.Errorf("bad uuid %q: %w", str, err)
}
return &uuid, nil
}
func unionCtx(ctx1, ctx2 context.Context) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(context.Background())
go func() {
defer cancel()
select {
case <-ctx1.Done():
case <-ctx2.Done():
}
}()
return ctx, cancel
}
func nonNilErrs(errs ...error) []error {
var nonNil []error
for _, err := range errs {
if err != nil {
nonNil = append(nonNil, err)
}
}
return nonNil
}