Skip to content

Commit

Permalink
message: re-implement elements() with iter.Seq2
Browse files Browse the repository at this point in the history
This is a perfect use case for the new iterator pattern.
  • Loading branch information
enr0n committed Aug 26, 2024
1 parent 138e81f commit 2ae9d00
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions vici/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"errors"
"fmt"
"io"
"iter"
"reflect"
"strconv"
"strings"
Expand Down Expand Up @@ -247,19 +248,18 @@ func (m *Message) addItem(key string, value any) error {
return nil
}

type messageElement struct {
k string
v any
}

func (m *Message) elements() []messageElement {
ordered := make([]messageElement, len(m.keys))
func (m *Message) elements() iter.Seq2[string, any] {
return func(yield func(string, any) bool) {
if m.keys == nil || m.data == nil {
return
}

for i, k := range m.keys {
ordered[i] = messageElement{k: k, v: m.data[k]}
for _, k := range m.keys {
if !yield(k, m.data[k]) {
return
}
}
}

return ordered
}

func safePutUint8(buf *bytes.Buffer, val int) error {
Expand Down Expand Up @@ -317,10 +317,7 @@ func safePutUint32(buf *bytes.Buffer, val int) error {
func (m *Message) encode() ([]byte, error) {
buf := bytes.NewBuffer([]byte{})

for _, e := range m.elements() {
k := e.k
v := e.v

for k, v := range m.elements() {
rv := reflect.ValueOf(v)

var (
Expand Down Expand Up @@ -515,9 +512,7 @@ func (m *Message) encodeSection(key string, section *Message) ([]byte, error) {
}

// Encode the sections elements
for _, e := range section.elements() {
k := e.k
v := e.v
for k, v := range section.elements() {

rv := reflect.ValueOf(v)

Expand Down Expand Up @@ -1018,9 +1013,9 @@ func (m *Message) unmarshalToMap(rv reflect.Value) error {
return fmt.Errorf("%v: map keys of type %v are not compatible with string", errUnmarshalTypeMismatch, rv.Type().Key().Kind())
}

for _, e := range m.elements() {
key := reflect.ValueOf(e.k)
val := reflect.ValueOf(e.v)
for k, v := range m.elements() {
key := reflect.ValueOf(k)
val := reflect.ValueOf(v)

rfv := rv.MapIndex(key)
mapElemType := rv.Type().Elem()
Expand Down

0 comments on commit 2ae9d00

Please sign in to comment.