/
properties.go
51 lines (44 loc) · 1.21 KB
/
properties.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
package projection
import (
"encoding/json"
"fmt"
"github.com/operator-framework/operator-registry/pkg/api"
)
const (
PropertiesAnnotationKey = "operatorframework.io/properties"
)
type property struct {
Type string `json:"type"`
Value json.RawMessage `json:"value"`
}
type propertiesAnnotation struct {
Properties []property `json:"properties,omitempty"`
}
func PropertiesAnnotationFromPropertyList(props []*api.Property) (string, error) {
var anno propertiesAnnotation
for _, prop := range props {
anno.Properties = append(anno.Properties, property{
Type: prop.Type,
Value: json.RawMessage(prop.Value),
})
}
v, err := json.Marshal(&anno)
if err != nil {
return "", fmt.Errorf("failed to marshal properties annotation: %w", err)
}
return string(v), nil
}
func PropertyListFromPropertiesAnnotation(raw string) ([]*api.Property, error) {
var anno propertiesAnnotation
if err := json.Unmarshal([]byte(raw), &anno); err != nil {
return nil, fmt.Errorf("failed to unmarshal properties annotation: %w", err)
}
var result []*api.Property
for _, each := range anno.Properties {
result = append(result, &api.Property{
Type: each.Type,
Value: string(each.Value),
})
}
return result, nil
}