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

Added webhook filters #53

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
138 changes: 138 additions & 0 deletions webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,144 @@ type Webhook struct {
HTTPBasicUsername string `json:"httpBasicUsername,omitempty"`
HTTPBasicPassword string `json:"httpBasicPassword,omitempty"`
Headers []*WebhookHeader `json:"headers,omitempty"`
Filters []WebhookFilter `json:"filters,omitempty"`
}

// UnmarshalJSON for custom json unmarshaling
func (webhook *Webhook) UnmarshalJSON(data []byte) error {
payload := map[string]interface{}{}
if err := json.Unmarshal(data, &payload); err != nil {
return err
}

if val, ok := payload["sys"]; ok {
byteArray, err := json.Marshal(val)
if err != nil {
return nil
}

var sys Sys
if err := json.Unmarshal(byteArray, &sys); err != nil {
return err
}

webhook.Sys = &sys
}

if val, ok := payload["name"]; ok && val != nil {
webhook.Name = val.(string)
}

if val, ok := payload["url"]; ok && val != nil {
webhook.URL = val.(string)
}

if val, ok := payload["topics"]; ok && val != nil {
byteArray, err := json.Marshal(val)
if err != nil {
return nil
}

var topics []string
if err := json.Unmarshal(byteArray, &topics); err != nil {
return err
}

webhook.Topics = topics
}

if val, ok := payload["httpBasicUsername"]; ok && val != nil {
webhook.HTTPBasicUsername = val.(string)
}

if val, ok := payload["headers"]; ok && val != nil {
byteArray, err := json.Marshal(val)
if err != nil {
return nil
}

var headers []*WebhookHeader
if err := json.Unmarshal(byteArray, &headers); err != nil {
return err
}

webhook.Headers = headers
}

if val, ok := payload["filters"]; ok && val != nil {
filters, err := ParseFilters(val.([]interface{}))
if err != nil {
return err
}

webhook.Filters = filters
}

return nil
}

// ParseFilters converts json representation to go struct
func ParseFilters(data []interface{}) (filters []WebhookFilter, err error) {
for _, value := range data {
var byteArray []byte
var filterOpCondition bool
var filter map[string]interface{}

if filterMap, ok := value.(map[string]interface{}); ok {
byteArray, err = json.Marshal(filterMap)
if err != nil {
return nil, err
}

filterOpCondition = true
filter = filterMap
}

if notFilter, ok := filter["not"].(map[string]interface{}); ok {
byteArray, err = json.Marshal(notFilter)
if err != nil {
return nil, err
}

filterOpCondition = false
filter = notFilter
}

if _, ok := filter["equals"]; ok {
var webhookFilterEquals WebhookFilterEquals
err = json.Unmarshal(byteArray, &webhookFilterEquals)
if err != nil {
return nil, err
}

webhookFilterEquals.Condition = filterOpCondition
filters = append(filters, webhookFilterEquals)
}

if _, ok := filter["in"]; ok {
var webhookFilterIn WebhookFilterIn
err = json.Unmarshal(byteArray, &webhookFilterIn)
if err != nil {
return nil, err
}

webhookFilterIn.Condition = filterOpCondition
filters = append(filters, webhookFilterIn)
}

if _, ok := filter["regexp"]; ok {
var webhookFilterRegexp WebhookFilterRegexp
err = json.Unmarshal(byteArray, &webhookFilterRegexp)
if err != nil {
return nil, err
}

webhookFilterRegexp.Condition = filterOpCondition
filters = append(filters, webhookFilterRegexp)
}
}

return filters, nil
}

// WebhookHeader model
Expand Down
201 changes: 201 additions & 0 deletions webhook_filters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package contentful

import (
"encoding/json"
)

// Webhook doc constants
const (
WebhookFilterDocContentType = "sys.contentType.sys.id"
WebhookFilterDocEnvironment = "sys.environment.sys.id"
WebhookFilterDocEntity = "sys.id"
)

// WebhookFilter interface
type WebhookFilter interface{}

// WebhookFilterEquals model
type WebhookFilterEquals struct {
Condition bool
Doc string
Equals string
}

// MarshalJSON for custom json marshaling
func (f WebhookFilterEquals) MarshalJSON() ([]byte, error) {
if !f.Condition {
return json.Marshal(&map[string]map[string][]interface{}{
"not": {
"equals": {
struct {
Doc string `json:"doc"`
}{
f.Doc,
},
f.Equals,
},
},
})
}
return json.Marshal(&map[string][]interface{}{
"equals": {
struct {
Doc string `json:"doc"`
}{
f.Doc,
},
f.Equals,
},
})
}

// UnmarshalJSON for custom json unmarshaling
func (f *WebhookFilterEquals) UnmarshalJSON(data []byte) error {
var payload map[string][]interface{}
if err := json.Unmarshal(data, &payload); err != nil {
return err
}

equals := payload["equals"]

for _, item := range equals {
if value, ok := item.(string); ok {
f.Equals = value
}

if valueMap, ok := item.(map[string]interface{}); ok {
f.Doc = valueMap["doc"].(string)
}
}

return nil
}

// WebhookFilterIn model
type WebhookFilterIn struct {
Condition bool
Doc string
In []string
}

// MarshalJSON for custom json marshaling
func (f WebhookFilterIn) MarshalJSON() ([]byte, error) {
if !f.Condition {
return json.Marshal(&map[string]map[string][]interface{}{
"not": {
"in": {
struct {
Doc string `json:"doc"`
}{
f.Doc,
},
f.In,
},
},
})
}

return json.Marshal(&map[string][]interface{}{
"in": {
struct {
Doc string `json:"doc"`
}{
f.Doc,
},
f.In,
},
})
}

// UnmarshalJSON for custom json unmarshaling
func (f *WebhookFilterIn) UnmarshalJSON(data []byte) error {
var payload map[string][]interface{}
if err := json.Unmarshal(data, &payload); err != nil {
return err
}

in := payload["in"]

for _, item := range in {
if values, ok := item.([]interface{}); ok {
var in []string
for _, value := range values {
in = append(in, value.(string))
}
f.In = in
}

if valueMap, ok := item.(map[string]interface{}); ok {
f.Doc = valueMap["doc"].(string)
}
}

return nil
}

// WebhookFilterRegexp model
type WebhookFilterRegexp struct {
Condition bool
Doc string
Pattern string
}

// MarshalJSON for custom json marshaling
func (f WebhookFilterRegexp) MarshalJSON() ([]byte, error) {
if !f.Condition {
return json.Marshal(&map[string]map[string][]interface{}{
"not": {
"regexp": {
struct {
Doc string `json:"doc"`
}{
f.Doc,
},
struct {
Pattern string `json:"pattern"`
}{
f.Pattern,
},
},
},
})
}

return json.Marshal(&map[string][]interface{}{
"regexp": {
struct {
Doc string `json:"doc"`
}{
f.Doc,
},
struct {
Pattern string `json:"pattern"`
}{
f.Pattern,
},
},
})
}

// UnmarshalJSON for custom json unmarshaling
func (f *WebhookFilterRegexp) UnmarshalJSON(data []byte) error {
var payload map[string][]interface{}
if err := json.Unmarshal(data, &payload); err != nil {
return err
}

regexp := payload["regexp"]

for _, item := range regexp {
if valueMap, ok := item.(map[string]interface{}); ok {
if value, ok := valueMap["doc"]; ok {
f.Doc = value.(string)
}
if value, ok := valueMap["pattern"]; ok {
f.Pattern = value.(string)
}
}
}

return nil
}
Loading