Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix marshalling of LogpushJob with no filter #937

Merged
merged 3 commits into from Jun 18, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 23 additions & 17 deletions logpush.go
Expand Up @@ -12,18 +12,18 @@ import (

// LogpushJob describes a Logpush job.
type LogpushJob struct {
ID int `json:"id,omitempty"`
Dataset string `json:"dataset"`
Enabled bool `json:"enabled"`
Name string `json:"name"`
LogpullOptions string `json:"logpull_options"`
DestinationConf string `json:"destination_conf"`
OwnershipChallenge string `json:"ownership_challenge,omitempty"`
LastComplete *time.Time `json:"last_complete,omitempty"`
LastError *time.Time `json:"last_error,omitempty"`
ErrorMessage string `json:"error_message,omitempty"`
Frequency string `json:"frequency,omitempty"`
Filter LogpushJobFilters `json:"filter,omitempty"`
ID int `json:"id,omitempty"`
Dataset string `json:"dataset"`
Enabled bool `json:"enabled"`
Name string `json:"name"`
LogpullOptions string `json:"logpull_options"`
DestinationConf string `json:"destination_conf"`
OwnershipChallenge string `json:"ownership_challenge,omitempty"`
LastComplete *time.Time `json:"last_complete,omitempty"`
LastError *time.Time `json:"last_error,omitempty"`
ErrorMessage string `json:"error_message,omitempty"`
Frequency string `json:"frequency,omitempty"`
Filter *LogpushJobFilters `json:"filter,omitempty"`
}

type LogpushJobFilters struct {
Expand Down Expand Up @@ -131,17 +131,23 @@ type LogpushDestinationExistsRequest struct {
func (f LogpushJob) MarshalJSON() ([]byte, error) {
type Alias LogpushJob

filter, err := json.Marshal(f.Filter)
var filter string

if err != nil {
return nil, err
if f.Filter != nil {
b, err := json.Marshal(f.Filter)

if err != nil {
return nil, err
}

filter = string(b)
}

return json.Marshal(&struct {
Filter string `json:"filter,omitempty"`
Alias
}{
Filter: string(filter),
Filter: filter,
Alias: (Alias)(f),
})
}
Expand All @@ -167,7 +173,7 @@ func (f *LogpushJob) UnmarshalJSON(data []byte) error {
if err := filter.Where.Validate(); err != nil {
return err
}
f.Filter = filter
f.Filter = &filter
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion logpush_example_test.go
Expand Up @@ -180,7 +180,7 @@ func ExampleLogpushJob_MarshalJSON() {
LogpullOptions: "fields=RayID,ClientIP,EdgeStartTimestamp&timestamps=rfc3339&CVE-2021-44228=true",
Dataset: "http_requests",
DestinationConf: "s3://<BUCKET_PATH>?region=us-west-2/",
Filter: cloudflare.LogpushJobFilters{
Filter: &cloudflare.LogpushJobFilters{
Where: cloudflare.LogpushJobFilter{
And: []cloudflare.LogpushJobFilter{
{Key: "ClientRequestPath", Operator: cloudflare.Contains, Value: "/static\\"},
Expand Down
58 changes: 55 additions & 3 deletions logpush_test.go
Expand Up @@ -17,8 +17,8 @@ import (
const (
jobID = 1
serverLogpushJobDescription = `{
"id": %d,
"dataset": "http_requests",
"id": %d,
"dataset": "http_requests",
"enabled": false,
"name": "example.com",
"logpull_options": "fields=RayID,ClientIP,EdgeStartTimestamp&timestamps=rfc3339",
Expand Down Expand Up @@ -389,7 +389,7 @@ func TestLogpushJob_Unmarshall(t *testing.T) {
LogpullOptions: "fields=RayID,ClientIP,EdgeStartTimestamp&timestamps=rfc3339&CVE-2021-44228=true",
Dataset: "http_requests",
DestinationConf: "s3://<BUCKET_PATH>?region=us-west-2/",
Filter: LogpushJobFilters{
Filter: &LogpushJobFilters{
Where: LogpushJobFilter{
And: []LogpushJobFilter{
{Key: "ClientRequestPath", Operator: Contains, Value: "/static\\"},
Expand Down Expand Up @@ -423,3 +423,55 @@ func TestLogpushJob_Unmarshall(t *testing.T) {
}, job)
})
}

func TestLogPushJob_Marshall(t *testing.T) {
testCases := []struct {
job LogpushJob
want string
}{
{
job: LogpushJob{
Dataset: "http_requests",
Name: "valid filter",
LogpullOptions: "fields=RayID,ClientIP,EdgeStartTimestamp&timestamps=rfc3339",
DestinationConf: "https://example.com",
Filter: &LogpushJobFilters{
Where: LogpushJobFilter{Key: "ClientRequestHost", Operator: Equal, Value: "example.com"},
},
},
want: `{
"dataset": "http_requests",
"enabled": false,
"name": "valid filter",
"logpull_options": "fields=RayID,ClientIP,EdgeStartTimestamp&timestamps=rfc3339",
"destination_conf": "https://example.com",
"filter":"{\"where\":{\"key\":\"ClientRequestHost\",\"operator\":\"eq\",\"value\":\"example.com\"}}"
}`,
},
{
job: LogpushJob{
Dataset: "http_requests",
Name: "no filter",
LogpullOptions: "fields=RayID,ClientIP,EdgeStartTimestamp&timestamps=rfc3339",
DestinationConf: "https://example.com",
},
want: `{
"dataset": "http_requests",
"enabled": false,
"name": "no filter",
"logpull_options": "fields=RayID,ClientIP,EdgeStartTimestamp&timestamps=rfc3339",
"destination_conf": "https://example.com"
}`,
},
}

for _, tc := range testCases {
t.Run(tc.job.Name, func(t *testing.T) {
got, err := json.Marshal(tc.job)

if assert.NoError(t, err) {
assert.JSONEq(t, tc.want, string(got))
}
})
}
}