equal.go raw

   1  // Copyright 2024 The Go Authors. All rights reserved.
   2  // Use of this source code is governed by a BSD-style
   3  // license that can be found in the LICENSE file.
   4  
   5  package impl
   6  
   7  import (
   8  	"bytes"
   9  
  10  	"google.golang.org/protobuf/encoding/protowire"
  11  	"google.golang.org/protobuf/reflect/protoreflect"
  12  	"google.golang.org/protobuf/runtime/protoiface"
  13  )
  14  
  15  func equal(in protoiface.EqualInput) protoiface.EqualOutput {
  16  	return protoiface.EqualOutput{Equal: equalMessage(in.MessageA, in.MessageB)}
  17  }
  18  
  19  // equalMessage is a fast-path variant of protoreflect.equalMessage.
  20  // It takes advantage of the internal messageState type to avoid
  21  // unnecessary allocations, type assertions.
  22  func equalMessage(mx, my protoreflect.Message) bool {
  23  	if mx == nil || my == nil {
  24  		return mx == my
  25  	}
  26  	if mx.Descriptor() != my.Descriptor() {
  27  		return false
  28  	}
  29  
  30  	msx, ok := mx.(*messageState)
  31  	if !ok {
  32  		return protoreflect.ValueOfMessage(mx).Equal(protoreflect.ValueOfMessage(my))
  33  	}
  34  	msy, ok := my.(*messageState)
  35  	if !ok {
  36  		return protoreflect.ValueOfMessage(mx).Equal(protoreflect.ValueOfMessage(my))
  37  	}
  38  
  39  	mi := msx.messageInfo()
  40  	miy := msy.messageInfo()
  41  	if mi != miy {
  42  		return protoreflect.ValueOfMessage(mx).Equal(protoreflect.ValueOfMessage(my))
  43  	}
  44  	mi.init()
  45  	// Compares regular fields
  46  	// Modified Message.Range code that compares two messages of the same type
  47  	// while going over the fields.
  48  	for _, ri := range mi.rangeInfos {
  49  		var fd protoreflect.FieldDescriptor
  50  		var vx, vy protoreflect.Value
  51  
  52  		switch ri := ri.(type) {
  53  		case *fieldInfo:
  54  			hx := ri.has(msx.pointer())
  55  			hy := ri.has(msy.pointer())
  56  			if hx != hy {
  57  				return false
  58  			}
  59  			if !hx {
  60  				continue
  61  			}
  62  			fd = ri.fieldDesc
  63  			vx = ri.get(msx.pointer())
  64  			vy = ri.get(msy.pointer())
  65  		case *oneofInfo:
  66  			fnx := ri.which(msx.pointer())
  67  			fny := ri.which(msy.pointer())
  68  			if fnx != fny {
  69  				return false
  70  			}
  71  			if fnx <= 0 {
  72  				continue
  73  			}
  74  			fi := mi.fields[fnx]
  75  			fd = fi.fieldDesc
  76  			vx = fi.get(msx.pointer())
  77  			vy = fi.get(msy.pointer())
  78  		}
  79  
  80  		if !equalValue(fd, vx, vy) {
  81  			return false
  82  		}
  83  	}
  84  
  85  	// Compare extensions.
  86  	// This is more complicated because mx or my could have empty/nil extension maps,
  87  	// however some populated extension map values are equal to nil extension maps.
  88  	emx := mi.extensionMap(msx.pointer())
  89  	emy := mi.extensionMap(msy.pointer())
  90  	if emx != nil {
  91  		for k, x := range *emx {
  92  			xd := x.Type().TypeDescriptor()
  93  			xv := x.Value()
  94  			var y ExtensionField
  95  			ok := false
  96  			if emy != nil {
  97  				y, ok = (*emy)[k]
  98  			}
  99  			// We need to treat empty lists as equal to nil values
 100  			if emy == nil || !ok {
 101  				if xd.IsList() && xv.List().Len() == 0 {
 102  					continue
 103  				}
 104  				return false
 105  			}
 106  
 107  			if !equalValue(xd, xv, y.Value()) {
 108  				return false
 109  			}
 110  		}
 111  	}
 112  	if emy != nil {
 113  		// emy may have extensions emx does not have, need to check them as well
 114  		for k, y := range *emy {
 115  			if emx != nil {
 116  				// emx has the field, so we already checked it
 117  				if _, ok := (*emx)[k]; ok {
 118  					continue
 119  				}
 120  			}
 121  			// Empty lists are equal to nil
 122  			if y.Type().TypeDescriptor().IsList() && y.Value().List().Len() == 0 {
 123  				continue
 124  			}
 125  
 126  			// Cant be equal if the extension is populated
 127  			return false
 128  		}
 129  	}
 130  
 131  	return equalUnknown(mx.GetUnknown(), my.GetUnknown())
 132  }
 133  
 134  func equalValue(fd protoreflect.FieldDescriptor, vx, vy protoreflect.Value) bool {
 135  	// slow path
 136  	if fd.Kind() != protoreflect.MessageKind {
 137  		return vx.Equal(vy)
 138  	}
 139  
 140  	// fast path special cases
 141  	if fd.IsMap() {
 142  		if fd.MapValue().Kind() == protoreflect.MessageKind {
 143  			return equalMessageMap(vx.Map(), vy.Map())
 144  		}
 145  		return vx.Equal(vy)
 146  	}
 147  
 148  	if fd.IsList() {
 149  		return equalMessageList(vx.List(), vy.List())
 150  	}
 151  
 152  	return equalMessage(vx.Message(), vy.Message())
 153  }
 154  
 155  // Mostly copied from protoreflect.equalMap.
 156  // This variant only works for messages as map types.
 157  // All other map types should be handled via Value.Equal.
 158  func equalMessageMap(mx, my protoreflect.Map) bool {
 159  	if mx.Len() != my.Len() {
 160  		return false
 161  	}
 162  	equal := true
 163  	mx.Range(func(k protoreflect.MapKey, vx protoreflect.Value) bool {
 164  		if !my.Has(k) {
 165  			equal = false
 166  			return false
 167  		}
 168  		vy := my.Get(k)
 169  		equal = equalMessage(vx.Message(), vy.Message())
 170  		return equal
 171  	})
 172  	return equal
 173  }
 174  
 175  // Mostly copied from protoreflect.equalList.
 176  // The only change is the usage of equalImpl instead of protoreflect.equalValue.
 177  func equalMessageList(lx, ly protoreflect.List) bool {
 178  	if lx.Len() != ly.Len() {
 179  		return false
 180  	}
 181  	for i := 0; i < lx.Len(); i++ {
 182  		// We only operate on messages here since equalImpl will not call us in any other case.
 183  		if !equalMessage(lx.Get(i).Message(), ly.Get(i).Message()) {
 184  			return false
 185  		}
 186  	}
 187  	return true
 188  }
 189  
 190  // equalUnknown compares unknown fields by direct comparison on the raw bytes
 191  // of each individual field number.
 192  // Copied from protoreflect.equalUnknown.
 193  func equalUnknown(x, y protoreflect.RawFields) bool {
 194  	if len(x) != len(y) {
 195  		return false
 196  	}
 197  	if bytes.Equal([]byte(x), []byte(y)) {
 198  		return true
 199  	}
 200  
 201  	mx := make(map[protoreflect.FieldNumber]protoreflect.RawFields)
 202  	my := make(map[protoreflect.FieldNumber]protoreflect.RawFields)
 203  	for len(x) > 0 {
 204  		fnum, _, n := protowire.ConsumeField(x)
 205  		mx[fnum] = append(mx[fnum], x[:n]...)
 206  		x = x[n:]
 207  	}
 208  	for len(y) > 0 {
 209  		fnum, _, n := protowire.ConsumeField(y)
 210  		my[fnum] = append(my[fnum], y[:n]...)
 211  		y = y[n:]
 212  	}
 213  	if len(mx) != len(my) {
 214  		return false
 215  	}
 216  
 217  	for k, v1 := range mx {
 218  		if v2, ok := my[k]; !ok || !bytes.Equal([]byte(v1), []byte(v2)) {
 219  			return false
 220  		}
 221  	}
 222  
 223  	return true
 224  }
 225