Skip to content

Commit

Permalink
Feat/improver (#15)
Browse files Browse the repository at this point in the history
* feat: update readme

* feat: remove github.com/tkeel-io/core/pkg/tql

* feat: update gomod

* fix: number parse

* feat: embed collectjs & fix test

* feat: fix parse bug

* fix: unit test (GroupBy\MergeBy\KeyBy)

* feat: refactor Node

* feat: clean code

* feat: update gomod

* feat: clean code

* fix: path with "_"

* fix: upload sjson

* feat: add new
  • Loading branch information
imneov authored Mar 5, 2022
1 parent 812c7e6 commit f28a65e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
33 changes: 31 additions & 2 deletions collectjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,36 @@ func New(raw interface{}) *Collect {
return UNDEFINED_RESULT
}

func NewInt64(raw int64) *Collect {
str := fmt.Sprintf("%v", raw)
return &Collect{
value: []byte(str),
datatype: Int,
}
}

func NewBool(raw bool) *Collect {
str := fmt.Sprintf("%v", raw)
return &Collect{
value: []byte(str),
datatype: Bool,
}
}

func NewString(raw string) *Collect {
return &Collect{
value: []byte(raw),
datatype: String,
}
}

func NewFloat64(raw float64) *Collect {
str := fmt.Sprintf("%.6f", raw)
return &Collect{
value: []byte(str),
datatype: Float,
}
}

func newCollect(data []byte) *Collect {
collect := &Collect{}
Expand All @@ -34,8 +63,8 @@ func newCollect(data []byte) *Collect {
collect.value = value
if _, jtype, _, err := jsonparser.Get(data); err == nil {
collect.datatype = datetype(jtype)
if collect.datatype == String{
collect.value = collect.value[1:len(collect.value)-1]
if collect.datatype == String {
collect.value = collect.value[1 : len(collect.value)-1]
}
} else {
collect.err = err
Expand Down
23 changes: 23 additions & 0 deletions collectjs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,29 @@ var rawArray = Byte(`[{"v":{"cv":1}},{"v":{"cv":2}},{"v":{"cv":3}}]`)
var rawEmptyArray = Byte(`[]`)
var rawEmptyObject = Byte(`{}`)

func TestCollect_New(t *testing.T) {
tests := []struct {
name string
path string
raw *Collect
want string
}{
{"1", "", New(raw), "{}"},
{"2", "cpu", NewString("1"), `{"cpu":"1"}`},
{"2", "cpu", NewBool(true), `{"cpu":true}`},
{"2", "cpu", NewInt64(3456), `{"cpu":3456}`},
{"2", "cpu", NewFloat64(1 / 3.0), `{"cpu":0.333333}`},
}
for _, tt := range tests {
got := New("{}")
got.Set(tt.path, tt.raw)
t.Run(tt.name, func(t *testing.T) {
if !reflect.DeepEqual(string(got.Raw()), tt.want) {
t.Errorf("Get() = %v, want %v", string(got.Raw()), tt.want)
}
})
}
}
func TestCollect_Get(t *testing.T) {
tests := []struct {
name string
Expand Down

0 comments on commit f28a65e

Please sign in to comment.