Skip to content

Commit

Permalink
Merge pull request #185 from EvanLjp/supply-index-paramter
Browse files Browse the repository at this point in the history
supply index parameter
  • Loading branch information
shabicheng authored Nov 7, 2022
2 parents aa27c2c + 085d14f commit a9c91db
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 2 deletions.
9 changes: 7 additions & 2 deletions model.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,13 @@ type IndexLine struct {

// Index is an index config for a log store.
type Index struct {
Keys map[string]IndexKey `json:"keys,omitempty"`
Line *IndexLine `json:"line,omitempty"`
Keys map[string]IndexKey `json:"keys,omitempty"`
Line *IndexLine `json:"line,omitempty"`
Ttl uint32 `json:"ttl,omitempty"`
MaxTextLen uint32 `json:"max_text_len,omitempty"`
LogReduce bool `json:"log_reduce"`
LogReduceWhiteListDict []string `json:"log_reduce_white_list,omitempty"`
LogReduceBlackListDict []string `json:"log_reduce_black_list,omitempty"`
}

// CreateDefaultIndex return a full text index config
Expand Down
72 changes: 72 additions & 0 deletions model_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package sls

import (
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestIndex_MarshalJSON(t *testing.T) {

type fields struct {
Keys map[string]IndexKey
Line *IndexLine
Ttl uint32
MaxTextLen uint32
LogReduce bool
LogReduceWhiteListDict []string
LogReduceBlackListDict []string
}
tests := []struct {
name string
fields fields
want []byte
}{
{
name: "keys and line",
fields: fields{
Keys: map[string]IndexKey{
"test1": {},
},
Line: &IndexLine{},
},
want: []byte(`{"keys":{"test1":{"token":null,"caseSensitive":false,"type":"","chn":false}},"line":{"token":null,"caseSensitive":false,"chn":false},"log_reduce":false}`),
},
{
name: "only ttl",
fields: fields{
Ttl: 2,
MaxTextLen: 3,
},
want: []byte(`{"ttl":2,"max_text_len":3,"log_reduce":false}`),
},
{
name: "white & black",
fields: fields{
LogReduceWhiteListDict: []string{"key1"},
LogReduceBlackListDict: []string{"key2"},
LogReduce: true,
},
want: []byte(`{"log_reduce":true,"log_reduce_white_list":["key1"],"log_reduce_black_list":["key2"]}`),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
u := &Index{
Keys: tt.fields.Keys,
Line: tt.fields.Line,
Ttl: tt.fields.Ttl,
MaxTextLen: tt.fields.MaxTextLen,
LogReduce: tt.fields.LogReduce,
LogReduceWhiteListDict: tt.fields.LogReduceWhiteListDict,
LogReduceBlackListDict: tt.fields.LogReduceBlackListDict,
}
got, _ := json.Marshal(u)
fmt.Printf("%s", got)
assert.Equalf(t, tt.want, got, "MarshalJSON()")
})
}

}

0 comments on commit a9c91db

Please sign in to comment.