Skip to content

Commit

Permalink
Fix extraction dimension bug (grafadruid#92)
Browse files Browse the repository at this point in the history
Co-authored-by: Shrivardhan Rao <Shrivardhan_Rao@intuit.com>
  • Loading branch information
2 people authored and galtshifter committed Aug 20, 2024
1 parent 9962f01 commit 3e4a3fb
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 3 deletions.
108 changes: 108 additions & 0 deletions builder/dimension/dimension_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dimension

import (
"github.com/grafadruid/go-druid/builder"
"github.com/grafadruid/go-druid/builder/extractionfn"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -15,3 +17,109 @@ func TestLoadUnsupportedType(t *testing.T) {
assert.NotNil(err, "error should not be nil")
assert.Error(err, "unsupported dimension type")
}

func TestLoad(t *testing.T) {
type args struct {
data []byte
}
tests := []struct {
name string
args args
want func() builder.Dimension
wantErr bool
}{
{
name: "load extraction dimension success",
args: args{data: []byte(`{
"type": "extraction",
"dimension": "lookupKey",
"outputName": "assetTier",
"extractionFn": {
"type": "cascade",
"extractionFns": [
{
"type": "registeredLookup",
"lookup": "asset_id_to_metadata",
"retainMissingValue": true
},
{
"type": "regex",
"expr": "(?<=tier\":\")(.*?)(?=\")",
"replaceMissingValue": false
}
]
}
}`)},
want: func() builder.Dimension {
eFunc1 := extractionfn.NewRegisteredLookup()
eFunc1.Lookup = "asset_id_to_metadata"
eFunc1RetainMissingValue := true
eFunc1.RetainMissingValue = &eFunc1RetainMissingValue

eFunc2 := extractionfn.NewRegex()
eFunc2.Expr = "(?<=tier\":\")(.*?)(?=\")"
eFunc2ReplaceMissingValue := false
eFunc2.ReplaceMissingValue = &eFunc2ReplaceMissingValue

eFunc := extractionfn.NewCascade()
eFunc.SetExtractionFns([]builder.ExtractionFn{
eFunc1,
eFunc2,
})

d := NewExtraction()
d.OutputName = "assetTier"
d.Dimension = "lookupKey"
d.ExtractionFn = eFunc

return d
},
wantErr: false,
},
{
name: "load extraction dimension empty extraction func",
args: args{data: []byte(`{
"type": "extraction",
"dimension": "lookupKey",
"outputName": "assetTier",
"extractionFn": null
}`)},
want: func() builder.Dimension {
d := NewExtraction()
d.OutputName = "assetTier"
d.Dimension = "lookupKey"
d.ExtractionFn = nil

return d
},
wantErr: false,
},
{
name: "load extraction dimension missing extraction func",
args: args{data: []byte(`{
"type": "extraction",
"dimension": "lookupKey",
"outputName": "assetTier"
}`)},
want: func() builder.Dimension {
d := NewExtraction()
d.OutputName = "assetTier"
d.Dimension = "lookupKey"

return d
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := Load(tt.args.data)
if tt.wantErr {
assert.NotNil(t, err)
return
}
assert.NotNil(t, got)
assert.Equalf(t, tt.want(), got, "Load(%v)", tt.args.data)
})
}
}
11 changes: 8 additions & 3 deletions builder/dimension/extraction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dimension

import (
"encoding/json"
"github.com/grafadruid/go-druid/builder/extractionfn"

"github.com/adjoeio/go-druid/builder"
"github.com/adjoeio/go-druid/builder/types"
Expand Down Expand Up @@ -46,9 +47,13 @@ func (e *Extraction) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &tmp); err != nil {
return err
}
ef, err := Load(tmp.ExtractionFn)
if err != nil {
return err
var ef builder.ExtractionFn
if tmp.ExtractionFn != nil {
var err error
ef, err = extractionfn.Load(tmp.ExtractionFn)
if err != nil {
return err
}
}
e.Base = tmp.Base
e.ExtractionFn = ef
Expand Down

0 comments on commit 3e4a3fb

Please sign in to comment.