func (p PolicyConverter) ConvertKVPairToAPI(d *model.KVPair) (unversioned.Resource, error) {
	bp := d.Value.(*model.Policy)
	bk := d.Key.(model.PolicyKey)
	ap := api.NewPolicy()
	ap.Metadata.Name = bk.Name
	ap.Metadata.Annotations = bp.Annotations
	ap.Spec.Order = bp.Order
	ap.Spec.IngressRules = RulesBackendToAPI(bp.InboundRules)
	ap.Spec.EgressRules = RulesBackendToAPI(bp.OutboundRules)
	ap.Spec.Selector = bp.Selector
	ap.Spec.DoNotTrack = bp.DoNotTrack
	ap.Spec.PreDNAT = bp.PreDNAT
	ap.Spec.Types = nil
	if len(bp.Types) == 0 {
		if bp.PreDNAT {
			ap.Spec.Types = []api.PolicyType{api.PolicyTypeIngress}
		} else {
			ap.Spec.Types = []api.PolicyType{api.PolicyTypeIngress, api.PolicyTypeEgress}
		}
	} else {
		ap.Spec.Types = make([]api.PolicyType, len(bp.Types))
		for i, t := range bp.Types {
			ap.Spec.Types[i] = api.PolicyType(t)
		}
	}
	return ap, nil
}
ConvertKVPairToAPI converts a KVPair containing a backend Policy and PolicyKey
to an API Policy structure.
package converter
import (
	api "github.com/projectcalico/libcalico-go/lib/apis/v1"
	"github.com/projectcalico/libcalico-go/lib/apis/v1/unversioned"
	"github.com/projectcalico/libcalico-go/lib/backend/model"
)
type PolicyConverter struct{}
func (p PolicyConverter) ConvertMetadataToKey(m unversioned.ResourceMetadata) (model.Key, error) {
	pm := m.(api.PolicyMetadata)
	k := model.PolicyKey{
		Name: pm.Name,
	}
	return k, nil
}
func (p PolicyConverter) ConvertAPIToKVPair(a unversioned.Resource) (*model.KVPair, error) {
	ap := a.(api.Policy)
	k, err := p.ConvertMetadataToKey(ap.Metadata)
	if err != nil {
		return nil, err
	}
	d := model.KVPair{
		Key: k,
		Value: &model.Policy{
			Order:         ap.Spec.Order,
			InboundRules:  RulesAPIToBackend(ap.Spec.IngressRules),
			OutboundRules: RulesAPIToBackend(ap.Spec.EgressRules),
			Selector:      ap.Spec.Selector,
			DoNotTrack:    ap.Spec.DoNotTrack,
			Annotations:   ap.Metadata.Annotations,
			PreDNAT:       ap.Spec.PreDNAT,
			Types:         nil,  
		},
	}
	if ap.Spec.DoNotTrack || ap.Spec.PreDNAT {
		d.Value.(*model.Policy).ApplyOnForward = true
	}
	if len(ap.Spec.Types) == 0 {
		if len(ap.Spec.EgressRules) == 0 {
			d.Value.(*model.Policy).Types = []string{string(api.PolicyTypeIngress)}
		} else if len(ap.Spec.IngressRules) == 0 {
			d.Value.(*model.Policy).Types = []string{string(api.PolicyTypeEgress)}
		} else {
			d.Value.(*model.Policy).Types = []string{string(api.PolicyTypeIngress), string(api.PolicyTypeEgress)}
		}
	} else {
		d.Value.(*model.Policy).Types = make([]string, len(ap.Spec.Types))
		for i, t := range ap.Spec.Types {
			d.Value.(*model.Policy).Types[i] = string(t)
		}
	}
	return &d, nil
}