Skip to content
This repository has been archived by the owner on Jun 19, 2022. It is now read-only.

Add a lib to label/delabel events as cloudevents extensions #700

Merged
merged 6 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 39 additions & 30 deletions pkg/broker/config/targets.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/broker/config/targets.proto
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ message Target {

// The Pubsub subscription name for retrying the events.
string retry_subscription = 8;

// The broker name that the trigger is referencing.
string broker = 9;
}

// NamespacedTargets is the collection of targets grouped by namespaces.
Expand Down
78 changes: 78 additions & 0 deletions pkg/broker/eventutil/label.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright 2020 Google LLC

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.
*/

package eventutil

import (
"strings"

cloudevents "github.com/cloudevents/sdk-go/v2"
)

const (
// labelPrefix is the prefix for label keys.
labelPrefix = "kgcp"
)

// LabeledEvent is a wrapper of a cloudevent
// that allows labeling the event.
type LabeledEvent struct {
event *cloudevents.Event
}

// NewLabeledEvent creates a new LabeledEvent.
func NewLabeledEvent(e *cloudevents.Event) *LabeledEvent {
return &LabeledEvent{event: e}
}

// WithLabel attaches a label to the event as an extension.
func (le *LabeledEvent) WithLabel(key, value string) *LabeledEvent {
if value == "" {
le.event.SetExtension(labelPrefix+key, nil)
} else {
le.event.SetExtension(labelPrefix+key, value)
}
return le
}

// GetLabels gets all the labels as a map.
func (le *LabeledEvent) GetLabels() map[string]string {
m := make(map[string]string)
exts := le.event.Extensions()
for k, v := range exts {
if strings.HasPrefix(strings.ToLower(k), labelPrefix) {
m[strings.TrimPrefix(strings.ToLower(k), labelPrefix)] = v.(string)
}
}
return m
}

// Event returns the LabeledEvent as a cloudevent.
func (le *LabeledEvent) Event() *cloudevents.Event {
return le.event
}

// Delabeled returns the cloudevent without labels.
func (le *LabeledEvent) Delabeled() *cloudevents.Event {
e := le.event.Clone()
for k := range e.Extensions() {
if strings.HasPrefix(strings.ToLower(k), labelPrefix) {
// Set to nil to delete that extension.
e.SetExtension(k, nil)
}
}
return &e
}
62 changes: 62 additions & 0 deletions pkg/broker/eventutil/label_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2020 Google LLC

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.
*/

package eventutil

import (
"testing"

cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/google/go-cmp/cmp"
)

func TestLabeledEevent(t *testing.T) {
e := cloudevents.NewEvent()
e.SetSource("example/uri")
e.SetType("example.type")
e.SetData(cloudevents.ApplicationJSON, map[string]string{"hello": "world"})
e.SetExtension("custom", "foo")

wantDelabeled := e.Clone()
wantLabeled := e.Clone()
wantLabeled.SetExtension("kgcplabel1", "val1")
wantLabeled.SetExtension("kgcplabel2", "val2")

le := NewLabeledEvent(&e).WithLabel("label1", "val1").WithLabel("label2", "val2")

wantLabels := map[string]string{
"label1": "val1",
"label2": "val2",
}
gotLabels := le.GetLabels()
if diff := cmp.Diff(wantLabels, gotLabels); diff != "" {
t.Errorf("LabeledEvent.GetLabels() (-want,+got): %v", diff)
}

gotDelabeled := le.Delabeled()
if diff := cmp.Diff(&wantDelabeled, gotDelabeled); diff != "" {
t.Errorf("LabeledEvent.Delabeled() (-want,+got): %v", diff)
}
gotLabeled := le.Event()
if diff := cmp.Diff(&wantLabeled, gotLabeled); diff != "" {
t.Errorf("LabeledEvent.Event() (-want,+got): %v", diff)
}

le.WithLabel("label1", "").WithLabel("label2", "")
if gotCnt := len(le.GetLabels()); gotCnt != 0 {
t.Errorf("Labels count after removing labels got=%d want=0", gotCnt)
}
}
Loading