Skip to content

Commit

Permalink
No need to pass data converter to string formatting routine
Browse files Browse the repository at this point in the history
There is only one possibility for the data converter; users don't get
to set it.
  • Loading branch information
dandavison committed Nov 27, 2023
1 parent bf6f66a commit e31398e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 31 deletions.
17 changes: 9 additions & 8 deletions common/stringify/stringify.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const (
maxWordLength = 120 // if text length is larger than maxWordLength, it will be inserted spaces
)

func AnyToString(val interface{}, printFully bool, maxFieldLength int, dc converter.DataConverter) string {
func AnyToString(val interface{}, printFully bool, maxFieldLength int) string {
dc := converter.GetDefaultDataConverter()
v := reflect.ValueOf(val)
if val == nil || (v.Kind() == reflect.Ptr && v.IsNil()) {
return ""
Expand Down Expand Up @@ -65,9 +66,9 @@ func AnyToString(val interface{}, printFully bool, maxFieldLength int, dc conver
return ""
case reflect.Slice:
// All but []byte which is already handled.
return sliceToString(v, printFully, maxFieldLength, dc)
return sliceToString(v, printFully, maxFieldLength)
case reflect.Ptr:
return AnyToString(v.Elem().Interface(), printFully, maxFieldLength, dc)
return AnyToString(v.Elem().Interface(), printFully, maxFieldLength)
case reflect.Map:
type keyValuePair struct {
key string
Expand All @@ -82,11 +83,11 @@ func AnyToString(val interface{}, printFully bool, maxFieldLength int, dc conver
if !mapKey.CanInterface() || !mapVal.CanInterface() {
continue
}
mapKeyStr := AnyToString(mapKey.Interface(), true, 0, dc)
mapKeyStr := AnyToString(mapKey.Interface(), true, 0)
if mapKeyStr == "" {
continue
}
mapValStr := AnyToString(mapVal.Interface(), true, 0, dc)
mapValStr := AnyToString(mapVal.Interface(), true, 0)
if mapValStr == "" {
continue
}
Expand Down Expand Up @@ -127,7 +128,7 @@ func AnyToString(val interface{}, printFully bool, maxFieldLength int, dc conver
}

fieldName := t.Field(i).Name
fieldStr := AnyToString(f.Interface(), printFully, maxFieldLength, dc)
fieldStr := AnyToString(f.Interface(), printFully, maxFieldLength)
if fieldStr == "" {
continue
}
Expand Down Expand Up @@ -168,12 +169,12 @@ func AnyToString(val interface{}, printFully bool, maxFieldLength int, dc conver
}
}

func sliceToString(slice reflect.Value, printFully bool, maxFieldLength int, dc converter.DataConverter) string {
func sliceToString(slice reflect.Value, printFully bool, maxFieldLength int) string {
var b strings.Builder
b.WriteRune('[')
for i := 0; i < slice.Len(); i++ {
if i == 0 || printFully {
b.WriteString(AnyToString(slice.Index(i).Interface(), printFully, maxFieldLength, dc))
b.WriteString(AnyToString(slice.Index(i).Interface(), printFully, maxFieldLength))
if i < slice.Len()-1 {
b.WriteRune(',')
}
Expand Down
29 changes: 12 additions & 17 deletions common/stringify/stringify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,11 @@ import (
historypb "go.temporal.io/api/history/v1"
taskqueuepb "go.temporal.io/api/taskqueue/v1"
workflowpb "go.temporal.io/api/workflow/v1"
"go.temporal.io/sdk/converter"
"go.temporal.io/server/common/payload"
"go.temporal.io/server/common/payloads"
"go.temporal.io/server/common/primitives/timestamp"
)

var (
dataConverter = converter.GetDefaultDataConverter()
)

type stringifySuite struct {
suite.Suite
}
Expand Down Expand Up @@ -63,7 +58,7 @@ func (s *stringifySuite) TestAnyToString() {
Input: payloads.EncodeString(arg),
}},
}
res := AnyToString(event, false, 500, dataConverter)
res := AnyToString(event, false, 500)
ss, l := tablewriter.WrapString(res, 10)
s.Equal(7, len(ss))
s.Equal(105, l)
Expand All @@ -77,49 +72,49 @@ func (s *stringifySuite) TestAnyToString_DecodeMapValues() {
Status: enumspb.WORKFLOW_EXECUTION_STATUS_RUNNING,
Memo: &commonpb.Memo{Fields: fields},
}
s.Equal(`{Status:Running, HistoryLength:0, Memo:{Fields:map{TestKey:"testValue"}}, StateTransitionCount:0, HistorySizeBytes:0}`, AnyToString(execution, true, 0, dataConverter))
s.Equal(`{Status:Running, HistoryLength:0, Memo:{Fields:map{TestKey:"testValue"}}, StateTransitionCount:0, HistorySizeBytes:0}`, AnyToString(execution, true, 0))

fields["TestKey2"] = payload.EncodeString("anotherTestValue")
execution.Memo = &commonpb.Memo{Fields: fields}
got := AnyToString(execution, true, 0, dataConverter)
got := AnyToString(execution, true, 0)
expected := `{Status:Running, HistoryLength:0, Memo:{Fields:map{TestKey:"testValue", TestKey2:"anotherTestValue"}}, StateTransitionCount:0, HistorySizeBytes:0}`
s.Equal(expected, got)
}

func (s *stringifySuite) TestAnyToString_Slice() {
var fields []string
got := AnyToString(fields, true, 0, dataConverter)
got := AnyToString(fields, true, 0)
s.Equal("[]", got)

fields = make([]string, 0)
got = AnyToString(fields, true, 0, dataConverter)
got = AnyToString(fields, true, 0)
s.Equal("[]", got)

fields = make([]string, 1)
got = AnyToString(fields, true, 0, dataConverter)
got = AnyToString(fields, true, 0)
s.Equal("[]", got)

fields[0] = "qwe"
got = AnyToString(fields, true, 0, dataConverter)
got = AnyToString(fields, true, 0)
s.Equal("[qwe]", got)
got = AnyToString(fields, false, 0, dataConverter)
got = AnyToString(fields, false, 0)
s.Equal("[qwe]", got)

fields = make([]string, 2)
fields[0] = "asd"
fields[1] = "zxc"
got = AnyToString(fields, true, 0, dataConverter)
got = AnyToString(fields, true, 0)
s.Equal("[asd,zxc]", got)
got = AnyToString(fields, false, 0, dataConverter)
got = AnyToString(fields, false, 0)
s.Equal("[asd,...1 more]", got)

fields = make([]string, 3)
fields[0] = "0"
fields[1] = "1"
fields[2] = "2"
got = AnyToString(fields, true, 0, dataConverter)
got = AnyToString(fields, true, 0)
s.Equal("[0,1,2]", got)
got = AnyToString(fields, false, 0, dataConverter)
got = AnyToString(fields, false, 0)
s.Equal("[0,...2 more]", got)

}
Expand Down
11 changes: 5 additions & 6 deletions workflow/workflow_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
workflowpb "go.temporal.io/api/workflow/v1"
"go.temporal.io/api/workflowservice/v1"
sdkclient "go.temporal.io/sdk/client"
"go.temporal.io/sdk/converter"
clispb "go.temporal.io/server/api/cli/v1"
scommon "go.temporal.io/server/common"
"go.temporal.io/server/common/backoff"
Expand Down Expand Up @@ -595,7 +594,7 @@ func queryWorkflowHelper(c *cli.Context, queryType string) error {
if queryResponse.QueryRejected != nil {
fmt.Printf("Query was rejected, workflow has status: %v\n", queryResponse.QueryRejected.GetStatus())
} else {
queryResult := stringify.AnyToString(queryResponse.QueryResult, true, 0, converter.GetDefaultDataConverter())
queryResult := stringify.AnyToString(queryResponse.QueryResult, true, 0)
fmt.Printf("Query result:\n%v\n", queryResult)
}

Expand Down Expand Up @@ -781,7 +780,7 @@ func convertDescribeWorkflowExecutionResponse(c *cli.Context, resp *workflowserv
}

if pendingActivity.GetHeartbeatDetails() != nil {
pendingActivityStr.HeartbeatDetails = stringify.AnyToString(pendingActivity.GetHeartbeatDetails(), true, 0, converter.GetDefaultDataConverter())
pendingActivityStr.HeartbeatDetails = stringify.AnyToString(pendingActivity.GetHeartbeatDetails(), true, 0)
}
pendingActivitiesStr = append(pendingActivitiesStr, pendingActivityStr)
}
Expand Down Expand Up @@ -835,7 +834,7 @@ func printRunStatus(c *cli.Context, event *historypb.HistoryEvent) {
switch event.GetEventType() {
case enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_COMPLETED:
fmt.Printf(" Status: %s\n", color.Green(c, "COMPLETED"))
result := stringify.AnyToString(event.GetWorkflowExecutionCompletedEventAttributes().GetResult(), true, 0, converter.GetDefaultDataConverter())
result := stringify.AnyToString(event.GetWorkflowExecutionCompletedEventAttributes().GetResult(), true, 0)
fmt.Printf(" Output: %s\n", result)
case enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_FAILED:
fmt.Printf(" Status: %s\n", color.Red(c, "FAILED"))
Expand All @@ -845,7 +844,7 @@ func printRunStatus(c *cli.Context, event *historypb.HistoryEvent) {
fmt.Printf(" Retry status: %s\n", event.GetWorkflowExecutionTimedOutEventAttributes().GetRetryState())
case enumspb.EVENT_TYPE_WORKFLOW_EXECUTION_CANCELED:
fmt.Printf(" Status: %s\n", color.Red(c, "CANCELED"))
details := stringify.AnyToString(event.GetWorkflowExecutionCanceledEventAttributes().GetDetails(), true, 0, converter.GetDefaultDataConverter())
details := stringify.AnyToString(event.GetWorkflowExecutionCanceledEventAttributes().GetDetails(), true, 0)
fmt.Printf(" Detail: %s\n", details)
}
}
Expand Down Expand Up @@ -1577,7 +1576,7 @@ func findWorkflowStatusValue(name string) (enumspb.WorkflowExecutionStatus, bool
// historyEventToString convert HistoryEvent to string
func historyEventToString(e *historypb.HistoryEvent, printFully bool, maxFieldLength int) string {
data := getEventAttributes(e)
return stringify.AnyToString(data, printFully, maxFieldLength, converter.GetDefaultDataConverter())
return stringify.AnyToString(data, printFully, maxFieldLength)
}

func getEventAttributes(e *historypb.HistoryEvent) interface{} {
Expand Down

0 comments on commit e31398e

Please sign in to comment.