Skip to content

Commit 5a748bd

Browse files
authored
default to json encoding (#232)
* json encoding * use encoding interface as type of field for hostEnvImpl
1 parent a01ec47 commit 5a748bd

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

internal_worker.go

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"bytes"
2727
"context"
2828
"encoding/gob"
29+
"encoding/json"
2930
"errors"
3031
"fmt"
3132
"reflect"
@@ -359,8 +360,8 @@ type hostEnvImpl struct {
359360
workflowAliasMap map[string]string
360361
activityFuncMap map[string]activity
361362
activityAliasMap map[string]string
362-
encoding gobEncoding
363-
tEncoding thriftEncoding
363+
encoding encoding
364+
tEncoding encoding
364365
activityRegistrationInterceptors []interceptorFn
365366
workflowRegistrationInterceptors []interceptorFn
366367
}
@@ -850,7 +851,8 @@ func newHostEnvironment() *hostEnvImpl {
850851
workflowAliasMap: make(map[string]string),
851852
activityFuncMap: make(map[string]activity),
852853
activityAliasMap: make(map[string]string),
853-
encoding: gobEncoding{},
854+
encoding: jsonEncoding{},
855+
tEncoding: thriftEncoding{},
854856
}
855857
}
856858

@@ -1161,6 +1163,40 @@ func (g gobEncoding) Unmarshal(data []byte, objs []interface{}) error {
11611163
return nil
11621164
}
11631165

1166+
// jsonEncoding encapsulates json encoding and decoding
1167+
type jsonEncoding struct {
1168+
}
1169+
1170+
// Register implements the encoding interface
1171+
func (g jsonEncoding) Register(obj interface{}) error {
1172+
return nil
1173+
}
1174+
1175+
// Marshal encodes an array of object into bytes
1176+
func (g jsonEncoding) Marshal(objs []interface{}) ([]byte, error) {
1177+
var buf bytes.Buffer
1178+
enc := json.NewEncoder(&buf)
1179+
for i, obj := range objs {
1180+
if err := enc.Encode(obj); err != nil {
1181+
return nil, fmt.Errorf(
1182+
"unable to encode argument: %d, %v, with json error: %v", i, reflect.TypeOf(obj), err)
1183+
}
1184+
}
1185+
return buf.Bytes(), nil
1186+
}
1187+
1188+
// Unmarshal decodes a byte array into the passed in objects
1189+
func (g jsonEncoding) Unmarshal(data []byte, objs []interface{}) error {
1190+
dec := json.NewDecoder(bytes.NewBuffer(data))
1191+
for i, obj := range objs {
1192+
if err := dec.Decode(obj); err != nil {
1193+
return fmt.Errorf(
1194+
"unable to decode argument: %d, %v, with json error: %v", i, reflect.TypeOf(obj), err)
1195+
}
1196+
}
1197+
return nil
1198+
}
1199+
11641200
func isThriftType(v interface{}) bool {
11651201
// NOTE: Thrift serialization works only if the values are pointers.
11661202
// Thrift has a validation that it meets thift.TStruct which has Read/Write pointer receivers.

0 commit comments

Comments
 (0)