Skip to content

Commit e6d4c27

Browse files
authored
Convert Header message to use Payload (#111)
1 parent e369525 commit e6d4c27

12 files changed

+97
-57
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
github.com/uber-go/tally v3.3.15+incompatible
2323
github.com/uber/jaeger-client-go v2.22.1+incompatible
2424
github.com/uber/jaeger-lib v2.2.0+incompatible // indirect
25-
go.temporal.io/temporal-proto v0.20.27
25+
go.temporal.io/temporal-proto v0.20.28
2626
go.uber.org/atomic v1.6.0
2727
go.uber.org/goleak v1.0.0
2828
go.uber.org/zap v1.14.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ github.com/uber/jaeger-client-go v2.22.1+incompatible/go.mod h1:WVhlPFC8FDjOFMMW
9393
github.com/uber/jaeger-lib v2.2.0+incompatible h1:MxZXOiR2JuoANZ3J6DE/U0kSFv/eJ/GfSYVCjK7dyaw=
9494
github.com/uber/jaeger-lib v2.2.0+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
9595
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
96-
go.temporal.io/temporal-proto v0.20.27 h1:g/UMo4TZyUGCGCAsvz81IIlkN6YQW7S5Ua4bwpjECvQ=
97-
go.temporal.io/temporal-proto v0.20.27/go.mod h1:Lv8L8YBpbp0Z7V5nbvw5UD0j7x0isebhCOIDLkBqn6s=
96+
go.temporal.io/temporal-proto v0.20.28 h1:syCkMj1bBEqPCj4dKmPQksBKH3qXeZ+PayfsBMdQEOY=
97+
go.temporal.io/temporal-proto v0.20.28/go.mod h1:Lv8L8YBpbp0Z7V5nbvw5UD0j7x0isebhCOIDLkBqn6s=
9898
go.uber.org/atomic v1.6.0 h1:Ezj3JGmsOnG1MoRWQkPBsKLe9DwWD9QeXzTRzzldNVk=
9999
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
100100
go.uber.org/goleak v1.0.0 h1:qsup4IcBdlmsnGfqyLl4Ntn3C2XCCuKAE7DwHpScyUo=

internal/error_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"fmt"
3030
"testing"
3131

32+
"github.com/stretchr/testify/assert"
3233
"github.com/stretchr/testify/require"
3334

3435
commonpb "go.temporal.io/temporal-proto/common"
@@ -437,8 +438,10 @@ func Test_ContinueAsNewError(t *testing.T) {
437438
return NewContinueAsNewError(ctx, continueAsNewWfName, a1, a2)
438439
}
439440

441+
headerValue, err := DefaultDataConverter.ToData("test-data")
442+
assert.NoError(t, err)
440443
header := &commonpb.Header{
441-
Fields: map[string][]byte{"test": []byte("test-data")},
444+
Fields: map[string]*commonpb.Payload{"test": headerValue},
442445
}
443446

444447
s := &WorkflowTestSuite{
@@ -450,7 +453,7 @@ func Test_ContinueAsNewError(t *testing.T) {
450453
Name: continueAsNewWfName,
451454
})
452455
wfEnv.ExecuteWorkflow(continueAsNewWorkflowFn, 101, "another random string")
453-
err := wfEnv.GetWorkflowError()
456+
err = wfEnv.GetWorkflowError()
454457

455458
require.Error(t, err)
456459
continueAsNewErr, ok := err.(*ContinueAsNewError)

internal/headers.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ import (
3232

3333
// HeaderWriter is an interface to write information to temporal headers
3434
type HeaderWriter interface {
35-
Set(string, []byte)
35+
Set(string, *commonpb.Payload)
3636
}
3737

3838
// HeaderReader is an interface to read information from temporal headers
3939
type HeaderReader interface {
40-
ForEachKey(handler func(string, []byte) error) error
40+
ForEachKey(handler func(string, *commonpb.Payload) error) error
4141
}
4242

4343
// ContextPropagator is an interface that determines what information from
@@ -62,7 +62,7 @@ type headerReader struct {
6262
header *commonpb.Header
6363
}
6464

65-
func (hr *headerReader) ForEachKey(handler func(string, []byte) error) error {
65+
func (hr *headerReader) ForEachKey(handler func(string, *commonpb.Payload) error) error {
6666
if hr.header == nil {
6767
return nil
6868
}
@@ -83,7 +83,7 @@ type headerWriter struct {
8383
header *commonpb.Header
8484
}
8585

86-
func (hw *headerWriter) Set(key string, value []byte) {
86+
func (hw *headerWriter) Set(key string, value *commonpb.Payload) {
8787
if hw.header == nil {
8888
return
8989
}
@@ -93,7 +93,7 @@ func (hw *headerWriter) Set(key string, value []byte) {
9393
// NewHeaderWriter returns a header writer interface
9494
func NewHeaderWriter(header *commonpb.Header) HeaderWriter {
9595
if header != nil && header.Fields == nil {
96-
header.Fields = make(map[string][]byte)
96+
header.Fields = make(map[string]*commonpb.Payload)
9797
}
9898
return &headerWriter{header}
9999
}

internal/headers_test.go

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,50 +37,50 @@ func TestHeaderWriter(t *testing.T) {
3737
name string
3838
initial *commonpb.Header
3939
expected *commonpb.Header
40-
vals map[string][]byte
40+
vals map[string]*commonpb.Payload
4141
}{
4242
{
4343
"no values",
4444
&commonpb.Header{
45-
Fields: map[string][]byte{},
45+
Fields: map[string]*commonpb.Payload{},
4646
},
4747
&commonpb.Header{
48-
Fields: map[string][]byte{},
48+
Fields: map[string]*commonpb.Payload{},
4949
},
50-
map[string][]byte{},
50+
map[string]*commonpb.Payload{},
5151
},
5252
{
5353
"add values",
5454
&commonpb.Header{
55-
Fields: map[string][]byte{},
55+
Fields: map[string]*commonpb.Payload{},
5656
},
5757
&commonpb.Header{
58-
Fields: map[string][]byte{
59-
"key1": []byte("val1"),
60-
"key2": []byte("val2"),
58+
Fields: map[string]*commonpb.Payload{
59+
"key1": encodeString(t, "val1"),
60+
"key2": encodeString(t, "val2"),
6161
},
6262
},
63-
map[string][]byte{
64-
"key1": []byte("val1"),
65-
"key2": []byte("val2"),
63+
map[string]*commonpb.Payload{
64+
"key1": encodeString(t, "val1"),
65+
"key2": encodeString(t, "val2"),
6666
},
6767
},
6868
{
6969
"overwrite values",
7070
&commonpb.Header{
71-
Fields: map[string][]byte{
72-
"key1": []byte("unexpected"),
71+
Fields: map[string]*commonpb.Payload{
72+
"key1": encodeString(t, "unexpected"),
7373
},
7474
},
7575
&commonpb.Header{
76-
Fields: map[string][]byte{
77-
"key1": []byte("val1"),
78-
"key2": []byte("val2"),
76+
Fields: map[string]*commonpb.Payload{
77+
"key1": encodeString(t, "val1"),
78+
"key2": encodeString(t, "val2"),
7979
},
8080
},
81-
map[string][]byte{
82-
"key1": []byte("val1"),
83-
"key2": []byte("val2"),
81+
map[string]*commonpb.Payload{
82+
"key1": encodeString(t, "val1"),
83+
"key2": encodeString(t, "val2"),
8484
},
8585
},
8686
}
@@ -98,6 +98,12 @@ func TestHeaderWriter(t *testing.T) {
9898
}
9999
}
100100

101+
func encodeString(t *testing.T, s string) *commonpb.Payload {
102+
p, err := DefaultDataConverter.ToData(s)
103+
assert.NoError(t, err)
104+
return p
105+
}
106+
101107
func TestHeaderReader(t *testing.T) {
102108
t.Parallel()
103109
tests := []struct {
@@ -109,9 +115,9 @@ func TestHeaderReader(t *testing.T) {
109115
{
110116
"valid values",
111117
&commonpb.Header{
112-
Fields: map[string][]byte{
113-
"key1": []byte("val1"),
114-
"key2": []byte("val2"),
118+
Fields: map[string]*commonpb.Payload{
119+
"key1": encodeString(t, "val1"),
120+
"key2": encodeString(t, "val2"),
115121
},
116122
},
117123
map[string]struct{}{"key1": {}, "key2": {}},
@@ -120,9 +126,9 @@ func TestHeaderReader(t *testing.T) {
120126
{
121127
"invalid values",
122128
&commonpb.Header{
123-
Fields: map[string][]byte{
124-
"key1": []byte("val1"),
125-
"key2": []byte("val2"),
129+
Fields: map[string]*commonpb.Payload{
130+
"key1": encodeString(t, "val1"),
131+
"key2": encodeString(t, "val2"),
126132
},
127133
},
128134
map[string]struct{}{"key2": {}},
@@ -135,7 +141,7 @@ func TestHeaderReader(t *testing.T) {
135141
t.Run(test.name, func(t *testing.T) {
136142
t.Parallel()
137143
reader := NewHeaderReader(test.header)
138-
err := reader.ForEachKey(func(key string, val []byte) error {
144+
err := reader.ForEachKey(func(key string, _ *commonpb.Payload) error {
139145
if _, ok := test.keys[key]; !ok {
140146
return assert.AnError
141147
}

internal/internal_workflow.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1194,7 +1194,7 @@ func getContextPropagatorsFromWorkflowContext(ctx Context) []ContextPropagator {
11941194

11951195
func getHeadersFromContext(ctx Context) *commonpb.Header {
11961196
header := &commonpb.Header{
1197-
Fields: make(map[string][]byte),
1197+
Fields: make(map[string]*commonpb.Payload),
11981198
}
11991199
contextPropagators := getContextPropagatorsFromWorkflowContext(ctx)
12001200
for _, ctxProp := range contextPropagators {

internal/internal_workflow_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ func (wc *WorkflowClient) CloseConnection() error {
948948

949949
func (wc *WorkflowClient) getWorkflowHeader(ctx context.Context) *commonpb.Header {
950950
header := &commonpb.Header{
951-
Fields: make(map[string][]byte),
951+
Fields: make(map[string]*commonpb.Payload),
952952
}
953953
writer := NewHeaderWriter(header)
954954
for _, ctxProp := range wc.contextPropagators {

internal/internal_workflow_client_test.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,11 @@ func (s *stringMapPropagator) Inject(ctx context.Context, writer HeaderWriter) e
9292
if !ok {
9393
return fmt.Errorf("unable to extract key from context %v", key)
9494
}
95-
writer.Set(key, []byte(value))
95+
encodedValue, err := DefaultDataConverter.ToData(value)
96+
if err != nil {
97+
return err
98+
}
99+
writer.Set(key, encodedValue)
96100
}
97101
return nil
98102
}
@@ -104,16 +108,25 @@ func (s *stringMapPropagator) InjectFromWorkflow(ctx Context, writer HeaderWrite
104108
if !ok {
105109
return fmt.Errorf("unable to extract key from context %v", key)
106110
}
107-
writer.Set(key, []byte(value))
111+
encodedValue, err := DefaultDataConverter.ToData(value)
112+
if err != nil {
113+
return err
114+
}
115+
writer.Set(key, encodedValue)
108116
}
109117
return nil
110118
}
111119

112120
// Extract extracts values from headers and puts them into context
113121
func (s *stringMapPropagator) Extract(ctx context.Context, reader HeaderReader) (context.Context, error) {
114-
if err := reader.ForEachKey(func(key string, value []byte) error {
122+
if err := reader.ForEachKey(func(key string, value *commonpb.Payload) error {
115123
if _, ok := s.keys[key]; ok {
116-
ctx = context.WithValue(ctx, contextKey(key), string(value))
124+
var decodedValue string
125+
err := DefaultDataConverter.FromData(value, &decodedValue)
126+
if err != nil {
127+
return err
128+
}
129+
ctx = context.WithValue(ctx, contextKey(key), decodedValue)
117130
}
118131
return nil
119132
}); err != nil {
@@ -124,9 +137,14 @@ func (s *stringMapPropagator) Extract(ctx context.Context, reader HeaderReader)
124137

125138
// ExtractToWorkflow extracts values from headers and puts them into context
126139
func (s *stringMapPropagator) ExtractToWorkflow(ctx Context, reader HeaderReader) (Context, error) {
127-
if err := reader.ForEachKey(func(key string, value []byte) error {
140+
if err := reader.ForEachKey(func(key string, value *commonpb.Payload) error {
128141
if _, ok := s.keys[key]; ok {
129-
ctx = WithValue(ctx, contextKey(key), string(value))
142+
var decodedValue string
143+
err := DefaultDataConverter.FromData(value, &decodedValue)
144+
if err != nil {
145+
return err
146+
}
147+
ctx = WithValue(ctx, contextKey(key), decodedValue)
130148
}
131149
return nil
132150
}); err != nil {

internal/internal_workflow_testsuite_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (s *WorkflowTestSuiteUnitTest) SetupSuite() {
6161
ScheduleToCloseTimeout: 3 * time.Second,
6262
}
6363
s.header = &commonpb.Header{
64-
Fields: map[string][]byte{"test": []byte("test-data")},
64+
Fields: map[string]*commonpb.Payload{"test": encodeString(s.T(), "test-data")},
6565
}
6666
s.contextPropagators = []ContextPropagator{NewStringMapPropagator([]string{"test"})}
6767
}
@@ -387,8 +387,8 @@ func (s *WorkflowTestSuiteUnitTest) Test_ActivityWithHeaderContext() {
387387
}
388388

389389
s.SetHeader(&commonpb.Header{
390-
Fields: map[string][]byte{
391-
testHeader: []byte("test-data"),
390+
Fields: map[string]*commonpb.Payload{
391+
testHeader: encodeString(s.T(), "test-data"),
392392
},
393393
})
394394

@@ -1609,8 +1609,8 @@ func (s *WorkflowTestSuiteUnitTest) Test_WorkflowHeaderContext() {
16091609

16101610
s.SetContextPropagators([]ContextPropagator{NewStringMapPropagator([]string{testHeader})})
16111611
s.SetHeader(&commonpb.Header{
1612-
Fields: map[string][]byte{
1613-
testHeader: []byte("test-data"),
1612+
Fields: map[string]*commonpb.Payload{
1613+
testHeader: encodeString(s.T(), "test-data"),
16141614
},
16151615
})
16161616

internal/tracer.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,38 @@ import (
2828
"context"
2929

3030
"github.com/opentracing/opentracing-go"
31+
commonpb "go.temporal.io/temporal-proto/common"
3132
"go.uber.org/zap"
3233
)
3334

3435
type tracingReader struct {
3536
reader HeaderReader
3637
}
3738

39+
// This is important requirement for t.tracer.Extract to work.
40+
var _ opentracing.TextMapReader = (*tracingReader)(nil)
41+
3842
func (t tracingReader) ForeachKey(handler func(key, val string) error) error {
39-
return t.reader.ForEachKey(func(k string, v []byte) error {
40-
return handler(k, string(v))
43+
return t.reader.ForEachKey(func(k string, v *commonpb.Payload) error {
44+
var decodedValue string
45+
err := DefaultDataConverter.FromData(v, &decodedValue)
46+
if err != nil {
47+
return err
48+
}
49+
return handler(k, decodedValue)
4150
})
4251
}
4352

4453
type tracingWriter struct {
4554
writer HeaderWriter
4655
}
4756

57+
// This is important requirement for t.tracer.Inject to work.
58+
var _ opentracing.TextMapWriter = (*tracingWriter)(nil)
59+
4860
func (t tracingWriter) Set(key, val string) {
49-
t.writer.Set(key, []byte(val))
61+
encodedValue, _ := DefaultDataConverter.ToData(val)
62+
t.writer.Set(key, encodedValue)
5063
}
5164

5265
// tracingContextPropagator implements the ContextPropagator interface for

0 commit comments

Comments
 (0)