common

package
v1.127.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2024 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var BasicDiff = func(fieldName protoreflect.Name, a, b proto.Message) (bool, error) {
	aField := a.ProtoReflect().Descriptor().Fields().ByName(fieldName)
	bField := b.ProtoReflect().Descriptor().Fields().ByName(fieldName)

	if IsFieldBehavior(aField, annotations.FieldBehavior_OUTPUT_ONLY) {
		return false, nil
	}

	if bField == nil {
		return true, nil
	}
	if !aField.Kind().IsValid() {
		return false, fmt.Errorf("unimplemented kind: " + aField.Kind().String())
	}

	diff := false
	aVal := a.ProtoReflect().Get(aField)
	bVal := b.ProtoReflect().Get(bField)
	switch aField.Kind() {
	case protoreflect.MessageKind:
		if aField.IsList() || aField.IsMap() {
			if !aVal.Equal(bVal) {
				diff = true
			}
		} else {
			m := aVal.Message().Interface()

			switch m.(type) {
			case *timestamppb.Timestamp:
				if !aVal.Equal(bVal) {
					diff = true
				}
			case *durationpb.Duration:
				if !aVal.Equal(bVal) {
					diff = true
				}
			default:
				return false, fmt.Errorf("field %s not recursed", fieldName)
			}
		}
	default:
		if !aVal.Equal(bVal) {
			diff = true
		}
	}
	if diff && IsFieldBehavior(aField, annotations.FieldBehavior_IMMUTABLE) {
		return false, fmt.Errorf("change to immutable field %s", fieldName)
	}
	return diff, nil
}

Functions

func AddLabelManagedByCNRM added in v1.126.0

func AddLabelManagedByCNRM(labels map[string]string) map[string]string

func CompareProtoMessage added in v1.125.0

func CompareProtoMessage(a, b proto.Message, compareDiff CompareDiff) (sets.Set[string], error)

func ComputeGCPLabels

func ComputeGCPLabels(labels map[string]string) map[string]string

func IsFieldBehavior added in v1.125.0

func IsFieldBehavior(field protoreflect.FieldDescriptor, fieldBehavior annotations.FieldBehavior) bool

func NormalizeReferences added in v1.126.0

func NormalizeReferences(ctx context.Context, reader client.Reader, obj client.Object, projectRef *refs.Project) error

func NormalizeStringToTimestamp

func NormalizeStringToTimestamp(ts string) *timestamppb.Timestamp

func RemoveByPrefixes

func RemoveByPrefixes(a map[string]string, prefixes ...string)

func VisitFields added in v1.126.0

func VisitFields(obj any, visitor Visitor) error

Types

type CompareDiff added in v1.125.0

type CompareDiff func(fieldName protoreflect.Name, a, b proto.Message) (bool, error)

type Visitor added in v1.126.0

type Visitor interface {
	VisitField(path string, value any) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL