Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an Iterator over Properties #4254

Merged
merged 1 commit into from
Aug 23, 2024
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
10 changes: 10 additions & 0 deletions internal/entities/properties/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/puzpuzpuz/xsync/v3"
"google.golang.org/protobuf/types/known/structpb"
"iter"
)

// Property is a struct that holds a value. It's just a wrapper around structpb.Value
Expand Down Expand Up @@ -233,3 +234,12 @@ func (p *Properties) GetProperty(key string) *Property {
}
return &prop
}

// Iterate implements the seq2 iterator so that the caller can call for key, prop := range Iterate()
func (p *Properties) Iterate() iter.Seq2[string, *Property] {
return func(yield func(string, *Property) bool) {
p.props.Range(func(key string, v Property) bool {
return yield(key, &v)
})
}
}
22 changes: 22 additions & 0 deletions internal/entities/properties/properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,3 +450,25 @@ func TestUnwrapTypeErrors(t *testing.T) {
})
}
}

func TestIterator(t *testing.T) {
t.Parallel()

input := map[string]any{
"name": "test",
"is_private": true,
}

output := make(map[string]any)

props, err := NewProperties(input)
require.NoError(t, err)

count := 0
for key, p := range props.Iterate() {
count++
output[key] = p.RawValue()
}
require.Equal(t, input, output)
require.Equal(t, 2, count)
}
Loading