Skip to content

Commit

Permalink
fix: copy the slice
Browse files Browse the repository at this point in the history
There is a possibility that slices share the memory.
  • Loading branch information
zoncoen committed Jun 13, 2019
1 parent ccf6ca9 commit 7a2f1c4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
8 changes: 7 additions & 1 deletion query.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ func New(opts ...Option) *Query {

// Append appends extractor to q and returns updated q.
func (q Query) Append(extractor Extractor) *Query {
q.extractors = append(q.extractors, extractor)
length := len(q.extractors)
extractors := make([]Extractor, length+1)
for i, e := range q.extractors {
extractors[i] = e
}
extractors[length] = extractor
q.extractors = extractors
return &q
}

Expand Down
13 changes: 13 additions & 0 deletions query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ import (
"github.com/google/go-cmp/cmp"
)

func TestQuery_Append(t *testing.T) {
q := New()
q.extractors = make([]Extractor, 0, 1)
q1 := q.Key("1")
q2 := q.Key("2")
if got, expect := q1.String(), ".1"; got != expect {
t.Errorf(`expected "%s" but got "%s"`, expect, got)
}
if got, expect := q2.String(), ".2"; got != expect {
t.Errorf(`expected "%s" but got "%s"`, expect, got)
}
}

func TestQuery_Extract(t *testing.T) {
type debug struct {
Prof map[string][]*keyExtractor
Expand Down

0 comments on commit 7a2f1c4

Please sign in to comment.