From e31398ee4bad4a703d74980d453ab550633871a6 Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Mon, 27 Nov 2023 07:37:30 -0500 Subject: [PATCH] No need to pass data converter to string formatting routine There is only one possibility for the data converter; users don't get to set it. --- common/stringify/stringify.go | 17 +++++++++-------- common/stringify/stringify_test.go | 29 ++++++++++++----------------- workflow/workflow_commands.go | 11 +++++------ 3 files changed, 26 insertions(+), 31 deletions(-) diff --git a/common/stringify/stringify.go b/common/stringify/stringify.go index 1aedf4c2..18c8cf41 100644 --- a/common/stringify/stringify.go +++ b/common/stringify/stringify.go @@ -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 "" @@ -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 @@ -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 } @@ -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 } @@ -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(',') } diff --git a/common/stringify/stringify_test.go b/common/stringify/stringify_test.go index 814a9360..f2c5822b 100644 --- a/common/stringify/stringify_test.go +++ b/common/stringify/stringify_test.go @@ -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 } @@ -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) @@ -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) } diff --git a/workflow/workflow_commands.go b/workflow/workflow_commands.go index 0559b83c..3bcc21a5 100644 --- a/workflow/workflow_commands.go +++ b/workflow/workflow_commands.go @@ -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" @@ -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) } @@ -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) } @@ -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")) @@ -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) } } @@ -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{} {