-
Notifications
You must be signed in to change notification settings - Fork 20
/
envelope.go
116 lines (106 loc) · 4.18 KB
/
envelope.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
// Copyright (c) 2022 Silverton Data, Inc.
// You may use, distribute, and modify this code under the terms of the Apache-2.0 license, a copy of
// which may be found at https://github.com/silverton-io/buz/blob/main/LICENSE
package envelope
import (
"encoding/json"
"time"
"github.com/google/uuid"
"github.com/silverton-io/buz/pkg/config"
"github.com/silverton-io/buz/pkg/constants"
)
const (
PROTOCOL string = "protocol"
VENDOR string = "vendor"
NAMESPACE string = "namespace"
VERSION string = "version"
SCHEMA string = "schema"
IS_VALID string = "isValid"
)
// An envelope consisting of minimally-defined properties
type Envelope struct {
Uuid uuid.UUID `json:"uuid"`
Timestamp time.Time `json:"timestamp" sql:"index"`
BuzTimestamp time.Time `json:"buzTimestamp" sql:"index"`
BuzVersion string `json:"buzVersion"`
BuzName string `json:"buzName"`
BuzEnv string `json:"buzEnv"`
Protocol string `json:"protocol"`
Schema string `json:"schema"`
Vendor string `json:"vendor"`
Namespace string `json:"namespace"`
Version string `json:"version"`
IsValid bool `json:"isValid"`
ValidationError *ValidationError `json:"validationError,omitempty" gorm:"type:json"`
Contexts *Contexts `json:"contexts,omitempty" gorm:"type:json"`
Payload Payload `json:"payload" gorm:"type:json"`
}
func (e *Envelope) AsMap() (map[string]interface{}, error) {
var m map[string]interface{}
marshaledEnvelope, err := json.Marshal(e)
if err != nil {
return nil, err
}
if err := json.Unmarshal(marshaledEnvelope, &m); err != nil {
return nil, err
}
return m, nil
}
func (e *Envelope) AsByte() ([]byte, error) {
eBytes, err := json.Marshal(e)
if err != nil {
return nil, err
}
return eBytes, nil
}
type JsonbEnvelope struct {
Uuid uuid.UUID `json:"uuid" gorm:"type:uuid"`
Timestamp time.Time `json:"timestamp" sql:"index"`
BuzTimestamp time.Time `json:"buzTimestamp" sql:"index"`
BuzVersion string `json:"buzVersion"`
BuzName string `json:"buzName"`
BuzEnv string `json:"buzEnv"`
Protocol string `json:"protocol"`
Schema string `json:"schema"`
Vendor string `json:"vendor"`
Namespace string `json:"namespace"`
Version string `json:"version"`
IsValid bool `json:"isValid"`
ValidationError *ValidationError `json:"validationError,omitempty" gorm:"type:jsonb"`
Contexts *Contexts `json:"contexts,omitempty" gorm:"type:jsonb"`
Payload Payload `json:"payload" gorm:"type:jsonb"`
}
type StringEnvelope struct {
Uuid uuid.UUID `json:"uuid" gorm:"type:uuid"`
Timestamp time.Time `json:"timestamp" sql:"index"`
BuzTimestamp time.Time `json:"buzTimestamp" sql:"index"`
BuzVersion string `json:"buzVersion"`
BuzName string `json:"buzName"`
BuzEnv string `json:"buzEnv"`
Protocol string `json:"protocol"`
Schema string `json:"schema"`
Vendor string `json:"vendor"`
Namespace string `json:"namespace"`
Version string `json:"version"`
IsValid bool `json:"isValid"`
ValidationError *ValidationError `json:"validationError,omitempty" gorm:"type:string"`
Contexts *Contexts `json:"contexts,omitempty" gorm:"type:string"`
Payload Payload `json:"payload" gorm:"type:string"`
}
// Build a new envelope with base fields populated
func NewEnvelope(conf config.App) Envelope {
now := time.Now().UTC()
envelope := Envelope{
Uuid: uuid.New(),
Timestamp: now,
BuzTimestamp: now,
BuzVersion: conf.Version,
BuzName: conf.Name,
BuzEnv: conf.Env,
Schema: constants.UNKNOWN,
Vendor: constants.UNKNOWN,
Namespace: constants.UNKNOWN,
Version: constants.UNKNOWN,
}
return envelope
}