Skip to content

Commit

Permalink
Merge pull request #1413 from openmeterio/fix-notification-api
Browse files Browse the repository at this point in the history
refactor: annotations field for notification event
  • Loading branch information
chrisgacsal authored Aug 23, 2024
2 parents 1244af9 + 0e94724 commit 1cfd53c
Show file tree
Hide file tree
Showing 6 changed files with 374 additions and 349 deletions.
326 changes: 163 additions & 163 deletions api/api.gen.go

Large diffs are not rendered by default.

326 changes: 163 additions & 163 deletions api/client/go/client.gen.go

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3463,8 +3463,7 @@ components:
description: |
List of annotations managed by the system.
type: object
additionalProperties:
type: string
additionalProperties: true
example:
test-event: "true"

Expand Down
3 changes: 3 additions & 0 deletions internal/notification/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"time"

"github.com/samber/lo"

"github.com/openmeterio/openmeter/api"
"github.com/openmeterio/openmeter/pkg/models"
"github.com/openmeterio/openmeter/pkg/pagination"
Expand Down Expand Up @@ -78,6 +80,7 @@ func (e Event) AsNotificationEvent() (api.NotificationEvent, error) {
DeliveryStatus: deliveryStatuses,
Id: e.ID,
Rule: rule,
Annotations: lo.EmptyableToPtr(e.Annotations),
}

switch e.Type {
Expand Down
23 changes: 2 additions & 21 deletions internal/notification/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func (c service) CreateChannel(ctx context.Context, params CreateChannelInput) (
switch params.Type {
case ChannelTypeWebhook:
var headers map[string]string
headers, err = interfaceMapToStringMap(channel.Config.WebHook.CustomHeaders)
headers, err = StrictInterfaceMapToStringMap(channel.Config.WebHook.CustomHeaders)
if err != nil {
return nil, fmt.Errorf("failed to cast custom headers: %w", err)
}
Expand Down Expand Up @@ -262,7 +262,7 @@ func (c service) UpdateChannel(ctx context.Context, params UpdateChannelInput) (
switch params.Type {
case ChannelTypeWebhook:
var headers map[string]string
headers, err = interfaceMapToStringMap(channel.Config.WebHook.CustomHeaders)
headers, err = StrictInterfaceMapToStringMap(channel.Config.WebHook.CustomHeaders)
if err != nil {
return nil, fmt.Errorf("failed to cast custom headers: %w", err)
}
Expand Down Expand Up @@ -479,22 +479,3 @@ func (c service) GetEventDeliveryStatus(ctx context.Context, params GetEventDeli

return c.repo.GetEventDeliveryStatus(ctx, params)
}

func interfaceMapToStringMap(m map[string]interface{}) (map[string]string, error) {
var s map[string]string
if len(m) > 0 {
s = make(map[string]string, len(m))
for k, v := range m {
switch t := v.(type) {
case string:
s[k] = t
case fmt.Stringer:
s[k] = t.String()
default:
return s, fmt.Errorf("failed to cast value with %T to string", t)
}
}
}

return s, nil
}
42 changes: 42 additions & 0 deletions internal/notification/utils.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package notification

import "fmt"

// ChannelTypes returns a set of ChannelType from Channel slice
func ChannelTypes(channels []Channel) []ChannelType {
seen := make(map[ChannelType]struct{}, len(channels))
Expand Down Expand Up @@ -48,3 +50,43 @@ func DeliveryStatusStates(statuses []EventDeliveryStatus) []EventDeliveryStatusS

return types
}

func interfaceMapToStringMap(m map[string]interface{}, strict bool) (map[string]string, error) {
var s map[string]string

if len(m) > 0 {
s = make(map[string]string, len(m))
for k, v := range m {
switch t := v.(type) {
case string:
s[k] = t
case fmt.Stringer:
s[k] = t.String()
case int, int32, int64:
s[k] = fmt.Sprintf("%d", t)
case float32, float64:
s[k] = fmt.Sprintf("%f", t)
case bool:
s[k] = fmt.Sprintf("%t", t)
default:
if strict {
return nil, fmt.Errorf("failed to cast value with %T to string", t)
} else {
continue
}
}
}
}

return s, nil
}

func StrictInterfaceMapToStringMap(m map[string]interface{}) (map[string]string, error) {
return interfaceMapToStringMap(m, true)
}

func InterfaceMapToStringMap(m map[string]interface{}) map[string]string {
s, _ := interfaceMapToStringMap(m, false)

return s
}

0 comments on commit 1cfd53c

Please sign in to comment.