package handlers

import (
	"io"
    "encoding/json"
    "strings"
    "strconv"
    "math/rand"
    "time"
    "github.com/nelsonleduc/calmanbot/handlers/models"
)

func ParseJSON(bytes []byte, path string) string {
    
    var stuff interface{}

    json.Unmarshal(bytes, &stuff)
    elements := strings.Split(path, ".")

    for _, el := range elements {
        
        converted := ConvertedComponent(el, stuff)
        num, err := strconv.ParseInt(converted, 10, 64)
        
        if err == nil {
            arr := stuff.([]interface{})
            stuff = arr[num]
        } else {
            switch t := stuff.(type) {
            case map[string]interface{}:
                stuff = t[converted]
            default:
                stuff = ""
                break
            }

        }
    }
    
    return stuff.(string)
}

func ParseMessageJSON(reader io.Reader) models.Message {
    message := new(models.Message)
    json.NewDecoder(reader).Decode(message)
    
    return *message
}

func ConvertedComponent(s string, stuff interface{}) string {
    
    if s == "{_randomInt_}" {
        switch t := stuff.(type) {
        case []interface{}:
            rand.Seed(time.Now().UnixNano())
            num := rand.Intn(len(t))
            return strconv.Itoa(num)
        default:
            return s
        }
    }
    
    return s
}
package handlers

import (
	"io"
    "encoding/json"
    "strings"
    "strconv"
    "math/rand"
    "time"
    "github.com/nelsonleduc/calmanbot/handlers/models"
)

func ParseJSON(bytes []byte, path string) string {
    
    var stuff interface{}

    json.Unmarshal(bytes, &stuff)
    elements := strings.Split(path, ".")

    for _, el := range elements {
        
        converted := ConvertedComponent(el, stuff)
        num, err := strconv.ParseInt(converted, 10, 64)
        
        if err == nil {
            if num < 0 {
                stuff = ""
                break
            }
            
            arr := stuff.([]interface{})
            stuff = arr[num]
        } else {
            switch t := stuff.(type) {
            case map[string]interface{}:
                stuff = t[converted]
            default:
                stuff = ""
                break
            }

        }
    }
    
    return stuff.(string)
}

func ParseMessageJSON(reader io.Reader) models.Message {
    message := new(models.Message)
    json.NewDecoder(reader).Decode(message)
    
    return *message
}

func ConvertedComponent(s string, stuff interface{}) string {
    
    if s == "{_randomInt_}" {
        switch t := stuff.(type) {
        case []interface{}:
            length := len(t)
            var num int
            if length > 0 {
                rand.Seed(time.Now().UnixNano())
                num = rand.Intn(length)
            } else {
                num = -1
            }
            return strconv.Itoa(num)
        default:
            return s
        }
    }
    
    return s
}
Fix crash with empty array
Fix crash with empty array
