go.uber.org/cadence@v1.2.9/internal/encoding.go (about)

     1  // Copyright (c) 2017-2020 Uber Technologies Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package internal
    22  
    23  import (
    24  	"bytes"
    25  	"encoding/json"
    26  	"fmt"
    27  	"io"
    28  	"reflect"
    29  
    30  	"github.com/apache/thrift/lib/go/thrift"
    31  
    32  	"go.uber.org/cadence/internal/common"
    33  )
    34  
    35  // encoding is capable of encoding and decoding objects
    36  type encoding interface {
    37  	Marshal([]interface{}) ([]byte, error)
    38  	Unmarshal([]byte, []interface{}) error
    39  }
    40  
    41  // jsonEncoding encapsulates json encoding and decoding
    42  type jsonEncoding struct {
    43  }
    44  
    45  // Marshal encodes an array of object into bytes
    46  func (g jsonEncoding) Marshal(objs []interface{}) ([]byte, error) {
    47  	var buf bytes.Buffer
    48  	enc := json.NewEncoder(&buf)
    49  	for i, obj := range objs {
    50  		if err := enc.Encode(obj); err != nil {
    51  			if err == io.EOF {
    52  				return nil, fmt.Errorf("missing argument at index %d of type %T", i, obj)
    53  			}
    54  			return nil, fmt.Errorf(
    55  				"unable to encode argument: %d, %v, with json error: %v", i, reflect.TypeOf(obj), err)
    56  		}
    57  	}
    58  	return buf.Bytes(), nil
    59  }
    60  
    61  // Unmarshal decodes a byte array into the passed in objects
    62  func (g jsonEncoding) Unmarshal(data []byte, objs []interface{}) error {
    63  	dec := json.NewDecoder(bytes.NewBuffer(data))
    64  	dec.UseNumber()
    65  	for i, obj := range objs {
    66  		if err := dec.Decode(obj); err != nil {
    67  			return fmt.Errorf(
    68  				"unable to decode argument: %d, %v, with json error: %v", i, reflect.TypeOf(obj), err)
    69  		}
    70  	}
    71  	return nil
    72  }
    73  
    74  // thriftEncoding encapsulates thrift serializer/de-serializer.
    75  type thriftEncoding struct{}
    76  
    77  // Marshal encodes an array of thrift into bytes
    78  func (g thriftEncoding) Marshal(objs []interface{}) ([]byte, error) {
    79  	var tlist []thrift.TStruct
    80  	for i := 0; i < len(objs); i++ {
    81  		if !common.IsThriftType(objs[i]) {
    82  			return nil, fmt.Errorf("pointer to thrift.TStruct type is required for %v argument", i+1)
    83  		}
    84  		t := reflect.ValueOf(objs[i]).Interface().(thrift.TStruct)
    85  		tlist = append(tlist, t)
    86  	}
    87  	return common.TListSerialize(tlist)
    88  }
    89  
    90  // Unmarshal decodes an array of thrift into bytes
    91  func (g thriftEncoding) Unmarshal(data []byte, objs []interface{}) error {
    92  	var tlist []thrift.TStruct
    93  	for i := 0; i < len(objs); i++ {
    94  		rVal := reflect.ValueOf(objs[i])
    95  		if rVal.Kind() != reflect.Ptr || !common.IsThriftType(reflect.Indirect(rVal).Interface()) {
    96  			return fmt.Errorf("pointer to pointer thrift.TStruct type is required for %v argument", i+1)
    97  		}
    98  		t := reflect.New(rVal.Elem().Type().Elem()).Interface().(thrift.TStruct)
    99  		tlist = append(tlist, t)
   100  	}
   101  
   102  	if err := common.TListDeserialize(tlist, data); err != nil {
   103  		return err
   104  	}
   105  
   106  	for i := 0; i < len(tlist); i++ {
   107  		reflect.ValueOf(objs[i]).Elem().Set(reflect.ValueOf(tlist[i]))
   108  	}
   109  
   110  	return nil
   111  }