/
walk.go
56 lines (45 loc) · 1.38 KB
/
walk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package common
import (
"github.com/mandelsoft/logging"
ocmlog "github.com/open-component-model/ocm/v2/pkg/logging"
"github.com/open-component-model/ocm/v2/pkg/utils"
)
type NameVersionInfo[T any] map[NameVersion]T
func (s NameVersionInfo[T]) Add(nv NameVersion, data ...T) bool {
if _, ok := s[nv]; !ok {
s[nv] = utils.Optional(data...)
return true
}
return false
}
func (s NameVersionInfo[T]) Contains(nv NameVersion) bool {
_, ok := s[nv]
return ok
}
type WalkingState[T any, C any] struct {
LogCtx logging.Context
Logger logging.Logger
Context C
Closure NameVersionInfo[T]
History History
}
func NewWalkingState[T any, C any](ctx C, lctx ...logging.Context) WalkingState[T, C] {
logctx := utils.OptionalDefaulted[logging.Context](ocmlog.Context(), lctx...)
return WalkingState[T, C]{Context: ctx, Closure: NameVersionInfo[T]{}, LogCtx: logctx, Logger: logctx.Logger()}
}
func (s *WalkingState[T, C]) Add(kind string, nv NameVersion) (bool, error) {
if err := s.History.Add(kind, nv); err != nil {
return false, err
}
return s.Closure.Add(nv), nil
}
func (s *WalkingState[T, C]) Contains(nv NameVersion) bool {
_, ok := s.Closure[nv]
return ok
}
func (s *WalkingState[T, C]) Get(nv NameVersion) T {
return s.Closure[nv]
}