go.uber.org/cadence@v1.2.9/internal/common/util/stringer.go (about)

     1  // Copyright (c) 2017 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 util
    22  
    23  import (
    24  	"bytes"
    25  	"fmt"
    26  	"reflect"
    27  
    28  	s "go.uber.org/cadence/.gen/go/shared"
    29  )
    30  
    31  func anyToString(d interface{}) string {
    32  	v := reflect.ValueOf(d)
    33  	switch v.Kind() {
    34  	case reflect.Ptr:
    35  		return anyToString(v.Elem().Interface())
    36  	case reflect.Struct:
    37  		var buf bytes.Buffer
    38  		t := reflect.TypeOf(d)
    39  		buf.WriteString("(")
    40  		for i := 0; i < v.NumField(); i++ {
    41  			f := v.Field(i)
    42  			if f.Kind() == reflect.Invalid {
    43  				continue
    44  			}
    45  			fieldValue := valueToString(f)
    46  			if len(fieldValue) == 0 {
    47  				continue
    48  			}
    49  			if buf.Len() > 1 {
    50  				buf.WriteString(", ")
    51  			}
    52  			buf.WriteString(fmt.Sprintf("%s:%s", t.Field(i).Name, fieldValue))
    53  		}
    54  		buf.WriteString(")")
    55  		return buf.String()
    56  	default:
    57  		return fmt.Sprint(d)
    58  	}
    59  }
    60  
    61  func valueToString(v reflect.Value) string {
    62  	switch v.Kind() {
    63  	case reflect.Ptr:
    64  		return valueToString(v.Elem())
    65  	case reflect.Struct:
    66  		return anyToString(v.Interface())
    67  	case reflect.Invalid:
    68  		return ""
    69  	case reflect.Slice:
    70  		if v.Type().Elem().Kind() == reflect.Uint8 {
    71  			return fmt.Sprintf("[%v]", string(v.Bytes()))
    72  		}
    73  		return fmt.Sprintf("[len=%d]", v.Len())
    74  	default:
    75  		return fmt.Sprint(v.Interface())
    76  	}
    77  }
    78  
    79  // HistoryEventToString convert HistoryEvent to string
    80  func HistoryEventToString(e *s.HistoryEvent) string {
    81  	var data interface{}
    82  	switch e.GetEventType() {
    83  	case s.EventTypeWorkflowExecutionStarted:
    84  		data = e.WorkflowExecutionStartedEventAttributes
    85  
    86  	case s.EventTypeWorkflowExecutionCompleted:
    87  		data = e.WorkflowExecutionCompletedEventAttributes
    88  
    89  	case s.EventTypeWorkflowExecutionFailed:
    90  		data = e.WorkflowExecutionFailedEventAttributes
    91  
    92  	case s.EventTypeWorkflowExecutionTimedOut:
    93  		data = e.WorkflowExecutionTimedOutEventAttributes
    94  
    95  	case s.EventTypeDecisionTaskScheduled:
    96  		data = e.DecisionTaskScheduledEventAttributes
    97  
    98  	case s.EventTypeDecisionTaskStarted:
    99  		data = e.DecisionTaskStartedEventAttributes
   100  
   101  	case s.EventTypeDecisionTaskCompleted:
   102  		data = e.DecisionTaskCompletedEventAttributes
   103  
   104  	case s.EventTypeDecisionTaskTimedOut:
   105  		data = e.DecisionTaskTimedOutEventAttributes
   106  
   107  	case s.EventTypeActivityTaskScheduled:
   108  		data = e.ActivityTaskScheduledEventAttributes
   109  
   110  	case s.EventTypeActivityTaskStarted:
   111  		data = e.ActivityTaskStartedEventAttributes
   112  
   113  	case s.EventTypeActivityTaskCompleted:
   114  		data = e.ActivityTaskCompletedEventAttributes
   115  
   116  	case s.EventTypeActivityTaskFailed:
   117  		data = e.ActivityTaskFailedEventAttributes
   118  
   119  	case s.EventTypeActivityTaskTimedOut:
   120  		data = e.ActivityTaskTimedOutEventAttributes
   121  
   122  	case s.EventTypeActivityTaskCancelRequested:
   123  		data = e.ActivityTaskCancelRequestedEventAttributes
   124  
   125  	case s.EventTypeRequestCancelActivityTaskFailed:
   126  		data = e.RequestCancelActivityTaskFailedEventAttributes
   127  
   128  	case s.EventTypeActivityTaskCanceled:
   129  		data = e.ActivityTaskCanceledEventAttributes
   130  
   131  	case s.EventTypeTimerStarted:
   132  		data = e.TimerStartedEventAttributes
   133  
   134  	case s.EventTypeTimerFired:
   135  		data = e.TimerFiredEventAttributes
   136  
   137  	case s.EventTypeCancelTimerFailed:
   138  		data = e.CancelTimerFailedEventAttributes
   139  
   140  	case s.EventTypeTimerCanceled:
   141  		data = e.TimerCanceledEventAttributes
   142  
   143  	case s.EventTypeMarkerRecorded:
   144  		data = e.MarkerRecordedEventAttributes
   145  
   146  	case s.EventTypeWorkflowExecutionTerminated:
   147  		data = e.WorkflowExecutionTerminatedEventAttributes
   148  
   149  	default:
   150  		data = e
   151  	}
   152  
   153  	return e.GetEventType().String() + ": " + anyToString(data)
   154  }
   155  
   156  // DecisionToString convert Decision to string
   157  func DecisionToString(d *s.Decision) string {
   158  	var data interface{}
   159  	switch d.GetDecisionType() {
   160  	case s.DecisionTypeScheduleActivityTask:
   161  		data = d.ScheduleActivityTaskDecisionAttributes
   162  
   163  	case s.DecisionTypeRequestCancelActivityTask:
   164  		data = d.RequestCancelActivityTaskDecisionAttributes
   165  
   166  	case s.DecisionTypeStartTimer:
   167  		data = d.StartTimerDecisionAttributes
   168  
   169  	case s.DecisionTypeCancelTimer:
   170  		data = d.CancelTimerDecisionAttributes
   171  
   172  	case s.DecisionTypeCompleteWorkflowExecution:
   173  		data = d.CompleteWorkflowExecutionDecisionAttributes
   174  
   175  	case s.DecisionTypeFailWorkflowExecution:
   176  		data = d.FailWorkflowExecutionDecisionAttributes
   177  
   178  	case s.DecisionTypeRecordMarker:
   179  		data = d.RecordMarkerDecisionAttributes
   180  
   181  	default:
   182  		data = d
   183  	}
   184  
   185  	return d.GetDecisionType().String() + ": " + anyToString(data)
   186  }