/
util.go
65 lines (57 loc) · 1.74 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package util
import (
"fmt"
"os"
"strings"
"unicode"
"unicode/utf8"
"github.com/algorand/go-codec/codec"
)
// PrintableUTF8OrEmpty checks to see if the entire string is a UTF8 printable string.
// If this is the case, the string is returned as is. Otherwise, the empty string is returned.
func PrintableUTF8OrEmpty(in string) string {
// iterate throughout all the characters in the string to see if they are all printable.
// when range iterating on go strings, go decode each element as a utf8 rune.
for _, c := range in {
// is this a printable character, or invalid rune ?
if c == utf8.RuneError || !unicode.IsPrint(c) {
return ""
}
}
return in
}
// KeysStringBool returns all of the keys in the map joined by a comma.
func KeysStringBool(m map[string]bool) string {
keys := make([]string, 0, len(m))
for k := range m {
keys = append(keys, k)
}
return strings.Join(keys, ", ")
}
// MaybeFail exits if there was an error.
func MaybeFail(err error, errfmt string, params ...interface{}) {
if err == nil {
return
}
fmt.Fprintf(os.Stderr, errfmt, params...)
fmt.Fprintf(os.Stderr, "\nError: %v\n", err)
os.Exit(1)
}
var oneLineJSONCodecHandle *codec.JsonHandle
// JSONOneLine converts an object into JSON
func JSONOneLine(obj interface{}) string {
var b []byte
enc := codec.NewEncoderBytes(&b, oneLineJSONCodecHandle)
enc.MustEncode(obj)
return string(b)
}
func init() {
oneLineJSONCodecHandle = new(codec.JsonHandle)
oneLineJSONCodecHandle.ErrorIfNoField = true
oneLineJSONCodecHandle.ErrorIfNoArrayExpand = true
oneLineJSONCodecHandle.Canonical = true
oneLineJSONCodecHandle.RecursiveEmptyCheck = true
oneLineJSONCodecHandle.HTMLCharsAsIs = true
oneLineJSONCodecHandle.Indent = 0
oneLineJSONCodecHandle.MapKeyAsString = true
}