-
Notifications
You must be signed in to change notification settings - Fork 178
/
id.go
72 lines (57 loc) · 1.21 KB
/
id.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 request
import (
"errors"
"fmt"
"regexp"
"github.com/onflow/flow-go/model/flow"
)
const MaxIDsLength = 50
type ID flow.Identifier
func (i *ID) Parse(raw string) error {
if raw == "" { // allow empty
*i = ID(flow.ZeroID)
return nil
}
valid, _ := regexp.MatchString(`^[0-9a-fA-F]{64}$`, raw)
if !valid {
return errors.New("invalid ID format")
}
flowID, err := flow.HexStringToIdentifier(raw)
if err != nil {
return fmt.Errorf("invalid ID: %w", err)
}
*i = ID(flowID)
return nil
}
func (i ID) Flow() flow.Identifier {
return flow.Identifier(i)
}
type IDs []ID
func (i *IDs) Parse(raw []string) error {
if len(raw) > MaxIDsLength {
return fmt.Errorf("at most %d IDs can be requested at a time", MaxIDsLength)
}
// make a map to have only unique values as keys
ids := make(IDs, 0)
uniqueIDs := make(map[string]bool)
for _, r := range raw {
var id ID
err := id.Parse(r)
if err != nil {
return err
}
if !uniqueIDs[id.Flow().String()] {
uniqueIDs[id.Flow().String()] = true
ids = append(ids, id)
}
}
*i = ids
return nil
}
func (i IDs) Flow() []flow.Identifier {
ids := make([]flow.Identifier, len(i))
for j, id := range i {
ids[j] = id.Flow()
}
return ids
}