123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- package main
- func printAccount(session *lmaxapi.Session) {
- account := session.GetOrderBook().GetAccount()
- fmt.Println(account)
- }
- func printDetail(session *lmaxapi.Session) {
- account := session.GetOrderBook().GetAccount()
- printStruct(account)
- fmt.Println("..............")
- position := session.GetOrderBook().GetPositions(false)
- for i := 0; i < len(position); i++ {
- printStruct(position[i])
- fmt.Println("------")
- }
- fmt.Println("..............")
- orders := session.GetOrderBook().GetOrders(false)
- for i := 0; i < len(orders); i++ {
- printStruct(orders[i])
- fmt.Println("------")
- }
- fmt.Println("..............")
- }
- func printStruct(i interface{}) {
- if i == nil {
- return
- }
- k, v, err := structKV(i, true)
- if err != nil {
- fmt.Println(err)
- return
- }
- for i := 0; i < len(k); i++ {
- fmt.Println(k[i], " = ", v[i])
- }
- }
- func structKV(i interface{}, recursion bool) ([]string, []string, error) {
- values := reflect.ValueOf(i)
- if values.Kind() == reflect.Ptr {
- if values.IsNil() {
- return nil, nil, fmt.Errorf("StructKeyValues: nil struct")
- }
- return structKV(values.Elem().Interface(), recursion)
- }
- if values.Kind() != reflect.Struct {
- return nil, nil, fmt.Errorf("StructKeyValues: in params not a struct")
- }
- types := reflect.TypeOf(i)
- num := types.NumField()
- keys := make([]string, 0)
- elms := make([]string, 0)
- for i := 0; i < num; i++ {
- elm := values.Field(i)
- if elm.CanInterface() {
- if recursion && elm.Kind() == reflect.Struct {
- k, v, err := structKV(elm.Interface(), recursion)
- if err == nil && len(k) > 0 {
- keys = append(keys, k...)
- elms = append(elms, v...)
- }
- continue
- }
- field := types.Field(i)
- keys = append(keys, field.Name)
- elms = append(elms, varToString(elm.Interface()))
- }
- }
- return keys, elms, nil
- }
- func varToString(data interface{}) string {
- values := reflect.ValueOf(data)
- kind := values.Kind()
- if kind == reflect.String {
- return data.(string)
- }
- return fmt.Sprint(data)
- }
|