github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/pkg/models/status/status.go (about)

     1  package status
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strings"
     7  
     8  	"github.com/bitfinexcom/bitfinex-api-go/pkg/convert"
     9  )
    10  
    11  // FromWSRaw - based on condition will return snapshot or single record of
    12  // derivative or liquidation data structure
    13  func FromWSRaw(key string, data []interface{}) (interface{}, error) {
    14  	if len(data) == 0 {
    15  		return nil, errors.New("empty data slice")
    16  	}
    17  
    18  	_, isSnapshot := data[0].([]interface{})
    19  	ss := strings.SplitN(key, ":", 2)
    20  	if len(ss) < 2 {
    21  		return nil, fmt.Errorf("unexpected key: %s", key)
    22  	}
    23  
    24  	if isSnapshot && ss[0] == "deriv" {
    25  		return DerivSnapshotFromRaw(ss[1], convert.ToInterfaceArray(data))
    26  	}
    27  
    28  	if !isSnapshot && ss[0] == "deriv" {
    29  		return DerivFromRaw(ss[1], data)
    30  	}
    31  
    32  	if isSnapshot && ss[0] == "liq" {
    33  		return LiqSnapshotFromRaw(convert.ToInterfaceArray(data))
    34  	}
    35  
    36  	if !isSnapshot && ss[0] == "liq" {
    37  		return LiqFromRaw(data)
    38  	}
    39  
    40  	return nil, fmt.Errorf("%s: unrecognized data slice:%#v", key, data)
    41  }