-
Notifications
You must be signed in to change notification settings - Fork 18
/
state.go
51 lines (39 loc) · 1.35 KB
/
state.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
// SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and Open Component Model contributors.
//
// SPDX-License-Identifier: Apache-2.0
package ctf
import (
"fmt"
"reflect"
"github.com/mandelsoft/vfs/pkg/vfs"
"github.com/open-component-model/ocm/v2/pkg/common/accessobj"
"github.com/open-component-model/ocm/v2/pkg/contexts/oci/repositories/ctf/index"
)
type StateHandler struct{}
var _ accessobj.StateHandler = &StateHandler{}
func NewStateHandler(fs vfs.FileSystem) accessobj.StateHandler {
return &StateHandler{}
}
func (i StateHandler) Initial() interface{} {
return index.NewRepositoryIndex()
}
func (i StateHandler) Encode(d interface{}) ([]byte, error) {
return index.Encode(d.(*index.RepositoryIndex).GetDescriptor())
}
func (i StateHandler) Decode(data []byte) (interface{}, error) {
idx, err := index.Decode(data)
if err != nil {
return nil, fmt.Errorf("unable to parse artifact index read from %s: %w", ArtifactIndexFileName, err)
}
if idx.SchemaVersion != index.SchemaVersion {
return nil, fmt.Errorf("unknown schema version %d for artifact index %s", index.SchemaVersion, ArtifactIndexFileName)
}
artifacts := index.NewRepositoryIndex()
for i := range idx.Index {
artifacts.AddArtifactInfo(&idx.Index[i])
}
return artifacts, nil
}
func (i StateHandler) Equivalent(a, b interface{}) bool {
return reflect.DeepEqual(a, b)
}