From 368063709037fd8ff883e601581992ace5c92aec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Mur=C3=A9?= Date: Wed, 27 Nov 2024 15:57:13 +0100 Subject: [PATCH] container: add a way to check for a single invocation, also iterator --- pkg/container/reader.go | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/pkg/container/reader.go b/pkg/container/reader.go index 1ca3982..f54846e 100644 --- a/pkg/container/reader.go +++ b/pkg/container/reader.go @@ -19,6 +19,7 @@ import ( ) var ErrNotFound = fmt.Errorf("not found") +var ErrMultipleInvocations = fmt.Errorf("multiple invocations") // Reader is a token container reader. It exposes the tokens conveniently decoded. type Reader map[cid.Cid]token.Token @@ -61,15 +62,36 @@ func (ctn Reader) GetAllDelegations() iter.Seq2[cid.Cid, *delegation.Token] { } } -// GetInvocation returns the first found invocation.Token. +// GetInvocation returns a single invocation.Token. // If none are found, ErrNotFound is returned. +// If more than one invocation exist, ErrMultipleInvocations is returned. func (ctn Reader) GetInvocation() (*invocation.Token, error) { + var res *invocation.Token for _, t := range ctn { if inv, ok := t.(*invocation.Token); ok { - return inv, nil + if res != nil { + return nil, ErrMultipleInvocations + } + res = inv + } + } + if res == nil { + return nil, ErrNotFound + } + return res, nil +} + +// GetAllInvocations returns all the invocation.Token in the container. +func (ctn Reader) GetAllInvocations() iter.Seq2[cid.Cid, *invocation.Token] { + return func(yield func(cid.Cid, *invocation.Token) bool) { + for c, t := range ctn { + if t, ok := t.(*invocation.Token); ok { + if !yield(c, t) { + return + } + } } } - return nil, ErrNotFound } // FromCbor decodes a DAG-CBOR encoded container.