github.com/songzhibin97/gkit@v1.2.13/distributed/task/result.go (about) 1 package task 2 3 import ( 4 "fmt" 5 "reflect" 6 "strings" 7 ) 8 9 // Result 任务返回携带的kv键值对 10 type Result struct { 11 // Type 标注返回的类型 12 Type string `json:"type" bson:"type"` 13 // Value 根据type解压value 14 Value interface{} `json:"value" bson:"value"` 15 } 16 17 // ConvertResult 将Result类型转换成reflect.Value 18 func ConvertResult(result []*Result) ([]reflect.Value, error) { 19 convertResult := make([]reflect.Value, 0, len(result)) 20 for _, r := range result { 21 _value, err := ReflectValue(r.Type, r.Value) 22 if err != nil { 23 return nil, err 24 } 25 convertResult = append(convertResult, _value) 26 } 27 return convertResult, nil 28 } 29 30 // FormatResult 将reflect.Value转换为可读答案 31 func FormatResult(values []reflect.Value) string { 32 ln := len(values) 33 switch ln { 34 case 0: 35 return "" 36 case 1: 37 return fmt.Sprintf("[%v]", values[0].Interface()) 38 default: 39 builder := strings.Builder{} 40 for i, value := range values { 41 if i == 0 { 42 builder.WriteString("[ ") 43 } 44 builder.WriteString(fmt.Sprintf("%v", value.Interface())) 45 if i != ln-1 { 46 builder.WriteString(", ") 47 } else { 48 builder.WriteString(" ]") 49 } 50 } 51 return builder.String() 52 } 53 }