-
Notifications
You must be signed in to change notification settings - Fork 3
/
mapper.go
178 lines (152 loc) · 4.8 KB
/
mapper.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/Financial-Times/go-logger/v2"
uuidUtils "github.com/Financial-Times/uuid-utils-go"
)
const (
videoUUIDField = "uuid"
videoIDField = "id"
relatedField = "related"
deletedField = "deleted"
relatedItemIDField = "uuid"
collectionType = "story-package"
contentURIPrefix = "http://next-video-content-collection-mapper.svc.ft.com/content-collection/story-package/"
uuidGenerationSalt = "storypackage"
)
type relatedContentMapper struct {
sc serviceConfig
strContent string
tid string
lastModified string
unmarshalled map[string]interface{}
log *logger.UPPLogger
}
func (m *relatedContentMapper) mapRelatedContent() ([]byte, string, error) {
var uuidField string
if m.isDeleteEvent() {
uuidField = videoUUIDField
} else {
uuidField = videoIDField
}
videoUUID, err := getRequiredStringField(uuidField, m.unmarshalled)
if err != nil {
return nil, "", err
}
contentCollectionUUID, err := generateContentCollectionUUID(videoUUID)
if err != nil {
m.log.WithTransactionID(m.tid).WithUUID(videoUUID).Warn(err.Error())
return nil, "", errors.New("Error generating story package UUID")
}
var cc ContentCollection
if !m.isDeleteEvent() {
relatedItemsArray, err := getObjectsArrayField(relatedField, m.unmarshalled, videoUUID, m)
if err != nil {
return nil, videoUUID, err
}
relatedItems := m.retrieveRelatedItems(relatedItemsArray, videoUUID)
if len(relatedItems) > 0 {
cc = m.newContentCollection(contentCollectionUUID, relatedItems)
}
} else {
cc.Deleted = true
cc.UUID = videoUUID
}
mc := m.newMappedContent(contentCollectionUUID, cc)
marshalledPubEvent, err := json.Marshal(mc)
if err != nil {
m.log.WithTransactionID(m.tid).WithUUID(videoUUID).Warn("Error marshalling processed related items")
return nil, videoUUID, err
}
return marshalledPubEvent, videoUUID, nil
}
func (m *relatedContentMapper) retrieveRelatedItems(relatedItemsArray []map[string]interface{}, videoUUID string) []Item {
var result = make([]Item, 0)
for _, relatedItem := range relatedItemsArray {
itemID, err := getRequiredStringField(relatedItemIDField, relatedItem)
if err != nil {
m.log.WithTransactionID(m.tid).WithUUID(videoUUID).WithError(err).Warn("Cannot extract related item id from related field")
continue
}
result = append(result, Item{UUID: itemID})
}
return result
}
func (m *relatedContentMapper) newMappedContent(ccUUID string, cc ContentCollection) MappedContent {
return MappedContent{
Payload: cc,
ContentURI: contentURIPrefix + ccUUID,
LastModified: m.lastModified,
UUID: ccUUID,
}
}
func (m *relatedContentMapper) newContentCollection(ccUUID string, items []Item) ContentCollection {
return ContentCollection{
UUID: ccUUID,
Items: items,
PublishReference: m.tid,
LastModified: m.lastModified,
CollectionType: collectionType,
}
}
func getRequiredStringField(key string, obj map[string]interface{}) (string, error) {
valueI, ok := obj[key]
if !ok || valueI == nil {
return "", nullFieldError(key)
}
val, ok := valueI.(string)
if !ok {
return "", wrongFieldTypeError("string", key, valueI)
}
return val, nil
}
func getObjectsArrayField(key string, obj map[string]interface{}, videoUUID string, vm *relatedContentMapper) ([]map[string]interface{}, error) {
var objArrayI interface{}
var result = make([]map[string]interface{}, 0)
objArrayI, ok := obj[key]
if !ok {
vm.log.WithTransactionID(vm.tid).WithUUID(videoUUID).
WithField("event", "map").
Infof(nullFieldError(key).Error())
return result, nil
}
var objArray []interface{}
objArray, ok = objArrayI.([]interface{})
if !ok {
return nil, wrongFieldTypeError("object array", key, objArrayI)
}
for _, objI := range objArray {
obj, ok = objI.(map[string]interface{})
if !ok {
return nil, wrongFieldTypeError("object array", key, objArrayI)
}
result = append(result, obj)
}
return result, nil
}
func nullFieldError(fieldKey string) error {
return fmt.Errorf("[%s] field of native Next video JSON is missing or is null", fieldKey)
}
func wrongFieldTypeError(expectedType, fieldKey string, value interface{}) error {
return fmt.Errorf("[%s] field of native Next video JSON is not of type %s: [%v]", fieldKey, expectedType, value)
}
func (m *relatedContentMapper) isDeleteEvent() bool {
if _, present := m.unmarshalled[deletedField]; present {
return true
}
return false
}
func generateContentCollectionUUID(videoUUID string) (string, error) {
uuid, err := uuidUtils.NewUUIDFromString(videoUUID)
if err != nil {
return "", err
}
uuidDeriver := uuidUtils.NewUUIDDeriverWith(uuidGenerationSalt)
contentCollectionUUID, err := uuidDeriver.From(uuid)
if err != nil {
return "", err
}
return contentCollectionUUID.String(), nil
}