forked from veraison/corim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathueid.go
91 lines (72 loc) · 1.66 KB
/
ueid.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
// Copyright 2021-2024 Contributors to the Veraison project.
// SPDX-License-Identifier: Apache-2.0
package comid
import (
"encoding/base64"
"fmt"
"github.com/veraison/eat"
)
const UEIDType = "ueid"
// UEID is an Unique Entity Identifier
type UEID eat.UEID
func (o UEID) Empty() bool {
return len(o) == 0
}
// Valid checks that the target UEID is in one of the defined formats: IMEI, EUI or RAND
func (o UEID) Valid() error {
if err := eat.UEID(o).Validate(); err != nil {
return fmt.Errorf("UEID validation failed: %w", err)
}
return nil
}
func (o UEID) String() string {
return base64.StdEncoding.EncodeToString(o)
}
// TaggedUEID is an alias to allow automatic tagging of an UEID type
type TaggedUEID UEID
func NewTaggedUEID(val any) (*TaggedUEID, error) {
var ret TaggedUEID
if val == nil {
return &ret, nil
}
switch t := val.(type) {
case string:
b, err := base64.StdEncoding.DecodeString(t)
if err != nil {
return nil, fmt.Errorf("bad UEID: %w", err)
}
ret = TaggedUEID(b)
case []byte:
ret = TaggedUEID(t)
case TaggedUEID:
ret = append(ret, t...)
case *TaggedUEID:
ret = append(ret, *t...)
case UEID:
ret = append(ret, t...)
case *UEID:
ret = append(ret, *t...)
case eat.UEID:
ret = append(ret, t...)
case *eat.UEID:
ret = append(ret, *t...)
default:
return nil, fmt.Errorf("unexpeted type for UEID: %T", t)
}
if err := ret.Valid(); err != nil {
return nil, err
}
return &ret, nil
}
func (o TaggedUEID) Valid() error {
return UEID(o).Valid()
}
func (o TaggedUEID) String() string {
return UEID(o).String()
}
func (o TaggedUEID) Type() string {
return "ueid"
}
func (o TaggedUEID) Bytes() []byte {
return []byte(o)
}