1+ #! /usr/bin/env bash
2+
3+ # This script can be used to add a new event with empty data model
4+ # based on the subject and predicate names
5+
6+ # Usage:
7+ # ./add-event.sh <subject> <predicate>
8+ #
9+ # Both subect and predicate should be give in camelcase
10+
11+ BASE_DIR=" $( cd " $( dirname " $0 " ) /.." > /dev/null 2>&1 && pwd ) "
12+
13+ set -e
14+
15+ SUBJECT=$1
16+ PREDICATE=$2
17+ SUBJECT_LOWER_CAMEL=${SUBJECT,}
18+ SUBJECT_UPPER_CAMEL=${SUBJECT^}
19+ SUBJECT_LOWER=${SUBJECT,,}
20+ PREDICATE_LOWER_CAMEL=${PREDICATE,}
21+ PREDICATE_UPPER_CAMEL=${PREDICATE^}
22+ PREDICATE_LOWER=${PREDICATE,,}
23+
24+ cat > " ${BASE_DIR} /pkg/api/${SUBJECT_LOWER}${PREDICATE_LOWER} .go" << EOF
25+ /*
26+ Copyright 2022 The CDEvents Authors
27+
28+ Licensed under the Apache License, Version 2.0 (the "License");
29+ you may not use this file except in compliance with the License.
30+ You may obtain a copy of the License at
31+
32+ http://www.apache.org/licenses/LICENSE-2.0
33+
34+ Unless required by applicable law or agreed to in writing, software
35+ distributed under the License is distributed on an "AS IS" BASIS,
36+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37+ See the License for the specific language governing permissions and
38+ limitations under the License.
39+
40+ SPDX-License-Identifier: Apache-2.0
41+ */
42+
43+ package api
44+
45+ import (
46+ "time"
47+ )
48+
49+ const (
50+ // ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} event
51+ ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} EventV1 CDEventType = "dev.cdevents.${SUBJECT_LOWER} .${PREDICATE_LOWER} .v1"
52+ )
53+
54+ type ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} SubjectContent struct {}
55+
56+ type ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Subject struct {
57+ SubjectBase
58+ Content ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} SubjectContent \` json:"content"\`
59+ }
60+
61+ func (sc ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Subject) GetEventType() CDEventType {
62+ return ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} EventV1
63+ }
64+
65+ func (sc ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Subject) GetSubjectType() SubjectType {
66+ return ${SUBJECT_UPPER_CAMEL} SubjectType
67+ }
68+
69+ type ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Event struct {
70+ Context Context \` json:"context"\`
71+ Subject ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Subject \` json:"subject"\`
72+ }
73+
74+ // CDEventsReader implementation
75+
76+ func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Event) GetType() CDEventType {
77+ return ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} EventV1
78+ }
79+
80+ func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Event) GetVersion() string {
81+ return CDEventsSpecVersion
82+ }
83+
84+ func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Event) GetId() string {
85+ return e.Context.Id
86+ }
87+
88+ func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Event) GetSource() string {
89+ return e.Context.Source
90+ }
91+
92+ func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Event) GetTimestamp() time.Time {
93+ return e.Context.Timestamp
94+ }
95+
96+ func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Event) GetSubjectId() string {
97+ return e.Subject.Id
98+ }
99+
100+ func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Event) GetSubjectSource() string {
101+ return e.Subject.Source
102+ }
103+
104+ func (e ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Event) GetSubject() Subject {
105+ return e.Subject
106+ }
107+
108+ // CDEventsWriter implementation
109+
110+ func (e *${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Event) SetId(id string) {
111+ e.Context.Id = id
112+ }
113+
114+ func (e *${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Event) SetSource(source string) {
115+ e.Context.Source = source
116+ // Default the subject source to the event source
117+ if e.Subject.Source == "" {
118+ e.Subject.Source = source
119+ }
120+ }
121+
122+ func (e *${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Event) SetTimestamp(timestamp time.Time) {
123+ e.Context.Timestamp = timestamp
124+ }
125+
126+ func (e *${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Event) SetSubjectId(subjectId string) {
127+ e.Subject.Id = subjectId
128+ }
129+
130+ func (e *${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Event) SetSubjectSource(subjectSource string) {
131+ e.Subject.Source = subjectSource
132+ }
133+
134+ func new${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Event() CDEvent {
135+ return &${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Event{
136+ Context: Context{
137+ Type: ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} EventV1,
138+ Version: CDEventsSpecVersion,
139+ },
140+ Subject: ${SUBJECT_UPPER_CAMEL}${PREDICATE_UPPER_CAMEL} Subject{},
141+ }
142+ }
143+ EOF
144+
145+ echo " Created ${BASE_DIR} /pkg/api/${SUBJECT_LOWER}${PREDICATE_LOWER} .go"
0 commit comments