trpc.group/trpc-go/trpc-go@v1.0.3/codec/serialization.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  )
    19  
    20  // Serializer defines body serialization interface.
    21  type Serializer interface {
    22  	// Unmarshal deserialize the in bytes into body
    23  	Unmarshal(in []byte, body interface{}) error
    24  
    25  	// Marshal returns the bytes serialized from body.
    26  	Marshal(body interface{}) (out []byte, err error)
    27  }
    28  
    29  // SerializationType defines the code of different serializers, such as
    30  // protobuf, json, http-get-query and http-get-restful.
    31  //
    32  //   - code 0-127 is used for common modes in all language versions of trpc.
    33  //   - code 128-999 is used for modes in any language specific version of trpc.
    34  //   - code 1000+ is used for customized occasions in which conflicts should
    35  //     be avoided.
    36  const (
    37  	// SerializationTypePB is protobuf serialization code.
    38  	SerializationTypePB = 0
    39  	// 1 is reserved by Tencent for internal usage.
    40  	_ = 1
    41  	// SerializationTypeJSON is json serialization code.
    42  	SerializationTypeJSON = 2
    43  	// SerializationTypeFlatBuffer is flatbuffer serialization code.
    44  	SerializationTypeFlatBuffer = 3
    45  	// SerializationTypeNoop is bytes empty serialization code.
    46  	SerializationTypeNoop = 4
    47  	// SerializationTypeXML is xml serialization code (application/xml for http).
    48  	SerializationTypeXML = 5
    49  	// SerializationTypeTextXML is xml serialization code (text/xml for http).
    50  	SerializationTypeTextXML = 6
    51  
    52  	// SerializationTypeUnsupported is unsupported serialization code.
    53  	SerializationTypeUnsupported = 128
    54  	// SerializationTypeForm is used to handle form request.
    55  	SerializationTypeForm = 129
    56  	// SerializationTypeGet is used to handle http get request.
    57  	SerializationTypeGet = 130
    58  	// SerializationTypeFormData is used to handle form data.
    59  	SerializationTypeFormData = 131
    60  )
    61  
    62  var serializers = make(map[int]Serializer)
    63  
    64  // RegisterSerializer registers serializer, will be called by init function
    65  // in third package.
    66  func RegisterSerializer(serializationType int, s Serializer) {
    67  	serializers[serializationType] = s
    68  }
    69  
    70  // GetSerializer returns the serializer defined by serialization code.
    71  func GetSerializer(serializationType int) Serializer {
    72  	return serializers[serializationType]
    73  }
    74  
    75  // Unmarshal deserializes the in bytes into body. The specific serialization
    76  // mode is defined by serializationType code, protobuf is default mode.
    77  func Unmarshal(serializationType int, in []byte, body interface{}) error {
    78  	if body == nil {
    79  		return nil
    80  	}
    81  	if len(in) == 0 {
    82  		return nil
    83  	}
    84  	if serializationType == SerializationTypeUnsupported {
    85  		return nil
    86  	}
    87  
    88  	s := GetSerializer(serializationType)
    89  	if s == nil {
    90  		return errors.New("serializer not registered")
    91  	}
    92  	return s.Unmarshal(in, body)
    93  }
    94  
    95  // Marshal returns the serialized bytes from body. The specific serialization
    96  // mode is defined by serializationType code, protobuf is default mode.
    97  func Marshal(serializationType int, body interface{}) ([]byte, error) {
    98  	if body == nil {
    99  		return nil, nil
   100  	}
   101  	if serializationType == SerializationTypeUnsupported {
   102  		return nil, nil
   103  	}
   104  
   105  	s := GetSerializer(serializationType)
   106  	if s == nil {
   107  		return nil, errors.New("serializer not registered")
   108  	}
   109  	return s.Marshal(body)
   110  }