2016-11-05 20:12:24 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2018-03-29 11:20:55 +02:00
|
|
|
// PanicAfter throws panic() after waitSeconds, unless abort channel receives
|
|
|
|
// notification.
|
|
|
|
func PanicAfter(waitSeconds time.Duration, abort chan struct{}, desc string) {
|
2016-11-05 20:12:24 +03:00
|
|
|
go func() {
|
2018-03-29 11:20:55 +02:00
|
|
|
select {
|
|
|
|
case <-abort:
|
2016-11-05 20:12:24 +03:00
|
|
|
return
|
2018-03-29 11:20:55 +02:00
|
|
|
case <-time.After(waitSeconds):
|
|
|
|
panic("whatever you were doing takes toooo long: " + desc)
|
2016-11-05 20:12:24 +03:00
|
|
|
}
|
|
|
|
}()
|
2017-08-04 23:14:17 +07:00
|
|
|
}
|
|
|
|
|
2018-03-29 11:20:55 +02:00
|
|
|
// ParseJSONArray parses JSON array into Go array of string.
|
|
|
|
func ParseJSONArray(items string) ([]string, error) {
|
|
|
|
var parsedItems []string
|
|
|
|
err := json.Unmarshal([]byte(items), &parsedItems)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-05-03 17:24:48 +03:00
|
|
|
}
|
2017-03-16 00:03:01 +03:00
|
|
|
|
2018-03-29 11:20:55 +02:00
|
|
|
return parsedItems, nil
|
2017-08-10 17:31:29 +02:00
|
|
|
}
|