github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/types/cmp.go (about)

     1  /*
     2  Copyright 2022 Gravitational, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package types
    18  
    19  import (
    20  	"strings"
    21  
    22  	"github.com/gogo/protobuf/proto"
    23  	"github.com/google/go-cmp/cmp"
    24  )
    25  
    26  // protoKnownFieldsEqual returns true if the provided proto messages are equal,
    27  // ignoring any unknown fields found during unmarshal (ie, this ignores XXX_*
    28  // fields).
    29  // This is not a substitute for [proto.Equal] and should not be used as a full
    30  // equility check.
    31  // Do not use this method lightly or without a strong reason to do so.
    32  func protoKnownFieldsEqual(a, b proto.Message) bool {
    33  	return cmp.Equal(a, b, cmp.FilterPath(func(path cmp.Path) bool {
    34  		if field, ok := path.Last().(cmp.StructField); ok {
    35  			return strings.HasPrefix(field.Name(), "XXX_")
    36  		}
    37  		return false
    38  	}, cmp.Ignore()))
    39  }