func filterLoggableFields()

in grpc/server/ctxlogger/ctxlogger.go [81:114]


func filterLoggableFields(currentMap map[string]interface{}, message protoreflect.Message) map[string]interface{} {
	// Check if the map or the message is nil
	if currentMap == nil || message == nil {
		return currentMap
	}
	for name, value := range currentMap {
		// Get the field descriptor by name
		fd := message.Descriptor().Fields().ByName(protoreflect.Name(name))
		// Check if the field descriptor is nil
		if fd == nil {
			continue
		}
		opts := fd.Options()
		fdOpts := opts.(*descriptorpb.FieldOptions)
		loggable := proto.GetExtension(fdOpts, loggable.E_Loggable)

		// Delete the field from the map if it is not loggable
		if !loggable.(bool) {
			delete(currentMap, name)
			continue
		}
		// Check if the value is another map[string]interface{}
		if subMap, ok := value.(map[string]interface{}); ok {
			// Check if its a simple map or one containing messages
			if fd.Message() != nil && !fd.Message().IsMapEntry() {
				// Get the sub-message for the field
				subMessage := message.Get(fd).Message()
				// Call the helper function recursively on the subMap and subMessage
				currentMap[name] = filterLoggableFields(subMap, subMessage)
			}
		}
	}
	return currentMap
}