-
Notifications
You must be signed in to change notification settings - Fork 0
/
storedobject.go
90 lines (80 loc) · 2.94 KB
/
storedobject.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
/*******************************************************************************
* Copyright 2019 Dell Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*******************************************************************************/
// contracts are implementation agnostic data storage models.
package contracts
import (
"errors"
"github.com/google/uuid"
)
// StoredObject is the atomic and most abstract description of what is collected by the export store system.
type StoredObject struct {
// ID uniquely identifies this StoredObject
ID string
// AppServiceKey identifies the app to which this data belongs.
AppServiceKey string
// Payload is the data to be exported
Payload []byte
// RetryCount is how many times this has tried to be exported
RetryCount int
// PipelineId is the ID of the pipeline that needs to be restarted.
PipelineId string
// PipelinePosition is where to pickup in the pipeline
PipelinePosition int
// Version is a hash of the functions to know if the pipeline has changed.
Version string
// CorrelationID is an identifier provided by EdgeX to track this record as it moves
CorrelationID string
// ContextData is a snapshot of data used by the pipeline at runtime
ContextData map[string]string
}
// NewStoredObject creates a new instance of StoredObject and is the preferred way to create one.
func NewStoredObject(appServiceKey string, payload []byte, pipelineId string, pipelinePosition int,
version string, contextData map[string]string) StoredObject {
return StoredObject{
AppServiceKey: appServiceKey,
Payload: payload,
RetryCount: 0,
PipelineId: pipelineId,
PipelinePosition: pipelinePosition,
Version: version,
ContextData: contextData,
}
}
// ValidateContract ensures that the required fields are present on the object.
func (o *StoredObject) ValidateContract(IDRequired bool) error {
if IDRequired {
if o.ID == "" {
return errors.New("invalid contract, ID cannot be empty")
}
} else {
if o.ID == "" {
o.ID = uuid.New().String()
}
}
parsed, err := uuid.Parse(o.ID)
if err != nil {
return errors.New("invalid contract, ID must be UUID")
}
o.ID = parsed.String()
if o.AppServiceKey == "" {
return errors.New("invalid contract, app service key cannot be empty")
}
if len(o.Payload) == 0 {
return errors.New("invalid contract, payload cannot be empty")
}
if o.Version == "" {
return errors.New("invalid contract, version cannot be empty")
}
return nil
}