github.com/muxinc/mux-go@v1.1.1/model_get_metric_timeseries_data_response.go (about)

     1  // Mux Go - Copyright 2019 Mux Inc.
     2  // NOTE: This file is auto generated. Do not edit this file manually.
     3  
     4  package muxgo
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  )
    10  
    11  type GetMetricTimeseriesDataResponse struct {
    12  	Data          [][]string `json:"data,omitempty"`
    13  	TotalRowCount int64      `json:"total_row_count,omitempty"`
    14  	Timeframe     []int64    `json:"timeframe,omitempty"`
    15  }
    16  
    17  // !!! 🐉 Here be dragons 🐉 !!!
    18  // We use a custom Unmarshal to work around one awkward API call where we can't model the response
    19  // from the API elegantly since go doesn't have heterogeneous arrays. This isn't perfect, or memory
    20  // friendly, but it works.
    21  func (this *GetMetricTimeseriesDataResponse) UnmarshalJSON(data []byte) error {
    22  
    23  	// Unmarshal JSON into a string => interface{} map
    24  	var result map[string]interface{}
    25  	json.Unmarshal(data, &result)
    26  
    27  	// Build up a new list of each of the datapoints from data as [][]string, nil checking as we go
    28  	datapoints := [][]string{}
    29  	for _, node := range result["data"].([]interface{}) {
    30  		nodeAsArray := node.([]interface{})
    31  		d := make([]string, 3)
    32  		d[0] = nodeAsArray[0].(string)
    33  		if nodeAsArray[1] != nil {
    34  			d[1] = fmt.Sprintf("%f", nodeAsArray[1].(float64))
    35  		}
    36  		if nodeAsArray[2] != nil {
    37  			d[2] = fmt.Sprintf("%f", nodeAsArray[2].(float64))
    38  		}
    39  		datapoints = append(datapoints, d)
    40  	}
    41  
    42  	// Build the array of timeframe
    43  	timeframes := []int64{}
    44  	for _, time := range result["timeframe"].([]interface{}) {
    45  		timefloat := time.(float64)
    46  		timeframes = append(timeframes, int64(timefloat))
    47  	}
    48  
    49  	// Set the fields on the response object to what we've pieced together
    50  	this.Data = datapoints
    51  	this.Timeframe = timeframes
    52  	this.TotalRowCount = int64(result["total_row_count"].(float64))
    53  
    54  	return nil
    55  }