github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/message/supported.go (about)

     1  // Copyright 2020 DataStax
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package message
    16  
    17  import (
    18  	"errors"
    19  	"fmt"
    20  	"io"
    21  
    22  	"github.com/datastax/go-cassandra-native-protocol/primitive"
    23  )
    24  
    25  const (
    26  	// SupportedProtocolVersions is a Supported.Options multimap key returned by Cassandra from protocol v5 onwards.
    27  	// It holds the list of native protocol versions that are supported, encoded as the version number followed by a
    28  	// slash and the version description. For example: 3/v3, 4/v4, 5/v5-beta. If a version is in beta, it will have the
    29  	// word "beta" in its description.
    30  	SupportedProtocolVersions = "PROTOCOL_VERSIONS"
    31  )
    32  
    33  // Supported is a response message sent in reply to an Options request.
    34  // +k8s:deepcopy-gen=true
    35  // +k8s:deepcopy-gen:interfaces=github.com/datastax/go-cassandra-native-protocol/message.Message
    36  type Supported struct {
    37  	// This multimap gives for each of the supported Startup options, the list of supported values.
    38  	// See Startup.Options for details about supported option keys.
    39  	Options map[string][]string
    40  }
    41  
    42  func (m *Supported) IsResponse() bool {
    43  	return true
    44  }
    45  
    46  func (m *Supported) GetOpCode() primitive.OpCode {
    47  	return primitive.OpCodeSupported
    48  }
    49  
    50  func (m *Supported) String() string {
    51  	return fmt.Sprintf("SUPPORTED %v", m.Options)
    52  }
    53  
    54  type supportedCodec struct{}
    55  
    56  func (c *supportedCodec) Encode(msg Message, dest io.Writer, _ primitive.ProtocolVersion) error {
    57  	supported, ok := msg.(*Supported)
    58  	if !ok {
    59  		return errors.New(fmt.Sprintf("expected *message.Supported, got %T", msg))
    60  	}
    61  	if err := primitive.WriteStringMultiMap(supported.Options, dest); err != nil {
    62  		return err
    63  	}
    64  	return nil
    65  }
    66  
    67  func (c *supportedCodec) EncodedLength(msg Message, _ primitive.ProtocolVersion) (int, error) {
    68  	supported, ok := msg.(*Supported)
    69  	if !ok {
    70  		return -1, errors.New(fmt.Sprintf("expected *message.Supported, got %T", msg))
    71  	}
    72  	return primitive.LengthOfStringMultiMap(supported.Options), nil
    73  }
    74  
    75  func (c *supportedCodec) Decode(source io.Reader, _ primitive.ProtocolVersion) (Message, error) {
    76  	if options, err := primitive.ReadStringMultiMap(source); err != nil {
    77  		return nil, err
    78  	} else {
    79  		return &Supported{Options: options}, nil
    80  	}
    81  }
    82  
    83  func (c *supportedCodec) GetOpCode() primitive.OpCode {
    84  	return primitive.OpCodeSupported
    85  }