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

     1  package pulse
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/bitfinexcom/bitfinex-api-go/pkg/convert"
     7  	"github.com/bitfinexcom/bitfinex-api-go/pkg/models/pulseprofile"
     8  )
     9  
    10  // Pulse message data structure
    11  type Pulse struct {
    12  	ID               string                     `json:"id,omitempty"`
    13  	Parent           string                     `json:"parent,omitempty"`
    14  	MTS              int64                      `json:"mts,omitempty"`
    15  	UserID           string                     `json:"userId,omitempty"`
    16  	Title            string                     `json:"title,omitempty"`
    17  	Content          string                     `json:"content,omitempty"`
    18  	IsPin            int                        `json:"isPin"`
    19  	IsPublic         int                        `json:"isPublic"`
    20  	CommentsDisabled int                        `json:"commentsDisabled"`
    21  	Tags             []string                   `json:"tags,omitempty"`
    22  	Attachments      []string                   `json:"attachments,omitempty"`
    23  	Likes            int                        `json:"likes,omitempty"`
    24  	PulseProfile     *pulseprofile.PulseProfile `json:"pulseProfile,omitempty"`
    25  	Comments         int
    26  }
    27  
    28  var pulseFields = map[string]int{
    29  	"ID":  0,
    30  	"Mts": 1,
    31  	// "PLACEHOLDER": 2,
    32  	"UserID": 3,
    33  	// "PLACEHOLDER": 4,
    34  	"Title":   5,
    35  	"Content": 6,
    36  	// "PLACEHOLDER": 7,
    37  	// "PLACEHOLDER": 8,
    38  	"IsPin":            9,
    39  	"IsPublic":         10,
    40  	"CommentsDisabled": 11,
    41  	"Tags":             12,
    42  	"Attachments":      13,
    43  	"Meta":             14,
    44  	"Likes":            15,
    45  	// "PLACEHOLDER": 16,
    46  	// "PLACEHOLDER": 17,
    47  	"PulseProfile": 18,
    48  	"Comments":     19,
    49  	// "PLACEHOLDER": 20,
    50  	// "PLACEHOLDER": 21,
    51  }
    52  
    53  // FromRaw returns pointer to Pulse message
    54  func FromRaw(raw []interface{}) (*Pulse, error) {
    55  	if len(raw) < 19 {
    56  		return nil, fmt.Errorf("data slice too short for Pulse Message: %#v", raw)
    57  	}
    58  
    59  	p := &Pulse{}
    60  	var err error
    61  
    62  	p.ID = convert.SValOrEmpty(raw[pulseFields["ID"]])
    63  	p.MTS = convert.I64ValOrZero(raw[pulseFields["Mts"]])
    64  	p.UserID = convert.SValOrEmpty(raw[pulseFields["UserID"]])
    65  	p.Title = convert.SValOrEmpty(raw[pulseFields["Title"]])
    66  	p.Content = convert.SValOrEmpty(raw[pulseFields["Content"]])
    67  	p.IsPin = convert.ToInt(raw[pulseFields["IsPin"]])
    68  	p.IsPublic = convert.ToInt(raw[pulseFields["IsPublic"]])
    69  	p.CommentsDisabled = convert.ToInt(raw[pulseFields["CommentsDisabled"]])
    70  	p.Likes = convert.ToInt(raw[pulseFields["Likes"]])
    71  	p.Comments = convert.ToInt(raw[pulseFields["Comments"]])
    72  
    73  	p.Tags, err = convert.ItfToStrSlice(raw[pulseFields["Tags"]])
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	p.Attachments, err = convert.ItfToStrSlice(raw[pulseFields["Attachments"]])
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	rawProfile := raw[pulseFields["PulseProfile"]]
    84  	rawProfileItf, ok := rawProfile.([]interface{})
    85  	if !ok {
    86  		return p, nil
    87  	}
    88  
    89  	profilePayload := convert.ToInterfaceArray(rawProfileItf)
    90  	if len(profilePayload) < 1 {
    91  		return p, nil
    92  	}
    93  
    94  	p.PulseProfile, err = pulseprofile.NewFromRaw(profilePayload[0])
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  
    99  	return p, nil
   100  }
   101  
   102  // SnapshotFromRaw returns slice of Pulse message pointers
   103  func SnapshotFromRaw(raws []interface{}) ([]*Pulse, error) {
   104  	if len(raws) < 1 {
   105  		return nil, fmt.Errorf("data slice is too short for Pulse History: %#v", raws)
   106  	}
   107  
   108  	res := []*Pulse{}
   109  
   110  	for _, raw := range raws {
   111  		raw := raw.([]interface{})
   112  		p, err := FromRaw(raw)
   113  		if err != nil {
   114  			return nil, err
   115  		}
   116  
   117  		res = append(res, p)
   118  	}
   119  
   120  	return res, nil
   121  }