Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions msgp/setof/_gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ func (s *Foo) DecodeMsg(reader *msgp.Reader) error {
if dst == nil {
dst = make(Foo, sz)
} else {
for k := range dst {
delete(dst, k)
}
clear(dst)
}
for i := uint32(0); i < sz; i++ {
var k string
Expand Down Expand Up @@ -52,9 +50,7 @@ func (s *Foo) UnmarshalMsg(bytes []byte) ([]byte, error) {
if dst == nil {
dst = make(Foo, sz)
} else {
for k := range dst {
delete(dst, k)
}
clear(dst)
}
for i := uint32(0); i < sz; i++ {
var k string
Expand All @@ -80,6 +76,18 @@ func (s Foo) Msgsize() int {
size += len(s) * msgp.StringPrefixSize
return size
}

// FooFromSlice creates a Foo from a slice.
func FooFromSlice(s []string) Foo {
if s == nil {
return nil
}
dst := make(Foo, len(s))
for _, v := range s {
dst[v] = struct{}{}
}
return dst
}
`

const unsorted = `
Expand Down Expand Up @@ -119,6 +127,18 @@ func (s Foo) MarshalMsg(bytes []byte) ([]byte, error) {
}
return bytes, nil
}

// AsSlice returns the set as a slice.
func (s Foo) AsSlice() []string {
if s == nil {
return nil
}
dst := make([]string, 0, len(s))
for k := range s {
dst = append(dst, k)
}
return dst
}
`

const sorted = `
Expand Down Expand Up @@ -169,6 +189,19 @@ func (s Foo) MarshalMsg(bytes []byte) ([]byte, error) {
}
return bytes, nil
}

// AsSlice returns the set as a sorted slice.
func (s Foo) AsSlice() []string {
if s == nil {
return nil
}
dst := make([]string, 0, len(s))
for k := range s {
dst = append(dst, k)
}
sort.Slice(dst, func(i, j int) bool { return dst[i] < dst[j] })
return dst
}
`

type replacer struct {
Expand Down
Loading
Loading