/
schema.go
158 lines (132 loc) · 5.49 KB
/
schema.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
// SPDX-FileCopyrightText: 2023, SAS Institute Inc., Cary, NC, USA. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package storage
import (
"encoding/json"
"io"
"github.com/graph-gophers/graphql-go"
"github.com/sassoftware/event-provenance-registry/pkg/api/graphql/schema/types"
"gopkg.in/yaml.v3"
)
// Event type represents an event with various properties and a relationship to an event receiver.
type Event struct {
ID graphql.ID `json:"id" gorm:"type:varchar(255);primary_key;not null"`
Name string `json:"name" gorm:"type:varchar(255);not null"`
Version string `json:"version" gorm:"type:varchar(255);not null"`
Release string `json:"release" gorm:"type:varchar(255);not null"`
PlatformID string `json:"platform_id" gorm:"type:varchar(255);not null"`
Package string `json:"package" gorm:"type:varchar(255);not null"`
Description string `json:"description" gorm:"type:varchar(255);not null"`
Payload types.JSON `json:"payload" gorm:"not null"`
Success bool `json:"success" gorm:"not null"`
CreatedAt types.Time `json:"created_at" gorm:"type:timestamptz; not null; default:CURRENT_TIMESTAMP"`
EventReceiverID graphql.ID `json:"event_receiver_id" gorm:"type:varchar(255);not null"`
EventReceiver EventReceiver
}
// EventReceiver type represents an event receiver with various properties such as ID, name, type, version, etc...
type EventReceiver struct {
ID graphql.ID `json:"id" gorm:"type:varchar(255);primary_key;not null"`
Name string `json:"name" gorm:"type:varchar(255);not null"`
Type string `json:"type" gorm:"type:varchar(255);not null"`
Version string `json:"version" gorm:"type:varchar(255);not null"`
Description string `json:"description" gorm:"type:varchar(255);not null"`
Schema types.JSON `json:"schema" gorm:"not null"`
Fingerprint string `json:"fingerprint" gorm:"type:varchar(255);not null"`
CreatedAt types.Time `json:"created_at" gorm:"type:timestamptz;not null;default:CURRENT_TIMESTAMP"`
}
// EventReceiverGroup represents a group of event receivers with various properties.
type EventReceiverGroup struct {
ID graphql.ID `json:"id" gorm:"type:varchar(255);primary_key;not null"`
Name string `json:"name" gorm:"type:varchar(255);not null"`
Type string `json:"type" gorm:"type:varchar(255);not null"`
Version string `json:"version" gorm:"type:varchar(255);not null"`
Description string `json:"description" gorm:"type:varchar(255);not null"`
Enabled bool `json:"enabled" gorm:"not null"`
EventReceiverIDs []graphql.ID `json:"event_receiver_ids" gorm:"-"`
CreatedAt types.Time `json:"created_at" gorm:"type:timestamptz; not null; default:CURRENT_TIMESTAMP"`
UpdatedAt types.Time `json:"updated_at" gorm:"type:timestamptz; not null; default:CURRENT_TIMESTAMP"`
}
// EventReceiverGroupToEventReceiver represents the relationship between an
type EventReceiverGroupToEventReceiver struct {
ID int `json:"id" gorm:"primaryKey;autoIncrement"`
EventReceiverID graphql.ID `json:"event_receiver_id" gorm:"type:varchar(255);not null"`
EventReceiver EventReceiver
EventReceiverGroup EventReceiverGroup
EventReceiverGroupID graphql.ID `json:"event_receiver_group_id" gorm:"type:varchar(255);not null"`
}
// ToJSON() function is a method defined on the `Event` struct. It converts an instance of the
// `Event` struct to a JSON string representation.
func (e *Event) ToJSON() (string, error) {
content, err := json.MarshalIndent(e, "", " ")
if err != nil {
return "", err
}
return string(content), nil
}
// ToYAML() converts struct to a YAML string representation.
func (e *Event) ToYAML() (string, error) {
content, err := yaml.Marshal(e)
if err != nil {
return "", err
}
return string(content), nil
}
// EventFromJSON reads JSON data from a reader and returns an Event object.
func EventFromJSON(reader io.Reader) (*Event, error) {
e := &Event{}
err := json.NewDecoder(reader).Decode(e)
if err != nil {
return nil, err
}
return e, nil
}
// ToJSON() converts an instance of a JSON string representation.
func (e *EventReceiver) ToJSON() (string, error) {
content, err := json.MarshalIndent(e, "", " ")
if err != nil {
return "", err
}
return string(content), nil
}
// ToYAML() converts struct to a YAML string representation.
func (e *EventReceiver) ToYAML() (string, error) {
content, err := yaml.Marshal(e)
if err != nil {
return "", err
}
return string(content), nil
}
// EventReceiverFromJSON reads JSON data from a reader and returns an EventReceiver object.
func EventReceiverFromJSON(reader io.Reader) (*EventReceiver, error) {
e := &EventReceiver{}
err := json.NewDecoder(reader).Decode(e)
if err != nil {
return nil, err
}
return e, nil
}
// ToJSON() converts struct to a JSON string representation.
func (e *EventReceiverGroup) ToJSON() (string, error) {
content, err := json.MarshalIndent(e, "", " ")
if err != nil {
return "", err
}
return string(content), nil
}
// ToYAML() converts struct to a YAML string representation.
func (e *EventReceiverGroup) ToYAML() (string, error) {
content, err := yaml.Marshal(e)
if err != nil {
return "", err
}
return string(content), nil
}
// EventReceiverGroupFromJSON reads JSON data from a reader and returns an EventReceiverGroup object.
func EventReceiverGroupFromJSON(reader io.Reader) (*EventReceiverGroup, error) {
e := &EventReceiverGroup{}
err := json.NewDecoder(reader).Decode(e)
if err != nil {
return nil, err
}
return e, nil
}