print.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package main
  2. func printAccount(session *lmaxapi.Session) {
  3. account := session.GetOrderBook().GetAccount()
  4. fmt.Println(account)
  5. }
  6. func printDetail(session *lmaxapi.Session) {
  7. account := session.GetOrderBook().GetAccount()
  8. printStruct(account)
  9. fmt.Println("..............")
  10. position := session.GetOrderBook().GetPositions(false)
  11. for i := 0; i < len(position); i++ {
  12. printStruct(position[i])
  13. fmt.Println("------")
  14. }
  15. fmt.Println("..............")
  16. orders := session.GetOrderBook().GetOrders(false)
  17. for i := 0; i < len(orders); i++ {
  18. printStruct(orders[i])
  19. fmt.Println("------")
  20. }
  21. fmt.Println("..............")
  22. }
  23. func printStruct(i interface{}) {
  24. if i == nil {
  25. return
  26. }
  27. k, v, err := structKV(i, true)
  28. if err != nil {
  29. fmt.Println(err)
  30. return
  31. }
  32. for i := 0; i < len(k); i++ {
  33. fmt.Println(k[i], " = ", v[i])
  34. }
  35. }
  36. func structKV(i interface{}, recursion bool) ([]string, []string, error) {
  37. values := reflect.ValueOf(i)
  38. if values.Kind() == reflect.Ptr {
  39. if values.IsNil() {
  40. return nil, nil, fmt.Errorf("StructKeyValues: nil struct")
  41. }
  42. return structKV(values.Elem().Interface(), recursion)
  43. }
  44. if values.Kind() != reflect.Struct {
  45. return nil, nil, fmt.Errorf("StructKeyValues: in params not a struct")
  46. }
  47. types := reflect.TypeOf(i)
  48. num := types.NumField()
  49. keys := make([]string, 0)
  50. elms := make([]string, 0)
  51. for i := 0; i < num; i++ {
  52. elm := values.Field(i)
  53. if elm.CanInterface() {
  54. if recursion && elm.Kind() == reflect.Struct {
  55. k, v, err := structKV(elm.Interface(), recursion)
  56. if err == nil && len(k) > 0 {
  57. keys = append(keys, k...)
  58. elms = append(elms, v...)
  59. }
  60. continue
  61. }
  62. field := types.Field(i)
  63. keys = append(keys, field.Name)
  64. elms = append(elms, varToString(elm.Interface()))
  65. }
  66. }
  67. return keys, elms, nil
  68. }
  69. func varToString(data interface{}) string {
  70. values := reflect.ValueOf(data)
  71. kind := values.Kind()
  72. if kind == reflect.String {
  73. return data.(string)
  74. }
  75. return fmt.Sprint(data)
  76. }