...

Source file src/go.mongodb.org/mongo-driver/mongo/integration/mtest/wiremessage_helpers.go

Documentation: go.mongodb.org/mongo-driver/mongo/integration/mtest

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package mtest
     8  
     9  import (
    10  	"errors"
    11  	"fmt"
    12  
    13  	"go.mongodb.org/mongo-driver/x/mongo/driver"
    14  	"go.mongodb.org/mongo-driver/x/mongo/driver/wiremessage"
    15  )
    16  
    17  func copyBytes(original []byte) []byte {
    18  	newSlice := make([]byte, len(original))
    19  	copy(newSlice, original)
    20  	return newSlice
    21  }
    22  
    23  // assertMsgSectionType asserts that the next section type in the OP_MSG wire message is equal to the provided type.
    24  // It returns the remainder of the wire message and an error if the section type could not be read or was not equal
    25  // to the expected type.
    26  func assertMsgSectionType(wm []byte, expected wiremessage.SectionType) ([]byte, error) {
    27  	var actual wiremessage.SectionType
    28  	var ok bool
    29  
    30  	actual, wm, ok = wiremessage.ReadMsgSectionType(wm)
    31  	if !ok {
    32  		return wm, errors.New("failed to read section type")
    33  	}
    34  	if expected != actual {
    35  		return wm, fmt.Errorf("unexpected section type %v; expected %v", actual, expected)
    36  	}
    37  	return wm, nil
    38  }
    39  
    40  func parseOpCompressed(wm []byte) (wiremessage.OpCode, []byte, error) {
    41  	// Store the original opcode to forward to another parser later.
    42  	originalOpcode, wm, ok := wiremessage.ReadCompressedOriginalOpCode(wm)
    43  	if !ok {
    44  		return originalOpcode, nil, errors.New("failed to read original opcode")
    45  	}
    46  
    47  	uncompressedSize, wm, ok := wiremessage.ReadCompressedUncompressedSize(wm)
    48  	if !ok {
    49  		return originalOpcode, nil, errors.New("failed to read uncompressed size")
    50  	}
    51  
    52  	compressorID, wm, ok := wiremessage.ReadCompressedCompressorID(wm)
    53  	if !ok {
    54  		return originalOpcode, nil, errors.New("failed to read compressor ID")
    55  	}
    56  
    57  	compressedMsg, _, ok := wiremessage.ReadCompressedCompressedMessage(wm, int32(len(wm)))
    58  	if !ok {
    59  		return originalOpcode, nil, errors.New("failed to read compressed message")
    60  	}
    61  
    62  	opts := driver.CompressionOpts{
    63  		Compressor:       compressorID,
    64  		UncompressedSize: uncompressedSize,
    65  	}
    66  	decompressed, err := driver.DecompressPayload(compressedMsg, opts)
    67  	if err != nil {
    68  		return originalOpcode, nil, fmt.Errorf("error decompressing payload: %w", err)
    69  	}
    70  
    71  	return originalOpcode, decompressed, nil
    72  }
    73  

View as plain text