FieldValueEquals returns true if the value of the given fields is the same.
(this, other *pb.Field)
| 26 | |
| 27 | // FieldValueEquals returns true if the value of the given fields is the same. |
| 28 | func FieldValueEquals(this, other *pb.Field) bool { |
| 29 | // check for pointer + primitive matches and nil values |
| 30 | switch { |
| 31 | case this == other: |
| 32 | return true |
| 33 | case this == nil || other == nil: |
| 34 | return false |
| 35 | case this.GetValue() == other.GetValue(): |
| 36 | return true |
| 37 | case this.GetValue() == nil || other.GetValue() == nil: |
| 38 | return false |
| 39 | } |
| 40 | |
| 41 | // check for matches with objects and arrays |
| 42 | switch val := this.GetValue().(type) { |
| 43 | case *pb.Field_Number: |
| 44 | otherVal, ok := other.GetValue().(*pb.Field_Number) |
| 45 | if !ok { |
| 46 | return false |
| 47 | } |
| 48 | return val.Number == otherVal.Number |
| 49 | case *pb.Field_Str: |
| 50 | otherVal, ok := other.GetValue().(*pb.Field_Str) |
| 51 | if !ok { |
| 52 | return false |
| 53 | } |
| 54 | return val.Str == otherVal.Str |
| 55 | case *pb.Field_Boolean: |
| 56 | otherVal, ok := other.GetValue().(*pb.Field_Boolean) |
| 57 | if !ok { |
| 58 | return false |
| 59 | } |
| 60 | return val.Boolean == otherVal.Boolean |
| 61 | case *pb.Field_Object: |
| 62 | otherVal, ok := other.GetValue().(*pb.Field_Object) |
| 63 | if !ok { |
| 64 | return false |
| 65 | } |
| 66 | items, otherItems := val.Object.GetItems(), otherVal.Object.GetItems() |
| 67 | |
| 68 | if len(items) != len(otherItems) { |
| 69 | return false |
| 70 | } |
| 71 | |
| 72 | for k, v := range items { |
| 73 | otherV, ok := otherItems[k] |
| 74 | if !ok { |
| 75 | return false |
| 76 | } |
| 77 | |
| 78 | return FieldValueEquals(v, otherV) |
| 79 | } |
| 80 | case *pb.Field_Array: |
| 81 | otherVal, ok := other.GetValue().(*pb.Field_Array) |
| 82 | if !ok { |
| 83 | return false |
| 84 | } |
| 85 | items, otherItems := val.Array.GetItems(), otherVal.Array.GetItems() |