Skip to content

Instantly share code, notes, and snippets.

@owulveryck
Created June 14, 2021 09:55
Show Gist options
  • Save owulveryck/b218808c3e9bdf55b311f09218bf4b09 to your computer and use it in GitHub Desktop.
Save owulveryck/b218808c3e9bdf55b311f09218bf4b09 to your computer and use it in GitHub Desktop.
Simple cloudevents creation from a cue value
package main
import (
"encoding/json"
"log"
"reflect"
"testing"
"time"
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/cloudevents/sdk-go/v2/event"
"github.com/google/uuid"
)
// DataProduct is a structure that holds the definition as a CUE value
type DataProduct struct {
definition cue.Value
source string // the source of the event
// ...
}
// CreateEvent from the parameters and the payload encoded in JSON
func (d *DataProduct) CreateEvent(sub, typ string, payload cue.Value) (event.Event, error) {
e := cloudevents.NewEvent()
b, err := payload.MarshalJSON()
if err != nil {
return e, err
}
e.SetType(typ)
e.SetSource(d.source)
e.SetSubject(sub)
e.SetTime(time.Now())
e.SetID(uuid.Must(uuid.NewRandom()).String())
err = e.SetData(cloudevents.ApplicationJSON, b)
if err != nil {
return e, err
}
out, _ := json.MarshalIndent(e, " ", " ")
log.Println(string(out))
return e, nil
}
//TestDAtaProduct_CreateEvent validates the event creation
// This function is for demonstration only; IRL, the event would not be created from a JSOM payload
// in a test function.
func TestDataProduct_CreateEvent(t *testing.T) {
ctx := cuecontext.New()
val := ctx.CompileString(`{
"first": "John",
"Last": "Doe",
"Age": 40
}`)
expectedEventJSON := []byte(`{
"specversion": "1.0",
"id": "",
"source": "mysource",
"type": "mytype",
"subject": "subject",
"datacontenttype": "application/json",
"time": "2021-06-14T09:34:34.297881Z",
"data_base64": "eyJmaXJzdCI6IkpvaG4iLCJMYXN0IjoiRG9lIiwiQWdlIjo0MH0="
}`)
expectedEvent := cloudevents.NewEvent()
err := expectedEvent.UnmarshalJSON(expectedEventJSON)
if err != nil {
t.Fatal(err)
}
type args struct {
sub string
typ string
payload cue.Value
}
tests := []struct {
name string
d *DataProduct
args args
want event.Event
wantErr bool
}{
{
"simple valid",
&DataProduct{
source: "mysource",
},
args{
sub: "subject",
typ: "mytype",
payload: val,
},
expectedEvent,
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := tt.d.CreateEvent(tt.args.sub, tt.args.typ, tt.args.payload)
if (err != nil) != tt.wantErr {
t.Errorf("DataProduct.CreateEvent() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got.Data(), tt.want.Data()) {
t.Errorf("DataProduct.CreateEvent() = %v, want %v", got, tt.want)
}
if !reflect.DeepEqual(got.Source(), tt.want.Source()) {
t.Errorf("DataProduct.CreateEvent() = %v, want %v", got, tt.want)
}
if !reflect.DeepEqual(got.Type(), tt.want.Type()) {
t.Errorf("DataProduct.CreateEvent() = %v, want %v", got, tt.want)
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment