trpc.group/trpc-go/trpc-go@v1.0.3/codec/serialization_noop.go (about)

     1  //
     2  //
     3  // Tencent is pleased to support the open source community by making tRPC available.
     4  //
     5  // Copyright (C) 2023 THL A29 Limited, a Tencent company.
     6  // All rights reserved.
     7  //
     8  // If you have downloaded a copy of the tRPC source code from Tencent,
     9  // please note that tRPC source code is licensed under the  Apache 2.0 License,
    10  // A copy of the Apache 2.0 License is included in this file.
    11  //
    12  //
    13  
    14  package codec
    15  
    16  import (
    17  	"errors"
    18  	"fmt"
    19  )
    20  
    21  func init() {
    22  	RegisterSerializer(SerializationTypeNoop, &NoopSerialization{})
    23  }
    24  
    25  // BytesBodyIn is used to check if Body implements BytesBodyIn
    26  // method when compile.
    27  var _ BytesBodyIn = &Body{}
    28  
    29  // BytesBodyIn is used to check if Body implements BytesBodyOut
    30  // method when compile.
    31  var _ BytesBodyOut = &Body{}
    32  
    33  // BytesBodyOut is used to receive custom type body.
    34  type BytesBodyOut interface {
    35  	Bytes() ([]byte, error)
    36  }
    37  
    38  // BytesBodyIn is used to receive custom type body.
    39  type BytesBodyIn interface {
    40  	SetBytes([]byte) error
    41  }
    42  
    43  // Body is bytes pack layer, it is not need serialized
    44  // and used in gateway service generally.
    45  type Body struct {
    46  	Data []byte
    47  }
    48  
    49  // String returns body data as string.
    50  func (b *Body) String() string {
    51  	return fmt.Sprintf("%v", b.Data)
    52  }
    53  
    54  // SetBytes sets body data and implements ByteBodyIn interface.
    55  func (b *Body) SetBytes(p []byte) error {
    56  	if b == nil {
    57  		return errors.New("body nil")
    58  	}
    59  	b.Data = p
    60  	return nil
    61  }
    62  
    63  // Bytes returns body data and implements ByteBodyOut interface.
    64  func (b *Body) Bytes() ([]byte, error) {
    65  	if b == nil {
    66  		return nil, errors.New("body nil")
    67  	}
    68  	return b.Data, nil
    69  }
    70  
    71  // NoopSerialization provides empty serialization, it is
    72  // used to serialize bytes.
    73  type NoopSerialization struct {
    74  }
    75  
    76  // Unmarshal deserializes the in bytes into body, body should be a Body or implements
    77  // BytesBodyIn interface.
    78  func (s *NoopSerialization) Unmarshal(in []byte, body interface{}) error {
    79  	bytesBodyIn, ok := body.(BytesBodyIn)
    80  	if ok {
    81  		return bytesBodyIn.SetBytes(in)
    82  	}
    83  	noop, ok := body.(*Body)
    84  	if !ok {
    85  		return errors.New("body type invalid")
    86  	}
    87  	if noop == nil {
    88  		return errors.New("body nil")
    89  	}
    90  	noop.Data = in
    91  	return nil
    92  }
    93  
    94  // Marshal returns the serialized bytes. body should be a Body or implements
    95  // BytesBodyOut interface.
    96  func (s *NoopSerialization) Marshal(body interface{}) ([]byte, error) {
    97  	bytesBody, ok := body.(BytesBodyOut)
    98  	if ok {
    99  		return bytesBody.Bytes()
   100  	}
   101  	noop, ok := body.(*Body)
   102  	if !ok {
   103  		return nil, errors.New("body type invalid")
   104  	}
   105  	if noop == nil {
   106  		return nil, errors.New("body nil")
   107  	}
   108  	return noop.Data, nil
   109  }