Skip to content

Commit

Permalink
extraction function optional on filters
Browse files Browse the repository at this point in the history
  • Loading branch information
jbguerraz committed Nov 22, 2020
1 parent 270837b commit ab7942a
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 40 deletions.
12 changes: 8 additions & 4 deletions builder/filter/bound.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func (b *Bound) SetOrdering(ordering types.StringComparator) *Bound {
}

func (b *Bound) UnmarshalJSON(data []byte) error {
var err error
var tmp struct {
Base
Dimension string `json:"dimension"`
Expand All @@ -71,12 +72,15 @@ func (b *Bound) UnmarshalJSON(data []byte) error {
ExtractionFn json.RawMessage `json:"extractionFn"`
Ordering types.StringComparator `json:"ordering"`
}
if err := json.Unmarshal(data, &tmp); err != nil {
if err = json.Unmarshal(data, &tmp); err != nil {
return err
}
e, err := extractionfn.Load(tmp.ExtractionFn)
if err != nil {
return err
var e builder.ExtractionFn
if tmp.ExtractionFn != nil {
e, err = extractionfn.Load(tmp.ExtractionFn)
if err != nil {
return err
}
}
b.Base = tmp.Base
b.Dimension = tmp.Dimension
Expand Down
4 changes: 2 additions & 2 deletions builder/filter/column_comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ func (c *ColumnComparison) SetDimensions(dimensions []builder.Dimension) *Column
}

func (c *ColumnComparison) UnmarshalJSON(data []byte) error {
var err error
var tmp struct {
Base
Dimensions []json.RawMessage `json:"dimensions"`
}
if err := json.Unmarshal(data, &tmp); err != nil {
if err = json.Unmarshal(data, &tmp); err != nil {
return err
}
var err error
var d builder.Dimension
dd := make([]builder.Dimension, len(tmp.Dimensions))
for i := range tmp.Dimensions {
Expand Down
12 changes: 8 additions & 4 deletions builder/filter/extraction.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,22 @@ func (e *Extraction) SetExtractionFn(extractionFn builder.ExtractionFn) *Extract
}

func (e *Extraction) UnmarshalJSON(data []byte) error {
var err error
var tmp struct {
Base
Dimension string `json:"dimension"`
Value string `json:"value"`
ExtractionFn json.RawMessage `json:"extractionFn"`
}
if err := json.Unmarshal(data, &tmp); err != nil {
if err = json.Unmarshal(data, &tmp); err != nil {
return err
}
ex, err := extractionfn.Load(tmp.ExtractionFn)
if err != nil {
return err
var ex builder.ExtractionFn
if tmp.ExtractionFn != nil {
ex, err = extractionfn.Load(tmp.ExtractionFn)
if err != nil {
return err
}
}
e.Base = tmp.Base
e.Dimension = tmp.Dimension
Expand Down
12 changes: 8 additions & 4 deletions builder/filter/in.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,23 @@ func (i *In) SetFilterTuning(filterTuning *FilterTuning) *In {
}

func (i *In) UnmarshalJSON(data []byte) error {
var err error
var tmp struct {
Base
Dimension string `json:"dimension"`
Values []string `json:"values"`
ExtractionFn json.RawMessage `json:"extractionFn"`
FilterTuning *FilterTuning `json:"filterTuning"`
}
if err := json.Unmarshal(data, &tmp); err != nil {
if err = json.Unmarshal(data, &tmp); err != nil {
return err
}
e, err := extractionfn.Load(tmp.ExtractionFn)
if err != nil {
return err
var e builder.ExtractionFn
if tmp.ExtractionFn != nil {
e, err = extractionfn.Load(tmp.ExtractionFn)
if err != nil {
return err
}
}
i.Base = tmp.Base
i.Dimension = tmp.Dimension
Expand Down
12 changes: 8 additions & 4 deletions builder/filter/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,23 @@ func (i *Interval) SetFilterTuning(filterTuning *FilterTuning) *Interval {
}

func (i *Interval) UnmarshalJSON(data []byte) error {
var err error
var tmp struct {
Base
Dimension string `json:"dimension"`
Intervals []*types.Interval `json:"intervals"`
ExtractionFn json.RawMessage `json:"extractionFn"`
FilterTuning *FilterTuning `json:"filterTuning"`
}
if err := json.Unmarshal(data, &tmp); err != nil {
if err = json.Unmarshal(data, &tmp); err != nil {
return err
}
e, err := extractionfn.Load(tmp.ExtractionFn)
if err != nil {
return err
var e builder.ExtractionFn
if tmp.ExtractionFn != nil {
e, err = extractionfn.Load(tmp.ExtractionFn)
if err != nil {
return err
}
}
i.Base = tmp.Base
i.Dimension = tmp.Dimension
Expand Down
12 changes: 8 additions & 4 deletions builder/filter/javascript.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,23 @@ func (j *Javascript) SetFilterTuning(filterTuning *FilterTuning) *Javascript {
}

func (j *Javascript) UnmarshalJSON(data []byte) error {
var err error
var tmp struct {
Base
Dimension string `json:"dimension"`
Function string `json:"function"`
ExtractionFn json.RawMessage `json:"extractionFn"`
FilterTuning *FilterTuning `json:"filterTuning"`
}
if err := json.Unmarshal(data, &tmp); err != nil {
if err = json.Unmarshal(data, &tmp); err != nil {
return err
}
e, err := extractionfn.Load(tmp.ExtractionFn)
if err != nil {
return err
var e builder.ExtractionFn
if tmp.ExtractionFn != nil {
e, err = extractionfn.Load(tmp.ExtractionFn)
if err != nil {
return err
}
}
j.Base = tmp.Base
j.Dimension = tmp.Dimension
Expand Down
12 changes: 8 additions & 4 deletions builder/filter/like.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (l *Like) SetFilterTuning(filterTuning *FilterTuning) *Like {
}

func (l *Like) UnmarshalJSON(data []byte) error {
var err error
var tmp struct {
Base
Dimension string `json:"dimension"`
Expand All @@ -56,12 +57,15 @@ func (l *Like) UnmarshalJSON(data []byte) error {
ExtractionFn json.RawMessage `json:"extractionFn"`
FilterTuning *FilterTuning `json:"filterTuning"`
}
if err := json.Unmarshal(data, &tmp); err != nil {
if err = json.Unmarshal(data, &tmp); err != nil {
return err
}
e, err := extractionfn.Load(tmp.ExtractionFn)
if err != nil {
return err
var e builder.ExtractionFn
if tmp.ExtractionFn != nil {
e, err = extractionfn.Load(tmp.ExtractionFn)
if err != nil {
return err
}
}
l.Base = tmp.Base
l.Dimension = tmp.Dimension
Expand Down
12 changes: 8 additions & 4 deletions builder/filter/regex.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,23 @@ func (r *Regex) SetFilterTuning(filterTuning *FilterTuning) *Regex {
}

func (r *Regex) UnmarshalJSON(data []byte) error {
var err error
var tmp struct {
Base
Dimension string `json:"dimension"`
Pattern string `json:"pattern"`
ExtractionFn json.RawMessage `json:"extractionFn"`
FilterTuning *FilterTuning `json:"filterTuning"`
}
if err := json.Unmarshal(data, &tmp); err != nil {
if err = json.Unmarshal(data, &tmp); err != nil {
return err
}
e, err := extractionfn.Load(tmp.ExtractionFn)
if err != nil {
return err
var e builder.ExtractionFn
if tmp.ExtractionFn != nil {
e, err = extractionfn.Load(tmp.ExtractionFn)
if err != nil {
return err
}
}
r.Base = tmp.Base
r.Dimension = tmp.Dimension
Expand Down
12 changes: 8 additions & 4 deletions builder/filter/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,23 @@ func (s *Search) SetFilterTuning(filterTuning *FilterTuning) *Search {
}

func (s *Search) UnmarshalJSON(data []byte) error {
var err error
var tmp struct {
Base
Dimension string `json:"dimension"`
Query string `json:"builder"`
ExtractionFn json.RawMessage `json:"extractionFn"`
FilterTuning *FilterTuning `json:"filterTuning"`
}
if err := json.Unmarshal(data, &tmp); err != nil {
if err = json.Unmarshal(data, &tmp); err != nil {
return err
}
e, err := extractionfn.Load(tmp.ExtractionFn)
if err != nil {
return err
var e builder.ExtractionFn
if tmp.ExtractionFn != nil {
e, err = extractionfn.Load(tmp.ExtractionFn)
if err != nil {
return err
}
}
s.Base = tmp.Base
s.Dimension = tmp.Dimension
Expand Down
12 changes: 8 additions & 4 deletions builder/filter/selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,23 @@ func (s *Selector) SetFilterTuning(filterTuning *FilterTuning) *Selector {
}

func (s *Selector) UnmarshalJSON(data []byte) error {
var err error
var tmp struct {
Base
Dimension string `json:"dimension"`
Value string `json:"value"`
ExtractionFn json.RawMessage `json:"extractionFn"`
FilterTuning *FilterTuning `json:"filterTuning"`
}
if err := json.Unmarshal(data, &tmp); err != nil {
if err = json.Unmarshal(data, &tmp); err != nil {
return err
}
e, err := extractionfn.Load(tmp.ExtractionFn)
if err != nil {
return err
var e builder.ExtractionFn
if tmp.ExtractionFn != nil {
e, err = extractionfn.Load(tmp.ExtractionFn)
if err != nil {
return err
}
}
s.Base = tmp.Base
s.Dimension = tmp.Dimension
Expand Down
4 changes: 2 additions & 2 deletions builder/query/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
type Scan struct {
Base
VirtualColumns []builder.VirtualColumn `json:"virtualColumns"`
ResultFormat string `json:"resultFormat"`
ResultFormat string `json:"resultFormat,omitempty"`
BatchSize int64 `json:"batchSize"`
Limit int64 `json:"limit"`
Order Order `json:"order"`
Expand Down Expand Up @@ -102,7 +102,7 @@ func (s *Scan) UnmarshalJSON(data []byte) error {
Columns []string `json:"columns"`
Legacy bool `json:"legacy"`
}
if err := json.Unmarshal(data, &tmp); err != nil {
if err = json.Unmarshal(data, &tmp); err != nil {
return err
}
var v builder.VirtualColumn
Expand Down
5 changes: 5 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func main() {
d.Query().Execute(q, &results)
spew.Dump(results)

q, err = d.Query().Load([]byte("{\"batchSize\":20480,\"columns\":[\"__time\",\"channel\",\"cityName\",\"comment\",\"count\",\"countryIsoCode\",\"diffUrl\",\"flags\",\"isAnonymous\",\"isMinor\",\"isNew\",\"isRobot\",\"isUnpatrolled\",\"metroCode\",\"namespace\",\"page\",\"regionIsoCode\",\"regionName\",\"sum_added\",\"sum_commentLength\",\"sum_deleted\",\"sum_delta\",\"sum_deltaBucket\",\"user\",\"v0\"],\"context\":{\"plopa\":\"plep\"},\"dataSource\":{\"name\":\"wikipedia\",\"type\":\"table\"},\"filter\":{\"dimension\":\"countryName\",\"type\":\"selector\",\"value\":\"France\"},\"intervals\":[\"2016-06-27T01:55:25.000Z/2016-06-27T10:07:01.000Z\"],\"limit\":10,\"order\":\"none\",\"queryType\":\"scan\",\"virtualColumns\":[{\"expression\":\"'France'\",\"name\":\"v0\",\"outputType\":\"STRING\",\"type\":\"expression\"}]}"))
spew.Dump(q)
d.Query().Execute(q, &results)
spew.Dump(results)

q, err = d.Query().Load([]byte("{\"batchSize\":20480,\"columns\":[\"__time\",\"channel\",\"cityName\",\"comment\",\"count\",\"countryIsoCode\",\"diffUrl\",\"flags\",\"isAnonymous\",\"isMinor\",\"isNew\",\"isRobot\",\"isUnpatrolled\",\"metroCode\",\"namespace\",\"page\",\"regionIsoCode\",\"regionName\",\"sum_added\",\"sum_commentLength\",\"sum_deleted\",\"sum_delta\",\"sum_deltaBucket\",\"user\"],\"dataSource\":{\"name\":\"wikipedia\",\"type\":\"table\"},\"filter\":{\"dimension\":\"countryName\",\"extractionFn\":{\"locale\":\"\",\"type\":\"lower\"},\"type\":\"selector\",\"value\":\"france\"},\"intervals\":[\"1980-01-26T23:00:00.000Z/2020-01-26T23:00:00.000Z\"],\"limit\":10,\"order\":\"descending\",\"queryType\":\"scan\", \"resultFormat\":\"compactedList\"}"))
spew.Dump(q)
d.Query().Execute(q, &results)
Expand Down

0 comments on commit ab7942a

Please sign in to comment.