github.com/batchcorp/thrift-iterator@v0.0.0-20220918180557-4c4a158fc6e9/test/combinations.go (about)

     1  package test
     2  
     3  import (
     4  	"bytes"
     5  	"github.com/apache/thrift/lib/go/thrift"
     6  	"github.com/batchcorp/thrift-iterator"
     7  	"github.com/batchcorp/thrift-iterator/spi"
     8  )
     9  
    10  type Combination struct {
    11  	CreateProtocol func() (*thrift.TMemoryBuffer, thrift.TProtocol)
    12  	CreateStream   func() spi.Stream
    13  	CreateIterator func(buf []byte) spi.Iterator
    14  	Unmarshal      func(buf []byte, val interface{}) error
    15  	Marshal        func(val interface{}) ([]byte, error)
    16  }
    17  
    18  var binaryCfg = thrifter.Config{Protocol: thrifter.ProtocolBinary}
    19  var binary = Combination{
    20  	CreateProtocol: func() (*thrift.TMemoryBuffer, thrift.TProtocol) {
    21  		buf := thrift.NewTMemoryBuffer()
    22  		proto := thrift.NewTBinaryProtocol(buf, true, true)
    23  		return buf, proto
    24  	},
    25  	CreateStream: func() spi.Stream {
    26  		return binaryCfg.Froze().NewStream(nil, nil)
    27  	},
    28  	CreateIterator: func(buf []byte) spi.Iterator {
    29  		return binaryCfg.Froze().NewIterator(nil, buf)
    30  	},
    31  	Unmarshal: func(buf []byte, val interface{}) error {
    32  		return binaryCfg.Froze().Unmarshal(buf, val)
    33  	},
    34  	Marshal: func(val interface{}) ([]byte, error) {
    35  		return binaryCfg.Froze().Marshal(val)
    36  	},
    37  }
    38  
    39  var binaryEncoderDecoder = Combination{
    40  	CreateProtocol: func() (*thrift.TMemoryBuffer, thrift.TProtocol) {
    41  		buf := thrift.NewTMemoryBuffer()
    42  		proto := thrift.NewTBinaryProtocol(buf, true, true)
    43  		return buf, proto
    44  	},
    45  	CreateStream: func() spi.Stream {
    46  		return binaryCfg.Froze().NewStream(nil, nil)
    47  	},
    48  	CreateIterator: func(buf []byte) spi.Iterator {
    49  		return binaryCfg.Froze().NewIterator(bytes.NewBuffer(buf), nil)
    50  	},
    51  	Unmarshal: func(buf []byte, val interface{}) error {
    52  		decoder := binaryCfg.Froze().NewDecoder(bytes.NewBuffer(buf), nil)
    53  		return decoder.Decode(val)
    54  	},
    55  	Marshal: func(val interface{}) ([]byte, error) {
    56  		encoder := binaryCfg.Froze().NewEncoder(nil)
    57  		err := encoder.Encode(val)
    58  		if err != nil {
    59  			return nil, err
    60  		}
    61  		return encoder.Buffer(), nil
    62  	},
    63  }
    64  
    65  var compactCfg = thrifter.Config{Protocol: thrifter.ProtocolCompact}
    66  var compact = Combination{
    67  	CreateProtocol: func() (*thrift.TMemoryBuffer, thrift.TProtocol) {
    68  		buf := thrift.NewTMemoryBuffer()
    69  		proto := thrift.NewTCompactProtocol(buf)
    70  		return buf, proto
    71  	},
    72  	CreateStream: func() spi.Stream {
    73  		return compactCfg.Froze().NewStream(nil, nil)
    74  	},
    75  	CreateIterator: func(buf []byte) spi.Iterator {
    76  		return compactCfg.Froze().NewIterator(nil, buf)
    77  	},
    78  	Unmarshal: func(buf []byte, val interface{}) error {
    79  		return compactCfg.Froze().Unmarshal(buf, val)
    80  	},
    81  	Marshal: func(val interface{}) ([]byte, error) {
    82  		return compactCfg.Froze().Marshal(val)
    83  	},
    84  }
    85  
    86  var compactEncoderDecoder = Combination{
    87  	CreateProtocol: func() (*thrift.TMemoryBuffer, thrift.TProtocol) {
    88  		buf := thrift.NewTMemoryBuffer()
    89  		proto := thrift.NewTCompactProtocol(buf)
    90  		return buf, proto
    91  	},
    92  	CreateStream: func() spi.Stream {
    93  		return compactCfg.Froze().NewStream(nil, nil)
    94  	},
    95  	CreateIterator: func(buf []byte) spi.Iterator {
    96  		return compactCfg.Froze().NewIterator(bytes.NewBuffer(buf), nil)
    97  	},
    98  	Unmarshal: func(buf []byte, val interface{}) error {
    99  		decoder := compactCfg.Froze().NewDecoder(bytes.NewBuffer(buf), nil)
   100  		return decoder.Decode(val)
   101  	},
   102  	Marshal: func(val interface{}) ([]byte, error) {
   103  		encoder := compactCfg.Froze().NewEncoder(nil)
   104  		err := encoder.Encode(val)
   105  		if err != nil {
   106  			return nil, err
   107  		}
   108  		return encoder.Buffer(), nil
   109  	},
   110  }
   111  
   112  var binaryDynamicCfg = thrifter.Config{Protocol: thrifter.ProtocolBinary, StaticCodegen: false}
   113  var binaryDynamic = Combination{
   114  	CreateProtocol: func() (*thrift.TMemoryBuffer, thrift.TProtocol) {
   115  		buf := thrift.NewTMemoryBuffer()
   116  		proto := thrift.NewTBinaryProtocol(buf, true, true)
   117  		return buf, proto
   118  	},
   119  	CreateIterator: func(buf []byte) spi.Iterator {
   120  		return binaryDynamicCfg.Froze().NewIterator(nil, buf)
   121  	},
   122  	Unmarshal: func(buf []byte, val interface{}) error {
   123  		return binaryDynamicCfg.Froze().Unmarshal(buf, val)
   124  	},
   125  	Marshal: func(val interface{}) ([]byte, error) {
   126  		return binaryDynamicCfg.Froze().Marshal(val)
   127  	},
   128  }
   129  var compactDynamicCfg = thrifter.Config{Protocol: thrifter.ProtocolCompact, StaticCodegen: false}
   130  var compactDynamic = Combination{
   131  	CreateProtocol: func() (*thrift.TMemoryBuffer, thrift.TProtocol) {
   132  		buf := thrift.NewTMemoryBuffer()
   133  		proto := thrift.NewTCompactProtocol(buf)
   134  		return buf, proto
   135  	},
   136  	CreateIterator: func(buf []byte) spi.Iterator {
   137  		return compactDynamicCfg.Froze().NewIterator(nil, buf)
   138  	},
   139  	Unmarshal: func(buf []byte, val interface{}) error {
   140  		return compactDynamicCfg.Froze().Unmarshal(buf, val)
   141  	},
   142  	Marshal: func(val interface{}) ([]byte, error) {
   143  		return compactDynamicCfg.Froze().Marshal(val)
   144  	},
   145  }
   146  
   147  var Combinations = []Combination{
   148  	binary, binaryEncoderDecoder, compact, compactEncoderDecoder,
   149  }
   150  
   151  var UnmarshalCombinations = append(Combinations,
   152  	binaryDynamic, compactDynamic)
   153  var MarshalCombinations = UnmarshalCombinations