-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a duration type that can be unmarshalled from JSON (#101)
- Loading branch information
1 parent
5697cf5
commit a90d03e
Showing
6 changed files
with
258 additions
and
22 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
package query | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/xichen2020/eventdb/calculation" | ||
"github.com/xichen2020/eventdb/document/field" | ||
"github.com/xichen2020/eventdb/filter" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUnparsedGroupByQueryParse(t *testing.T) { | ||
input := `{ | ||
"namespace": "foo", | ||
"end_time": 9876, | ||
"time_range": "1h", | ||
"filters": [ | ||
{ | ||
"filters": [ | ||
{ | ||
"field": "field1.field11", | ||
"op": "=", | ||
"value": "value1" | ||
}, | ||
{ | ||
"field": "field2", | ||
"op": ">", | ||
"value": "value2" | ||
} | ||
], | ||
"filter_combinator": "AND" | ||
} | ||
], | ||
"group_by": [ | ||
"field3.field31", | ||
"field4" | ||
], | ||
"calculations": [ | ||
{ | ||
"op": "COUNT" | ||
}, | ||
{ | ||
"op": "AVG", | ||
"field": "field5" | ||
} | ||
], | ||
"order_by": [ | ||
{ | ||
"field": "field3.field31", | ||
"order": "ascending" | ||
}, | ||
{ | ||
"field": "field5", | ||
"op": "AVG", | ||
"order": "descending" | ||
} | ||
], | ||
"limit": 10 | ||
}` | ||
|
||
var ( | ||
valueUnion1 = field.NewStringUnion("value1") | ||
valueUnion2 = field.NewStringUnion("value2") | ||
) | ||
expected := ParsedQuery{ | ||
Namespace: "foo", | ||
StartTimeNanos: 6276000000000, | ||
EndTimeNanos: 9876000000000, | ||
Filters: []FilterList{ | ||
{ | ||
Filters: []Filter{ | ||
{ | ||
FieldPath: []string{"field1", "field11"}, | ||
Op: filter.Equals, | ||
Value: &valueUnion1, | ||
}, | ||
{ | ||
FieldPath: []string{"field2"}, | ||
Op: filter.LargerThan, | ||
Value: &valueUnion2, | ||
}, | ||
}, | ||
FilterCombinator: filter.And, | ||
}, | ||
}, | ||
GroupBy: [][]string{ | ||
{"field3", "field31"}, | ||
{"field4"}, | ||
}, | ||
Calculations: []Calculation{ | ||
{ | ||
Op: calculation.Count, | ||
}, | ||
{ | ||
Op: calculation.Avg, | ||
FieldPath: []string{"field5"}, | ||
}, | ||
}, | ||
OrderBy: []OrderBy{ | ||
{ | ||
FieldType: GroupByField, | ||
FieldIndex: 0, | ||
FieldPath: []string{"field3", "field31"}, | ||
SortOrder: Ascending, | ||
}, | ||
{ | ||
FieldType: CalculationField, | ||
FieldIndex: 1, | ||
FieldPath: []string{"field5"}, | ||
SortOrder: Descending, | ||
}, | ||
}, | ||
Limit: 10, | ||
} | ||
|
||
var p UnparsedQuery | ||
err := json.Unmarshal([]byte(input), &p) | ||
require.NoError(t, err) | ||
parsed, err := p.Parse(ParseOptions{ | ||
FieldPathSeparator: byte('.'), | ||
}) | ||
require.NoError(t, err) | ||
|
||
queryCmpOpts := cmpopts.IgnoreUnexported(ParsedQuery{}) | ||
require.True(t, cmp.Equal(expected, parsed, queryCmpOpts)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package time | ||
|
||
import ( | ||
"encoding/json" | ||
"time" | ||
) | ||
|
||
// Duration is a time duration that can be unmarshalled from JSON. | ||
type Duration time.Duration | ||
|
||
// String returns the duration string. | ||
func (d Duration) String() string { return time.Duration(d).String() } | ||
|
||
// MarshalJSON marshals the duration as a string. | ||
func (d Duration) MarshalJSON() ([]byte, error) { | ||
return json.Marshal(time.Duration(d).String()) | ||
} | ||
|
||
// UnmarshalJSON unmarshals the raw bytes into a duration. | ||
func (d *Duration) UnmarshalJSON(b []byte) error { | ||
var v string | ||
if err := json.Unmarshal(b, &v); err != nil { | ||
return err | ||
} | ||
dur, err := time.ParseDuration(v) | ||
if err != nil { | ||
return err | ||
} | ||
*d = Duration(dur) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package time | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestDurationMarshalJSON(t *testing.T) { | ||
inputs := []struct { | ||
dur time.Duration | ||
expected string | ||
}{ | ||
{ | ||
dur: time.Second, | ||
expected: `"1s"`, | ||
}, | ||
{ | ||
dur: 5 * time.Minute, | ||
expected: `"5m0s"`, | ||
}, | ||
{ | ||
dur: 2 * time.Hour, | ||
expected: `"2h0m0s"`, | ||
}, | ||
} | ||
|
||
for _, input := range inputs { | ||
b, err := json.Marshal(Duration(input.dur)) | ||
require.NoError(t, err) | ||
require.Equal(t, input.expected, string(b)) | ||
} | ||
} | ||
|
||
func TestDurationUnmarshalJSON(t *testing.T) { | ||
inputs := []struct { | ||
expected time.Duration | ||
str string | ||
}{ | ||
{ | ||
str: `"1s"`, | ||
expected: time.Second, | ||
}, | ||
{ | ||
str: `"5m"`, | ||
expected: 5 * time.Minute, | ||
}, | ||
{ | ||
str: `"2h"`, | ||
expected: 2 * time.Hour, | ||
}, | ||
} | ||
|
||
for _, input := range inputs { | ||
var dur Duration | ||
err := json.Unmarshal([]byte(input.str), &dur) | ||
require.NoError(t, err) | ||
require.Equal(t, input.expected, time.Duration(dur)) | ||
} | ||
} |