go.ligato.io/vpp-agent/v3@v3.5.0/plugins/kvscheduler/internal/utils/conversions.go (about)

     1  // Copyright (c) 2018 Cisco and/or its affiliates.
     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 utils
    16  
    17  import (
    18  	"google.golang.org/protobuf/encoding/prototext"
    19  	"google.golang.org/protobuf/proto"
    20  	"google.golang.org/protobuf/types/known/emptypb"
    21  )
    22  
    23  // ProtoToString converts proto message to string.
    24  func ProtoToString(msg proto.Message) string {
    25  	if recProto, wrapped := msg.(*RecordedProtoMessage); wrapped {
    26  		if recProto != nil {
    27  			msg = recProto.Message
    28  		} else {
    29  			msg = nil
    30  		}
    31  	}
    32  	if msg == nil {
    33  		return "<NIL>"
    34  	}
    35  	if _, isEmpty := msg.(*emptypb.Empty); isEmpty {
    36  		return "<EMPTY>"
    37  	}
    38  	b, err := prototext.Marshal(msg)
    39  	if err != nil {
    40  		return err.Error()
    41  	}
    42  	// wrap with curly braces, it is easier to read
    43  	return "{ " + string(b) + " }"
    44  }
    45  
    46  // ErrorToString converts error to string.
    47  func ErrorToString(err error) string {
    48  	if err == nil {
    49  		return "<NIL>"
    50  	}
    51  	return err.Error()
    52  }