-
Notifications
You must be signed in to change notification settings - Fork 18
/
view_r.go
177 lines (142 loc) · 5.29 KB
/
view_r.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package repocpi
import (
"fmt"
"io"
"github.com/open-component-model/ocm/pkg/contexts/credentials"
"github.com/open-component-model/ocm/pkg/contexts/ocm/cpi"
"github.com/open-component-model/ocm/pkg/errors"
"github.com/open-component-model/ocm/pkg/refmgmt"
"github.com/open-component-model/ocm/pkg/refmgmt/resource"
"github.com/open-component-model/ocm/pkg/utils"
)
// View objects are the user facing generic implementations of the context interfaces.
// They are responsible to handle the reference counting and use
// shared implementations objects for th concrete type-specific implementations.
// Additionally, they are used to implement interface functionality which is
// common to all implementations and NOT dependent on the backend system technology.
////////////////////////////////////////////////////////////////////////////////
type _repositoryView interface {
resource.ResourceViewInt[cpi.Repository] // here you have to redeclare
}
type RepositoryViewManager = resource.ViewManager[cpi.Repository] // here you have to use an alias
type RepositoryBridge interface {
resource.ResourceImplementation[cpi.Repository]
GetContext() cpi.Context
IsReadOnly() bool
SetReadOnly()
GetSpecification() cpi.RepositorySpec
ComponentLister() cpi.ComponentLister
ExistsComponentVersion(name string, version string) (bool, error)
LookupComponentVersion(name string, version string) (cpi.ComponentVersionAccess, error)
LookupComponent(name string) (cpi.ComponentAccess, error)
io.Closer
}
type repositoryView struct {
_repositoryView
bridge RepositoryBridge
}
var (
_ cpi.Repository = (*repositoryView)(nil)
_ credentials.ConsumerIdentityProvider = (*repositoryView)(nil)
_ utils.Unwrappable = (*repositoryView)(nil)
)
func GetRepositoryBridge(n cpi.Repository) (RepositoryBridge, error) {
if v, ok := n.(*repositoryView); ok {
return v.bridge, nil
}
return nil, errors.ErrNotSupported("repository implementation type", fmt.Sprintf("%T", n))
}
func GetRepositoryImplementation(n cpi.Repository) (RepositoryImpl, error) {
if v, ok := n.(*repositoryView); ok {
if b, ok := v.bridge.(*repositoryBridge); ok {
return b.impl, nil
}
return nil, errors.ErrNotSupported("repository base type", fmt.Sprintf("%T", v.bridge))
}
return nil, errors.ErrNotSupported("repository implementation type", fmt.Sprintf("%T", n))
}
func repositoryViewCreator(i RepositoryBridge, v resource.CloserView, d RepositoryViewManager) cpi.Repository {
return &repositoryView{
_repositoryView: resource.NewView[cpi.Repository](v, d),
bridge: i,
}
}
// NewNoneRefRepositoryView provides a repository reflecting the state of the
// view manager without holding an additional reference.
func NewNoneRefRepositoryView(i RepositoryBridge) cpi.Repository {
return &repositoryView{
_repositoryView: resource.NewView[cpi.Repository](resource.NewNonRefView[cpi.Repository](i), i),
bridge: i,
}
}
func NewRepository(impl RepositoryImpl, kind string, closer ...io.Closer) cpi.Repository {
bridge := newRepositoryBridge(impl, kind, closer...)
if kind == "" {
kind = "OCM repository"
}
return resource.NewResource[cpi.Repository](bridge, repositoryViewCreator, kind, true)
}
func (r *repositoryView) Unwrap() interface{} {
return r.bridge
}
func (r *repositoryView) GetConsumerId(uctx ...credentials.UsageContext) credentials.ConsumerIdentity {
return credentials.GetProvidedConsumerId(r.bridge, uctx...)
}
func (r *repositoryView) GetIdentityMatcher() string {
return credentials.GetProvidedIdentityMatcher(r.bridge)
}
func (r *repositoryView) GetSpecification() cpi.RepositorySpec {
return r.bridge.GetSpecification()
}
func (r *repositoryView) GetContext() cpi.Context {
return r.bridge.GetContext()
}
func (r *repositoryView) IsReadOnly() bool {
return r.bridge.IsReadOnly()
}
func (r *repositoryView) SetReadOnly() {
r.bridge.SetReadOnly()
}
func (r *repositoryView) ComponentLister() cpi.ComponentLister {
return r.bridge.ComponentLister()
}
func (r *repositoryView) ExistsComponentVersion(name string, version string) (ok bool, err error) {
err = r.Execute(func() error {
ok, err = r.bridge.ExistsComponentVersion(name, version)
return err
})
return ok, err
}
func (r *repositoryView) LookupComponentVersion(name string, version string) (acc cpi.ComponentVersionAccess, err error) {
err = r.Execute(func() error {
acc, err = r.bridge.LookupComponentVersion(name, version)
return err
})
return acc, err
}
func (r *repositoryView) LookupComponent(name string) (acc cpi.ComponentAccess, err error) {
err = r.Execute(func() error {
acc, err = r.bridge.LookupComponent(name)
return err
})
return acc, err
}
func (r *repositoryView) NewComponentVersion(comp, vers string, overrides ...bool) (cpi.ComponentVersionAccess, error) {
c, err := refmgmt.ToLazy(r.LookupComponent(comp))
if err != nil {
return nil, err
}
defer c.Close()
return c.NewVersion(vers, overrides...)
}
func (r *repositoryView) AddComponentVersion(cv cpi.ComponentVersionAccess, overrides ...bool) error {
c, err := refmgmt.ToLazy(r.LookupComponent(cv.GetName()))
if err != nil {
return err
}
defer c.Close()
return c.AddVersion(cv, overrides...)
}