-
Notifications
You must be signed in to change notification settings - Fork 0
/
v3toeventresponse.go
96 lines (81 loc) · 2.39 KB
/
v3toeventresponse.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
package veracity
import (
"fmt"
"time"
v2assets "github.com/datatrails/go-datatrails-common-api-gen/assets/v2/assets"
"github.com/datatrails/go-datatrails-common-api-gen/attribute/v2/attribute"
"github.com/datatrails/go-datatrails-simplehash/simplehash"
"google.golang.org/protobuf/types/known/timestamppb"
)
func NewAttribute(value any) (*attribute.Attribute, error) {
switch v := value.(type) {
case string:
return attribute.NewStringAttribute(v), nil
case map[string]string:
return attribute.NewDictAttribute(v), nil
case []map[string]string:
return attribute.NewListAttribute(v), nil
// case []map[string]interface{}:
case []interface{}:
lv := []map[string]string{}
for _, it := range v {
mIface, ok := it.(map[string]interface{})
if !ok {
continue
}
mString := map[string]string{}
for k, i := range mIface {
s, ok := i.(string)
if !ok {
continue
}
mString[k] = s
}
lv = append(lv, mString)
}
return attribute.NewListAttribute(lv), nil
default:
return nil, fmt.Errorf("value not string, map or list")
}
}
func newEventResponseFromV3(v3 simplehash.V3Event) (*v2assets.EventResponse, error) {
var err error
event := &v2assets.EventResponse{
EventAttributes: map[string]*attribute.Attribute{},
AssetAttributes: map[string]*attribute.Attribute{},
}
event.Identity = v3.Identity
for k, v := range v3.EventAttributes {
if event.EventAttributes[k], err = NewAttribute(v); err != nil {
return nil, err
}
}
for k, v := range v3.AssetAttributes {
if event.AssetAttributes[k], err = NewAttribute(v); err != nil {
return nil, err
}
}
event.Operation = v3.Operation
event.Behaviour = v3.Behaviour
var t time.Time
if t, err = time.Parse(time.RFC3339Nano, v3.TimestampDeclared); err != nil {
return nil, err
}
event.TimestampDeclared = timestamppb.New(t)
if t, err = time.Parse(time.RFC3339Nano, v3.TimestampAccepted); err != nil {
return nil, err
}
event.TimestampAccepted = timestamppb.New(t)
if t, err = time.Parse(time.RFC3339Nano, v3.TimestampCommitted); err != nil {
return nil, err
}
event.TimestampCommitted = timestamppb.New(t)
if event.PrincipalDeclared, err = newPrincipalFromJson(v3.PrincipalDeclared); err != nil {
return nil, err
}
if event.PrincipalAccepted, err = newPrincipalFromJson(v3.PrincipalAccepted); err != nil {
return nil, err
}
event.TenantIdentity = v3.TenantIdentity
return event, nil
}