From 708bace0dee9b07ea7f5443cc34d1d8784ee5dc6 Mon Sep 17 00:00:00 2001 From: James Mackie Date: Fri, 28 Jan 2022 14:38:13 +1100 Subject: [PATCH] Allow string values for dimension and datasource Change intervals value to be loaded as string array --- builder/datasource/data_source.go | 10 +++++++++- builder/dimension/dimension.go | 14 +++++++++++++- builder/intervals/intervals.go | 23 ++++++++++++----------- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/builder/datasource/data_source.go b/builder/datasource/data_source.go index 667697f..278638f 100644 --- a/builder/datasource/data_source.go +++ b/builder/datasource/data_source.go @@ -29,7 +29,15 @@ func Load(data []byte) (builder.DataSource, error) { Typ builder.ComponentType `json:"type,omitempty"` } if err := json.Unmarshal(data, &t); err != nil { - return nil, err + // the datasource may be a string which is supported by Druid + var tstr string + if err := json.Unmarshal(data, &tstr); err != nil { + return nil, err + } + // We will just make it table which is the most common type according to the Druid docs + d = NewTable() + d.(*Table).SetName(tstr) + return d, nil } switch t.Typ { case "globalTable": diff --git a/builder/dimension/dimension.go b/builder/dimension/dimension.go index d303864..4312fc3 100644 --- a/builder/dimension/dimension.go +++ b/builder/dimension/dimension.go @@ -48,8 +48,20 @@ func Load(data []byte) (builder.Dimension, error) { Typ builder.ComponentType `json:"type,omitempty"` } if err := json.Unmarshal(data, &t); err != nil { - return nil, err + // the dimension may be a string which is supported by Druid + var tstr string + if err := json.Unmarshal(data, &tstr); err != nil { + return nil, err + } + // We will just make it a 'default' dimension + d = &Base{} + d.(*Base).SetType("Default") + d.(*Base).SetDimension(tstr) + d.(*Base).SetOutputName(tstr) + d.(*Base).SetOutputType(types.String) + return d, nil } + switch t.Typ { case "default": d = NewDefault() diff --git a/builder/intervals/intervals.go b/builder/intervals/intervals.go index f6601d1..0637518 100644 --- a/builder/intervals/intervals.go +++ b/builder/intervals/intervals.go @@ -2,7 +2,6 @@ package intervals import ( "encoding/json" - "errors" "github.com/grafadruid/go-druid/builder" ) @@ -25,17 +24,19 @@ func Load(data []byte) (builder.Intervals, error) { if string(data) == "null" { return i, nil } - var t struct { - Typ builder.ComponentType `json:"type,omitempty"` - } - if err := json.Unmarshal(data, &t); err != nil { + // "intervals" in the spec is just an string array + var intv []string + if err := json.Unmarshal(data, &intv); err != nil { return nil, err } - switch t.Typ { - case "intervals": - i = NewIntervals() - default: - return nil, errors.New("unsupported intervals type") + // Now cast the only array item into an actual "interval" + interval := Interval(intv[0]) + // create our "intervals" object with "Typ" + i = &Intervals{ + Base: Base{ + Typ: "intervals", + }, + Intervals: []*Interval{&interval}, } - return i, json.Unmarshal(data, &i) + return i, nil }