Skip to content

Commit

Permalink
added rand, patched udf registartion
Browse files Browse the repository at this point in the history
  • Loading branch information
adranwit committed Feb 14, 2019
1 parent b0c49cf commit e7569db
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## Feb 14 2019 - v0.17.1
- Added Rand udf, patched udf mappings

## Feb 13 2019 - v0.17.0
- Added $Select UDF

Expand Down
1 change: 1 addition & 0 deletions data/udf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@
- Base64DecodeText
- TrimSpace
- Elapsed elapsed time
- Rand
8 changes: 5 additions & 3 deletions data/udf/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ func Register(aMap data.Map) {
aMap.Put("Base64DecodeText", Base64DecodeText)
aMap.Put("TrimSpace", TrimSpace)
aMap.Put("Elapsed", Elapsed)
aMap.Put("Sum", Elapsed)
aMap.Put("Count", Elapsed)
aMap.Put("AsNumber", Elapsed)
aMap.Put("Sum", Sum)
aMap.Put("Count", Count)
aMap.Put("AsNumber", AsNumber)
aMap.Put("Select", Select)
aMap.Put("Rand", Rand)


}
19 changes: 19 additions & 0 deletions data/udf/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"fmt"
"github.com/viant/toolbox"
"github.com/viant/toolbox/data"
"math/rand"
"net/url"
"strings"
"time"
)

//Length returns length of slice or string
Expand Down Expand Up @@ -336,3 +338,20 @@ func matchPath(xPath string, state data.Map, handler func(value interface{}) err
}
return nil
}

//Rand returns random
func Rand(params interface{}, state data.Map) (interface{}, error) {
source:=rand.NewSource(time.Now().UnixNano())
generator := rand.New(source)
floatValue := generator.Float64()
if params == nil || ! toolbox.IsSlice(params) {
return floatValue, nil
}
parameters := toolbox.AsSlice(params)
if len(parameters) != 2 {
return floatValue, nil
}
min := toolbox.AsInt(parameters[0])
max := toolbox.AsInt(parameters[1])
return min + int(float64(max-min) * floatValue), nil
}
19 changes: 19 additions & 0 deletions data/udf/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package udf

import (
"github.com/stretchr/testify/assert"
"github.com/viant/toolbox"
"github.com/viant/toolbox/data"
"testing"
)
Expand Down Expand Up @@ -182,3 +183,21 @@ func TestSelect(t *testing.T) {
}

}


func TestRand(t *testing.T) {
{
randValue, err := Rand(nil, nil)
assert.Nil(t, err)
floatValue, err := toolbox.ToFloat(randValue)
assert.Nil(t, err)
assert.True(t, toolbox.IsFloat(randValue) && floatValue >=0.0 && floatValue <1.0)
}
{
randValue, err := Rand([]interface{}{2, 15}, nil)
assert.Nil(t, err)
intValue, err := toolbox.ToInt(randValue)
assert.Nil(t, err)
assert.True(t, toolbox.IsInt(randValue) && intValue >=2 && intValue <15)
}
}

0 comments on commit e7569db

Please sign in to comment.