github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/controller/registry/resolver/projection/properties.go (about)

     1  package projection
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/operator-framework/operator-registry/pkg/api"
     8  )
     9  
    10  const (
    11  	PropertiesAnnotationKey = "operatorframework.io/properties"
    12  )
    13  
    14  type property struct {
    15  	Type  string          `json:"type"`
    16  	Value json.RawMessage `json:"value"`
    17  }
    18  
    19  type propertiesAnnotation struct {
    20  	Properties []property `json:"properties,omitempty"`
    21  }
    22  
    23  func PropertiesAnnotationFromPropertyList(props []*api.Property) (string, error) {
    24  	var anno propertiesAnnotation
    25  	for _, prop := range props {
    26  		anno.Properties = append(anno.Properties, property{
    27  			Type:  prop.Type,
    28  			Value: json.RawMessage(prop.Value),
    29  		})
    30  	}
    31  	v, err := json.Marshal(&anno)
    32  	if err != nil {
    33  		return "", fmt.Errorf("failed to marshal properties annotation: %w", err)
    34  	}
    35  	return string(v), nil
    36  }
    37  
    38  func PropertyListFromPropertiesAnnotation(raw string) ([]*api.Property, error) {
    39  	var anno propertiesAnnotation
    40  	if err := json.Unmarshal([]byte(raw), &anno); err != nil {
    41  		return nil, fmt.Errorf("failed to unmarshal properties annotation: %w", err)
    42  	}
    43  	var result []*api.Property
    44  	for _, each := range anno.Properties {
    45  		result = append(result, &api.Property{
    46  			Type:  each.Type,
    47  			Value: string(each.Value),
    48  		})
    49  	}
    50  	return result, nil
    51  }